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> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 41 | #include <linux/mount.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 42 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 43 | #include "ctree.h" |
| 44 | #include "disk-io.h" |
| 45 | #include "transaction.h" |
| 46 | #include "btrfs_inode.h" |
| 47 | #include "ioctl.h" |
| 48 | #include "print-tree.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" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 52 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 53 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 54 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 55 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 56 | #include "inode-map.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 57 | |
| 58 | struct btrfs_iget_args { |
| 59 | u64 ino; |
| 60 | struct btrfs_root *root; |
| 61 | }; |
| 62 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 63 | static const struct inode_operations btrfs_dir_inode_operations; |
| 64 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 65 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 66 | static const struct inode_operations btrfs_special_inode_operations; |
| 67 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 68 | static const struct address_space_operations btrfs_aops; |
| 69 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 70 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 71 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 72 | |
| 73 | static struct kmem_cache *btrfs_inode_cachep; |
| 74 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 75 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 76 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 77 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 78 | |
| 79 | #define S_SHIFT 12 |
| 80 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 81 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 82 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 83 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 84 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 85 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 86 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 87 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 88 | }; |
| 89 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 90 | static int btrfs_setsize(struct inode *inode, loff_t newsize); |
| 91 | static int btrfs_truncate(struct inode *inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 92 | 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] | 93 | static noinline int cow_file_range(struct inode *inode, |
| 94 | struct page *locked_page, |
| 95 | u64 start, u64 end, int *page_started, |
| 96 | unsigned long *nr_written, int unlock); |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 97 | static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 98 | struct btrfs_root *root, struct inode *inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 99 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 100 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 101 | struct inode *inode, struct inode *dir, |
| 102 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 103 | { |
| 104 | int err; |
| 105 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 106 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 107 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 108 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 109 | return err; |
| 110 | } |
| 111 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 112 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 113 | * this does all the hard work for inserting an inline extent into |
| 114 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 115 | * no overlapping inline items exist in the btree |
| 116 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 117 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 118 | struct btrfs_root *root, struct inode *inode, |
| 119 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 120 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 121 | struct page **compressed_pages) |
| 122 | { |
| 123 | struct btrfs_key key; |
| 124 | struct btrfs_path *path; |
| 125 | struct extent_buffer *leaf; |
| 126 | struct page *page = NULL; |
| 127 | char *kaddr; |
| 128 | unsigned long ptr; |
| 129 | struct btrfs_file_extent_item *ei; |
| 130 | int err = 0; |
| 131 | int ret; |
| 132 | size_t cur_size = size; |
| 133 | size_t datasize; |
| 134 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 136 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 137 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 138 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 139 | path = btrfs_alloc_path(); |
| 140 | if (!path) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 141 | return -ENOMEM; |
| 142 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 143 | path->leave_spinning = 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 144 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 145 | key.objectid = btrfs_ino(inode); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | key.offset = start; |
| 147 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 149 | |
| 150 | inode_add_bytes(inode, size); |
| 151 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 152 | datasize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 153 | if (ret) { |
| 154 | err = ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 155 | goto fail; |
| 156 | } |
| 157 | leaf = path->nodes[0]; |
| 158 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 159 | struct btrfs_file_extent_item); |
| 160 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 161 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 162 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 163 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 164 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 165 | ptr = btrfs_file_extent_inline_start(ei); |
| 166 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 167 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 168 | struct page *cpage; |
| 169 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 170 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 171 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 172 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 173 | PAGE_CACHE_SIZE); |
| 174 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 175 | kaddr = kmap_atomic(cpage, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 177 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | |
| 179 | i++; |
| 180 | ptr += cur_size; |
| 181 | compressed_size -= cur_size; |
| 182 | } |
| 183 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 184 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 185 | } else { |
| 186 | page = find_get_page(inode->i_mapping, |
| 187 | start >> PAGE_CACHE_SHIFT); |
| 188 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 189 | kaddr = kmap_atomic(page, KM_USER0); |
| 190 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 191 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
| 192 | kunmap_atomic(kaddr, KM_USER0); |
| 193 | page_cache_release(page); |
| 194 | } |
| 195 | btrfs_mark_buffer_dirty(leaf); |
| 196 | btrfs_free_path(path); |
| 197 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 198 | /* |
| 199 | * we're an inline extent, so nobody can |
| 200 | * extend the file past i_size without locking |
| 201 | * a page we already have locked. |
| 202 | * |
| 203 | * We must do any isize and inode updates |
| 204 | * before we unlock the pages. Otherwise we |
| 205 | * could end up racing with unlink. |
| 206 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 208 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 209 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 210 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 211 | fail: |
| 212 | btrfs_free_path(path); |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /* |
| 218 | * conditionally insert an inline extent into the file. This |
| 219 | * does the checks required to make sure the data is small enough |
| 220 | * to fit as an inline extent. |
| 221 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 222 | static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 223 | struct btrfs_root *root, |
| 224 | struct inode *inode, u64 start, u64 end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 225 | size_t compressed_size, int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 226 | struct page **compressed_pages) |
| 227 | { |
| 228 | u64 isize = i_size_read(inode); |
| 229 | u64 actual_end = min(end + 1, isize); |
| 230 | u64 inline_len = actual_end - start; |
| 231 | u64 aligned_end = (end + root->sectorsize - 1) & |
| 232 | ~((u64)root->sectorsize - 1); |
| 233 | u64 hint_byte; |
| 234 | u64 data_len = inline_len; |
| 235 | int ret; |
| 236 | |
| 237 | if (compressed_size) |
| 238 | data_len = compressed_size; |
| 239 | |
| 240 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 241 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 243 | (!compressed_size && |
| 244 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 245 | end + 1 < isize || |
| 246 | data_len > root->fs_info->max_inline) { |
| 247 | return 1; |
| 248 | } |
| 249 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 250 | ret = btrfs_drop_extents(trans, inode, start, aligned_end, |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 251 | &hint_byte, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 252 | if (ret) |
| 253 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 254 | |
| 255 | if (isize > actual_end) |
| 256 | inline_len = min_t(u64, isize, actual_end); |
| 257 | ret = insert_inline_extent(trans, root, inode, start, |
| 258 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 259 | compress_type, compressed_pages); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 260 | if (ret) { |
| 261 | btrfs_abort_transaction(trans, root, ret); |
| 262 | return ret; |
| 263 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 264 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 265 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 269 | struct async_extent { |
| 270 | u64 start; |
| 271 | u64 ram_size; |
| 272 | u64 compressed_size; |
| 273 | struct page **pages; |
| 274 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 275 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 276 | struct list_head list; |
| 277 | }; |
| 278 | |
| 279 | struct async_cow { |
| 280 | struct inode *inode; |
| 281 | struct btrfs_root *root; |
| 282 | struct page *locked_page; |
| 283 | u64 start; |
| 284 | u64 end; |
| 285 | struct list_head extents; |
| 286 | struct btrfs_work work; |
| 287 | }; |
| 288 | |
| 289 | static noinline int add_async_extent(struct async_cow *cow, |
| 290 | u64 start, u64 ram_size, |
| 291 | u64 compressed_size, |
| 292 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 293 | unsigned long nr_pages, |
| 294 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 295 | { |
| 296 | struct async_extent *async_extent; |
| 297 | |
| 298 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 299 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 300 | async_extent->start = start; |
| 301 | async_extent->ram_size = ram_size; |
| 302 | async_extent->compressed_size = compressed_size; |
| 303 | async_extent->pages = pages; |
| 304 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 305 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 306 | list_add_tail(&async_extent->list, &cow->extents); |
| 307 | return 0; |
| 308 | } |
| 309 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 310 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 311 | * we create compressed extents in two phases. The first |
| 312 | * phase compresses a range of pages that have already been |
| 313 | * locked (both pages and state bits are locked). |
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 | * This is done inside an ordered work queue, and the compression |
| 316 | * is spread across many cpus. The actual IO submission is step |
| 317 | * two, and the ordered work queue takes care of making sure that |
| 318 | * happens in the same order things were put onto the queue by |
| 319 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 320 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 321 | * If this code finds it can't get good compression, it puts an |
| 322 | * entry onto the work queue to write the uncompressed bytes. This |
| 323 | * makes sure that both compressed inodes and uncompressed inodes |
| 324 | * are written in the same order that pdflush sent them down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 325 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 326 | static noinline int compress_file_range(struct inode *inode, |
| 327 | struct page *locked_page, |
| 328 | u64 start, u64 end, |
| 329 | struct async_cow *async_cow, |
| 330 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 331 | { |
| 332 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 333 | struct btrfs_trans_handle *trans; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 334 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 335 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 336 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 337 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 338 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 339 | struct page **pages = NULL; |
| 340 | unsigned long nr_pages; |
| 341 | unsigned long nr_pages_ret = 0; |
| 342 | unsigned long total_compressed = 0; |
| 343 | unsigned long total_in = 0; |
| 344 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 345 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 346 | int i; |
| 347 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 348 | int compress_type = root->fs_info->compress_type; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 349 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 350 | /* if this is a small write inside eof, kick off a defrag */ |
| 351 | if ((end - start + 1) < 16 * 1024 && |
| 352 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 353 | btrfs_add_inode_defrag(NULL, inode); |
| 354 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 355 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 356 | again: |
| 357 | will_compress = 0; |
| 358 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 359 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 360 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 361 | /* |
| 362 | * we don't want to send crud past the end of i_size through |
| 363 | * compression, that's just a waste of CPU time. So, if the |
| 364 | * end of the file is before the start of our current |
| 365 | * requested range of bytes, we bail out to the uncompressed |
| 366 | * cleanup code that can deal with all of this. |
| 367 | * |
| 368 | * It isn't really the fastest way to fix things, but this is a |
| 369 | * very uncommon corner. |
| 370 | */ |
| 371 | if (actual_end <= start) |
| 372 | goto cleanup_and_bail_uncompressed; |
| 373 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 374 | total_compressed = actual_end - start; |
| 375 | |
| 376 | /* we want to make sure that amount of ram required to uncompress |
| 377 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 378 | * of a compressed extent to 128k. This is a crucial number |
| 379 | * because it also controls how easily we can spread reads across |
| 380 | * cpus for decompression. |
| 381 | * |
| 382 | * We also want to make sure the amount of IO required to do |
| 383 | * a random read is reasonably small, so we limit the size of |
| 384 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 385 | */ |
| 386 | total_compressed = min(total_compressed, max_uncompressed); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 387 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 388 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 389 | total_in = 0; |
| 390 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 391 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 392 | /* |
| 393 | * we do compression for mount -o compress and when the |
| 394 | * inode has not been flagged as nocompress. This flag can |
| 395 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 396 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 397 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 398 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 399 | (BTRFS_I(inode)->force_compress) || |
| 400 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 401 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 402 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 403 | if (!pages) { |
| 404 | /* just bail out to the uncompressed code */ |
| 405 | goto cont; |
| 406 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 407 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 408 | if (BTRFS_I(inode)->force_compress) |
| 409 | compress_type = BTRFS_I(inode)->force_compress; |
| 410 | |
| 411 | ret = btrfs_compress_pages(compress_type, |
| 412 | inode->i_mapping, start, |
| 413 | total_compressed, pages, |
| 414 | nr_pages, &nr_pages_ret, |
| 415 | &total_in, |
| 416 | &total_compressed, |
| 417 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 418 | |
| 419 | if (!ret) { |
| 420 | unsigned long offset = total_compressed & |
| 421 | (PAGE_CACHE_SIZE - 1); |
| 422 | struct page *page = pages[nr_pages_ret - 1]; |
| 423 | char *kaddr; |
| 424 | |
| 425 | /* zero the tail end of the last page, we might be |
| 426 | * sending it down to disk |
| 427 | */ |
| 428 | if (offset) { |
| 429 | kaddr = kmap_atomic(page, KM_USER0); |
| 430 | memset(kaddr + offset, 0, |
| 431 | PAGE_CACHE_SIZE - offset); |
| 432 | kunmap_atomic(kaddr, KM_USER0); |
| 433 | } |
| 434 | will_compress = 1; |
| 435 | } |
| 436 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 437 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | if (start == 0) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 439 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 440 | if (IS_ERR(trans)) { |
| 441 | ret = PTR_ERR(trans); |
| 442 | trans = NULL; |
| 443 | goto cleanup_and_out; |
| 444 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 445 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 446 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 447 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 448 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 449 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 450 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | */ |
| 452 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 453 | start, end, 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 454 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 455 | /* try making a compressed inline extent */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 456 | ret = cow_file_range_inline(trans, root, inode, |
| 457 | start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 458 | total_compressed, |
| 459 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 460 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 461 | if (ret <= 0) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 462 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 463 | * inline extent creation worked or returned error, |
| 464 | * we don't need to create any more async work items. |
| 465 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 466 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 467 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 468 | &BTRFS_I(inode)->io_tree, |
| 469 | start, end, NULL, |
| 470 | EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 471 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 472 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 473 | |
| 474 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 475 | goto free_pages_out; |
| 476 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 477 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if (will_compress) { |
| 481 | /* |
| 482 | * we aren't doing an inline extent round the compressed size |
| 483 | * up to a block size boundary so the allocator does sane |
| 484 | * things |
| 485 | */ |
| 486 | total_compressed = (total_compressed + blocksize - 1) & |
| 487 | ~(blocksize - 1); |
| 488 | |
| 489 | /* |
| 490 | * one last check to make sure the compression is really a |
| 491 | * win, compare the page count read with the blocks on disk |
| 492 | */ |
| 493 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & |
| 494 | ~(PAGE_CACHE_SIZE - 1); |
| 495 | if (total_compressed >= total_in) { |
| 496 | will_compress = 0; |
| 497 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 498 | num_bytes = total_in; |
| 499 | } |
| 500 | } |
| 501 | if (!will_compress && pages) { |
| 502 | /* |
| 503 | * the compression code ran but failed to make things smaller, |
| 504 | * free any pages it allocated and our page pointer array |
| 505 | */ |
| 506 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 507 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 508 | page_cache_release(pages[i]); |
| 509 | } |
| 510 | kfree(pages); |
| 511 | pages = NULL; |
| 512 | total_compressed = 0; |
| 513 | nr_pages_ret = 0; |
| 514 | |
| 515 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 516 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 517 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 518 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 519 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 520 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 521 | if (will_compress) { |
| 522 | *num_added += 1; |
| 523 | |
| 524 | /* the async work queues will take care of doing actual |
| 525 | * allocation on disk for these compressed pages, |
| 526 | * and will submit them to the elevator. |
| 527 | */ |
| 528 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 529 | total_compressed, pages, nr_pages_ret, |
| 530 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 531 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 532 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 533 | start += num_bytes; |
| 534 | pages = NULL; |
| 535 | cond_resched(); |
| 536 | goto again; |
| 537 | } |
| 538 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 539 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 540 | /* |
| 541 | * No compression, but we still need to write the pages in |
| 542 | * the file we've been given so far. redirty the locked |
| 543 | * page if it corresponds to our extent and set things up |
| 544 | * for the async work queue to run cow_file_range to do |
| 545 | * the normal delalloc dance |
| 546 | */ |
| 547 | if (page_offset(locked_page) >= start && |
| 548 | page_offset(locked_page) <= end) { |
| 549 | __set_page_dirty_nobuffers(locked_page); |
| 550 | /* unlocked later on in the async handlers */ |
| 551 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 552 | add_async_extent(async_cow, start, end - start + 1, |
| 553 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 554 | *num_added += 1; |
| 555 | } |
| 556 | |
| 557 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 558 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 559 | |
| 560 | free_pages_out: |
| 561 | for (i = 0; i < nr_pages_ret; i++) { |
| 562 | WARN_ON(pages[i]->mapping); |
| 563 | page_cache_release(pages[i]); |
| 564 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 565 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 566 | |
| 567 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 568 | |
| 569 | cleanup_and_out: |
| 570 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 571 | start, end, NULL, |
| 572 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 573 | EXTENT_CLEAR_DIRTY | |
| 574 | EXTENT_CLEAR_DELALLOC | |
| 575 | EXTENT_SET_WRITEBACK | |
| 576 | EXTENT_END_WRITEBACK); |
| 577 | if (!trans || IS_ERR(trans)) |
| 578 | btrfs_error(root->fs_info, ret, "Failed to join transaction"); |
| 579 | else |
| 580 | btrfs_abort_transaction(trans, root, ret); |
| 581 | goto free_pages_out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* |
| 585 | * phase two of compressed writeback. This is the ordered portion |
| 586 | * of the code, which only gets called in the order the work was |
| 587 | * queued. We walk all the async extents created by compress_file_range |
| 588 | * and send them down to the disk. |
| 589 | */ |
| 590 | static noinline int submit_compressed_extents(struct inode *inode, |
| 591 | struct async_cow *async_cow) |
| 592 | { |
| 593 | struct async_extent *async_extent; |
| 594 | u64 alloc_hint = 0; |
| 595 | struct btrfs_trans_handle *trans; |
| 596 | struct btrfs_key ins; |
| 597 | struct extent_map *em; |
| 598 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 599 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 600 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 601 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 602 | |
| 603 | if (list_empty(&async_cow->extents)) |
| 604 | return 0; |
| 605 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 607 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 608 | async_extent = list_entry(async_cow->extents.next, |
| 609 | struct async_extent, list); |
| 610 | list_del(&async_extent->list); |
| 611 | |
| 612 | io_tree = &BTRFS_I(inode)->io_tree; |
| 613 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 614 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 615 | /* did the compression code fall back to uncompressed IO? */ |
| 616 | if (!async_extent->pages) { |
| 617 | int page_started = 0; |
| 618 | unsigned long nr_written = 0; |
| 619 | |
| 620 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 621 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 622 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 623 | |
| 624 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 625 | ret = cow_file_range(inode, async_cow->locked_page, |
| 626 | async_extent->start, |
| 627 | async_extent->start + |
| 628 | async_extent->ram_size - 1, |
| 629 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 630 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 631 | /* JDM XXX */ |
| 632 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 633 | /* |
| 634 | * if page_started, cow_file_range inserted an |
| 635 | * inline extent and took care of all the unlocking |
| 636 | * and IO for us. Otherwise, we need to submit |
| 637 | * all those pages down to the drive. |
| 638 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 639 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 640 | extent_write_locked_range(io_tree, |
| 641 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 642 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 643 | async_extent->ram_size - 1, |
| 644 | btrfs_get_extent, |
| 645 | WB_SYNC_ALL); |
| 646 | kfree(async_extent); |
| 647 | cond_resched(); |
| 648 | continue; |
| 649 | } |
| 650 | |
| 651 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 652 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 653 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 654 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 655 | if (IS_ERR(trans)) { |
| 656 | ret = PTR_ERR(trans); |
| 657 | } else { |
| 658 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 659 | ret = btrfs_reserve_extent(trans, root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | async_extent->compressed_size, |
| 661 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 662 | 0, alloc_hint, &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 663 | if (ret) |
| 664 | btrfs_abort_transaction(trans, root, ret); |
| 665 | btrfs_end_transaction(trans, root); |
| 666 | } |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 667 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 668 | if (ret) { |
| 669 | int i; |
| 670 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 671 | WARN_ON(async_extent->pages[i]->mapping); |
| 672 | page_cache_release(async_extent->pages[i]); |
| 673 | } |
| 674 | kfree(async_extent->pages); |
| 675 | async_extent->nr_pages = 0; |
| 676 | async_extent->pages = NULL; |
| 677 | unlock_extent(io_tree, async_extent->start, |
| 678 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 679 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 680 | if (ret == -ENOSPC) |
| 681 | goto retry; |
| 682 | goto out_free; /* JDM: Requeue? */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 683 | } |
| 684 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 685 | /* |
| 686 | * here we're doing allocation and writeback of the |
| 687 | * compressed pages |
| 688 | */ |
| 689 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 690 | async_extent->start + |
| 691 | async_extent->ram_size - 1, 0); |
| 692 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 693 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 694 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 695 | em->start = async_extent->start; |
| 696 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 697 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 698 | |
| 699 | em->block_start = ins.objectid; |
| 700 | em->block_len = ins.offset; |
| 701 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 702 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 703 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 704 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 705 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 706 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 707 | write_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 708 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 709 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 710 | if (ret != -EEXIST) { |
| 711 | free_extent_map(em); |
| 712 | break; |
| 713 | } |
| 714 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 715 | async_extent->start + |
| 716 | async_extent->ram_size - 1, 0); |
| 717 | } |
| 718 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 719 | ret = btrfs_add_ordered_extent_compress(inode, |
| 720 | async_extent->start, |
| 721 | ins.objectid, |
| 722 | async_extent->ram_size, |
| 723 | ins.offset, |
| 724 | BTRFS_ORDERED_COMPRESSED, |
| 725 | async_extent->compress_type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 726 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 728 | /* |
| 729 | * clear dirty, set writeback and unlock the pages. |
| 730 | */ |
| 731 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 732 | &BTRFS_I(inode)->io_tree, |
| 733 | async_extent->start, |
| 734 | async_extent->start + |
| 735 | async_extent->ram_size - 1, |
| 736 | NULL, EXTENT_CLEAR_UNLOCK_PAGE | |
| 737 | EXTENT_CLEAR_UNLOCK | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 738 | EXTENT_CLEAR_DELALLOC | |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 739 | EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 740 | |
| 741 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 742 | async_extent->start, |
| 743 | async_extent->ram_size, |
| 744 | ins.objectid, |
| 745 | ins.offset, async_extent->pages, |
| 746 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 747 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 748 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 749 | alloc_hint = ins.objectid + ins.offset; |
| 750 | kfree(async_extent); |
| 751 | cond_resched(); |
| 752 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 753 | ret = 0; |
| 754 | out: |
| 755 | return ret; |
| 756 | out_free: |
| 757 | kfree(async_extent); |
| 758 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 759 | } |
| 760 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 761 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 762 | u64 num_bytes) |
| 763 | { |
| 764 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 765 | struct extent_map *em; |
| 766 | u64 alloc_hint = 0; |
| 767 | |
| 768 | read_lock(&em_tree->lock); |
| 769 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 770 | if (em) { |
| 771 | /* |
| 772 | * if block start isn't an actual block number then find the |
| 773 | * first block in this inode and use that as a hint. If that |
| 774 | * block is also bogus then just don't worry about it. |
| 775 | */ |
| 776 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 777 | free_extent_map(em); |
| 778 | em = search_extent_mapping(em_tree, 0, 0); |
| 779 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 780 | alloc_hint = em->block_start; |
| 781 | if (em) |
| 782 | free_extent_map(em); |
| 783 | } else { |
| 784 | alloc_hint = em->block_start; |
| 785 | free_extent_map(em); |
| 786 | } |
| 787 | } |
| 788 | read_unlock(&em_tree->lock); |
| 789 | |
| 790 | return alloc_hint; |
| 791 | } |
| 792 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | /* |
| 794 | * when extent_io.c finds a delayed allocation range in the file, |
| 795 | * the call backs end up in this code. The basic idea is to |
| 796 | * allocate extents on disk for the range, and create ordered data structs |
| 797 | * in ram to track those extents. |
| 798 | * |
| 799 | * locked_page is the page that writepage had locked already. We use |
| 800 | * it to make sure we don't do extra locks or unlocks. |
| 801 | * |
| 802 | * *page_started is set to one if we unlock locked_page and do everything |
| 803 | * required to start IO on it. It may be clean and already done with |
| 804 | * IO when we return. |
| 805 | */ |
| 806 | static noinline int cow_file_range(struct inode *inode, |
| 807 | struct page *locked_page, |
| 808 | u64 start, u64 end, int *page_started, |
| 809 | unsigned long *nr_written, |
| 810 | int unlock) |
| 811 | { |
| 812 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 813 | struct btrfs_trans_handle *trans; |
| 814 | u64 alloc_hint = 0; |
| 815 | u64 num_bytes; |
| 816 | unsigned long ram_size; |
| 817 | u64 disk_num_bytes; |
| 818 | u64 cur_alloc_size; |
| 819 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 820 | struct btrfs_key ins; |
| 821 | struct extent_map *em; |
| 822 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 823 | int ret = 0; |
| 824 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 825 | BUG_ON(btrfs_is_free_space_inode(root, inode)); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 826 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 827 | if (IS_ERR(trans)) { |
| 828 | extent_clear_unlock_delalloc(inode, |
| 829 | &BTRFS_I(inode)->io_tree, |
| 830 | start, end, NULL, |
| 831 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 832 | EXTENT_CLEAR_UNLOCK | |
| 833 | EXTENT_CLEAR_DELALLOC | |
| 834 | EXTENT_CLEAR_DIRTY | |
| 835 | EXTENT_SET_WRITEBACK | |
| 836 | EXTENT_END_WRITEBACK); |
| 837 | return PTR_ERR(trans); |
| 838 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 839 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 840 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 841 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
| 842 | num_bytes = max(blocksize, num_bytes); |
| 843 | disk_num_bytes = num_bytes; |
| 844 | ret = 0; |
| 845 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 846 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 847 | if (num_bytes < 64 * 1024 && |
| 848 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 849 | btrfs_add_inode_defrag(trans, inode); |
| 850 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 851 | if (start == 0) { |
| 852 | /* lets try to make an inline extent */ |
| 853 | ret = cow_file_range_inline(trans, root, inode, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 854 | start, end, 0, 0, NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 855 | if (ret == 0) { |
| 856 | extent_clear_unlock_delalloc(inode, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 857 | &BTRFS_I(inode)->io_tree, |
| 858 | start, end, NULL, |
| 859 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 860 | EXTENT_CLEAR_UNLOCK | |
| 861 | EXTENT_CLEAR_DELALLOC | |
| 862 | EXTENT_CLEAR_DIRTY | |
| 863 | EXTENT_SET_WRITEBACK | |
| 864 | EXTENT_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 865 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 866 | *nr_written = *nr_written + |
| 867 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 868 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 870 | } else if (ret < 0) { |
| 871 | btrfs_abort_transaction(trans, root, ret); |
| 872 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 873 | } |
| 874 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 875 | |
| 876 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 877 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 878 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 879 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 880 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 881 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 882 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 883 | unsigned long op; |
| 884 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 885 | cur_alloc_size = disk_num_bytes; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 886 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 887 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 888 | &ins, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 889 | if (ret < 0) { |
| 890 | btrfs_abort_transaction(trans, root, ret); |
| 891 | goto out_unlock; |
| 892 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 893 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 894 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 895 | BUG_ON(!em); /* -ENOMEM */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 896 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 897 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | ram_size = ins.offset; |
| 899 | em->len = ins.offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 900 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 901 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 902 | em->block_len = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 903 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 904 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 905 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 906 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 907 | write_lock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 908 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 909 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 910 | if (ret != -EEXIST) { |
| 911 | free_extent_map(em); |
| 912 | break; |
| 913 | } |
| 914 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 915 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 916 | } |
| 917 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 918 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 919 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 920 | ram_size, cur_alloc_size, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 921 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 922 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 923 | if (root->root_key.objectid == |
| 924 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 925 | ret = btrfs_reloc_clone_csums(inode, start, |
| 926 | cur_alloc_size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 927 | if (ret) { |
| 928 | btrfs_abort_transaction(trans, root, ret); |
| 929 | goto out_unlock; |
| 930 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 931 | } |
| 932 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 933 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 934 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 935 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 936 | /* we're not doing compressed IO, don't unlock the first |
| 937 | * page (which the caller expects to stay locked), don't |
| 938 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 939 | * |
| 940 | * Do set the Private2 bit so we know this page was properly |
| 941 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 942 | */ |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 943 | op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0; |
| 944 | op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 945 | EXTENT_SET_PRIVATE2; |
| 946 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 947 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
| 948 | start, start + ram_size - 1, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 949 | locked_page, op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 950 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 951 | num_bytes -= cur_alloc_size; |
| 952 | alloc_hint = ins.objectid + ins.offset; |
| 953 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 954 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 955 | ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 956 | out: |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 957 | btrfs_end_transaction(trans, root); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 958 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 959 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 960 | out_unlock: |
| 961 | extent_clear_unlock_delalloc(inode, |
| 962 | &BTRFS_I(inode)->io_tree, |
| 963 | start, end, NULL, |
| 964 | EXTENT_CLEAR_UNLOCK_PAGE | |
| 965 | EXTENT_CLEAR_UNLOCK | |
| 966 | EXTENT_CLEAR_DELALLOC | |
| 967 | EXTENT_CLEAR_DIRTY | |
| 968 | EXTENT_SET_WRITEBACK | |
| 969 | EXTENT_END_WRITEBACK); |
| 970 | |
| 971 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 972 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 973 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 974 | /* |
| 975 | * work queue call back to started compression on a file and pages |
| 976 | */ |
| 977 | static noinline void async_cow_start(struct btrfs_work *work) |
| 978 | { |
| 979 | struct async_cow *async_cow; |
| 980 | int num_added = 0; |
| 981 | async_cow = container_of(work, struct async_cow, work); |
| 982 | |
| 983 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 984 | async_cow->start, async_cow->end, async_cow, |
| 985 | &num_added); |
| 986 | if (num_added == 0) |
| 987 | async_cow->inode = NULL; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * work queue call back to submit previously compressed pages |
| 992 | */ |
| 993 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 994 | { |
| 995 | struct async_cow *async_cow; |
| 996 | struct btrfs_root *root; |
| 997 | unsigned long nr_pages; |
| 998 | |
| 999 | async_cow = container_of(work, struct async_cow, work); |
| 1000 | |
| 1001 | root = async_cow->root; |
| 1002 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1003 | PAGE_CACHE_SHIFT; |
| 1004 | |
| 1005 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1006 | |
| 1007 | if (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1008 | 5 * 1042 * 1024 && |
| 1009 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1010 | wake_up(&root->fs_info->async_submit_wait); |
| 1011 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1012 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1013 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1014 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1015 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1016 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1017 | { |
| 1018 | struct async_cow *async_cow; |
| 1019 | async_cow = container_of(work, struct async_cow, work); |
| 1020 | kfree(async_cow); |
| 1021 | } |
| 1022 | |
| 1023 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1024 | u64 start, u64 end, int *page_started, |
| 1025 | unsigned long *nr_written) |
| 1026 | { |
| 1027 | struct async_cow *async_cow; |
| 1028 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1029 | unsigned long nr_pages; |
| 1030 | u64 cur_end; |
| 1031 | int limit = 10 * 1024 * 1042; |
| 1032 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1033 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1034 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1035 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1036 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1037 | BUG_ON(!async_cow); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1038 | async_cow->inode = inode; |
| 1039 | async_cow->root = root; |
| 1040 | async_cow->locked_page = locked_page; |
| 1041 | async_cow->start = start; |
| 1042 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1043 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1044 | cur_end = end; |
| 1045 | else |
| 1046 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1047 | |
| 1048 | async_cow->end = cur_end; |
| 1049 | INIT_LIST_HEAD(&async_cow->extents); |
| 1050 | |
| 1051 | async_cow->work.func = async_cow_start; |
| 1052 | async_cow->work.ordered_func = async_cow_submit; |
| 1053 | async_cow->work.ordered_free = async_cow_free; |
| 1054 | async_cow->work.flags = 0; |
| 1055 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1056 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1057 | PAGE_CACHE_SHIFT; |
| 1058 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1059 | |
| 1060 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1061 | &async_cow->work); |
| 1062 | |
| 1063 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1064 | wait_event(root->fs_info->async_submit_wait, |
| 1065 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1066 | limit)); |
| 1067 | } |
| 1068 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1069 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1070 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1071 | wait_event(root->fs_info->async_submit_wait, |
| 1072 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1073 | 0)); |
| 1074 | } |
| 1075 | |
| 1076 | *nr_written += nr_pages; |
| 1077 | start = cur_end + 1; |
| 1078 | } |
| 1079 | *page_started = 1; |
| 1080 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1081 | } |
| 1082 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1083 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1084 | u64 bytenr, u64 num_bytes) |
| 1085 | { |
| 1086 | int ret; |
| 1087 | struct btrfs_ordered_sum *sums; |
| 1088 | LIST_HEAD(list); |
| 1089 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1090 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1091 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1092 | if (ret == 0 && list_empty(&list)) |
| 1093 | return 0; |
| 1094 | |
| 1095 | while (!list_empty(&list)) { |
| 1096 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1097 | list_del(&sums->list); |
| 1098 | kfree(sums); |
| 1099 | } |
| 1100 | return 1; |
| 1101 | } |
| 1102 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1103 | /* |
| 1104 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1105 | * of the extents that exist in the file, and COWs the file as required. |
| 1106 | * |
| 1107 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1108 | * blocks on disk |
| 1109 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1110 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1111 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1112 | u64 start, u64 end, int *page_started, int force, |
| 1113 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1114 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1115 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1116 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1117 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1118 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1119 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1120 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1121 | u64 cow_start; |
| 1122 | u64 cur_offset; |
| 1123 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1124 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1125 | u64 disk_bytenr; |
| 1126 | u64 num_bytes; |
| 1127 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1128 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1129 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1130 | int nocow; |
| 1131 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1132 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1133 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1134 | |
| 1135 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1136 | if (!path) |
| 1137 | return -ENOMEM; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1138 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1139 | nolock = btrfs_is_free_space_inode(root, inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1140 | |
| 1141 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1142 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1143 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1144 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1145 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1146 | if (IS_ERR(trans)) { |
| 1147 | btrfs_free_path(path); |
| 1148 | return PTR_ERR(trans); |
| 1149 | } |
| 1150 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1151 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1152 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1153 | cow_start = (u64)-1; |
| 1154 | cur_offset = start; |
| 1155 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1156 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1157 | cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1158 | if (ret < 0) { |
| 1159 | btrfs_abort_transaction(trans, root, ret); |
| 1160 | goto error; |
| 1161 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1162 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1163 | leaf = path->nodes[0]; |
| 1164 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1165 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1166 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1167 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1168 | path->slots[0]--; |
| 1169 | } |
| 1170 | check_prev = 0; |
| 1171 | next_slot: |
| 1172 | leaf = path->nodes[0]; |
| 1173 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1174 | ret = btrfs_next_leaf(root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1175 | if (ret < 0) { |
| 1176 | btrfs_abort_transaction(trans, root, ret); |
| 1177 | goto error; |
| 1178 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1179 | if (ret > 0) |
| 1180 | break; |
| 1181 | leaf = path->nodes[0]; |
| 1182 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1183 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1184 | nocow = 0; |
| 1185 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1186 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1187 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1188 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1189 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1190 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1191 | found_key.offset > end) |
| 1192 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1193 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1194 | if (found_key.offset > cur_offset) { |
| 1195 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1196 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1197 | goto out_check; |
| 1198 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1199 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1200 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1201 | struct btrfs_file_extent_item); |
| 1202 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1203 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1204 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1205 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1206 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1207 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1208 | extent_end = found_key.offset + |
| 1209 | btrfs_file_extent_num_bytes(leaf, fi); |
| 1210 | if (extent_end <= start) { |
| 1211 | path->slots[0]++; |
| 1212 | goto next_slot; |
| 1213 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1214 | if (disk_bytenr == 0) |
| 1215 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1216 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1217 | btrfs_file_extent_encryption(leaf, fi) || |
| 1218 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1219 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1220 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1221 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1222 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1223 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1224 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1225 | found_key.offset - |
| 1226 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1227 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1228 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1229 | disk_bytenr += cur_offset - found_key.offset; |
| 1230 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1231 | /* |
| 1232 | * force cow if csum exists in the range. |
| 1233 | * this ensure that csum for a given extent are |
| 1234 | * either valid or do not exist. |
| 1235 | */ |
| 1236 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1237 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1238 | nocow = 1; |
| 1239 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1240 | extent_end = found_key.offset + |
| 1241 | btrfs_file_extent_inline_len(leaf, fi); |
| 1242 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1243 | } else { |
| 1244 | BUG_ON(1); |
| 1245 | } |
| 1246 | out_check: |
| 1247 | if (extent_end <= start) { |
| 1248 | path->slots[0]++; |
| 1249 | goto next_slot; |
| 1250 | } |
| 1251 | if (!nocow) { |
| 1252 | if (cow_start == (u64)-1) |
| 1253 | cow_start = cur_offset; |
| 1254 | cur_offset = extent_end; |
| 1255 | if (cur_offset > end) |
| 1256 | break; |
| 1257 | path->slots[0]++; |
| 1258 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1259 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1260 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1261 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1262 | if (cow_start != (u64)-1) { |
| 1263 | ret = cow_file_range(inode, locked_page, cow_start, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1264 | found_key.offset - 1, page_started, |
| 1265 | nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1266 | if (ret) { |
| 1267 | btrfs_abort_transaction(trans, root, ret); |
| 1268 | goto error; |
| 1269 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1270 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1271 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1272 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1273 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1274 | struct extent_map *em; |
| 1275 | struct extent_map_tree *em_tree; |
| 1276 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1277 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1278 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1279 | em->start = cur_offset; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 1280 | em->orig_start = em->start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1281 | em->len = num_bytes; |
| 1282 | em->block_len = num_bytes; |
| 1283 | em->block_start = disk_bytenr; |
| 1284 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1285 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 1286 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1287 | write_lock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1288 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1289 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1290 | if (ret != -EEXIST) { |
| 1291 | free_extent_map(em); |
| 1292 | break; |
| 1293 | } |
| 1294 | btrfs_drop_extent_cache(inode, em->start, |
| 1295 | em->start + em->len - 1, 0); |
| 1296 | } |
| 1297 | type = BTRFS_ORDERED_PREALLOC; |
| 1298 | } else { |
| 1299 | type = BTRFS_ORDERED_NOCOW; |
| 1300 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1301 | |
| 1302 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1303 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1304 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1305 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1306 | if (root->root_key.objectid == |
| 1307 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1308 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1309 | num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1310 | if (ret) { |
| 1311 | btrfs_abort_transaction(trans, root, ret); |
| 1312 | goto error; |
| 1313 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1314 | } |
| 1315 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1316 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1317 | cur_offset, cur_offset + num_bytes - 1, |
| 1318 | locked_page, EXTENT_CLEAR_UNLOCK_PAGE | |
| 1319 | EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC | |
| 1320 | EXTENT_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1321 | cur_offset = extent_end; |
| 1322 | if (cur_offset > end) |
| 1323 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1324 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1325 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1326 | |
| 1327 | if (cur_offset <= end && cow_start == (u64)-1) |
| 1328 | cow_start = cur_offset; |
| 1329 | if (cow_start != (u64)-1) { |
| 1330 | ret = cow_file_range(inode, locked_page, cow_start, end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1331 | page_started, nr_written, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1332 | if (ret) { |
| 1333 | btrfs_abort_transaction(trans, root, ret); |
| 1334 | goto error; |
| 1335 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1336 | } |
| 1337 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1338 | error: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1339 | if (nolock) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1340 | err = btrfs_end_transaction_nolock(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1341 | } else { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1342 | err = btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1343 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1344 | if (!ret) |
| 1345 | ret = err; |
| 1346 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1347 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1348 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1349 | } |
| 1350 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1351 | /* |
| 1352 | * extent_io.c call back to do delayed allocation processing |
| 1353 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1354 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1355 | u64 start, u64 end, int *page_started, |
| 1356 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1357 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1358 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1359 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1360 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1361 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1362 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1363 | page_started, 1, nr_written); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1364 | else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1365 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1366 | page_started, 0, nr_written); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1367 | else if (!btrfs_test_opt(root, COMPRESS) && |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 1368 | !(BTRFS_I(inode)->force_compress) && |
| 1369 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1370 | ret = cow_file_range(inode, locked_page, start, end, |
| 1371 | page_started, nr_written, 1); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1372 | else |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1373 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1374 | page_started, nr_written); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1375 | return ret; |
| 1376 | } |
| 1377 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1378 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1379 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1380 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1381 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1382 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1383 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1384 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1385 | spin_lock(&BTRFS_I(inode)->lock); |
| 1386 | BTRFS_I(inode)->outstanding_extents++; |
| 1387 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1392 | * extents so we can keep track of new extents that are just merged onto old |
| 1393 | * extents, such as when we are doing sequential writes, so we can properly |
| 1394 | * account for the metadata space we'll need. |
| 1395 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1396 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1397 | struct extent_state *new, |
| 1398 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1399 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1400 | /* not delalloc, ignore it */ |
| 1401 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1402 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1403 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1404 | spin_lock(&BTRFS_I(inode)->lock); |
| 1405 | BTRFS_I(inode)->outstanding_extents--; |
| 1406 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1409 | /* |
| 1410 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1411 | * bytes in this file, and to maintain the list of inodes that |
| 1412 | * have pending delalloc work to be done. |
| 1413 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1414 | static void btrfs_set_bit_hook(struct inode *inode, |
| 1415 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1416 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1417 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1418 | /* |
| 1419 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1420 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1421 | * bit, which is only set or cleared with irqs on |
| 1422 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1423 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1424 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1425 | u64 len = state->end + 1 - state->start; |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1426 | bool do_list = !btrfs_is_free_space_inode(root, inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1427 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1428 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1429 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1430 | } else { |
| 1431 | spin_lock(&BTRFS_I(inode)->lock); |
| 1432 | BTRFS_I(inode)->outstanding_extents++; |
| 1433 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1434 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1435 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1436 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1437 | BTRFS_I(inode)->delalloc_bytes += len; |
| 1438 | root->fs_info->delalloc_bytes += len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1439 | if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1440 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1441 | &root->fs_info->delalloc_inodes); |
| 1442 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1443 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1444 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1445 | } |
| 1446 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1447 | /* |
| 1448 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1449 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1450 | static void btrfs_clear_bit_hook(struct inode *inode, |
| 1451 | struct extent_state *state, int *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1452 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1453 | /* |
| 1454 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1455 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1456 | * bit, which is only set or cleared with irqs on |
| 1457 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1458 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1459 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1460 | u64 len = state->end + 1 - state->start; |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1461 | bool do_list = !btrfs_is_free_space_inode(root, inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1462 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1463 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1464 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1465 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1466 | spin_lock(&BTRFS_I(inode)->lock); |
| 1467 | BTRFS_I(inode)->outstanding_extents--; |
| 1468 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1469 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1470 | |
| 1471 | if (*bits & EXTENT_DO_ACCOUNTING) |
| 1472 | btrfs_delalloc_release_metadata(inode, len); |
| 1473 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1474 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
| 1475 | && do_list) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1476 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1477 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1478 | spin_lock(&root->fs_info->delalloc_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1479 | root->fs_info->delalloc_bytes -= len; |
| 1480 | BTRFS_I(inode)->delalloc_bytes -= len; |
| 1481 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1482 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1483 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1484 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1485 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1486 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1487 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1490 | /* |
| 1491 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1492 | * we don't create bios that span stripes or chunks |
| 1493 | */ |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1494 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1495 | size_t size, struct bio *bio, |
| 1496 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1497 | { |
| 1498 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1499 | struct btrfs_mapping_tree *map_tree; |
Chris Mason | a62b940 | 2008-10-03 16:31:08 -0400 | [diff] [blame] | 1500 | u64 logical = (u64)bio->bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1501 | u64 length = 0; |
| 1502 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1503 | int ret; |
| 1504 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1505 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1506 | return 0; |
| 1507 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 1508 | length = bio->bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1509 | map_tree = &root->fs_info->mapping_tree; |
| 1510 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1511 | ret = btrfs_map_block(map_tree, READ, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1512 | &map_length, NULL, 0); |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1513 | /* Will always return 0 or 1 with map_multi == NULL */ |
| 1514 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1515 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1516 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1517 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1518 | } |
| 1519 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1520 | /* |
| 1521 | * in order to insert checksums into the metadata in large chunks, |
| 1522 | * we wait until bio submission time. All the pages in the bio are |
| 1523 | * checksummed and sums are attached onto the ordered extent record. |
| 1524 | * |
| 1525 | * At IO completion time the cums attached on the ordered extent record |
| 1526 | * are inserted into the btree |
| 1527 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1528 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1529 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1530 | unsigned long bio_flags, |
| 1531 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1532 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1533 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1534 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1535 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1536 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1537 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1538 | return 0; |
| 1539 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1540 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1541 | /* |
| 1542 | * in order to insert checksums into the metadata in large chunks, |
| 1543 | * we wait until bio submission time. All the pages in the bio are |
| 1544 | * checksummed and sums are attached onto the ordered extent record. |
| 1545 | * |
| 1546 | * At IO completion time the cums attached on the ordered extent record |
| 1547 | * are inserted into the btree |
| 1548 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1549 | 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] | 1550 | int mirror_num, unsigned long bio_flags, |
| 1551 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1552 | { |
| 1553 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1554 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1557 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1558 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1559 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1560 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1561 | 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] | 1562 | int mirror_num, unsigned long bio_flags, |
| 1563 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1564 | { |
| 1565 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1566 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1567 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1568 | int metadata = 0; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1569 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1570 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1571 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1572 | if (btrfs_is_free_space_inode(root, inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1573 | metadata = 2; |
| 1574 | |
| 1575 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 1576 | if (ret) |
| 1577 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1578 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1579 | if (!(rw & REQ_WRITE)) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1580 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1581 | return btrfs_submit_compressed_read(inode, bio, |
| 1582 | mirror_num, bio_flags); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1583 | } else if (!skip_sum) { |
| 1584 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1585 | if (ret) |
| 1586 | return ret; |
| 1587 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1588 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1589 | } else if (!skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1590 | /* csum items have already been cloned */ |
| 1591 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1592 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1593 | /* we're doing a write, do the async checksumming */ |
| 1594 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1595 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1596 | bio_flags, bio_offset, |
| 1597 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1598 | __btrfs_submit_bio_done); |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1601 | mapit: |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 1602 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1603 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1604 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1605 | /* |
| 1606 | * given a list of ordered sums record them in the inode. This happens |
| 1607 | * at IO completion time based on sums calculated at bio submission time. |
| 1608 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1609 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1610 | struct inode *inode, u64 file_offset, |
| 1611 | struct list_head *list) |
| 1612 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1613 | struct btrfs_ordered_sum *sum; |
| 1614 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1615 | list_for_each_entry(sum, list, list) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1616 | btrfs_csum_file_blocks(trans, |
| 1617 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1618 | } |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1622 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1623 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1624 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1625 | if ((end & (PAGE_CACHE_SIZE - 1)) == 0) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1626 | WARN_ON(1); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1627 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1628 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1629 | } |
| 1630 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1631 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1632 | struct btrfs_writepage_fixup { |
| 1633 | struct page *page; |
| 1634 | struct btrfs_work work; |
| 1635 | }; |
| 1636 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1637 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1638 | { |
| 1639 | struct btrfs_writepage_fixup *fixup; |
| 1640 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1641 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1642 | struct page *page; |
| 1643 | struct inode *inode; |
| 1644 | u64 page_start; |
| 1645 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1646 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1647 | |
| 1648 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1649 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1650 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1651 | lock_page(page); |
| 1652 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1653 | ClearPageChecked(page); |
| 1654 | goto out_page; |
| 1655 | } |
| 1656 | |
| 1657 | inode = page->mapping->host; |
| 1658 | page_start = page_offset(page); |
| 1659 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1660 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1661 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1662 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1663 | |
| 1664 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1665 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1666 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1667 | |
| 1668 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1669 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1670 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1671 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1672 | unlock_page(page); |
| 1673 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1674 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1675 | goto again; |
| 1676 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1677 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1678 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1679 | if (ret) { |
| 1680 | mapping_set_error(page->mapping, ret); |
| 1681 | end_extent_writepage(page, ret, page_start, page_end); |
| 1682 | ClearPageChecked(page); |
| 1683 | goto out; |
| 1684 | } |
| 1685 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1686 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1687 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1688 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1689 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1690 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1691 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1692 | out_page: |
| 1693 | unlock_page(page); |
| 1694 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1695 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * There are a few paths in the higher layers of the kernel that directly |
| 1700 | * set the page dirty bit without asking the filesystem if it is a |
| 1701 | * good idea. This causes problems because we want to make sure COW |
| 1702 | * properly happens and the data=ordered rules are followed. |
| 1703 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1704 | * 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] | 1705 | * hasn't been properly setup for IO. We kick off an async process |
| 1706 | * to fix it up. The async helper will wait for ordered extents, set |
| 1707 | * the delalloc bit and make it safe to write the page. |
| 1708 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1709 | 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] | 1710 | { |
| 1711 | struct inode *inode = page->mapping->host; |
| 1712 | struct btrfs_writepage_fixup *fixup; |
| 1713 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1714 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1715 | /* this page is properly in the ordered list */ |
| 1716 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1717 | return 0; |
| 1718 | |
| 1719 | if (PageChecked(page)) |
| 1720 | return -EAGAIN; |
| 1721 | |
| 1722 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1723 | if (!fixup) |
| 1724 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1725 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1726 | SetPageChecked(page); |
| 1727 | page_cache_get(page); |
| 1728 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1729 | fixup->page = page; |
| 1730 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1731 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1732 | } |
| 1733 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1734 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1735 | struct inode *inode, u64 file_pos, |
| 1736 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1737 | u64 num_bytes, u64 ram_bytes, |
| 1738 | u8 compression, u8 encryption, |
| 1739 | u16 other_encoding, int extent_type) |
| 1740 | { |
| 1741 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1742 | struct btrfs_file_extent_item *fi; |
| 1743 | struct btrfs_path *path; |
| 1744 | struct extent_buffer *leaf; |
| 1745 | struct btrfs_key ins; |
| 1746 | u64 hint; |
| 1747 | int ret; |
| 1748 | |
| 1749 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1750 | if (!path) |
| 1751 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1752 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1753 | path->leave_spinning = 1; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1754 | |
| 1755 | /* |
| 1756 | * we may be replacing one extent in the tree with another. |
| 1757 | * The new extent is pinned in the extent map, and we don't want |
| 1758 | * to drop it from the cache until it is completely in the btree. |
| 1759 | * |
| 1760 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1761 | * the caller is expected to unpin it and allow it to be merged |
| 1762 | * with the others. |
| 1763 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1764 | ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes, |
| 1765 | &hint, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1766 | if (ret) |
| 1767 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1768 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1769 | ins.objectid = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1770 | ins.offset = file_pos; |
| 1771 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1772 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1773 | if (ret) |
| 1774 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1775 | leaf = path->nodes[0]; |
| 1776 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1777 | struct btrfs_file_extent_item); |
| 1778 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1779 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1780 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1781 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1782 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1783 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1784 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1785 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1786 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1787 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1788 | |
| 1789 | btrfs_unlock_up_safe(path, 1); |
| 1790 | btrfs_set_lock_blocking(leaf); |
| 1791 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1792 | btrfs_mark_buffer_dirty(leaf); |
| 1793 | |
| 1794 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1795 | |
| 1796 | ins.objectid = disk_bytenr; |
| 1797 | ins.offset = disk_num_bytes; |
| 1798 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1799 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1800 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1801 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1802 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1803 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1804 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1805 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1806 | } |
| 1807 | |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1808 | /* |
| 1809 | * helper function for btrfs_finish_ordered_io, this |
| 1810 | * just reads in some of the csum leaves to prime them into ram |
| 1811 | * before we start the transaction. It limits the amount of btree |
| 1812 | * reads required while inside the transaction. |
| 1813 | */ |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1814 | /* as ordered data IO finishes, this gets called so we can finish |
| 1815 | * an ordered extent if the range of bytes in the file it covers are |
| 1816 | * fully written. |
| 1817 | */ |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1818 | 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] | 1819 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1820 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1821 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | 5d13a98 | 2009-03-13 11:41:46 -0400 | [diff] [blame] | 1822 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1823 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1824 | struct extent_state *cached_state = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1825 | int compress_type = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1826 | int ret; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1827 | bool nolock; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1828 | |
Josef Bacik | 5a1a3df | 2010-02-02 20:51:14 +0000 | [diff] [blame] | 1829 | ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 1830 | end - start + 1); |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1831 | if (!ret) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1832 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1833 | BUG_ON(!ordered_extent); /* Logic error */ |
Josef Bacik | efd049f | 2010-02-02 20:50:10 +0000 | [diff] [blame] | 1834 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 1835 | nolock = btrfs_is_free_space_inode(root, inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1836 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1837 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1838 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1839 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 1840 | if (!ret) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1841 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1842 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1843 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1844 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1845 | if (IS_ERR(trans)) |
| 1846 | return PTR_ERR(trans); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1847 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 1848 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1849 | if (ret) /* -ENOMEM or corruption */ |
| 1850 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1851 | } |
| 1852 | goto out; |
| 1853 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1854 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1855 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 1856 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1857 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1858 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1859 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1860 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1861 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1862 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1863 | if (IS_ERR(trans)) { |
| 1864 | ret = PTR_ERR(trans); |
| 1865 | trans = NULL; |
| 1866 | goto out_unlock; |
| 1867 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1868 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1869 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1870 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1871 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1872 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1873 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1874 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1875 | ordered_extent->file_offset, |
| 1876 | ordered_extent->file_offset + |
| 1877 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1878 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 1879 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1880 | ret = insert_reserved_file_extent(trans, inode, |
| 1881 | ordered_extent->file_offset, |
| 1882 | ordered_extent->start, |
| 1883 | ordered_extent->disk_len, |
| 1884 | ordered_extent->len, |
| 1885 | ordered_extent->len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1886 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1887 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1888 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 1889 | ordered_extent->file_offset, |
| 1890 | ordered_extent->len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1891 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1892 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1893 | ordered_extent->file_offset + |
| 1894 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1895 | if (ret < 0) { |
| 1896 | btrfs_abort_transaction(trans, root, ret); |
| 1897 | goto out; |
| 1898 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1899 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1900 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 1901 | &ordered_extent->list); |
| 1902 | |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1903 | ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 1904 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 1905 | ret = btrfs_update_inode_fallback(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1906 | if (ret) { /* -ENOMEM or corruption */ |
| 1907 | btrfs_abort_transaction(trans, root, ret); |
| 1908 | goto out; |
| 1909 | } |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 1910 | } |
| 1911 | ret = 0; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 1912 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1913 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1914 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1915 | if (trans) { |
| 1916 | if (nolock) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1917 | btrfs_end_transaction_nolock(trans, root); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 1918 | else |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1919 | btrfs_end_transaction(trans, root); |
| 1920 | } |
| 1921 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1922 | /* once for us */ |
| 1923 | btrfs_put_ordered_extent(ordered_extent); |
| 1924 | /* once for the tree */ |
| 1925 | btrfs_put_ordered_extent(ordered_extent); |
| 1926 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1927 | return 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1928 | out_unlock: |
| 1929 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 1930 | ordered_extent->file_offset + |
| 1931 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
| 1932 | goto out; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1933 | } |
| 1934 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1935 | 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] | 1936 | struct extent_state *state, int uptodate) |
| 1937 | { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1938 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 1939 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1940 | ClearPagePrivate2(page); |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 1941 | return btrfs_finish_ordered_io(page->mapping->host, start, end); |
| 1942 | } |
| 1943 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1944 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1945 | * when reads are done, we need to check csums to verify the data is correct |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 1946 | * if there's a match, we allow the bio to finish. If not, the code in |
| 1947 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1948 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1949 | 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] | 1950 | struct extent_state *state) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1951 | { |
Chris Mason | 35ebb93 | 2007-10-30 16:56:53 -0400 | [diff] [blame] | 1952 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1953 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1954 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1955 | char *kaddr; |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 1956 | u64 private = ~(u32)0; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1957 | int ret; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1958 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1959 | u32 csum = ~(u32)0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1960 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1961 | if (PageChecked(page)) { |
| 1962 | ClearPageChecked(page); |
| 1963 | goto good; |
| 1964 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1965 | |
| 1966 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 1967 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1968 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1969 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1970 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1971 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 1972 | GFP_NOFS); |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
Yan | c2e639f | 2008-02-04 08:57:25 -0500 | [diff] [blame] | 1976 | if (state && state->start == start) { |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 1977 | private = state->private; |
| 1978 | ret = 0; |
| 1979 | } else { |
| 1980 | ret = get_state_private(io_tree, start, &private); |
| 1981 | } |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1982 | kaddr = kmap_atomic(page, KM_USER0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1983 | if (ret) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1984 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1985 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 1986 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
| 1987 | btrfs_csum_final(csum, (char *)&csum); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1988 | if (csum != private) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1989 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1990 | |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 1991 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1992 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 1993 | return 0; |
| 1994 | |
| 1995 | zeroit: |
Chris Mason | 945d896 | 2011-05-22 12:33:42 -0400 | [diff] [blame] | 1996 | 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] | 1997 | "private %llu\n", |
| 1998 | (unsigned long long)btrfs_ino(page->mapping->host), |
Chris Mason | 193f284 | 2009-04-27 07:29:05 -0400 | [diff] [blame] | 1999 | (unsigned long long)start, csum, |
| 2000 | (unsigned long long)private); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2001 | memset(kaddr + offset, 1, end - start + 1); |
| 2002 | flush_dcache_page(page); |
Chris Mason | 9ab86c8 | 2009-01-07 09:48:51 -0500 | [diff] [blame] | 2003 | kunmap_atomic(kaddr, KM_USER0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2004 | if (private == 0) |
| 2005 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2006 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2007 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2008 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2009 | struct delayed_iput { |
| 2010 | struct list_head list; |
| 2011 | struct inode *inode; |
| 2012 | }; |
| 2013 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2014 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2015 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2016 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2017 | { |
| 2018 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2019 | struct delayed_iput *delayed; |
| 2020 | |
| 2021 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2022 | return; |
| 2023 | |
| 2024 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2025 | delayed->inode = inode; |
| 2026 | |
| 2027 | spin_lock(&fs_info->delayed_iput_lock); |
| 2028 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2029 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2030 | } |
| 2031 | |
| 2032 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2033 | { |
| 2034 | LIST_HEAD(list); |
| 2035 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2036 | struct delayed_iput *delayed; |
| 2037 | int empty; |
| 2038 | |
| 2039 | spin_lock(&fs_info->delayed_iput_lock); |
| 2040 | empty = list_empty(&fs_info->delayed_iputs); |
| 2041 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2042 | if (empty) |
| 2043 | return; |
| 2044 | |
| 2045 | down_read(&root->fs_info->cleanup_work_sem); |
| 2046 | spin_lock(&fs_info->delayed_iput_lock); |
| 2047 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2048 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2049 | |
| 2050 | while (!list_empty(&list)) { |
| 2051 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2052 | list_del(&delayed->list); |
| 2053 | iput(delayed->inode); |
| 2054 | kfree(delayed); |
| 2055 | } |
| 2056 | up_read(&root->fs_info->cleanup_work_sem); |
| 2057 | } |
| 2058 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2059 | enum btrfs_orphan_cleanup_state { |
| 2060 | ORPHAN_CLEANUP_STARTED = 1, |
| 2061 | ORPHAN_CLEANUP_DONE = 2, |
| 2062 | }; |
| 2063 | |
| 2064 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2065 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2066 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2067 | * structure. |
| 2068 | */ |
| 2069 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2070 | struct btrfs_root *root) |
| 2071 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2072 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2073 | int ret; |
| 2074 | |
| 2075 | if (!list_empty(&root->orphan_list) || |
| 2076 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2077 | return; |
| 2078 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2079 | spin_lock(&root->orphan_lock); |
| 2080 | if (!list_empty(&root->orphan_list)) { |
| 2081 | spin_unlock(&root->orphan_lock); |
| 2082 | return; |
| 2083 | } |
| 2084 | |
| 2085 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2086 | spin_unlock(&root->orphan_lock); |
| 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | block_rsv = root->orphan_block_rsv; |
| 2091 | root->orphan_block_rsv = NULL; |
| 2092 | spin_unlock(&root->orphan_lock); |
| 2093 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2094 | if (root->orphan_item_inserted && |
| 2095 | btrfs_root_refs(&root->root_item) > 0) { |
| 2096 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2097 | root->root_key.objectid); |
| 2098 | BUG_ON(ret); |
| 2099 | root->orphan_item_inserted = 0; |
| 2100 | } |
| 2101 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2102 | if (block_rsv) { |
| 2103 | WARN_ON(block_rsv->size > 0); |
| 2104 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2109 | * This creates an orphan entry for the given inode in case something goes |
| 2110 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2111 | * |
| 2112 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2113 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2114 | */ |
| 2115 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2116 | { |
| 2117 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2118 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2119 | int reserve = 0; |
| 2120 | int insert = 0; |
| 2121 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2122 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2123 | if (!root->orphan_block_rsv) { |
| 2124 | block_rsv = btrfs_alloc_block_rsv(root); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2125 | if (!block_rsv) |
| 2126 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2127 | } |
| 2128 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2129 | spin_lock(&root->orphan_lock); |
| 2130 | if (!root->orphan_block_rsv) { |
| 2131 | root->orphan_block_rsv = block_rsv; |
| 2132 | } else if (block_rsv) { |
| 2133 | btrfs_free_block_rsv(root, block_rsv); |
| 2134 | block_rsv = NULL; |
| 2135 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2136 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2137 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2138 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
| 2139 | #if 0 |
| 2140 | /* |
| 2141 | * For proper ENOSPC handling, we should do orphan |
| 2142 | * cleanup when mounting. But this introduces backward |
| 2143 | * compatibility issue. |
| 2144 | */ |
| 2145 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2146 | insert = 2; |
| 2147 | else |
| 2148 | insert = 1; |
| 2149 | #endif |
| 2150 | insert = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2151 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2152 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2153 | if (!BTRFS_I(inode)->orphan_meta_reserved) { |
| 2154 | BTRFS_I(inode)->orphan_meta_reserved = 1; |
| 2155 | reserve = 1; |
| 2156 | } |
| 2157 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2158 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2159 | /* grab metadata reservation from transaction handle */ |
| 2160 | if (reserve) { |
| 2161 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2162 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2166 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2167 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2168 | if (ret && ret != -EEXIST) { |
| 2169 | btrfs_abort_transaction(trans, root, ret); |
| 2170 | return ret; |
| 2171 | } |
| 2172 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | /* insert an orphan item to track subvolume contains orphan files */ |
| 2176 | if (insert >= 2) { |
| 2177 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 2178 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2179 | if (ret && ret != -EEXIST) { |
| 2180 | btrfs_abort_transaction(trans, root, ret); |
| 2181 | return ret; |
| 2182 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2183 | } |
| 2184 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 2189 | * item for this particular inode. |
| 2190 | */ |
| 2191 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2192 | { |
| 2193 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2194 | int delete_item = 0; |
| 2195 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2196 | int ret = 0; |
| 2197 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2198 | spin_lock(&root->orphan_lock); |
| 2199 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
| 2200 | list_del_init(&BTRFS_I(inode)->i_orphan); |
| 2201 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2204 | if (BTRFS_I(inode)->orphan_meta_reserved) { |
| 2205 | BTRFS_I(inode)->orphan_meta_reserved = 0; |
| 2206 | release_rsv = 1; |
| 2207 | } |
| 2208 | spin_unlock(&root->orphan_lock); |
| 2209 | |
| 2210 | if (trans && delete_item) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2211 | ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2212 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2213 | } |
| 2214 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2215 | if (release_rsv) |
| 2216 | btrfs_orphan_release_metadata(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2217 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2218 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | /* |
| 2222 | * this cleans up any orphans that may be left on the list from the last use |
| 2223 | * of this root. |
| 2224 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2225 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2226 | { |
| 2227 | struct btrfs_path *path; |
| 2228 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2229 | struct btrfs_key key, found_key; |
| 2230 | struct btrfs_trans_handle *trans; |
| 2231 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2232 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2233 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 2234 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2235 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2236 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 2237 | |
| 2238 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2239 | if (!path) { |
| 2240 | ret = -ENOMEM; |
| 2241 | goto out; |
| 2242 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2243 | path->reada = -1; |
| 2244 | |
| 2245 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 2246 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 2247 | key.offset = (u64)-1; |
| 2248 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2249 | while (1) { |
| 2250 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2251 | if (ret < 0) |
| 2252 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2253 | |
| 2254 | /* |
| 2255 | * 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] | 2256 | * 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] | 2257 | * find the key and see if we have stuff that matches |
| 2258 | */ |
| 2259 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2260 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2261 | if (path->slots[0] == 0) |
| 2262 | break; |
| 2263 | path->slots[0]--; |
| 2264 | } |
| 2265 | |
| 2266 | /* pull out the item */ |
| 2267 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2268 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2269 | |
| 2270 | /* make sure the item matches what we want */ |
| 2271 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 2272 | break; |
| 2273 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 2274 | break; |
| 2275 | |
| 2276 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2277 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2278 | |
| 2279 | /* |
| 2280 | * this is where we are basically btrfs_lookup, without the |
| 2281 | * crossing root thing. we store the inode number in the |
| 2282 | * offset of the orphan item. |
| 2283 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 2284 | |
| 2285 | if (found_key.offset == last_objectid) { |
| 2286 | printk(KERN_ERR "btrfs: Error removing orphan entry, " |
| 2287 | "stopping orphan cleanup\n"); |
| 2288 | ret = -EINVAL; |
| 2289 | goto out; |
| 2290 | } |
| 2291 | |
| 2292 | last_objectid = found_key.offset; |
| 2293 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2294 | found_key.objectid = found_key.offset; |
| 2295 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 2296 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 2297 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2298 | ret = PTR_RET(inode); |
| 2299 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2300 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2301 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 2302 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 2303 | struct btrfs_root *dead_root; |
| 2304 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2305 | int is_dead_root = 0; |
| 2306 | |
| 2307 | /* |
| 2308 | * this is an orphan in the tree root. Currently these |
| 2309 | * could come from 2 sources: |
| 2310 | * a) a snapshot deletion in progress |
| 2311 | * b) a free space cache inode |
| 2312 | * We need to distinguish those two, as the snapshot |
| 2313 | * orphan must not get deleted. |
| 2314 | * find_dead_roots already ran before us, so if this |
| 2315 | * is a snapshot deletion, we should find the root |
| 2316 | * in the dead_roots list |
| 2317 | */ |
| 2318 | spin_lock(&fs_info->trans_lock); |
| 2319 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 2320 | root_list) { |
| 2321 | if (dead_root->root_key.objectid == |
| 2322 | found_key.objectid) { |
| 2323 | is_dead_root = 1; |
| 2324 | break; |
| 2325 | } |
| 2326 | } |
| 2327 | spin_unlock(&fs_info->trans_lock); |
| 2328 | if (is_dead_root) { |
| 2329 | /* prevent this orphan from being found again */ |
| 2330 | key.offset = found_key.objectid - 1; |
| 2331 | continue; |
| 2332 | } |
| 2333 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2334 | /* |
| 2335 | * Inode is already gone but the orphan item is still there, |
| 2336 | * kill the orphan item. |
| 2337 | */ |
| 2338 | if (ret == -ESTALE) { |
| 2339 | trans = btrfs_start_transaction(root, 1); |
| 2340 | if (IS_ERR(trans)) { |
| 2341 | ret = PTR_ERR(trans); |
| 2342 | goto out; |
| 2343 | } |
| 2344 | ret = btrfs_del_orphan_item(trans, root, |
| 2345 | found_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2346 | BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */ |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 2347 | btrfs_end_transaction(trans, root); |
| 2348 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2349 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2350 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2351 | /* |
| 2352 | * add this inode to the orphan list so btrfs_orphan_del does |
| 2353 | * the proper thing when we hit it |
| 2354 | */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2355 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2356 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2357 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2358 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2359 | /* if we have links, this was a truncate, lets do that */ |
| 2360 | if (inode->i_nlink) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2361 | if (!S_ISREG(inode->i_mode)) { |
| 2362 | WARN_ON(1); |
| 2363 | iput(inode); |
| 2364 | continue; |
| 2365 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2366 | nr_truncate++; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2367 | ret = btrfs_truncate(inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2368 | } else { |
| 2369 | nr_unlink++; |
| 2370 | } |
| 2371 | |
| 2372 | /* this will do delete_inode and everything for us */ |
| 2373 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2374 | if (ret) |
| 2375 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2376 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 2377 | /* release the path since we're done with it */ |
| 2378 | btrfs_release_path(path); |
| 2379 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2380 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 2381 | |
| 2382 | if (root->orphan_block_rsv) |
| 2383 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 2384 | (u64)-1); |
| 2385 | |
| 2386 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2387 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2388 | if (!IS_ERR(trans)) |
| 2389 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2390 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2391 | |
| 2392 | if (nr_unlink) |
| 2393 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); |
| 2394 | if (nr_truncate) |
| 2395 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 2396 | |
| 2397 | out: |
| 2398 | if (ret) |
| 2399 | printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); |
| 2400 | btrfs_free_path(path); |
| 2401 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2402 | } |
| 2403 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2404 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2405 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 2406 | * don't find any xattrs, we know there can't be any acls. |
| 2407 | * |
| 2408 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 2409 | */ |
| 2410 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
| 2411 | int slot, u64 objectid) |
| 2412 | { |
| 2413 | u32 nritems = btrfs_header_nritems(leaf); |
| 2414 | struct btrfs_key found_key; |
| 2415 | int scanned = 0; |
| 2416 | |
| 2417 | slot++; |
| 2418 | while (slot < nritems) { |
| 2419 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2420 | |
| 2421 | /* we found a different objectid, there must not be acls */ |
| 2422 | if (found_key.objectid != objectid) |
| 2423 | return 0; |
| 2424 | |
| 2425 | /* we found an xattr, assume we've got an acl */ |
| 2426 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) |
| 2427 | return 1; |
| 2428 | |
| 2429 | /* |
| 2430 | * we found a key greater than an xattr key, there can't |
| 2431 | * be any acls later on |
| 2432 | */ |
| 2433 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 2434 | return 0; |
| 2435 | |
| 2436 | slot++; |
| 2437 | scanned++; |
| 2438 | |
| 2439 | /* |
| 2440 | * it goes inode, inode backrefs, xattrs, extents, |
| 2441 | * so if there are a ton of hard links to an inode there can |
| 2442 | * be a lot of backrefs. Don't waste time searching too hard, |
| 2443 | * this is just an optimization |
| 2444 | */ |
| 2445 | if (scanned >= 8) |
| 2446 | break; |
| 2447 | } |
| 2448 | /* we hit the end of the leaf before we found an xattr or |
| 2449 | * something larger than an xattr. We have to assume the inode |
| 2450 | * has acls |
| 2451 | */ |
| 2452 | return 1; |
| 2453 | } |
| 2454 | |
| 2455 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2456 | * read an inode from the btree into the in-memory inode |
| 2457 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2458 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2459 | { |
| 2460 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2461 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2462 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2463 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2464 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2465 | struct btrfs_key location; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2466 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2467 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2468 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2469 | bool filled = false; |
| 2470 | |
| 2471 | ret = btrfs_fill_inode(inode, &rdev); |
| 2472 | if (!ret) |
| 2473 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2474 | |
| 2475 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 2476 | if (!path) |
| 2477 | goto make_bad; |
| 2478 | |
Josef Bacik | d90c732 | 2011-05-17 09:50:54 -0400 | [diff] [blame] | 2479 | path->leave_spinning = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2480 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 2481 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2482 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2483 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2484 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2485 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2486 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2487 | |
| 2488 | if (filled) |
| 2489 | goto cache_acl; |
| 2490 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2491 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2492 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2493 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2494 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2495 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); |
| 2496 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2497 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2498 | |
| 2499 | tspec = btrfs_inode_atime(inode_item); |
| 2500 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2501 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2502 | |
| 2503 | tspec = btrfs_inode_mtime(inode_item); |
| 2504 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2505 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2506 | |
| 2507 | tspec = btrfs_inode_ctime(inode_item); |
| 2508 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 2509 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 2510 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2511 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2512 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2513 | BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2514 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2515 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2516 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 2517 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2518 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2519 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 2520 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2521 | /* |
| 2522 | * try to precache a NULL acl entry for files that don't have |
| 2523 | * any xattrs or acls |
| 2524 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2525 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
| 2526 | btrfs_ino(inode)); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2527 | if (!maybe_acls) |
| 2528 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 2529 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2530 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2531 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2532 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2533 | case S_IFREG: |
| 2534 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2535 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2536 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2537 | inode->i_fop = &btrfs_file_operations; |
| 2538 | inode->i_op = &btrfs_file_inode_operations; |
| 2539 | break; |
| 2540 | case S_IFDIR: |
| 2541 | inode->i_fop = &btrfs_dir_file_operations; |
| 2542 | if (root == root->fs_info->tree_root) |
| 2543 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 2544 | else |
| 2545 | inode->i_op = &btrfs_dir_inode_operations; |
| 2546 | break; |
| 2547 | case S_IFLNK: |
| 2548 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2549 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 2550 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2551 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2552 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 2553 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2554 | init_special_inode(inode, inode->i_mode, rdev); |
| 2555 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2556 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2557 | |
| 2558 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2559 | return; |
| 2560 | |
| 2561 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2562 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2563 | make_bad_inode(inode); |
| 2564 | } |
| 2565 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2566 | /* |
| 2567 | * given a leaf and an inode, copy the inode fields into the leaf |
| 2568 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2569 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 2570 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2571 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2572 | struct inode *inode) |
| 2573 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2574 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
| 2575 | btrfs_set_inode_gid(leaf, item, inode->i_gid); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 2576 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2577 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
| 2578 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); |
| 2579 | |
| 2580 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), |
| 2581 | inode->i_atime.tv_sec); |
| 2582 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 2583 | inode->i_atime.tv_nsec); |
| 2584 | |
| 2585 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 2586 | inode->i_mtime.tv_sec); |
| 2587 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 2588 | inode->i_mtime.tv_nsec); |
| 2589 | |
| 2590 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 2591 | inode->i_ctime.tv_sec); |
| 2592 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 2593 | inode->i_ctime.tv_nsec); |
| 2594 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 2595 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2596 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 2597 | btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2598 | btrfs_set_inode_transid(leaf, item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2599 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
Yan | b98b676 | 2008-01-08 15:54:37 -0500 | [diff] [blame] | 2600 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 2601 | btrfs_set_inode_block_group(leaf, item, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2602 | } |
| 2603 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2604 | /* |
| 2605 | * copy everything in the in-memory inode into the btree. |
| 2606 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2607 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2608 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2609 | { |
| 2610 | struct btrfs_inode_item *inode_item; |
| 2611 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2612 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2613 | int ret; |
| 2614 | |
| 2615 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2616 | if (!path) |
| 2617 | return -ENOMEM; |
| 2618 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2619 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2620 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 2621 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2622 | if (ret) { |
| 2623 | if (ret > 0) |
| 2624 | ret = -ENOENT; |
| 2625 | goto failed; |
| 2626 | } |
| 2627 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2628 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2629 | leaf = path->nodes[0]; |
| 2630 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2631 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2632 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2633 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2634 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2635 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2636 | ret = 0; |
| 2637 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2638 | btrfs_free_path(path); |
| 2639 | return ret; |
| 2640 | } |
| 2641 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2642 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 2643 | * copy everything in the in-memory inode into the btree. |
| 2644 | */ |
| 2645 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 2646 | struct btrfs_root *root, struct inode *inode) |
| 2647 | { |
| 2648 | int ret; |
| 2649 | |
| 2650 | /* |
| 2651 | * If the inode is a free space inode, we can deadlock during commit |
| 2652 | * if we put it into the delayed code. |
| 2653 | * |
| 2654 | * The data relocation inode should also be directly updated |
| 2655 | * without delay |
| 2656 | */ |
| 2657 | if (!btrfs_is_free_space_inode(root, inode) |
| 2658 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 2659 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 2660 | if (!ret) |
| 2661 | btrfs_set_inode_last_trans(trans, inode); |
| 2662 | return ret; |
| 2663 | } |
| 2664 | |
| 2665 | return btrfs_update_inode_item(trans, root, inode); |
| 2666 | } |
| 2667 | |
| 2668 | static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 2669 | struct btrfs_root *root, struct inode *inode) |
| 2670 | { |
| 2671 | int ret; |
| 2672 | |
| 2673 | ret = btrfs_update_inode(trans, root, inode); |
| 2674 | if (ret == -ENOSPC) |
| 2675 | return btrfs_update_inode_item(trans, root, inode); |
| 2676 | return ret; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2680 | * unlink helper that gets used here in inode.c and in the tree logging |
| 2681 | * recovery code. It remove a link in a directory with a given name, and |
| 2682 | * also drops the back refs in the inode to the directory |
| 2683 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2684 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2685 | struct btrfs_root *root, |
| 2686 | struct inode *dir, struct inode *inode, |
| 2687 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2688 | { |
| 2689 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2690 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2691 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2692 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2693 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2694 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2695 | u64 ino = btrfs_ino(inode); |
| 2696 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2697 | |
| 2698 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2699 | if (!path) { |
| 2700 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 2701 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2702 | } |
| 2703 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2704 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2705 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2706 | name, name_len, -1); |
| 2707 | if (IS_ERR(di)) { |
| 2708 | ret = PTR_ERR(di); |
| 2709 | goto err; |
| 2710 | } |
| 2711 | if (!di) { |
| 2712 | ret = -ENOENT; |
| 2713 | goto err; |
| 2714 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2715 | leaf = path->nodes[0]; |
| 2716 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2717 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2718 | if (ret) |
| 2719 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2720 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2721 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2722 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 2723 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2724 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2725 | printk(KERN_INFO "btrfs failed to delete reference to %.*s, " |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2726 | "inode %llu parent %llu\n", name_len, name, |
| 2727 | (unsigned long long)ino, (unsigned long long)dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2728 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 2729 | goto err; |
| 2730 | } |
| 2731 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2732 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2733 | if (ret) { |
| 2734 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2735 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2736 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2737 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2738 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2739 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2740 | if (ret != 0 && ret != -ENOENT) { |
| 2741 | btrfs_abort_transaction(trans, root, ret); |
| 2742 | goto err; |
| 2743 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2744 | |
| 2745 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 2746 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 2747 | if (ret == -ENOENT) |
| 2748 | ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2749 | err: |
| 2750 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2751 | if (ret) |
| 2752 | goto out; |
| 2753 | |
| 2754 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 2755 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2756 | btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2757 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2758 | return ret; |
| 2759 | } |
| 2760 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 2761 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 2762 | struct btrfs_root *root, |
| 2763 | struct inode *dir, struct inode *inode, |
| 2764 | const char *name, int name_len) |
| 2765 | { |
| 2766 | int ret; |
| 2767 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 2768 | if (!ret) { |
| 2769 | btrfs_drop_nlink(inode); |
| 2770 | ret = btrfs_update_inode(trans, root, inode); |
| 2771 | } |
| 2772 | return ret; |
| 2773 | } |
| 2774 | |
| 2775 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2776 | /* helper to check if there is any shared block in the path */ |
| 2777 | static int check_path_shared(struct btrfs_root *root, |
| 2778 | struct btrfs_path *path) |
| 2779 | { |
| 2780 | struct extent_buffer *eb; |
| 2781 | int level; |
Dan Carpenter | 0e4dcbef | 2010-06-01 08:23:11 +0000 | [diff] [blame] | 2782 | u64 refs = 1; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2783 | |
| 2784 | for (level = 0; level < BTRFS_MAX_LEVEL; level++) { |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2785 | int ret; |
| 2786 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2787 | if (!path->nodes[level]) |
| 2788 | break; |
| 2789 | eb = path->nodes[level]; |
| 2790 | if (!btrfs_block_can_be_shared(root, eb)) |
| 2791 | continue; |
| 2792 | ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len, |
| 2793 | &refs, NULL); |
| 2794 | if (refs > 1) |
| 2795 | return 1; |
| 2796 | } |
Josef Bacik | dedefd7 | 2011-01-24 21:43:18 +0000 | [diff] [blame] | 2797 | return 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | * helper to start transaction for unlink and rmdir. |
| 2802 | * |
| 2803 | * unlink and rmdir are special in btrfs, they do not always free space. |
| 2804 | * so in enospc case, we should make sure they will free space before |
| 2805 | * allowing them to use the global metadata reservation. |
| 2806 | */ |
| 2807 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir, |
| 2808 | struct dentry *dentry) |
| 2809 | { |
| 2810 | struct btrfs_trans_handle *trans; |
| 2811 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2812 | struct btrfs_path *path; |
| 2813 | struct btrfs_inode_ref *ref; |
| 2814 | struct btrfs_dir_item *di; |
| 2815 | struct inode *inode = dentry->d_inode; |
| 2816 | u64 index; |
| 2817 | int check_link = 1; |
| 2818 | int err = -ENOSPC; |
| 2819 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2820 | u64 ino = btrfs_ino(inode); |
| 2821 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2822 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 2823 | /* |
| 2824 | * 1 for the possible orphan item |
| 2825 | * 1 for the dir item |
| 2826 | * 1 for the dir index |
| 2827 | * 1 for the inode ref |
| 2828 | * 1 for the inode ref in the tree log |
| 2829 | * 2 for the dir entries in the log |
| 2830 | * 1 for the inode |
| 2831 | */ |
| 2832 | trans = btrfs_start_transaction(root, 8); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2833 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 2834 | return trans; |
| 2835 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2836 | if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2837 | return ERR_PTR(-ENOSPC); |
| 2838 | |
| 2839 | /* check if there is someone else holds reference */ |
| 2840 | if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1) |
| 2841 | return ERR_PTR(-ENOSPC); |
| 2842 | |
| 2843 | if (atomic_read(&inode->i_count) > 2) |
| 2844 | return ERR_PTR(-ENOSPC); |
| 2845 | |
| 2846 | if (xchg(&root->fs_info->enospc_unlink, 1)) |
| 2847 | return ERR_PTR(-ENOSPC); |
| 2848 | |
| 2849 | path = btrfs_alloc_path(); |
| 2850 | if (!path) { |
| 2851 | root->fs_info->enospc_unlink = 0; |
| 2852 | return ERR_PTR(-ENOMEM); |
| 2853 | } |
| 2854 | |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2855 | /* 1 for the orphan item */ |
| 2856 | trans = btrfs_start_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2857 | if (IS_ERR(trans)) { |
| 2858 | btrfs_free_path(path); |
| 2859 | root->fs_info->enospc_unlink = 0; |
| 2860 | return trans; |
| 2861 | } |
| 2862 | |
| 2863 | path->skip_locking = 1; |
| 2864 | path->search_commit_root = 1; |
| 2865 | |
| 2866 | ret = btrfs_lookup_inode(trans, root, path, |
| 2867 | &BTRFS_I(dir)->location, 0); |
| 2868 | if (ret < 0) { |
| 2869 | err = ret; |
| 2870 | goto out; |
| 2871 | } |
| 2872 | if (ret == 0) { |
| 2873 | if (check_path_shared(root, path)) |
| 2874 | goto out; |
| 2875 | } else { |
| 2876 | check_link = 0; |
| 2877 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2878 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2879 | |
| 2880 | ret = btrfs_lookup_inode(trans, root, path, |
| 2881 | &BTRFS_I(inode)->location, 0); |
| 2882 | if (ret < 0) { |
| 2883 | err = ret; |
| 2884 | goto out; |
| 2885 | } |
| 2886 | if (ret == 0) { |
| 2887 | if (check_path_shared(root, path)) |
| 2888 | goto out; |
| 2889 | } else { |
| 2890 | check_link = 0; |
| 2891 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2892 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2893 | |
| 2894 | if (ret == 0 && S_ISREG(inode->i_mode)) { |
| 2895 | ret = btrfs_lookup_file_extent(trans, root, path, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2896 | ino, (u64)-1, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2897 | if (ret < 0) { |
| 2898 | err = ret; |
| 2899 | goto out; |
| 2900 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2901 | BUG_ON(ret == 0); /* Corruption */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2902 | if (check_path_shared(root, path)) |
| 2903 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2904 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | if (!check_link) { |
| 2908 | err = 0; |
| 2909 | goto out; |
| 2910 | } |
| 2911 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2912 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2913 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2914 | if (IS_ERR(di)) { |
| 2915 | err = PTR_ERR(di); |
| 2916 | goto out; |
| 2917 | } |
| 2918 | if (di) { |
| 2919 | if (check_path_shared(root, path)) |
| 2920 | goto out; |
| 2921 | } else { |
| 2922 | err = 0; |
| 2923 | goto out; |
| 2924 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2925 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2926 | |
| 2927 | ref = btrfs_lookup_inode_ref(trans, root, path, |
| 2928 | dentry->d_name.name, dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2929 | ino, dir_ino, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2930 | if (IS_ERR(ref)) { |
| 2931 | err = PTR_ERR(ref); |
| 2932 | goto out; |
| 2933 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2934 | BUG_ON(!ref); /* Logic error */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2935 | if (check_path_shared(root, path)) |
| 2936 | goto out; |
| 2937 | index = btrfs_inode_ref_index(path->nodes[0], ref); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2938 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2939 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 2940 | /* |
| 2941 | * This is a commit root search, if we can lookup inode item and other |
| 2942 | * relative items in the commit root, it means the transaction of |
| 2943 | * dir/file creation has been committed, and the dir index item that we |
| 2944 | * delay to insert has also been inserted into the commit root. So |
| 2945 | * we needn't worry about the delayed insertion of the dir index item |
| 2946 | * here. |
| 2947 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2948 | di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2949 | dentry->d_name.name, dentry->d_name.len, 0); |
| 2950 | if (IS_ERR(di)) { |
| 2951 | err = PTR_ERR(di); |
| 2952 | goto out; |
| 2953 | } |
| 2954 | BUG_ON(ret == -ENOENT); |
| 2955 | if (check_path_shared(root, path)) |
| 2956 | goto out; |
| 2957 | |
| 2958 | err = 0; |
| 2959 | out: |
| 2960 | btrfs_free_path(path); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2961 | /* Migrate the orphan reservation over */ |
| 2962 | if (!err) |
| 2963 | err = btrfs_block_rsv_migrate(trans->block_rsv, |
| 2964 | &root->fs_info->global_block_rsv, |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 2965 | trans->bytes_reserved); |
Josef Bacik | 3880a1b | 2011-10-14 14:46:51 -0400 | [diff] [blame] | 2966 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2967 | if (err) { |
| 2968 | btrfs_end_transaction(trans, root); |
| 2969 | root->fs_info->enospc_unlink = 0; |
| 2970 | return ERR_PTR(err); |
| 2971 | } |
| 2972 | |
| 2973 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 2974 | return trans; |
| 2975 | } |
| 2976 | |
| 2977 | static void __unlink_end_trans(struct btrfs_trans_handle *trans, |
| 2978 | struct btrfs_root *root) |
| 2979 | { |
| 2980 | if (trans->block_rsv == &root->fs_info->global_block_rsv) { |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 2981 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 2982 | trans->bytes_reserved); |
| 2983 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2984 | BUG_ON(!root->fs_info->enospc_unlink); |
| 2985 | root->fs_info->enospc_unlink = 0; |
| 2986 | } |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 2987 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2988 | } |
| 2989 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2990 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2991 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2992 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2993 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2994 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2995 | int ret; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 2996 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2997 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2998 | trans = __unlink_start_trans(dir, dentry); |
| 2999 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3000 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3001 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3002 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3003 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3004 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3005 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3006 | if (ret) |
| 3007 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3008 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3009 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3010 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3011 | if (ret) |
| 3012 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3013 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3014 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3015 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3016 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3017 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3018 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3019 | return ret; |
| 3020 | } |
| 3021 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3022 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3023 | struct btrfs_root *root, |
| 3024 | struct inode *dir, u64 objectid, |
| 3025 | const char *name, int name_len) |
| 3026 | { |
| 3027 | struct btrfs_path *path; |
| 3028 | struct extent_buffer *leaf; |
| 3029 | struct btrfs_dir_item *di; |
| 3030 | struct btrfs_key key; |
| 3031 | u64 index; |
| 3032 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3033 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3034 | |
| 3035 | path = btrfs_alloc_path(); |
| 3036 | if (!path) |
| 3037 | return -ENOMEM; |
| 3038 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3039 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3040 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3041 | if (IS_ERR_OR_NULL(di)) { |
| 3042 | if (!di) |
| 3043 | ret = -ENOENT; |
| 3044 | else |
| 3045 | ret = PTR_ERR(di); |
| 3046 | goto out; |
| 3047 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3048 | |
| 3049 | leaf = path->nodes[0]; |
| 3050 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3051 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3052 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3053 | if (ret) { |
| 3054 | btrfs_abort_transaction(trans, root, ret); |
| 3055 | goto out; |
| 3056 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3057 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3058 | |
| 3059 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3060 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3061 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3062 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3063 | if (ret != -ENOENT) { |
| 3064 | btrfs_abort_transaction(trans, root, ret); |
| 3065 | goto out; |
| 3066 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3067 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3068 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3069 | if (IS_ERR_OR_NULL(di)) { |
| 3070 | if (!di) |
| 3071 | ret = -ENOENT; |
| 3072 | else |
| 3073 | ret = PTR_ERR(di); |
| 3074 | btrfs_abort_transaction(trans, root, ret); |
| 3075 | goto out; |
| 3076 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3077 | |
| 3078 | leaf = path->nodes[0]; |
| 3079 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3080 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3081 | index = key.offset; |
| 3082 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3083 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3084 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3085 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3086 | if (ret) { |
| 3087 | btrfs_abort_transaction(trans, root, ret); |
| 3088 | goto out; |
| 3089 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3090 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3091 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
| 3092 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 3093 | ret = btrfs_update_inode(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3094 | if (ret) |
| 3095 | btrfs_abort_transaction(trans, root, ret); |
| 3096 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3097 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3098 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3099 | } |
| 3100 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3101 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3102 | { |
| 3103 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3104 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3105 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3106 | struct btrfs_trans_handle *trans; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3107 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3108 | |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 3109 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3110 | btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3111 | return -ENOTEMPTY; |
| 3112 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3113 | trans = __unlink_start_trans(dir, dentry); |
| 3114 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3115 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3116 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3117 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3118 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3119 | BTRFS_I(inode)->location.objectid, |
| 3120 | dentry->d_name.name, |
| 3121 | dentry->d_name.len); |
| 3122 | goto out; |
| 3123 | } |
| 3124 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3125 | err = btrfs_orphan_add(trans, inode); |
| 3126 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3127 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3128 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3129 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3130 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3131 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3132 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3133 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3134 | out: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3135 | nr = trans->blocks_used; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3136 | __unlink_end_trans(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3137 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3138 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3139 | return err; |
| 3140 | } |
| 3141 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3142 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3143 | * this can truncate away extent items, csum items and directory items. |
| 3144 | * 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] | 3145 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3146 | * |
| 3147 | * csum items that cross the new i_size are truncated to the new size |
| 3148 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3149 | * |
| 3150 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3151 | * 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] | 3152 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3153 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3154 | struct btrfs_root *root, |
| 3155 | struct inode *inode, |
| 3156 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3157 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3158 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3159 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3160 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3161 | struct btrfs_key key; |
| 3162 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3163 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3164 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3165 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3166 | u64 item_end = 0; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3167 | u64 mask = root->sectorsize - 1; |
| 3168 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3169 | int found_extent; |
| 3170 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3171 | int pending_del_nr = 0; |
| 3172 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3173 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3174 | int ret; |
| 3175 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3176 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3177 | |
| 3178 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3179 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3180 | path = btrfs_alloc_path(); |
| 3181 | if (!path) |
| 3182 | return -ENOMEM; |
| 3183 | path->reada = -1; |
| 3184 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3185 | if (root->ref_cows || root == root->fs_info->tree_root) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 3186 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3187 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3188 | /* |
| 3189 | * This function is also used to drop the items in the log tree before |
| 3190 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3191 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3192 | * items. |
| 3193 | */ |
| 3194 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3195 | btrfs_kill_delayed_inode_items(inode); |
| 3196 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3197 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3198 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3199 | key.type = (u8)-1; |
| 3200 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3201 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3202 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3203 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3204 | if (ret < 0) { |
| 3205 | err = ret; |
| 3206 | goto out; |
| 3207 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3208 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3209 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3210 | /* there are no items in the tree for us to truncate, we're |
| 3211 | * done |
| 3212 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3213 | if (path->slots[0] == 0) |
| 3214 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3215 | path->slots[0]--; |
| 3216 | } |
| 3217 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3218 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3219 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3220 | leaf = path->nodes[0]; |
| 3221 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3222 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3223 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3224 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3225 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3226 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3227 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3228 | break; |
| 3229 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3230 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3231 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3232 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3233 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3234 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3235 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3236 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3237 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3238 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3239 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3240 | fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3241 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 3242 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3243 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3244 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3245 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3246 | } else { |
| 3247 | if (item_end < new_size) |
| 3248 | break; |
| 3249 | if (found_key.offset >= new_size) |
| 3250 | del_item = 1; |
| 3251 | else |
| 3252 | del_item = 0; |
| 3253 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3254 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3255 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3256 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 3257 | goto delete; |
| 3258 | |
| 3259 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3260 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3261 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3262 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3263 | u64 orig_num_bytes = |
| 3264 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3265 | extent_num_bytes = new_size - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3266 | found_key.offset + root->sectorsize - 1; |
Yan | b1632b1 | 2008-01-30 11:54:04 -0500 | [diff] [blame] | 3267 | extent_num_bytes = extent_num_bytes & |
| 3268 | ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3269 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3270 | extent_num_bytes); |
| 3271 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3272 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3273 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3274 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3275 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3276 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3277 | extent_num_bytes = |
| 3278 | btrfs_file_extent_disk_num_bytes(leaf, |
| 3279 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3280 | extent_offset = found_key.offset - |
| 3281 | btrfs_file_extent_offset(leaf, fi); |
| 3282 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3283 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3284 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3285 | if (extent_start != 0) { |
| 3286 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3287 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3288 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3289 | } |
| 3290 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3291 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3292 | /* |
| 3293 | * we can't truncate inline items that have had |
| 3294 | * special encodings |
| 3295 | */ |
| 3296 | if (!del_item && |
| 3297 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 3298 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 3299 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3300 | u32 size = new_size - found_key.offset; |
| 3301 | |
| 3302 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3303 | inode_sub_bytes(inode, item_end + 1 - |
| 3304 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3305 | } |
| 3306 | size = |
| 3307 | btrfs_file_extent_calc_inline_size(size); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3308 | btrfs_truncate_item(trans, root, path, |
| 3309 | size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3310 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3311 | inode_sub_bytes(inode, item_end + 1 - |
| 3312 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 3313 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3314 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3315 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3316 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3317 | if (!pending_del_nr) { |
| 3318 | /* no pending yet, add ourselves */ |
| 3319 | pending_del_slot = path->slots[0]; |
| 3320 | pending_del_nr = 1; |
| 3321 | } else if (pending_del_nr && |
| 3322 | path->slots[0] + 1 == pending_del_slot) { |
| 3323 | /* hop on the pending chunk */ |
| 3324 | pending_del_nr++; |
| 3325 | pending_del_slot = path->slots[0]; |
| 3326 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3327 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3328 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3329 | } else { |
| 3330 | break; |
| 3331 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3332 | if (found_extent && (root->ref_cows || |
| 3333 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3334 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3335 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3336 | extent_num_bytes, 0, |
| 3337 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3338 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3339 | BUG_ON(ret); |
| 3340 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3341 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3342 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 3343 | break; |
| 3344 | |
| 3345 | if (path->slots[0] == 0 || |
| 3346 | path->slots[0] != pending_del_slot) { |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3347 | if (root->ref_cows && |
| 3348 | BTRFS_I(inode)->location.objectid != |
| 3349 | BTRFS_FREE_INO_OBJECTID) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3350 | err = -EAGAIN; |
| 3351 | goto out; |
| 3352 | } |
| 3353 | if (pending_del_nr) { |
| 3354 | ret = btrfs_del_items(trans, root, path, |
| 3355 | pending_del_slot, |
| 3356 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3357 | if (ret) { |
| 3358 | btrfs_abort_transaction(trans, |
| 3359 | root, ret); |
| 3360 | goto error; |
| 3361 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3362 | pending_del_nr = 0; |
| 3363 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3364 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3365 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3366 | } else { |
| 3367 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3368 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3369 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3370 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3371 | if (pending_del_nr) { |
| 3372 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 3373 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3374 | if (ret) |
| 3375 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3376 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3377 | error: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3378 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3379 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3380 | } |
| 3381 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3382 | /* |
| 3383 | * taken from block_truncate_page, but does cow as it zeros out |
| 3384 | * any bytes left in the last page in the file. |
| 3385 | */ |
| 3386 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 3387 | { |
| 3388 | struct inode *inode = mapping->host; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3389 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3390 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 3391 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3392 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3393 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3394 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3395 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 3396 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 3397 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3398 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3399 | int ret = 0; |
| 3400 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3401 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3402 | |
| 3403 | if ((offset & (blocksize - 1)) == 0) |
| 3404 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3405 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3406 | if (ret) |
| 3407 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3408 | |
| 3409 | ret = -ENOMEM; |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3410 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 3411 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3412 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3413 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3414 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3415 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3416 | |
| 3417 | page_start = page_offset(page); |
| 3418 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 3419 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3420 | if (!PageUptodate(page)) { |
| 3421 | ret = btrfs_readpage(NULL, page); |
| 3422 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3423 | if (page->mapping != mapping) { |
| 3424 | unlock_page(page); |
| 3425 | page_cache_release(page); |
| 3426 | goto again; |
| 3427 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3428 | if (!PageUptodate(page)) { |
| 3429 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3430 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3431 | } |
| 3432 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 3433 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3434 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3435 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3436 | set_page_extent_mapped(page); |
| 3437 | |
| 3438 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3439 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3440 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3441 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3442 | unlock_page(page); |
| 3443 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 3444 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3445 | btrfs_put_ordered_extent(ordered); |
| 3446 | goto again; |
| 3447 | } |
| 3448 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3449 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3450 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3451 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3452 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3453 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 3454 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3455 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3456 | unlock_extent_cached(io_tree, page_start, page_end, |
| 3457 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3458 | goto out_unlock; |
| 3459 | } |
| 3460 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3461 | ret = 0; |
| 3462 | if (offset != PAGE_CACHE_SIZE) { |
| 3463 | kaddr = kmap(page); |
| 3464 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 3465 | flush_dcache_page(page); |
| 3466 | kunmap(page); |
| 3467 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3468 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3469 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3470 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 3471 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3472 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 3473 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 3474 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3475 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3476 | unlock_page(page); |
| 3477 | page_cache_release(page); |
| 3478 | out: |
| 3479 | return ret; |
| 3480 | } |
| 3481 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 3482 | /* |
| 3483 | * This function puts in dummy file extents for the area we're creating a hole |
| 3484 | * for. So if we are truncating this file to a larger size we need to insert |
| 3485 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 3486 | * the range between oldsize and size |
| 3487 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3488 | 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] | 3489 | { |
| 3490 | struct btrfs_trans_handle *trans; |
| 3491 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3492 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3493 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3494 | struct extent_state *cached_state = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3495 | u64 mask = root->sectorsize - 1; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3496 | u64 hole_start = (oldsize + mask) & ~mask; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3497 | u64 block_end = (size + mask) & ~mask; |
| 3498 | u64 last_byte; |
| 3499 | u64 cur_offset; |
| 3500 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3501 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3502 | |
| 3503 | if (size <= hole_start) |
| 3504 | return 0; |
| 3505 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3506 | while (1) { |
| 3507 | struct btrfs_ordered_extent *ordered; |
| 3508 | btrfs_wait_ordered_range(inode, hole_start, |
| 3509 | block_end - hole_start); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3510 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3511 | &cached_state); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3512 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); |
| 3513 | if (!ordered) |
| 3514 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3515 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 3516 | &cached_state, GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3517 | btrfs_put_ordered_extent(ordered); |
| 3518 | } |
| 3519 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3520 | cur_offset = hole_start; |
| 3521 | while (1) { |
| 3522 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 3523 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3524 | if (IS_ERR(em)) { |
| 3525 | err = PTR_ERR(em); |
| 3526 | break; |
| 3527 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3528 | last_byte = min(extent_map_end(em), block_end); |
| 3529 | last_byte = (last_byte + mask) & ~mask; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3530 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3531 | u64 hint_byte = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3532 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3533 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3534 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3535 | if (IS_ERR(trans)) { |
| 3536 | err = PTR_ERR(trans); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3537 | break; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3538 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3539 | |
| 3540 | err = btrfs_drop_extents(trans, inode, cur_offset, |
| 3541 | cur_offset + hole_size, |
| 3542 | &hint_byte, 1); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3543 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3544 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3545 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3546 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3547 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3548 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3549 | err = btrfs_insert_file_extent(trans, root, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3550 | btrfs_ino(inode), cur_offset, 0, |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3551 | 0, hole_size, 0, hole_size, |
| 3552 | 0, 0, 0); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3553 | if (err) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3554 | btrfs_abort_transaction(trans, root, err); |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3555 | btrfs_end_transaction(trans, root); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 3556 | break; |
Miao Xie | 5b39737 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 3557 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3558 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3559 | btrfs_drop_extent_cache(inode, hole_start, |
| 3560 | last_byte - 1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3561 | |
Miao Xie | 3642320 | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3562 | btrfs_update_inode(trans, root, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3563 | btrfs_end_transaction(trans, root); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3564 | } |
| 3565 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3566 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3567 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3568 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3569 | break; |
| 3570 | } |
| 3571 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3572 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 3573 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 3574 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3575 | return err; |
| 3576 | } |
| 3577 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3578 | static int btrfs_setsize(struct inode *inode, loff_t newsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3579 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3580 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3581 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3582 | loff_t oldsize = i_size_read(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3583 | int ret; |
| 3584 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3585 | if (newsize == oldsize) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3586 | return 0; |
| 3587 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3588 | if (newsize > oldsize) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3589 | truncate_pagecache(inode, oldsize, newsize); |
| 3590 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3591 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3592 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3593 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 3594 | trans = btrfs_start_transaction(root, 1); |
| 3595 | if (IS_ERR(trans)) |
| 3596 | return PTR_ERR(trans); |
| 3597 | |
| 3598 | i_size_write(inode, newsize); |
| 3599 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 3600 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 3601 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3602 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3603 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3604 | /* |
| 3605 | * We're truncating a file that used to have good data down to |
| 3606 | * zero. Make sure it gets into the ordered flush list so that |
| 3607 | * any new writes get down to disk quickly. |
| 3608 | */ |
| 3609 | if (newsize == 0) |
| 3610 | BTRFS_I(inode)->ordered_data_close = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3611 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3612 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 3613 | truncate_setsize(inode, newsize); |
| 3614 | ret = btrfs_truncate(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3617 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3620 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 3621 | { |
| 3622 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3623 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3624 | int err; |
| 3625 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3626 | if (btrfs_root_readonly(root)) |
| 3627 | return -EROFS; |
| 3628 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3629 | err = inode_change_ok(inode, attr); |
| 3630 | if (err) |
| 3631 | return err; |
| 3632 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 3633 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3634 | err = btrfs_setsize(inode, attr->ia_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3635 | if (err) |
| 3636 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3637 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 3638 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3639 | if (attr->ia_valid) { |
| 3640 | setattr_copy(inode, attr); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 3641 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 3642 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 3643 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 3644 | err = btrfs_acl_chmod(inode); |
| 3645 | } |
| 3646 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3647 | return err; |
| 3648 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 3649 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3650 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3651 | { |
| 3652 | struct btrfs_trans_handle *trans; |
| 3653 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3654 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 3655 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3656 | unsigned long nr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3657 | int ret; |
| 3658 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 3659 | trace_btrfs_inode_evict(inode); |
| 3660 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3661 | truncate_inode_pages(&inode->i_data, 0); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3662 | if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 3663 | btrfs_is_free_space_inode(root, inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3664 | goto no_delete; |
| 3665 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3666 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3667 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3668 | goto no_delete; |
| 3669 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3670 | /* 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] | 3671 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3672 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3673 | if (root->fs_info->log_root_recovering) { |
| 3674 | BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan)); |
| 3675 | goto no_delete; |
| 3676 | } |
| 3677 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3678 | if (inode->i_nlink > 0) { |
| 3679 | BUG_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3680 | goto no_delete; |
| 3681 | } |
| 3682 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3683 | rsv = btrfs_alloc_block_rsv(root); |
| 3684 | if (!rsv) { |
| 3685 | btrfs_orphan_del(NULL, inode); |
| 3686 | goto no_delete; |
| 3687 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 3688 | rsv->size = min_size; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3689 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3690 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3691 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3692 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3693 | /* |
| 3694 | * This is a bit simpler than btrfs_truncate since |
| 3695 | * |
| 3696 | * 1) We've already reserved our space for our orphan item in the |
| 3697 | * unlink. |
| 3698 | * 2) We're going to delete the inode item, so we don't need to update |
| 3699 | * it at all. |
| 3700 | * |
| 3701 | * So we just need to reserve some slack space in case we add bytes when |
| 3702 | * doing the truncate. |
| 3703 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3704 | while (1) { |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 3705 | ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3706 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 3707 | /* |
| 3708 | * Try and steal from the global reserve since we will |
| 3709 | * likely not use this space anyway, we want to try as |
| 3710 | * hard as possible to get this to work. |
| 3711 | */ |
| 3712 | if (ret) |
| 3713 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 3714 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3715 | if (ret) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3716 | printk(KERN_WARNING "Could not get space for a " |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3717 | "delete, will truncate on mount %d\n", ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3718 | btrfs_orphan_del(NULL, inode); |
| 3719 | btrfs_free_block_rsv(root, rsv); |
| 3720 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3721 | } |
| 3722 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3723 | trans = btrfs_start_transaction(root, 0); |
| 3724 | if (IS_ERR(trans)) { |
| 3725 | btrfs_orphan_del(NULL, inode); |
| 3726 | btrfs_free_block_rsv(root, rsv); |
| 3727 | goto no_delete; |
| 3728 | } |
| 3729 | |
| 3730 | trans->block_rsv = rsv; |
| 3731 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3732 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3733 | if (ret != -EAGAIN) |
| 3734 | break; |
| 3735 | |
| 3736 | nr = trans->blocks_used; |
| 3737 | btrfs_end_transaction(trans, root); |
| 3738 | trans = NULL; |
| 3739 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3740 | } |
| 3741 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3742 | btrfs_free_block_rsv(root, rsv); |
| 3743 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3744 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3745 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3746 | ret = btrfs_orphan_del(trans, inode); |
| 3747 | BUG_ON(ret); |
| 3748 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3749 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 3750 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3751 | if (!(root == root->fs_info->tree_root || |
| 3752 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3753 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 3754 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3755 | nr = trans->blocks_used; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3756 | btrfs_end_transaction(trans, root); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 3757 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3758 | no_delete: |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 3759 | end_writeback(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3760 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | /* |
| 3764 | * this returns the key found in the dir entry in the location pointer. |
| 3765 | * If no dir entries were found, location->objectid is 0. |
| 3766 | */ |
| 3767 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 3768 | struct btrfs_key *location) |
| 3769 | { |
| 3770 | const char *name = dentry->d_name.name; |
| 3771 | int namelen = dentry->d_name.len; |
| 3772 | struct btrfs_dir_item *di; |
| 3773 | struct btrfs_path *path; |
| 3774 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3775 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3776 | |
| 3777 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 3778 | if (!path) |
| 3779 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3780 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3781 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3782 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 3783 | if (IS_ERR(di)) |
| 3784 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3785 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3786 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3787 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3788 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3789 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3791 | btrfs_free_path(path); |
| 3792 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3793 | out_err: |
| 3794 | location->objectid = 0; |
| 3795 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | /* |
| 3799 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 3800 | * needs to be changed to reflect the root directory of the tree root. This |
| 3801 | * is kind of like crossing a mount point. |
| 3802 | */ |
| 3803 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3804 | struct inode *dir, |
| 3805 | struct dentry *dentry, |
| 3806 | struct btrfs_key *location, |
| 3807 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3808 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3809 | struct btrfs_path *path; |
| 3810 | struct btrfs_root *new_root; |
| 3811 | struct btrfs_root_ref *ref; |
| 3812 | struct extent_buffer *leaf; |
| 3813 | int ret; |
| 3814 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3815 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3816 | path = btrfs_alloc_path(); |
| 3817 | if (!path) { |
| 3818 | err = -ENOMEM; |
| 3819 | goto out; |
| 3820 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3821 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3822 | err = -ENOENT; |
| 3823 | ret = btrfs_find_root_ref(root->fs_info->tree_root, path, |
| 3824 | BTRFS_I(dir)->root->root_key.objectid, |
| 3825 | location->objectid); |
| 3826 | if (ret) { |
| 3827 | if (ret < 0) |
| 3828 | err = ret; |
| 3829 | goto out; |
| 3830 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3831 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3832 | leaf = path->nodes[0]; |
| 3833 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3834 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3835 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 3836 | goto out; |
| 3837 | |
| 3838 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 3839 | (unsigned long)(ref + 1), |
| 3840 | dentry->d_name.len); |
| 3841 | if (ret) |
| 3842 | goto out; |
| 3843 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3844 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3845 | |
| 3846 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 3847 | if (IS_ERR(new_root)) { |
| 3848 | err = PTR_ERR(new_root); |
| 3849 | goto out; |
| 3850 | } |
| 3851 | |
| 3852 | if (btrfs_root_refs(&new_root->root_item) == 0) { |
| 3853 | err = -ENOENT; |
| 3854 | goto out; |
| 3855 | } |
| 3856 | |
| 3857 | *sub_root = new_root; |
| 3858 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 3859 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3860 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3861 | err = 0; |
| 3862 | out: |
| 3863 | btrfs_free_path(path); |
| 3864 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3865 | } |
| 3866 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3867 | static void inode_tree_add(struct inode *inode) |
| 3868 | { |
| 3869 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3870 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3871 | struct rb_node **p; |
| 3872 | struct rb_node *parent; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3873 | u64 ino = btrfs_ino(inode); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3874 | again: |
| 3875 | p = &root->inode_tree.rb_node; |
| 3876 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3877 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3878 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3879 | return; |
| 3880 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3881 | spin_lock(&root->inode_lock); |
| 3882 | while (*p) { |
| 3883 | parent = *p; |
| 3884 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 3885 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3886 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3887 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3888 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3889 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3890 | else { |
| 3891 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 3892 | (I_WILL_FREE | I_FREEING))); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3893 | rb_erase(parent, &root->inode_tree); |
| 3894 | RB_CLEAR_NODE(parent); |
| 3895 | spin_unlock(&root->inode_lock); |
| 3896 | goto again; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | rb_link_node(&BTRFS_I(inode)->rb_node, parent, p); |
| 3900 | rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
| 3901 | spin_unlock(&root->inode_lock); |
| 3902 | } |
| 3903 | |
| 3904 | static void inode_tree_del(struct inode *inode) |
| 3905 | { |
| 3906 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3907 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3908 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3909 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3910 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3911 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3912 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3913 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3914 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 3915 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3916 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3917 | /* |
| 3918 | * Free space cache has inodes in the tree root, but the tree root has a |
| 3919 | * root_refs of 0, so this could end up dropping the tree root as a |
| 3920 | * snapshot, so we need the extra !root->fs_info->tree_root check to |
| 3921 | * make sure we don't drop it. |
| 3922 | */ |
| 3923 | if (empty && btrfs_root_refs(&root->root_item) == 0 && |
| 3924 | root != root->fs_info->tree_root) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3925 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 3926 | spin_lock(&root->inode_lock); |
| 3927 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 3928 | spin_unlock(&root->inode_lock); |
| 3929 | if (empty) |
| 3930 | btrfs_add_dead_root(root); |
| 3931 | } |
| 3932 | } |
| 3933 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3934 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3935 | { |
| 3936 | struct rb_node *node; |
| 3937 | struct rb_node *prev; |
| 3938 | struct btrfs_inode *entry; |
| 3939 | struct inode *inode; |
| 3940 | u64 objectid = 0; |
| 3941 | |
| 3942 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 3943 | |
| 3944 | spin_lock(&root->inode_lock); |
| 3945 | again: |
| 3946 | node = root->inode_tree.rb_node; |
| 3947 | prev = NULL; |
| 3948 | while (node) { |
| 3949 | prev = node; |
| 3950 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 3951 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3952 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3953 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3954 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3955 | node = node->rb_right; |
| 3956 | else |
| 3957 | break; |
| 3958 | } |
| 3959 | if (!node) { |
| 3960 | while (prev) { |
| 3961 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3962 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3963 | node = prev; |
| 3964 | break; |
| 3965 | } |
| 3966 | prev = rb_next(prev); |
| 3967 | } |
| 3968 | } |
| 3969 | while (node) { |
| 3970 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3971 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3972 | inode = igrab(&entry->vfs_inode); |
| 3973 | if (inode) { |
| 3974 | spin_unlock(&root->inode_lock); |
| 3975 | if (atomic_read(&inode->i_count) > 1) |
| 3976 | d_prune_aliases(inode); |
| 3977 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 3978 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3979 | * the inode cache when its usage count |
| 3980 | * hits zero. |
| 3981 | */ |
| 3982 | iput(inode); |
| 3983 | cond_resched(); |
| 3984 | spin_lock(&root->inode_lock); |
| 3985 | goto again; |
| 3986 | } |
| 3987 | |
| 3988 | if (cond_resched_lock(&root->inode_lock)) |
| 3989 | goto again; |
| 3990 | |
| 3991 | node = rb_next(node); |
| 3992 | } |
| 3993 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3994 | } |
| 3995 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3996 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 3997 | { |
| 3998 | struct btrfs_iget_args *args = p; |
| 3999 | inode->i_ino = args->ino; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4000 | BTRFS_I(inode)->root = args->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4001 | btrfs_set_inode_space_info(args->root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4002 | return 0; |
| 4003 | } |
| 4004 | |
| 4005 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4006 | { |
| 4007 | struct btrfs_iget_args *args = opaque; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4008 | return args->ino == btrfs_ino(inode) && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4009 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4010 | } |
| 4011 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4012 | static struct inode *btrfs_iget_locked(struct super_block *s, |
| 4013 | u64 objectid, |
| 4014 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4015 | { |
| 4016 | struct inode *inode; |
| 4017 | struct btrfs_iget_args args; |
| 4018 | args.ino = objectid; |
| 4019 | args.root = root; |
| 4020 | |
| 4021 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 4022 | btrfs_init_locked_inode, |
| 4023 | (void *)&args); |
| 4024 | return inode; |
| 4025 | } |
| 4026 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4027 | /* Get an inode object given its location and corresponding root. |
| 4028 | * Returns in *is_new if the inode was read from disk |
| 4029 | */ |
| 4030 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4031 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4032 | { |
| 4033 | struct inode *inode; |
| 4034 | |
| 4035 | inode = btrfs_iget_locked(s, location->objectid, root); |
| 4036 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4037 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4038 | |
| 4039 | if (inode->i_state & I_NEW) { |
| 4040 | BTRFS_I(inode)->root = root; |
| 4041 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); |
| 4042 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4043 | if (!is_bad_inode(inode)) { |
| 4044 | inode_tree_add(inode); |
| 4045 | unlock_new_inode(inode); |
| 4046 | if (new) |
| 4047 | *new = 1; |
| 4048 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 4049 | unlock_new_inode(inode); |
| 4050 | iput(inode); |
| 4051 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 4052 | } |
| 4053 | } |
| 4054 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 4055 | return inode; |
| 4056 | } |
| 4057 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4058 | static struct inode *new_simple_dir(struct super_block *s, |
| 4059 | struct btrfs_key *key, |
| 4060 | struct btrfs_root *root) |
| 4061 | { |
| 4062 | struct inode *inode = new_inode(s); |
| 4063 | |
| 4064 | if (!inode) |
| 4065 | return ERR_PTR(-ENOMEM); |
| 4066 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4067 | BTRFS_I(inode)->root = root; |
| 4068 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
| 4069 | BTRFS_I(inode)->dummy_inode = 1; |
| 4070 | |
| 4071 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame^] | 4072 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4073 | inode->i_fop = &simple_dir_operations; |
| 4074 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 4075 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 4076 | |
| 4077 | return inode; |
| 4078 | } |
| 4079 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4080 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4081 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4082 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4083 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4084 | struct btrfs_root *sub_root = root; |
| 4085 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4086 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4087 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4088 | |
| 4089 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 4090 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4091 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4092 | if (unlikely(d_need_lookup(dentry))) { |
| 4093 | memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key)); |
| 4094 | kfree(dentry->d_fsdata); |
| 4095 | dentry->d_fsdata = NULL; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4096 | /* This thing is hashed, drop it for now */ |
| 4097 | d_drop(dentry); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4098 | } else { |
| 4099 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 4100 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4101 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4102 | if (ret < 0) |
| 4103 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4104 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4105 | if (location.objectid == 0) |
| 4106 | return NULL; |
| 4107 | |
| 4108 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4109 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4110 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4111 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4112 | |
| 4113 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 4114 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4115 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4116 | ret = fixup_tree_root_location(root, dir, dentry, |
| 4117 | &location, &sub_root); |
| 4118 | if (ret < 0) { |
| 4119 | if (ret != -ENOENT) |
| 4120 | inode = ERR_PTR(ret); |
| 4121 | else |
| 4122 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 4123 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 4124 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4125 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4126 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 4127 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 4128 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4129 | down_read(&root->fs_info->cleanup_work_sem); |
| 4130 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4131 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4132 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 4133 | if (ret) |
| 4134 | inode = ERR_PTR(ret); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4137 | return inode; |
| 4138 | } |
| 4139 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 4140 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4141 | { |
| 4142 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame^] | 4143 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4144 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame^] | 4145 | if (!inode && !IS_ROOT(dentry)) |
| 4146 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4147 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame^] | 4148 | if (inode) { |
| 4149 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4150 | if (btrfs_root_refs(&root->root_item) == 0) |
| 4151 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame^] | 4152 | |
| 4153 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 4154 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4155 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4156 | return 0; |
| 4157 | } |
| 4158 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4159 | static void btrfs_dentry_release(struct dentry *dentry) |
| 4160 | { |
| 4161 | if (dentry->d_fsdata) |
| 4162 | kfree(dentry->d_fsdata); |
| 4163 | } |
| 4164 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4165 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 4166 | struct nameidata *nd) |
| 4167 | { |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 4168 | struct dentry *ret; |
| 4169 | |
| 4170 | ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); |
| 4171 | if (unlikely(d_need_lookup(dentry))) { |
| 4172 | spin_lock(&dentry->d_lock); |
| 4173 | dentry->d_flags &= ~DCACHE_NEED_LOOKUP; |
| 4174 | spin_unlock(&dentry->d_lock); |
| 4175 | } |
| 4176 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4177 | } |
| 4178 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4179 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4180 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 4181 | }; |
| 4182 | |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 4183 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
| 4184 | filldir_t filldir) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4185 | { |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 4186 | struct inode *inode = filp->f_dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4187 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4188 | struct btrfs_item *item; |
| 4189 | struct btrfs_dir_item *di; |
| 4190 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4191 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4192 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4193 | struct list_head ins_list; |
| 4194 | struct list_head del_list; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4195 | struct qstr q; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4196 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4197 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4198 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4199 | unsigned char d_type; |
| 4200 | int over = 0; |
| 4201 | u32 di_cur; |
| 4202 | u32 di_total; |
| 4203 | u32 di_len; |
| 4204 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4205 | char tmp_name[32]; |
| 4206 | char *name_ptr; |
| 4207 | int name_len; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4208 | int is_curr = 0; /* filp->f_pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4209 | |
| 4210 | /* FIXME, use a real flag for deciding about the key type */ |
| 4211 | if (root->fs_info->tree_root == root) |
| 4212 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4213 | |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4214 | /* special case for "." */ |
| 4215 | if (filp->f_pos == 0) { |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4216 | over = filldir(dirent, ".", 1, |
| 4217 | filp->f_pos, btrfs_ino(inode), DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4218 | if (over) |
| 4219 | return 0; |
| 4220 | filp->f_pos = 1; |
| 4221 | } |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4222 | /* special case for .., just use the back ref */ |
| 4223 | if (filp->f_pos == 1) { |
David Woodhouse | 5ecc7e5 | 2008-08-17 15:14:48 +0100 | [diff] [blame] | 4224 | u64 pino = parent_ino(filp->f_path.dentry); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4225 | over = filldir(dirent, "..", 2, |
Hidetoshi Seto | 3765fef | 2011-09-18 10:20:46 -0400 | [diff] [blame] | 4226 | filp->f_pos, pino, DT_DIR); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4227 | if (over) |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4228 | return 0; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4229 | filp->f_pos = 2; |
| 4230 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4231 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4232 | if (!path) |
| 4233 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4234 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 4235 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4236 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4237 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4238 | INIT_LIST_HEAD(&ins_list); |
| 4239 | INIT_LIST_HEAD(&del_list); |
| 4240 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 4241 | } |
| 4242 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4243 | btrfs_set_key_type(&key, key_type); |
| 4244 | key.offset = filp->f_pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4245 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4246 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4247 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4248 | if (ret < 0) |
| 4249 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4250 | |
| 4251 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4252 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4253 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4254 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4255 | ret = btrfs_next_leaf(root, path); |
| 4256 | if (ret < 0) |
| 4257 | goto err; |
| 4258 | else if (ret > 0) |
| 4259 | break; |
| 4260 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4261 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4262 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4263 | item = btrfs_item_nr(leaf, slot); |
| 4264 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4265 | |
| 4266 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4267 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4268 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4269 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4270 | if (found_key.offset < filp->f_pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4271 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4272 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 4273 | btrfs_should_delete_dir_index(&del_list, |
| 4274 | found_key.offset)) |
| 4275 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4276 | |
| 4277 | filp->f_pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4278 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4279 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4280 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 4281 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4282 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4283 | |
| 4284 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4285 | struct btrfs_key location; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4286 | struct dentry *tmp; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4287 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 4288 | if (verify_dir_item(root, leaf, di)) |
| 4289 | break; |
| 4290 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4291 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4292 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4293 | name_ptr = tmp_name; |
| 4294 | } else { |
| 4295 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4296 | if (!name_ptr) { |
| 4297 | ret = -ENOMEM; |
| 4298 | goto err; |
| 4299 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4300 | } |
| 4301 | read_extent_buffer(leaf, name_ptr, |
| 4302 | (unsigned long)(di + 1), name_len); |
| 4303 | |
| 4304 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 4305 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4306 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 4307 | q.name = name_ptr; |
| 4308 | q.len = name_len; |
| 4309 | q.hash = full_name_hash(q.name, q.len); |
| 4310 | tmp = d_lookup(filp->f_dentry, &q); |
| 4311 | if (!tmp) { |
| 4312 | struct btrfs_key *newkey; |
| 4313 | |
| 4314 | newkey = kzalloc(sizeof(struct btrfs_key), |
| 4315 | GFP_NOFS); |
| 4316 | if (!newkey) |
| 4317 | goto no_dentry; |
| 4318 | tmp = d_alloc(filp->f_dentry, &q); |
| 4319 | if (!tmp) { |
| 4320 | kfree(newkey); |
| 4321 | dput(tmp); |
| 4322 | goto no_dentry; |
| 4323 | } |
| 4324 | memcpy(newkey, &location, |
| 4325 | sizeof(struct btrfs_key)); |
| 4326 | tmp->d_fsdata = newkey; |
| 4327 | tmp->d_flags |= DCACHE_NEED_LOOKUP; |
| 4328 | d_rehash(tmp); |
| 4329 | dput(tmp); |
| 4330 | } else { |
| 4331 | dput(tmp); |
| 4332 | } |
| 4333 | no_dentry: |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4334 | /* is this a reference to our own snapshot? If so |
| 4335 | * skip it |
| 4336 | */ |
| 4337 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 4338 | location.objectid == root->root_key.objectid) { |
| 4339 | over = 0; |
| 4340 | goto skip; |
| 4341 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4342 | over = filldir(dirent, name_ptr, name_len, |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4343 | found_key.offset, location.objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4344 | d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4345 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4346 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4347 | if (name_ptr != tmp_name) |
| 4348 | kfree(name_ptr); |
| 4349 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4350 | if (over) |
| 4351 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 4352 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4353 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4354 | di_cur += di_len; |
| 4355 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 4356 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 4357 | next: |
| 4358 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4359 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4360 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4361 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 4362 | if (is_curr) |
| 4363 | filp->f_pos++; |
| 4364 | ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir, |
| 4365 | &ins_list); |
| 4366 | if (ret) |
| 4367 | goto nopos; |
| 4368 | } |
| 4369 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 4370 | /* Reached end of directory/root. Bump pos past the last item. */ |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4371 | if (key_type == BTRFS_DIR_INDEX_KEY) |
Jan Engelhardt | 406266a | 2009-12-09 22:00:38 +0000 | [diff] [blame] | 4372 | /* |
| 4373 | * 32-bit glibc will use getdents64, but then strtol - |
| 4374 | * so the last number we can serve is this. |
| 4375 | */ |
| 4376 | filp->f_pos = 0x7fffffff; |
Yan Zheng | 5e591a0 | 2008-02-19 11:41:02 -0500 | [diff] [blame] | 4377 | else |
| 4378 | filp->f_pos++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4379 | nopos: |
| 4380 | ret = 0; |
| 4381 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4382 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 4383 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4384 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4385 | return ret; |
| 4386 | } |
| 4387 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4388 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4389 | { |
| 4390 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4391 | struct btrfs_trans_handle *trans; |
| 4392 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4393 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4394 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4395 | if (BTRFS_I(inode)->dummy_inode) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 4396 | return 0; |
| 4397 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 4398 | 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] | 4399 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4400 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 4401 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4402 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4403 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4404 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4405 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 4406 | if (IS_ERR(trans)) |
| 4407 | return PTR_ERR(trans); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4408 | if (nolock) |
| 4409 | ret = btrfs_end_transaction_nolock(trans, root); |
| 4410 | else |
| 4411 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4412 | } |
| 4413 | return ret; |
| 4414 | } |
| 4415 | |
| 4416 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4417 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4418 | * inode changes. But, it is most likely to find the inode in cache. |
| 4419 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 4420 | * to keep or drop this code. |
| 4421 | */ |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4422 | int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4423 | { |
| 4424 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4425 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4426 | int ret; |
| 4427 | |
| 4428 | if (BTRFS_I(inode)->dummy_inode) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4429 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4430 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4431 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4432 | if (IS_ERR(trans)) |
| 4433 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4434 | |
| 4435 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4436 | if (ret && ret == -ENOSPC) { |
| 4437 | /* whoops, lets try again with the full transaction */ |
| 4438 | btrfs_end_transaction(trans, root); |
| 4439 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4440 | if (IS_ERR(trans)) |
| 4441 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4442 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4443 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 4444 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4445 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4446 | if (BTRFS_I(inode)->delayed_node) |
| 4447 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4448 | |
| 4449 | return ret; |
| 4450 | } |
| 4451 | |
| 4452 | /* |
| 4453 | * This is a copy of file_update_time. We need this so we can return error on |
| 4454 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 4455 | */ |
| 4456 | int btrfs_update_time(struct file *file) |
| 4457 | { |
| 4458 | struct inode *inode = file->f_path.dentry->d_inode; |
| 4459 | struct timespec now; |
| 4460 | int ret; |
| 4461 | enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0; |
| 4462 | |
| 4463 | /* First try to exhaust all avenues to not sync */ |
| 4464 | if (IS_NOCMTIME(inode)) |
| 4465 | return 0; |
| 4466 | |
| 4467 | now = current_fs_time(inode->i_sb); |
| 4468 | if (!timespec_equal(&inode->i_mtime, &now)) |
| 4469 | sync_it = S_MTIME; |
| 4470 | |
| 4471 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 4472 | sync_it |= S_CTIME; |
| 4473 | |
| 4474 | if (IS_I_VERSION(inode)) |
| 4475 | sync_it |= S_VERSION; |
| 4476 | |
| 4477 | if (!sync_it) |
| 4478 | return 0; |
| 4479 | |
| 4480 | /* Finally allowed to write? Takes lock. */ |
| 4481 | if (mnt_want_write_file(file)) |
| 4482 | return 0; |
| 4483 | |
| 4484 | /* Only change inode inside the lock region */ |
| 4485 | if (sync_it & S_VERSION) |
| 4486 | inode_inc_iversion(inode); |
| 4487 | if (sync_it & S_CTIME) |
| 4488 | inode->i_ctime = now; |
| 4489 | if (sync_it & S_MTIME) |
| 4490 | inode->i_mtime = now; |
| 4491 | ret = btrfs_dirty_inode(inode); |
| 4492 | if (!ret) |
| 4493 | mark_inode_dirty_sync(inode); |
| 4494 | mnt_drop_write(file->f_path.mnt); |
| 4495 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4496 | } |
| 4497 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4498 | /* |
| 4499 | * find the highest existing sequence number in a directory |
| 4500 | * and then set the in-memory index_cnt variable to reflect |
| 4501 | * free sequence numbers |
| 4502 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4503 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 4504 | { |
| 4505 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4506 | struct btrfs_key key, found_key; |
| 4507 | struct btrfs_path *path; |
| 4508 | struct extent_buffer *leaf; |
| 4509 | int ret; |
| 4510 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4511 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4512 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 4513 | key.offset = (u64)-1; |
| 4514 | |
| 4515 | path = btrfs_alloc_path(); |
| 4516 | if (!path) |
| 4517 | return -ENOMEM; |
| 4518 | |
| 4519 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4520 | if (ret < 0) |
| 4521 | goto out; |
| 4522 | /* FIXME: we should be able to handle this */ |
| 4523 | if (ret == 0) |
| 4524 | goto out; |
| 4525 | ret = 0; |
| 4526 | |
| 4527 | /* |
| 4528 | * MAGIC NUMBER EXPLANATION: |
| 4529 | * since we search a directory based on f_pos we have to start at 2 |
| 4530 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 4531 | * else has to start at 2 |
| 4532 | */ |
| 4533 | if (path->slots[0] == 0) { |
| 4534 | BTRFS_I(inode)->index_cnt = 2; |
| 4535 | goto out; |
| 4536 | } |
| 4537 | |
| 4538 | path->slots[0]--; |
| 4539 | |
| 4540 | leaf = path->nodes[0]; |
| 4541 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4542 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4543 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4544 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 4545 | BTRFS_I(inode)->index_cnt = 2; |
| 4546 | goto out; |
| 4547 | } |
| 4548 | |
| 4549 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 4550 | out: |
| 4551 | btrfs_free_path(path); |
| 4552 | return ret; |
| 4553 | } |
| 4554 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4555 | /* |
| 4556 | * helper to find a free sequence number in a given directory. This current |
| 4557 | * code is very simple, later versions will do smarter things in the btree |
| 4558 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4559 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4560 | { |
| 4561 | int ret = 0; |
| 4562 | |
| 4563 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4564 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 4565 | if (ret) { |
| 4566 | ret = btrfs_set_inode_index_count(dir); |
| 4567 | if (ret) |
| 4568 | return ret; |
| 4569 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4570 | } |
| 4571 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4572 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4573 | BTRFS_I(dir)->index_cnt++; |
| 4574 | |
| 4575 | return ret; |
| 4576 | } |
| 4577 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4578 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 4579 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4580 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4581 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 4582 | u64 ref_objectid, u64 objectid, |
| 4583 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4584 | { |
| 4585 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4586 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4587 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4588 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4589 | struct btrfs_inode_ref *ref; |
| 4590 | struct btrfs_key key[2]; |
| 4591 | u32 sizes[2]; |
| 4592 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4593 | int ret; |
| 4594 | int owner; |
| 4595 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4596 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4597 | if (!path) |
| 4598 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4599 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4600 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4601 | if (!inode) { |
| 4602 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4603 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4604 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4605 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4606 | /* |
| 4607 | * we have to initialize this early, so we can reclaim the inode |
| 4608 | * number if we fail afterwards in this function. |
| 4609 | */ |
| 4610 | inode->i_ino = objectid; |
| 4611 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4612 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4613 | trace_btrfs_inode_request(dir); |
| 4614 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4615 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4616 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 4617 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4618 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4619 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4620 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4621 | } |
| 4622 | /* |
| 4623 | * index_cnt is ignored for everything but a dir, |
| 4624 | * btrfs_get_inode_index_count has an explanation for the magic |
| 4625 | * number |
| 4626 | */ |
| 4627 | BTRFS_I(inode)->index_cnt = 2; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4628 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4629 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 4630 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4631 | btrfs_set_inode_space_info(root, inode); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4632 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4633 | if (S_ISDIR(mode)) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4634 | owner = 0; |
| 4635 | else |
| 4636 | owner = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4637 | |
| 4638 | key[0].objectid = objectid; |
| 4639 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 4640 | key[0].offset = 0; |
| 4641 | |
| 4642 | key[1].objectid = objectid; |
| 4643 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 4644 | key[1].offset = ref_objectid; |
| 4645 | |
| 4646 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 4647 | sizes[1] = name_len + sizeof(*ref); |
| 4648 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4649 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4650 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 4651 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4652 | goto fail; |
| 4653 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 4654 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4655 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4656 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4657 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4658 | struct btrfs_inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4659 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4660 | |
| 4661 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 4662 | struct btrfs_inode_ref); |
| 4663 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4664 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4665 | ptr = (unsigned long)(ref + 1); |
| 4666 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 4667 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4668 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 4669 | btrfs_free_path(path); |
| 4670 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4671 | location = &BTRFS_I(inode)->location; |
| 4672 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4673 | location->offset = 0; |
| 4674 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 4675 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4676 | btrfs_inherit_iflags(inode, dir); |
| 4677 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 4678 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4679 | if (btrfs_test_opt(root, NODATASUM)) |
| 4680 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 4681 | if (btrfs_test_opt(root, NODATACOW) || |
| 4682 | (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW)) |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 4683 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 4684 | } |
| 4685 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4686 | insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4687 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4688 | |
| 4689 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 4690 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4691 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4692 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4693 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4694 | if (dir) |
| 4695 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4696 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 4697 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4698 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 4702 | { |
| 4703 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 4704 | } |
| 4705 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4706 | /* |
| 4707 | * utility function to add 'inode' into 'parent_inode' with |
| 4708 | * a give name and a given sequence number. |
| 4709 | * if 'add_backref' is true, also insert a backref from the |
| 4710 | * inode to the parent directory. |
| 4711 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4712 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 4713 | struct inode *parent_inode, struct inode *inode, |
| 4714 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4715 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4716 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4717 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4718 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4719 | u64 ino = btrfs_ino(inode); |
| 4720 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4721 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4722 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4723 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 4724 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4725 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4726 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 4727 | key.offset = 0; |
| 4728 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4729 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4730 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4731 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 4732 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4733 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4734 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4735 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 4736 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4737 | } |
| 4738 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4739 | /* Nothing to clean up yet */ |
| 4740 | if (ret) |
| 4741 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4742 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4743 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 4744 | parent_inode, &key, |
| 4745 | btrfs_inode_type(inode), index); |
| 4746 | if (ret == -EEXIST) |
| 4747 | goto fail_dir_item; |
| 4748 | else if (ret) { |
| 4749 | btrfs_abort_transaction(trans, root, ret); |
| 4750 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4751 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4752 | |
| 4753 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 4754 | name_len * 2); |
| 4755 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 4756 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 4757 | if (ret) |
| 4758 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4759 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 4760 | |
| 4761 | fail_dir_item: |
| 4762 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 4763 | u64 local_index; |
| 4764 | int err; |
| 4765 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 4766 | key.objectid, root->root_key.objectid, |
| 4767 | parent_ino, &local_index, name, name_len); |
| 4768 | |
| 4769 | } else if (add_backref) { |
| 4770 | u64 local_index; |
| 4771 | int err; |
| 4772 | |
| 4773 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 4774 | ino, parent_ino, &local_index); |
| 4775 | } |
| 4776 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4780 | struct inode *dir, struct dentry *dentry, |
| 4781 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4782 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4783 | int err = btrfs_add_link(trans, dir, inode, |
| 4784 | dentry->d_name.name, dentry->d_name.len, |
| 4785 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4786 | if (err > 0) |
| 4787 | err = -EEXIST; |
| 4788 | return err; |
| 4789 | } |
| 4790 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4791 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 4792 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4793 | { |
| 4794 | struct btrfs_trans_handle *trans; |
| 4795 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4796 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4797 | int err; |
| 4798 | int drop_inode = 0; |
| 4799 | u64 objectid; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4800 | unsigned long nr = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4801 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4802 | |
| 4803 | if (!new_valid_dev(rdev)) |
| 4804 | return -EINVAL; |
| 4805 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4806 | /* |
| 4807 | * 2 for inode item and ref |
| 4808 | * 2 for dir items |
| 4809 | * 1 for xattr if selinux is on |
| 4810 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4811 | trans = btrfs_start_transaction(root, 5); |
| 4812 | if (IS_ERR(trans)) |
| 4813 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4814 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4815 | err = btrfs_find_free_ino(root, &objectid); |
| 4816 | if (err) |
| 4817 | goto out_unlock; |
| 4818 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4819 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4820 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4821 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4822 | if (IS_ERR(inode)) { |
| 4823 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4824 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4825 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4826 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4827 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4828 | if (err) { |
| 4829 | drop_inode = 1; |
| 4830 | goto out_unlock; |
| 4831 | } |
| 4832 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 4833 | /* |
| 4834 | * If the active LSM wants to access the inode during |
| 4835 | * d_instantiate it needs these. Smack checks to see |
| 4836 | * if the filesystem supports xattrs by looking at the |
| 4837 | * ops vector. |
| 4838 | */ |
| 4839 | |
| 4840 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4841 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4842 | if (err) |
| 4843 | drop_inode = 1; |
| 4844 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4845 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 4846 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 4847 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4848 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4849 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4850 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4851 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4852 | btrfs_btree_balance_dirty(root, nr); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4853 | if (drop_inode) { |
| 4854 | inode_dec_link_count(inode); |
| 4855 | iput(inode); |
| 4856 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 4857 | return err; |
| 4858 | } |
| 4859 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4860 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 4861 | umode_t mode, struct nameidata *nd) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4862 | { |
| 4863 | struct btrfs_trans_handle *trans; |
| 4864 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4865 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4866 | int drop_inode = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4867 | int err; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4868 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4869 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4870 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4871 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4872 | /* |
| 4873 | * 2 for inode item and ref |
| 4874 | * 2 for dir items |
| 4875 | * 1 for xattr if selinux is on |
| 4876 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4877 | trans = btrfs_start_transaction(root, 5); |
| 4878 | if (IS_ERR(trans)) |
| 4879 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4880 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4881 | err = btrfs_find_free_ino(root, &objectid); |
| 4882 | if (err) |
| 4883 | goto out_unlock; |
| 4884 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4885 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4886 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 4887 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4888 | if (IS_ERR(inode)) { |
| 4889 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4890 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 4891 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4892 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 4893 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4894 | if (err) { |
| 4895 | drop_inode = 1; |
| 4896 | goto out_unlock; |
| 4897 | } |
| 4898 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 4899 | /* |
| 4900 | * If the active LSM wants to access the inode during |
| 4901 | * d_instantiate it needs these. Smack checks to see |
| 4902 | * if the filesystem supports xattrs by looking at the |
| 4903 | * ops vector. |
| 4904 | */ |
| 4905 | inode->i_fop = &btrfs_file_operations; |
| 4906 | inode->i_op = &btrfs_file_inode_operations; |
| 4907 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4908 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4909 | if (err) |
| 4910 | drop_inode = 1; |
| 4911 | else { |
| 4912 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 4913 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4914 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 4915 | d_instantiate(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4916 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4917 | out_unlock: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4918 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4919 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4920 | if (drop_inode) { |
| 4921 | inode_dec_link_count(inode); |
| 4922 | iput(inode); |
| 4923 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4924 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4925 | return err; |
| 4926 | } |
| 4927 | |
| 4928 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 4929 | struct dentry *dentry) |
| 4930 | { |
| 4931 | struct btrfs_trans_handle *trans; |
| 4932 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4933 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4934 | u64 index; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4935 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4936 | int err; |
| 4937 | int drop_inode = 0; |
| 4938 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4939 | /* do not allow sys_link's with other subvols of the same device */ |
| 4940 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 4941 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 4942 | |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 4943 | if (inode->i_nlink == ~0U) |
| 4944 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4945 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4946 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4947 | if (err) |
| 4948 | goto fail; |
| 4949 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4950 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4951 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4952 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4953 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4954 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 4955 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4956 | if (IS_ERR(trans)) { |
| 4957 | err = PTR_ERR(trans); |
| 4958 | goto fail; |
| 4959 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4960 | |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 4961 | btrfs_inc_nlink(inode); |
| 4962 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 4963 | ihold(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 4964 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 4965 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4966 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4967 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4968 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4969 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 4970 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4971 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4972 | if (err) |
| 4973 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 4974 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 4975 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4976 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4977 | |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4978 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4979 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 4980 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4981 | if (drop_inode) { |
| 4982 | inode_dec_link_count(inode); |
| 4983 | iput(inode); |
| 4984 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4985 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4986 | return err; |
| 4987 | } |
| 4988 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 4989 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4990 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4991 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4992 | struct btrfs_trans_handle *trans; |
| 4993 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 4994 | int err = 0; |
| 4995 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 4996 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 4997 | u64 index = 0; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 4998 | unsigned long nr = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4999 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5000 | /* |
| 5001 | * 2 items for inode and ref |
| 5002 | * 2 items for dir items |
| 5003 | * 1 for xattr if selinux is on |
| 5004 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5005 | trans = btrfs_start_transaction(root, 5); |
| 5006 | if (IS_ERR(trans)) |
| 5007 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5008 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5009 | err = btrfs_find_free_ino(root, &objectid); |
| 5010 | if (err) |
| 5011 | goto out_fail; |
| 5012 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5013 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5014 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5015 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5016 | if (IS_ERR(inode)) { |
| 5017 | err = PTR_ERR(inode); |
| 5018 | goto out_fail; |
| 5019 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5020 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5021 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5022 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5023 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5024 | if (err) |
| 5025 | goto out_fail; |
| 5026 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5027 | inode->i_op = &btrfs_dir_inode_operations; |
| 5028 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5029 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5030 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5031 | err = btrfs_update_inode(trans, root, inode); |
| 5032 | if (err) |
| 5033 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5034 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5035 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5036 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5037 | if (err) |
| 5038 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5039 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5040 | d_instantiate(dentry, inode); |
| 5041 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5042 | |
| 5043 | out_fail: |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5044 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5045 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5046 | if (drop_on_err) |
| 5047 | iput(inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 5048 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5049 | return err; |
| 5050 | } |
| 5051 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5052 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 5053 | * and an extent that you want to insert, deal with overlap and insert |
| 5054 | * the new extent into the tree. |
| 5055 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5056 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 5057 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5058 | struct extent_map *em, |
| 5059 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5060 | { |
| 5061 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5062 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5063 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 5064 | start_diff = map_start - em->start; |
| 5065 | em->start = map_start; |
| 5066 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5067 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 5068 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5069 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5070 | em->block_len -= start_diff; |
| 5071 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5072 | return add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5073 | } |
| 5074 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5075 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 5076 | struct inode *inode, struct page *page, |
| 5077 | size_t pg_offset, u64 extent_offset, |
| 5078 | struct btrfs_file_extent_item *item) |
| 5079 | { |
| 5080 | int ret; |
| 5081 | struct extent_buffer *leaf = path->nodes[0]; |
| 5082 | char *tmp; |
| 5083 | size_t max_size; |
| 5084 | unsigned long inline_size; |
| 5085 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5086 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5087 | |
| 5088 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5089 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5090 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 5091 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
| 5092 | btrfs_item_nr(leaf, path->slots[0])); |
| 5093 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 5094 | if (!tmp) |
| 5095 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5096 | ptr = btrfs_file_extent_inline_start(item); |
| 5097 | |
| 5098 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 5099 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 5100 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5101 | ret = btrfs_decompress(compress_type, tmp, page, |
| 5102 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5103 | if (ret) { |
| 5104 | char *kaddr = kmap_atomic(page, KM_USER0); |
| 5105 | unsigned long copy_size = min_t(u64, |
| 5106 | PAGE_CACHE_SIZE - pg_offset, |
| 5107 | max_size - extent_offset); |
| 5108 | memset(kaddr + pg_offset, 0, copy_size); |
| 5109 | kunmap_atomic(kaddr, KM_USER0); |
| 5110 | } |
| 5111 | kfree(tmp); |
| 5112 | return 0; |
| 5113 | } |
| 5114 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5115 | /* |
| 5116 | * 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] | 5117 | * the ugly parts come from merging extents from the disk with the in-ram |
| 5118 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5119 | * where the in-ram extents might be locked pending data=ordered completion. |
| 5120 | * |
| 5121 | * This also copies inline extents directly into the page. |
| 5122 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5123 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5124 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5125 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5126 | int create) |
| 5127 | { |
| 5128 | int ret; |
| 5129 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5130 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5131 | u64 extent_start = 0; |
| 5132 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5133 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5134 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5135 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5136 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5137 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5138 | struct extent_buffer *leaf; |
| 5139 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5140 | struct extent_map *em = NULL; |
| 5141 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5142 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5143 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5144 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5145 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5146 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5147 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5148 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5149 | if (em) |
| 5150 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5151 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5152 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5153 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5154 | if (em->start > start || em->start + em->len <= start) |
| 5155 | free_extent_map(em); |
| 5156 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5157 | free_extent_map(em); |
| 5158 | else |
| 5159 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5160 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5161 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5162 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5163 | err = -ENOMEM; |
| 5164 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5165 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5166 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5167 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 5168 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5169 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5170 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5171 | |
| 5172 | if (!path) { |
| 5173 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5174 | if (!path) { |
| 5175 | err = -ENOMEM; |
| 5176 | goto out; |
| 5177 | } |
| 5178 | /* |
| 5179 | * Chances are we'll be called again, so go ahead and do |
| 5180 | * readahead |
| 5181 | */ |
| 5182 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5183 | } |
| 5184 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5185 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 5186 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5187 | if (ret < 0) { |
| 5188 | err = ret; |
| 5189 | goto out; |
| 5190 | } |
| 5191 | |
| 5192 | if (ret != 0) { |
| 5193 | if (path->slots[0] == 0) |
| 5194 | goto not_found; |
| 5195 | path->slots[0]--; |
| 5196 | } |
| 5197 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5198 | leaf = path->nodes[0]; |
| 5199 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5200 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5201 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5202 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5203 | found_type = btrfs_key_type(&found_key); |
| 5204 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5205 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 5206 | goto not_found; |
| 5207 | } |
| 5208 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5209 | found_type = btrfs_file_extent_type(leaf, item); |
| 5210 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5211 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5212 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5213 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5214 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5215 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5216 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 5217 | size_t size; |
| 5218 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5219 | extent_end = (extent_start + size + root->sectorsize - 1) & |
| 5220 | ~((u64)root->sectorsize - 1); |
| 5221 | } |
| 5222 | |
| 5223 | if (start >= extent_end) { |
| 5224 | path->slots[0]++; |
| 5225 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 5226 | ret = btrfs_next_leaf(root, path); |
| 5227 | if (ret < 0) { |
| 5228 | err = ret; |
| 5229 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5230 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5231 | if (ret > 0) |
| 5232 | goto not_found; |
| 5233 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5234 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5235 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5236 | if (found_key.objectid != objectid || |
| 5237 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 5238 | goto not_found; |
| 5239 | if (start + len <= found_key.offset) |
| 5240 | goto not_found; |
| 5241 | em->start = start; |
| 5242 | em->len = found_key.offset - start; |
| 5243 | goto not_found_em; |
| 5244 | } |
| 5245 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5246 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 5247 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5248 | em->start = extent_start; |
| 5249 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5250 | em->orig_start = extent_start - |
| 5251 | btrfs_file_extent_offset(leaf, item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5252 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 5253 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5254 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5255 | goto insert; |
| 5256 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5257 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5258 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5259 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5260 | em->block_start = bytenr; |
| 5261 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 5262 | item); |
| 5263 | } else { |
| 5264 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 5265 | em->block_start = bytenr; |
| 5266 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5267 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 5268 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5269 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5270 | goto insert; |
| 5271 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5272 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5273 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5274 | size_t size; |
| 5275 | size_t extent_offset; |
| 5276 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5277 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5278 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5279 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5280 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5281 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5282 | goto out; |
| 5283 | } |
| 5284 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5285 | size = btrfs_file_extent_inline_len(leaf, item); |
| 5286 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5287 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5288 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 5289 | em->start = extent_start + extent_offset; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5290 | em->len = (copy_size + root->sectorsize - 1) & |
| 5291 | ~((u64)root->sectorsize - 1); |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 5292 | em->orig_start = EXTENT_MAP_INLINE; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5293 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5294 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5295 | em->compress_type = compress_type; |
| 5296 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 5297 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5298 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5299 | if (btrfs_file_extent_compression(leaf, item) != |
| 5300 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5301 | ret = uncompress_inline(path, inode, page, |
| 5302 | pg_offset, |
| 5303 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5304 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5305 | } else { |
| 5306 | map = kmap(page); |
| 5307 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 5308 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 5309 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 5310 | memset(map + pg_offset + copy_size, 0, |
| 5311 | PAGE_CACHE_SIZE - pg_offset - |
| 5312 | copy_size); |
| 5313 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5314 | kunmap(page); |
| 5315 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5316 | flush_dcache_page(page); |
| 5317 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 5318 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5319 | if (!trans) { |
| 5320 | kunmap(page); |
| 5321 | free_extent_map(em); |
| 5322 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5323 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5324 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5325 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5326 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5327 | if (IS_ERR(trans)) |
| 5328 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5329 | goto again; |
| 5330 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5331 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5332 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5333 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5334 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 5335 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5336 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5337 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 5338 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5339 | goto insert; |
| 5340 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5341 | printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5342 | WARN_ON(1); |
| 5343 | } |
| 5344 | not_found: |
| 5345 | em->start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5346 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5347 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5348 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 5349 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5350 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5351 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5352 | if (em->start > start || extent_map_end(em) <= start) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5353 | printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " |
| 5354 | "[%llu %llu]\n", (unsigned long long)em->start, |
| 5355 | (unsigned long long)em->len, |
| 5356 | (unsigned long long)start, |
| 5357 | (unsigned long long)len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5358 | err = -EIO; |
| 5359 | goto out; |
| 5360 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5361 | |
| 5362 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5363 | write_lock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5364 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5365 | /* it is possible that someone inserted the extent into the tree |
| 5366 | * while we had the lock dropped. It is also possible that |
| 5367 | * an overlapping map exists in the tree |
| 5368 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5369 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5370 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5371 | |
| 5372 | ret = 0; |
| 5373 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5374 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 5375 | if (existing && (existing->start > start || |
| 5376 | existing->start + existing->len <= start)) { |
| 5377 | free_extent_map(existing); |
| 5378 | existing = NULL; |
| 5379 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5380 | if (!existing) { |
| 5381 | existing = lookup_extent_mapping(em_tree, em->start, |
| 5382 | em->len); |
| 5383 | if (existing) { |
| 5384 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5385 | em, start, |
| 5386 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5387 | free_extent_map(existing); |
| 5388 | if (err) { |
| 5389 | free_extent_map(em); |
| 5390 | em = NULL; |
| 5391 | } |
| 5392 | } else { |
| 5393 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5394 | free_extent_map(em); |
| 5395 | em = NULL; |
| 5396 | } |
| 5397 | } else { |
| 5398 | free_extent_map(em); |
| 5399 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5400 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5401 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5402 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5403 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5404 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5405 | |
| 5406 | trace_btrfs_get_extent(root, em); |
| 5407 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 5408 | if (path) |
| 5409 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5410 | if (trans) { |
| 5411 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5412 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5413 | err = ret; |
| 5414 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5415 | if (err) { |
| 5416 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5417 | return ERR_PTR(err); |
| 5418 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5419 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5420 | return em; |
| 5421 | } |
| 5422 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5423 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 5424 | size_t pg_offset, u64 start, u64 len, |
| 5425 | int create) |
| 5426 | { |
| 5427 | struct extent_map *em; |
| 5428 | struct extent_map *hole_em = NULL; |
| 5429 | u64 range_start = start; |
| 5430 | u64 end; |
| 5431 | u64 found; |
| 5432 | u64 found_end; |
| 5433 | int err = 0; |
| 5434 | |
| 5435 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 5436 | if (IS_ERR(em)) |
| 5437 | return em; |
| 5438 | if (em) { |
| 5439 | /* |
| 5440 | * if our em maps to a hole, there might |
| 5441 | * actually be delalloc bytes behind it |
| 5442 | */ |
| 5443 | if (em->block_start != EXTENT_MAP_HOLE) |
| 5444 | return em; |
| 5445 | else |
| 5446 | hole_em = em; |
| 5447 | } |
| 5448 | |
| 5449 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 5450 | end = start + len; |
| 5451 | if (end < start) |
| 5452 | end = (u64)-1; |
| 5453 | else |
| 5454 | end -= 1; |
| 5455 | |
| 5456 | em = NULL; |
| 5457 | |
| 5458 | /* ok, we didn't find anything, lets look for delalloc */ |
| 5459 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 5460 | end, len, EXTENT_DELALLOC, 1); |
| 5461 | found_end = range_start + found; |
| 5462 | if (found_end < range_start) |
| 5463 | found_end = (u64)-1; |
| 5464 | |
| 5465 | /* |
| 5466 | * we didn't find anything useful, return |
| 5467 | * the original results from get_extent() |
| 5468 | */ |
| 5469 | if (range_start > end || found_end <= start) { |
| 5470 | em = hole_em; |
| 5471 | hole_em = NULL; |
| 5472 | goto out; |
| 5473 | } |
| 5474 | |
| 5475 | /* adjust the range_start to make sure it doesn't |
| 5476 | * go backwards from the start they passed in |
| 5477 | */ |
| 5478 | range_start = max(start,range_start); |
| 5479 | found = found_end - range_start; |
| 5480 | |
| 5481 | if (found > 0) { |
| 5482 | u64 hole_start = start; |
| 5483 | u64 hole_len = len; |
| 5484 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5485 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5486 | if (!em) { |
| 5487 | err = -ENOMEM; |
| 5488 | goto out; |
| 5489 | } |
| 5490 | /* |
| 5491 | * when btrfs_get_extent can't find anything it |
| 5492 | * returns one huge hole |
| 5493 | * |
| 5494 | * make sure what it found really fits our range, and |
| 5495 | * adjust to make sure it is based on the start from |
| 5496 | * the caller |
| 5497 | */ |
| 5498 | if (hole_em) { |
| 5499 | u64 calc_end = extent_map_end(hole_em); |
| 5500 | |
| 5501 | if (calc_end <= start || (hole_em->start > end)) { |
| 5502 | free_extent_map(hole_em); |
| 5503 | hole_em = NULL; |
| 5504 | } else { |
| 5505 | hole_start = max(hole_em->start, start); |
| 5506 | hole_len = calc_end - hole_start; |
| 5507 | } |
| 5508 | } |
| 5509 | em->bdev = NULL; |
| 5510 | if (hole_em && range_start > hole_start) { |
| 5511 | /* our hole starts before our delalloc, so we |
| 5512 | * have to return just the parts of the hole |
| 5513 | * that go until the delalloc starts |
| 5514 | */ |
| 5515 | em->len = min(hole_len, |
| 5516 | range_start - hole_start); |
| 5517 | em->start = hole_start; |
| 5518 | em->orig_start = hole_start; |
| 5519 | /* |
| 5520 | * don't adjust block start at all, |
| 5521 | * it is fixed at EXTENT_MAP_HOLE |
| 5522 | */ |
| 5523 | em->block_start = hole_em->block_start; |
| 5524 | em->block_len = hole_len; |
| 5525 | } else { |
| 5526 | em->start = range_start; |
| 5527 | em->len = found; |
| 5528 | em->orig_start = range_start; |
| 5529 | em->block_start = EXTENT_MAP_DELALLOC; |
| 5530 | em->block_len = found; |
| 5531 | } |
| 5532 | } else if (hole_em) { |
| 5533 | return hole_em; |
| 5534 | } |
| 5535 | out: |
| 5536 | |
| 5537 | free_extent_map(hole_em); |
| 5538 | if (err) { |
| 5539 | free_extent_map(em); |
| 5540 | return ERR_PTR(err); |
| 5541 | } |
| 5542 | return em; |
| 5543 | } |
| 5544 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5545 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5546 | struct extent_map *em, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5547 | u64 start, u64 len) |
| 5548 | { |
| 5549 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5550 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5551 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 5552 | struct btrfs_key ins; |
| 5553 | u64 alloc_hint; |
| 5554 | int ret; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5555 | bool insert = false; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5556 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5557 | /* |
| 5558 | * Ok if the extent map we looked up is a hole and is for the exact |
| 5559 | * range we want, there is no reason to allocate a new one, however if |
| 5560 | * it is not right then we need to free this one and drop the cache for |
| 5561 | * our range. |
| 5562 | */ |
| 5563 | if (em->block_start != EXTENT_MAP_HOLE || em->start != start || |
| 5564 | em->len != len) { |
| 5565 | free_extent_map(em); |
| 5566 | em = NULL; |
| 5567 | insert = true; |
| 5568 | btrfs_drop_extent_cache(inode, start, start + len - 1, 0); |
| 5569 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5570 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5571 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5572 | if (IS_ERR(trans)) |
| 5573 | return ERR_CAST(trans); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5574 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 5575 | if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024) |
| 5576 | btrfs_add_inode_defrag(trans, inode); |
| 5577 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5578 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5579 | |
| 5580 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
| 5581 | ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 5582 | alloc_hint, &ins, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5583 | if (ret) { |
| 5584 | em = ERR_PTR(ret); |
| 5585 | goto out; |
| 5586 | } |
| 5587 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5588 | if (!em) { |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5589 | em = alloc_extent_map(); |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5590 | if (!em) { |
| 5591 | em = ERR_PTR(-ENOMEM); |
| 5592 | goto out; |
| 5593 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | em->start = start; |
| 5597 | em->orig_start = em->start; |
| 5598 | em->len = ins.offset; |
| 5599 | |
| 5600 | em->block_start = ins.objectid; |
| 5601 | em->block_len = ins.offset; |
| 5602 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5603 | |
| 5604 | /* |
| 5605 | * We need to do this because if we're using the original em we searched |
| 5606 | * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that. |
| 5607 | */ |
| 5608 | em->flags = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5609 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5610 | |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5611 | while (insert) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5612 | write_lock(&em_tree->lock); |
| 5613 | ret = add_extent_mapping(em_tree, em); |
| 5614 | write_unlock(&em_tree->lock); |
| 5615 | if (ret != -EEXIST) |
| 5616 | break; |
| 5617 | btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0); |
| 5618 | } |
| 5619 | |
| 5620 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 5621 | ins.offset, ins.offset, 0); |
| 5622 | if (ret) { |
| 5623 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 5624 | em = ERR_PTR(ret); |
| 5625 | } |
| 5626 | out: |
| 5627 | btrfs_end_transaction(trans, root); |
| 5628 | return em; |
| 5629 | } |
| 5630 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5631 | /* |
| 5632 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 5633 | * block must be cow'd |
| 5634 | */ |
| 5635 | static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans, |
| 5636 | struct inode *inode, u64 offset, u64 len) |
| 5637 | { |
| 5638 | struct btrfs_path *path; |
| 5639 | int ret; |
| 5640 | struct extent_buffer *leaf; |
| 5641 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5642 | struct btrfs_file_extent_item *fi; |
| 5643 | struct btrfs_key key; |
| 5644 | u64 disk_bytenr; |
| 5645 | u64 backref_offset; |
| 5646 | u64 extent_end; |
| 5647 | u64 num_bytes; |
| 5648 | int slot; |
| 5649 | int found_type; |
| 5650 | |
| 5651 | path = btrfs_alloc_path(); |
| 5652 | if (!path) |
| 5653 | return -ENOMEM; |
| 5654 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5655 | ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5656 | offset, 0); |
| 5657 | if (ret < 0) |
| 5658 | goto out; |
| 5659 | |
| 5660 | slot = path->slots[0]; |
| 5661 | if (ret == 1) { |
| 5662 | if (slot == 0) { |
| 5663 | /* can't find the item, must cow */ |
| 5664 | ret = 0; |
| 5665 | goto out; |
| 5666 | } |
| 5667 | slot--; |
| 5668 | } |
| 5669 | ret = 0; |
| 5670 | leaf = path->nodes[0]; |
| 5671 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5672 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5673 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5674 | /* not our file or wrong item type, must cow */ |
| 5675 | goto out; |
| 5676 | } |
| 5677 | |
| 5678 | if (key.offset > offset) { |
| 5679 | /* Wrong offset, must cow */ |
| 5680 | goto out; |
| 5681 | } |
| 5682 | |
| 5683 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 5684 | found_type = btrfs_file_extent_type(leaf, fi); |
| 5685 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 5686 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 5687 | /* not a regular extent, must cow */ |
| 5688 | goto out; |
| 5689 | } |
| 5690 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5691 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 5692 | |
| 5693 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 5694 | if (extent_end < offset + len) { |
| 5695 | /* extent doesn't include our full range, must cow */ |
| 5696 | goto out; |
| 5697 | } |
| 5698 | |
| 5699 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 5700 | goto out; |
| 5701 | |
| 5702 | /* |
| 5703 | * look for other files referencing this extent, if we |
| 5704 | * find any we must cow |
| 5705 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5706 | if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5707 | key.offset - backref_offset, disk_bytenr)) |
| 5708 | goto out; |
| 5709 | |
| 5710 | /* |
| 5711 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 5712 | * in this extent we are about to write. If there |
| 5713 | * are any csums in that range we have to cow in order |
| 5714 | * to keep the csums correct |
| 5715 | */ |
| 5716 | disk_bytenr += backref_offset; |
| 5717 | disk_bytenr += offset - key.offset; |
| 5718 | num_bytes = min(offset + len, extent_end) - offset; |
| 5719 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 5720 | goto out; |
| 5721 | /* |
| 5722 | * all of the above have passed, it is safe to overwrite this extent |
| 5723 | * without cow |
| 5724 | */ |
| 5725 | ret = 1; |
| 5726 | out: |
| 5727 | btrfs_free_path(path); |
| 5728 | return ret; |
| 5729 | } |
| 5730 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5731 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 5732 | struct buffer_head *bh_result, int create) |
| 5733 | { |
| 5734 | struct extent_map *em; |
| 5735 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5736 | u64 start = iblock << inode->i_blkbits; |
| 5737 | u64 len = bh_result->b_size; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5738 | struct btrfs_trans_handle *trans; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5739 | |
| 5740 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 5741 | if (IS_ERR(em)) |
| 5742 | return PTR_ERR(em); |
| 5743 | |
| 5744 | /* |
| 5745 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 5746 | * io. INLINE is special, and we could probably kludge it in here, but |
| 5747 | * it's still buffered so for safety lets just fall back to the generic |
| 5748 | * buffered path. |
| 5749 | * |
| 5750 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 5751 | * decompress it, so there will be buffering required no matter what we |
| 5752 | * do, so go ahead and fallback to buffered. |
| 5753 | * |
| 5754 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 5755 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 5756 | * the generic code. |
| 5757 | */ |
| 5758 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 5759 | em->block_start == EXTENT_MAP_INLINE) { |
| 5760 | free_extent_map(em); |
| 5761 | return -ENOTBLK; |
| 5762 | } |
| 5763 | |
| 5764 | /* Just a good old fashioned hole, return */ |
| 5765 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 5766 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 5767 | free_extent_map(em); |
| 5768 | /* DIO will do one hole at a time, so just unlock a sector */ |
| 5769 | unlock_extent(&BTRFS_I(inode)->io_tree, start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 5770 | start + root->sectorsize - 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5771 | return 0; |
| 5772 | } |
| 5773 | |
| 5774 | /* |
| 5775 | * We don't allocate a new extent in the following cases |
| 5776 | * |
| 5777 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 5778 | * existing extent. |
| 5779 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 5780 | * just use the extent. |
| 5781 | * |
| 5782 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5783 | if (!create) { |
| 5784 | len = em->len - (start - em->start); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5785 | goto map; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5786 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5787 | |
| 5788 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 5789 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 5790 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5791 | int type; |
| 5792 | int ret; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5793 | u64 block_start; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5794 | |
| 5795 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5796 | type = BTRFS_ORDERED_PREALLOC; |
| 5797 | else |
| 5798 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5799 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5800 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5801 | |
| 5802 | /* |
| 5803 | * we're not going to log anything, but we do need |
| 5804 | * to make sure the current transaction stays open |
| 5805 | * while we look for nocow cross refs |
| 5806 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5807 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5808 | if (IS_ERR(trans)) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5809 | goto must_cow; |
| 5810 | |
| 5811 | if (can_nocow_odirect(trans, inode, start, len) == 1) { |
| 5812 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 5813 | block_start, len, len, type); |
| 5814 | btrfs_end_transaction(trans, root); |
| 5815 | if (ret) { |
| 5816 | free_extent_map(em); |
| 5817 | return ret; |
| 5818 | } |
| 5819 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5820 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5821 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5822 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5823 | must_cow: |
| 5824 | /* |
| 5825 | * this will cow the extent, reset the len in case we changed |
| 5826 | * it above |
| 5827 | */ |
| 5828 | len = bh_result->b_size; |
Josef Bacik | 16d299a | 2011-04-06 14:53:07 -0400 | [diff] [blame] | 5829 | em = btrfs_new_extent_direct(inode, em, start, len); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5830 | if (IS_ERR(em)) |
| 5831 | return PTR_ERR(em); |
| 5832 | len = min(len, em->len - (start - em->start)); |
| 5833 | unlock: |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 5834 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
| 5835 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, |
| 5836 | 0, NULL, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5837 | map: |
| 5838 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 5839 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 5840 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5841 | bh_result->b_bdev = em->bdev; |
| 5842 | set_buffer_mapped(bh_result); |
| 5843 | if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5844 | set_buffer_new(bh_result); |
| 5845 | |
| 5846 | free_extent_map(em); |
| 5847 | |
| 5848 | return 0; |
| 5849 | } |
| 5850 | |
| 5851 | struct btrfs_dio_private { |
| 5852 | struct inode *inode; |
| 5853 | u64 logical_offset; |
| 5854 | u64 disk_bytenr; |
| 5855 | u64 bytes; |
| 5856 | u32 *csums; |
| 5857 | void *private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5858 | |
| 5859 | /* number of bios pending for this dio */ |
| 5860 | atomic_t pending_bios; |
| 5861 | |
| 5862 | /* IO errors */ |
| 5863 | int errors; |
| 5864 | |
| 5865 | struct bio *orig_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5866 | }; |
| 5867 | |
| 5868 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 5869 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 5870 | struct btrfs_dio_private *dip = bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5871 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
| 5872 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5873 | struct inode *inode = dip->inode; |
| 5874 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5875 | u64 start; |
| 5876 | u32 *private = dip->csums; |
| 5877 | |
| 5878 | start = dip->logical_offset; |
| 5879 | do { |
| 5880 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 5881 | struct page *page = bvec->bv_page; |
| 5882 | char *kaddr; |
| 5883 | u32 csum = ~(u32)0; |
| 5884 | unsigned long flags; |
| 5885 | |
| 5886 | local_irq_save(flags); |
| 5887 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 5888 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
| 5889 | csum, bvec->bv_len); |
| 5890 | btrfs_csum_final(csum, (char *)&csum); |
| 5891 | kunmap_atomic(kaddr, KM_IRQ0); |
| 5892 | local_irq_restore(flags); |
| 5893 | |
| 5894 | flush_dcache_page(bvec->bv_page); |
| 5895 | if (csum != *private) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5896 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5897 | " %llu csum %u private %u\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5898 | (unsigned long long)btrfs_ino(inode), |
| 5899 | (unsigned long long)start, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5900 | csum, *private); |
| 5901 | err = -EIO; |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | start += bvec->bv_len; |
| 5906 | private++; |
| 5907 | bvec++; |
| 5908 | } while (bvec <= bvec_end); |
| 5909 | |
| 5910 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 5911 | dip->logical_offset + dip->bytes - 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5912 | bio->bi_private = dip->private; |
| 5913 | |
| 5914 | kfree(dip->csums); |
| 5915 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 5916 | |
| 5917 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 5918 | if (err) |
| 5919 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5920 | dio_end_io(bio, err); |
| 5921 | } |
| 5922 | |
| 5923 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 5924 | { |
| 5925 | struct btrfs_dio_private *dip = bio->bi_private; |
| 5926 | struct inode *inode = dip->inode; |
| 5927 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5928 | struct btrfs_trans_handle *trans; |
| 5929 | struct btrfs_ordered_extent *ordered = NULL; |
| 5930 | struct extent_state *cached_state = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5931 | u64 ordered_offset = dip->logical_offset; |
| 5932 | u64 ordered_bytes = dip->bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5933 | int ret; |
| 5934 | |
| 5935 | if (err) |
| 5936 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5937 | again: |
| 5938 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 5939 | &ordered_offset, |
| 5940 | ordered_bytes); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5941 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 5942 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5943 | |
| 5944 | BUG_ON(!ordered); |
| 5945 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5946 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5947 | if (IS_ERR(trans)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5948 | err = -ENOMEM; |
| 5949 | goto out; |
| 5950 | } |
| 5951 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 5952 | |
| 5953 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) { |
| 5954 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
| 5955 | if (!ret) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 5956 | err = btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5957 | goto out; |
| 5958 | } |
| 5959 | |
| 5960 | lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5961 | ordered->file_offset + ordered->len - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 5962 | &cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5963 | |
| 5964 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) { |
| 5965 | ret = btrfs_mark_extent_written(trans, inode, |
| 5966 | ordered->file_offset, |
| 5967 | ordered->file_offset + |
| 5968 | ordered->len); |
| 5969 | if (ret) { |
| 5970 | err = ret; |
| 5971 | goto out_unlock; |
| 5972 | } |
| 5973 | } else { |
| 5974 | ret = insert_reserved_file_extent(trans, inode, |
| 5975 | ordered->file_offset, |
| 5976 | ordered->start, |
| 5977 | ordered->disk_len, |
| 5978 | ordered->len, |
| 5979 | ordered->len, |
| 5980 | 0, 0, 0, |
| 5981 | BTRFS_FILE_EXTENT_REG); |
| 5982 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 5983 | ordered->file_offset, ordered->len); |
| 5984 | if (ret) { |
| 5985 | err = ret; |
| 5986 | WARN_ON(1); |
| 5987 | goto out_unlock; |
| 5988 | } |
| 5989 | } |
| 5990 | |
| 5991 | add_pending_csums(trans, inode, ordered->file_offset, &ordered->list); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5992 | ret = btrfs_ordered_update_i_size(inode, 0, ordered); |
Miao Xie | a39f752 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 5993 | if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 5994 | btrfs_update_inode_fallback(trans, root, inode); |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 5995 | ret = 0; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 5996 | out_unlock: |
| 5997 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset, |
| 5998 | ordered->file_offset + ordered->len - 1, |
| 5999 | &cached_state, GFP_NOFS); |
| 6000 | out: |
| 6001 | btrfs_delalloc_release_metadata(inode, ordered->len); |
| 6002 | btrfs_end_transaction(trans, root); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6003 | ordered_offset = ordered->file_offset + ordered->len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6004 | btrfs_put_ordered_extent(ordered); |
| 6005 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 6006 | |
| 6007 | out_test: |
| 6008 | /* |
| 6009 | * our bio might span multiple ordered extents. If we haven't |
| 6010 | * completed the accounting for the whole dio, go back and try again |
| 6011 | */ |
| 6012 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 6013 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 6014 | ordered_offset; |
| 6015 | goto again; |
| 6016 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6017 | out_done: |
| 6018 | bio->bi_private = dip->private; |
| 6019 | |
| 6020 | kfree(dip->csums); |
| 6021 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6022 | |
| 6023 | /* If we had an error make sure to clear the uptodate flag */ |
| 6024 | if (err) |
| 6025 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6026 | dio_end_io(bio, err); |
| 6027 | } |
| 6028 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6029 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 6030 | struct bio *bio, int mirror_num, |
| 6031 | unsigned long bio_flags, u64 offset) |
| 6032 | { |
| 6033 | int ret; |
| 6034 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6035 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6036 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6037 | return 0; |
| 6038 | } |
| 6039 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6040 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 6041 | { |
| 6042 | struct btrfs_dio_private *dip = bio->bi_private; |
| 6043 | |
| 6044 | if (err) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6045 | printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6046 | "sector %#Lx len %u err no %d\n", |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6047 | (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw, |
Jan Beulich | 3dd1462 | 2010-12-07 14:54:09 +0000 | [diff] [blame] | 6048 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6049 | dip->errors = 1; |
| 6050 | |
| 6051 | /* |
| 6052 | * before atomic variable goto zero, we must make sure |
| 6053 | * dip->errors is perceived to be set. |
| 6054 | */ |
| 6055 | smp_mb__before_atomic_dec(); |
| 6056 | } |
| 6057 | |
| 6058 | /* if there are more bios still pending for this dio, just exit */ |
| 6059 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 6060 | goto out; |
| 6061 | |
| 6062 | if (dip->errors) |
| 6063 | bio_io_error(dip->orig_bio); |
| 6064 | else { |
| 6065 | set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); |
| 6066 | bio_endio(dip->orig_bio, 0); |
| 6067 | } |
| 6068 | out: |
| 6069 | bio_put(bio); |
| 6070 | } |
| 6071 | |
| 6072 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 6073 | u64 first_sector, gfp_t gfp_flags) |
| 6074 | { |
| 6075 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 6076 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 6077 | } |
| 6078 | |
| 6079 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 6080 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6081 | u32 *csums, int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6082 | { |
| 6083 | int write = rw & REQ_WRITE; |
| 6084 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6085 | int ret; |
| 6086 | |
| 6087 | bio_get(bio); |
| 6088 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 6089 | if (ret) |
| 6090 | goto err; |
| 6091 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6092 | if (skip_sum) |
| 6093 | goto map; |
| 6094 | |
| 6095 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6096 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 6097 | inode, rw, bio, 0, 0, |
| 6098 | file_offset, |
| 6099 | __btrfs_submit_bio_start_direct_io, |
| 6100 | __btrfs_submit_bio_done); |
| 6101 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6102 | } else if (write) { |
| 6103 | /* |
| 6104 | * If we aren't doing async submit, calculate the csum of the |
| 6105 | * bio now. |
| 6106 | */ |
| 6107 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 6108 | if (ret) |
| 6109 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6110 | } else if (!skip_sum) { |
| 6111 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6112 | file_offset, csums); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 6113 | if (ret) |
| 6114 | goto err; |
| 6115 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6116 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6117 | map: |
| 6118 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6119 | err: |
| 6120 | bio_put(bio); |
| 6121 | return ret; |
| 6122 | } |
| 6123 | |
| 6124 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 6125 | int skip_sum) |
| 6126 | { |
| 6127 | struct inode *inode = dip->inode; |
| 6128 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6129 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6130 | struct bio *bio; |
| 6131 | struct bio *orig_bio = dip->orig_bio; |
| 6132 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
| 6133 | u64 start_sector = orig_bio->bi_sector; |
| 6134 | u64 file_offset = dip->logical_offset; |
| 6135 | u64 submit_len = 0; |
| 6136 | u64 map_length; |
| 6137 | int nr_pages = 0; |
| 6138 | u32 *csums = dip->csums; |
| 6139 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6140 | int async_submit = 0; |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6141 | int write = rw & REQ_WRITE; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6142 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6143 | map_length = orig_bio->bi_size; |
| 6144 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6145 | &map_length, NULL, 0); |
| 6146 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6147 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6148 | return -EIO; |
| 6149 | } |
| 6150 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6151 | if (map_length >= orig_bio->bi_size) { |
| 6152 | bio = orig_bio; |
| 6153 | goto submit; |
| 6154 | } |
| 6155 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6156 | async_submit = 1; |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6157 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 6158 | if (!bio) |
| 6159 | return -ENOMEM; |
| 6160 | bio->bi_private = dip; |
| 6161 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6162 | atomic_inc(&dip->pending_bios); |
| 6163 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6164 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 6165 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 6166 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 6167 | bvec->bv_offset) < bvec->bv_len)) { |
| 6168 | /* |
| 6169 | * inc the count before we submit the bio so |
| 6170 | * we know the end IO handler won't happen before |
| 6171 | * we inc the count. Otherwise, the dip might get freed |
| 6172 | * before we're done setting it up |
| 6173 | */ |
| 6174 | atomic_inc(&dip->pending_bios); |
| 6175 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 6176 | file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6177 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6178 | if (ret) { |
| 6179 | bio_put(bio); |
| 6180 | atomic_dec(&dip->pending_bios); |
| 6181 | goto out_err; |
| 6182 | } |
| 6183 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6184 | /* Write's use the ordered csums */ |
| 6185 | if (!write && !skip_sum) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6186 | csums = csums + nr_pages; |
| 6187 | start_sector += submit_len >> 9; |
| 6188 | file_offset += submit_len; |
| 6189 | |
| 6190 | submit_len = 0; |
| 6191 | nr_pages = 0; |
| 6192 | |
| 6193 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 6194 | start_sector, GFP_NOFS); |
| 6195 | if (!bio) |
| 6196 | goto out_err; |
| 6197 | bio->bi_private = dip; |
| 6198 | bio->bi_end_io = btrfs_end_dio_bio; |
| 6199 | |
| 6200 | map_length = orig_bio->bi_size; |
| 6201 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
| 6202 | &map_length, NULL, 0); |
| 6203 | if (ret) { |
| 6204 | bio_put(bio); |
| 6205 | goto out_err; |
| 6206 | } |
| 6207 | } else { |
| 6208 | submit_len += bvec->bv_len; |
| 6209 | nr_pages ++; |
| 6210 | bvec++; |
| 6211 | } |
| 6212 | } |
| 6213 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 6214 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6215 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 6216 | csums, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6217 | if (!ret) |
| 6218 | return 0; |
| 6219 | |
| 6220 | bio_put(bio); |
| 6221 | out_err: |
| 6222 | dip->errors = 1; |
| 6223 | /* |
| 6224 | * before atomic variable goto zero, we must |
| 6225 | * make sure dip->errors is perceived to be set. |
| 6226 | */ |
| 6227 | smp_mb__before_atomic_dec(); |
| 6228 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 6229 | bio_io_error(dip->orig_bio); |
| 6230 | |
| 6231 | /* bio_end_io() will handle error, so we needn't return it */ |
| 6232 | return 0; |
| 6233 | } |
| 6234 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6235 | static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, |
| 6236 | loff_t file_offset) |
| 6237 | { |
| 6238 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6239 | struct btrfs_dio_private *dip; |
| 6240 | struct bio_vec *bvec = bio->bi_io_vec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6241 | int skip_sum; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 6242 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6243 | int ret = 0; |
| 6244 | |
| 6245 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 6246 | |
| 6247 | dip = kmalloc(sizeof(*dip), GFP_NOFS); |
| 6248 | if (!dip) { |
| 6249 | ret = -ENOMEM; |
| 6250 | goto free_ordered; |
| 6251 | } |
| 6252 | dip->csums = NULL; |
| 6253 | |
Josef Bacik | 98bc314 | 2011-03-22 11:00:46 -0400 | [diff] [blame] | 6254 | /* Write's use the ordered csum stuff, so we don't need dip->csums */ |
| 6255 | if (!write && !skip_sum) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6256 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); |
| 6257 | if (!dip->csums) { |
Daniel J Blueman | b4966b7 | 2011-03-09 16:46:42 +0000 | [diff] [blame] | 6258 | kfree(dip); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6259 | ret = -ENOMEM; |
| 6260 | goto free_ordered; |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | dip->private = bio->bi_private; |
| 6265 | dip->inode = inode; |
| 6266 | dip->logical_offset = file_offset; |
| 6267 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6268 | dip->bytes = 0; |
| 6269 | do { |
| 6270 | dip->bytes += bvec->bv_len; |
| 6271 | bvec++; |
| 6272 | } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); |
| 6273 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6274 | dip->disk_bytenr = (u64)bio->bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6275 | bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6276 | dip->errors = 0; |
| 6277 | dip->orig_bio = bio; |
| 6278 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6279 | |
| 6280 | if (write) |
| 6281 | bio->bi_end_io = btrfs_endio_direct_write; |
| 6282 | else |
| 6283 | bio->bi_end_io = btrfs_endio_direct_read; |
| 6284 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6285 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 6286 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 6287 | return; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6288 | free_ordered: |
| 6289 | /* |
| 6290 | * If this is a write, we need to clean up the reserved space and kill |
| 6291 | * the ordered extent. |
| 6292 | */ |
| 6293 | if (write) { |
| 6294 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 6295 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6296 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 6297 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 6298 | btrfs_free_reserved_extent(root, ordered->start, |
| 6299 | ordered->disk_len); |
| 6300 | btrfs_put_ordered_extent(ordered); |
| 6301 | btrfs_put_ordered_extent(ordered); |
| 6302 | } |
| 6303 | bio_endio(bio, ret); |
| 6304 | } |
| 6305 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6306 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 6307 | const struct iovec *iov, loff_t offset, |
| 6308 | unsigned long nr_segs) |
| 6309 | { |
| 6310 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6311 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6312 | size_t size; |
| 6313 | unsigned long addr; |
| 6314 | unsigned blocksize_mask = root->sectorsize - 1; |
| 6315 | ssize_t retval = -EINVAL; |
| 6316 | loff_t end = offset; |
| 6317 | |
| 6318 | if (offset & blocksize_mask) |
| 6319 | goto out; |
| 6320 | |
| 6321 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 6322 | for (seg = 0; seg < nr_segs; seg++) { |
| 6323 | addr = (unsigned long)iov[seg].iov_base; |
| 6324 | size = iov[seg].iov_len; |
| 6325 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6326 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6327 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 6328 | |
| 6329 | /* If this is a write we don't need to check anymore */ |
| 6330 | if (rw & WRITE) |
| 6331 | continue; |
| 6332 | |
| 6333 | /* |
| 6334 | * Check to make sure we don't have duplicate iov_base's in this |
| 6335 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 6336 | * when reading back. |
| 6337 | */ |
| 6338 | for (i = seg + 1; i < nr_segs; i++) { |
| 6339 | if (iov[seg].iov_base == iov[i].iov_base) |
| 6340 | goto out; |
| 6341 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6342 | } |
| 6343 | retval = 0; |
| 6344 | out: |
| 6345 | return retval; |
| 6346 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6347 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 6348 | const struct iovec *iov, loff_t offset, |
| 6349 | unsigned long nr_segs) |
| 6350 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6351 | struct file *file = iocb->ki_filp; |
| 6352 | struct inode *inode = file->f_mapping->host; |
| 6353 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6354 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6355 | u64 lockstart, lockend; |
| 6356 | ssize_t ret; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6357 | int writing = rw & WRITE; |
| 6358 | int write_bits = 0; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6359 | size_t count = iov_length(iov, nr_segs); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6360 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6361 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
| 6362 | offset, nr_segs)) { |
| 6363 | return 0; |
| 6364 | } |
| 6365 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6366 | lockstart = offset; |
Chris Mason | 3f7c579 | 2010-05-26 10:59:53 -0400 | [diff] [blame] | 6367 | lockend = offset + count - 1; |
| 6368 | |
| 6369 | if (writing) { |
| 6370 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 6371 | if (ret) |
| 6372 | goto out; |
| 6373 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6374 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6375 | while (1) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6376 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6377 | 0, &cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6378 | /* |
| 6379 | * We're concerned with the entire range that we're going to be |
| 6380 | * doing DIO to, so we need to make sure theres no ordered |
| 6381 | * extents in this range. |
| 6382 | */ |
| 6383 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6384 | lockend - lockstart + 1); |
| 6385 | if (!ordered) |
| 6386 | break; |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6387 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6388 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6389 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6390 | btrfs_put_ordered_extent(ordered); |
| 6391 | cond_resched(); |
| 6392 | } |
| 6393 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6394 | /* |
| 6395 | * we don't use btrfs_set_extent_delalloc because we don't want |
| 6396 | * the dirty or uptodate bits |
| 6397 | */ |
| 6398 | if (writing) { |
| 6399 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; |
| 6400 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6401 | EXTENT_DELALLOC, NULL, &cached_state, |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6402 | GFP_NOFS); |
| 6403 | if (ret) { |
| 6404 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6405 | lockend, EXTENT_LOCKED | write_bits, |
| 6406 | 1, 0, &cached_state, GFP_NOFS); |
| 6407 | goto out; |
| 6408 | } |
| 6409 | } |
| 6410 | |
| 6411 | free_extent_state(cached_state); |
| 6412 | cached_state = NULL; |
| 6413 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 6414 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 6415 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 6416 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
| 6417 | btrfs_submit_direct, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6418 | |
| 6419 | if (ret < 0 && ret != -EIOCBQUEUED) { |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6420 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, |
| 6421 | offset + iov_length(iov, nr_segs) - 1, |
| 6422 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6423 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6424 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { |
| 6425 | /* |
| 6426 | * We're falling back to buffered, unlock the section we didn't |
| 6427 | * do IO on. |
| 6428 | */ |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6429 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, |
| 6430 | offset + iov_length(iov, nr_segs) - 1, |
| 6431 | EXTENT_LOCKED | write_bits, 1, 0, |
| 6432 | &cached_state, GFP_NOFS); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6433 | } |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 6434 | out: |
| 6435 | free_extent_state(cached_state); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6436 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 6437 | } |
| 6438 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6439 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 6440 | __u64 start, __u64 len) |
| 6441 | { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6442 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 6443 | } |
| 6444 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6445 | int btrfs_readpage(struct file *file, struct page *page) |
| 6446 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6447 | struct extent_io_tree *tree; |
| 6448 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 6449 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6450 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6451 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6452 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 6453 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6454 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6455 | |
| 6456 | |
| 6457 | if (current->flags & PF_MEMALLOC) { |
| 6458 | redirty_page_for_writepage(wbc, page); |
| 6459 | unlock_page(page); |
| 6460 | return 0; |
| 6461 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6462 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6463 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 6464 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6465 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6466 | int btrfs_writepages(struct address_space *mapping, |
| 6467 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6468 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6469 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 6470 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6471 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 6472 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 6473 | } |
| 6474 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6475 | static int |
| 6476 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 6477 | struct list_head *pages, unsigned nr_pages) |
| 6478 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6479 | struct extent_io_tree *tree; |
| 6480 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 6481 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 6482 | btrfs_get_extent); |
| 6483 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6484 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6485 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6486 | struct extent_io_tree *tree; |
| 6487 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6488 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6489 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6490 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 6491 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6492 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6493 | if (ret == 1) { |
| 6494 | ClearPagePrivate(page); |
| 6495 | set_page_private(page, 0); |
| 6496 | page_cache_release(page); |
| 6497 | } |
| 6498 | return ret; |
| 6499 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6500 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6501 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 6502 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 6503 | if (PageWriteback(page) || PageDirty(page)) |
| 6504 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 6505 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6506 | } |
| 6507 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6508 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
| 6509 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6510 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6511 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6512 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6513 | u64 page_start = page_offset(page); |
| 6514 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6515 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6516 | |
| 6517 | /* |
| 6518 | * we have the page locked, so new writeback can't start, |
| 6519 | * and the dirty bit won't be cleared while we are here. |
| 6520 | * |
| 6521 | * Wait for IO on this page so that we can safely clear |
| 6522 | * the PagePrivate2 bit and do ordered accounting |
| 6523 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6524 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6525 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6526 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6527 | if (offset) { |
| 6528 | btrfs_releasepage(page, GFP_NOFS); |
| 6529 | return; |
| 6530 | } |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6531 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6532 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, |
| 6533 | page_offset(page)); |
| 6534 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6535 | /* |
| 6536 | * IO on this page will never be started, so we need |
| 6537 | * to account for any ordered extents now |
| 6538 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6539 | clear_extent_bit(tree, page_start, page_end, |
| 6540 | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6541 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6542 | &cached_state, GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 6543 | /* |
| 6544 | * whoever cleared the private bit is responsible |
| 6545 | * for the finish_ordered_io |
| 6546 | */ |
| 6547 | if (TestClearPagePrivate2(page)) { |
| 6548 | btrfs_finish_ordered_io(page->mapping->host, |
| 6549 | page_start, page_end); |
| 6550 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6551 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6552 | cached_state = NULL; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6553 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6554 | } |
| 6555 | clear_extent_bit(tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6556 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6557 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6558 | __btrfs_releasepage(page, GFP_NOFS); |
| 6559 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6560 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6561 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 6562 | ClearPagePrivate(page); |
| 6563 | set_page_private(page, 0); |
| 6564 | page_cache_release(page); |
| 6565 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6566 | } |
| 6567 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6568 | /* |
| 6569 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 6570 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 6571 | * be careful to check for EOF conditions here. We set the page up correctly |
| 6572 | * for a written page which means we get ENOSPC checking when writing into |
| 6573 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 6574 | * support these features. |
| 6575 | * |
| 6576 | * We are not allowed to take the i_mutex here so we have to play games to |
| 6577 | * protect against truncate races as the page could now be beyond EOF. Because |
| 6578 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 6579 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 6580 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 6581 | * unlock the page. |
| 6582 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6583 | 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] | 6584 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 6585 | struct page *page = vmf->page; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 6586 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6587 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6588 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 6589 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6590 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6591 | char *kaddr; |
| 6592 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6593 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6594 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6595 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6596 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6597 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6598 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6599 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6600 | if (!ret) { |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 6601 | ret = btrfs_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6602 | reserved = 1; |
| 6603 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6604 | if (ret) { |
| 6605 | if (ret == -ENOMEM) |
| 6606 | ret = VM_FAULT_OOM; |
| 6607 | else /* -ENOSPC, -EIO, etc */ |
| 6608 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6609 | if (reserved) |
| 6610 | goto out; |
| 6611 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6612 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6613 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 6614 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6615 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6616 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6617 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6618 | page_start = page_offset(page); |
| 6619 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6620 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6621 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6622 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6623 | /* page got truncated out from underneath us */ |
| 6624 | goto out_unlock; |
| 6625 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6626 | wait_on_page_writeback(page); |
| 6627 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6628 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6629 | set_page_extent_mapped(page); |
| 6630 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6631 | /* |
| 6632 | * we can't set the delalloc bits if there are pending ordered |
| 6633 | * extents. Drop our locks and wait for them to finish |
| 6634 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6635 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6636 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6637 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6638 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6639 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 6640 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6641 | btrfs_put_ordered_extent(ordered); |
| 6642 | goto again; |
| 6643 | } |
| 6644 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6645 | /* |
| 6646 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 6647 | * if it was already dirty, so for space accounting reasons we need to |
| 6648 | * clear any delalloc bits for the range we are fixing to save. There |
| 6649 | * is probably a better way to do this, but for now keep consistent with |
| 6650 | * prepare_pages in the normal write path. |
| 6651 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6652 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 6653 | EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6654 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 6655 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6656 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 6657 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6658 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6659 | unlock_extent_cached(io_tree, page_start, page_end, |
| 6660 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6661 | ret = VM_FAULT_SIGBUS; |
| 6662 | goto out_unlock; |
| 6663 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6664 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6665 | |
| 6666 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6667 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6668 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6669 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6670 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6671 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6672 | if (zero_start != PAGE_CACHE_SIZE) { |
| 6673 | kaddr = kmap(page); |
| 6674 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 6675 | flush_dcache_page(page); |
| 6676 | kunmap(page); |
| 6677 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 6678 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6679 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6680 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6681 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6682 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 6683 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
| 6684 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6685 | 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] | 6686 | |
| 6687 | out_unlock: |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 6688 | if (!ret) |
| 6689 | return VM_FAULT_LOCKED; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6690 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6691 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6692 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 6693 | out_noreserve: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 6694 | return ret; |
| 6695 | } |
| 6696 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6697 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6698 | { |
| 6699 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6700 | struct btrfs_block_rsv *rsv; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6701 | int ret; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6702 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6703 | struct btrfs_trans_handle *trans; |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 6704 | unsigned long nr; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6705 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6706 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6707 | |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 6708 | ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 6709 | if (ret) |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6710 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6711 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 6712 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6713 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6714 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6715 | /* |
| 6716 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 6717 | * 3 things going on here |
| 6718 | * |
| 6719 | * 1) We need to reserve space for our orphan item and the space to |
| 6720 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 6721 | * orphan item because we didn't reserve space to remove it. |
| 6722 | * |
| 6723 | * 2) We need to reserve space to update our inode. |
| 6724 | * |
| 6725 | * 3) We need to have something to cache all the space that is going to |
| 6726 | * be free'd up by the truncate operation, but also have some slack |
| 6727 | * space reserved in case it uses space during the truncate (thank you |
| 6728 | * very much snapshotting). |
| 6729 | * |
| 6730 | * And we need these to all be seperate. The fact is we can use alot of |
| 6731 | * space doing the truncate, and we have no earthly idea how much space |
| 6732 | * we will use, so we need the truncate reservation to be seperate so it |
| 6733 | * doesn't end up using space reserved for updating the inode or |
| 6734 | * removing the orphan item. We also need to be able to stop the |
| 6735 | * transaction and start a new one, which means we need to be able to |
| 6736 | * update the inode several times, and we have no idea of knowing how |
| 6737 | * many times that will be, so we can't just reserve 1 item for the |
| 6738 | * entirety of the opration, so that has to be done seperately as well. |
| 6739 | * Then there is the orphan item, which does indeed need to be held on |
| 6740 | * to for the whole operation, and we need nobody to touch this reserved |
| 6741 | * space except the orphan code. |
| 6742 | * |
| 6743 | * So that leaves us with |
| 6744 | * |
| 6745 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 6746 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 6747 | * transaction reservation. |
| 6748 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 6749 | * updating the inode. |
| 6750 | */ |
| 6751 | rsv = btrfs_alloc_block_rsv(root); |
| 6752 | if (!rsv) |
| 6753 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 6754 | rsv->size = min_size; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6755 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6756 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 6757 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6758 | * 1 for the orphan item we're going to add |
| 6759 | * 1 for the orphan item deletion |
| 6760 | * 1 for updating the inode. |
| 6761 | */ |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6762 | trans = btrfs_start_transaction(root, 4); |
| 6763 | if (IS_ERR(trans)) { |
| 6764 | err = PTR_ERR(trans); |
| 6765 | goto out; |
| 6766 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6767 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6768 | /* Migrate the slack space for the truncate to our reserve */ |
| 6769 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 6770 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6771 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6772 | |
| 6773 | ret = btrfs_orphan_add(trans, inode); |
| 6774 | if (ret) { |
| 6775 | btrfs_end_transaction(trans, root); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6776 | goto out; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 6777 | } |
| 6778 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6779 | /* |
| 6780 | * setattr is responsible for setting the ordered_data_close flag, |
| 6781 | * but that is only tested during the last file release. That |
| 6782 | * could happen well after the next commit, leaving a great big |
| 6783 | * window where new writes may get lost if someone chooses to write |
| 6784 | * to this file after truncating to zero |
| 6785 | * |
| 6786 | * The inode doesn't have any dirty data here, and so if we commit |
| 6787 | * this is a noop. If someone immediately starts writing to the inode |
| 6788 | * it is very likely we'll catch some of their writes in this |
| 6789 | * transaction, and the commit will find this file on the ordered |
| 6790 | * data list with good things to send down. |
| 6791 | * |
| 6792 | * This is a best effort solution, there is still a window where |
| 6793 | * using truncate to replace the contents of the file will |
| 6794 | * end up with a zero length file after a crash. |
| 6795 | */ |
| 6796 | if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) |
| 6797 | btrfs_add_ordered_operation(trans, root, inode); |
| 6798 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6799 | while (1) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 6800 | ret = btrfs_block_rsv_refill(root, rsv, min_size); |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6801 | if (ret) { |
| 6802 | /* |
| 6803 | * This can only happen with the original transaction we |
| 6804 | * started above, every other time we shouldn't have a |
| 6805 | * transaction started yet. |
| 6806 | */ |
| 6807 | if (ret == -EAGAIN) |
| 6808 | goto end_trans; |
| 6809 | err = ret; |
| 6810 | break; |
| 6811 | } |
| 6812 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6813 | if (!trans) { |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6814 | /* Just need the 1 for updating the inode */ |
| 6815 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6816 | if (IS_ERR(trans)) { |
Josef Bacik | 7041ee9 | 2011-12-09 13:26:22 -0500 | [diff] [blame] | 6817 | ret = err = PTR_ERR(trans); |
| 6818 | trans = NULL; |
| 6819 | break; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6820 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6821 | } |
| 6822 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6823 | trans->block_rsv = rsv; |
| 6824 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6825 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 6826 | inode->i_size, |
| 6827 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6828 | if (ret != -EAGAIN) { |
| 6829 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6830 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6831 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6832 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6833 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6834 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6835 | if (ret) { |
| 6836 | err = ret; |
| 6837 | break; |
| 6838 | } |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 6839 | end_trans: |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6840 | nr = trans->blocks_used; |
| 6841 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6842 | trans = NULL; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6843 | btrfs_btree_balance_dirty(root, nr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6844 | } |
| 6845 | |
| 6846 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6847 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6848 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6849 | if (ret) |
| 6850 | err = ret; |
Josef Bacik | ded5db9 | 2011-03-04 14:09:46 -0500 | [diff] [blame] | 6851 | } else if (ret && inode->i_nlink > 0) { |
| 6852 | /* |
| 6853 | * Failed to do the truncate, remove us from the in memory |
| 6854 | * orphan list. |
| 6855 | */ |
| 6856 | ret = btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6859 | if (trans) { |
| 6860 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 6861 | ret = btrfs_update_inode(trans, root, inode); |
| 6862 | if (ret && !err) |
| 6863 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6864 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6865 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6866 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 6867 | btrfs_btree_balance_dirty(root, nr); |
| 6868 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 6869 | |
| 6870 | out: |
| 6871 | btrfs_free_block_rsv(root, rsv); |
| 6872 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6873 | if (ret && !err) |
| 6874 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 6875 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 6876 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6877 | } |
| 6878 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6879 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6880 | * create a new subvolume directory/inode (helper for the ioctl). |
| 6881 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6882 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6883 | struct btrfs_root *new_root, u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6884 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6885 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6886 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6887 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6888 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 6889 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 6890 | new_dirid, new_dirid, |
| 6891 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 6892 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6893 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 6894 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6895 | inode->i_op = &btrfs_dir_inode_operations; |
| 6896 | inode->i_fop = &btrfs_dir_file_operations; |
| 6897 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 6898 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6899 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 6900 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6901 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 6902 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6903 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 6904 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6905 | } |
| 6906 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6907 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 6908 | { |
| 6909 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6910 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6911 | |
| 6912 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 6913 | if (!ei) |
| 6914 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6915 | |
| 6916 | ei->root = NULL; |
| 6917 | ei->space_info = NULL; |
| 6918 | ei->generation = 0; |
| 6919 | ei->sequence = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 6920 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 6921 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6922 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6923 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6924 | ei->disk_i_size = 0; |
| 6925 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6926 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6927 | ei->index_cnt = (u64)-1; |
| 6928 | ei->last_unlink_trans = 0; |
| 6929 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6930 | spin_lock_init(&ei->lock); |
| 6931 | ei->outstanding_extents = 0; |
| 6932 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6933 | |
| 6934 | ei->ordered_data_close = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6935 | ei->orphan_meta_reserved = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6936 | ei->dummy_inode = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 6937 | ei->in_defrag = 0; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 6938 | ei->delalloc_meta_reserved = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6939 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6940 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 6941 | ei->delayed_node = NULL; |
| 6942 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6943 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 6944 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 6945 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 6946 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6947 | ei->io_tree.track_uptodate = 1; |
| 6948 | ei->io_failure_tree.track_uptodate = 1; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6949 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 6950 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6951 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6952 | INIT_LIST_HEAD(&ei->i_orphan); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6953 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6954 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6955 | RB_CLEAR_NODE(&ei->rb_node); |
| 6956 | |
| 6957 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6958 | } |
| 6959 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6960 | static void btrfs_i_callback(struct rcu_head *head) |
| 6961 | { |
| 6962 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 6963 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 6964 | } |
| 6965 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6966 | void btrfs_destroy_inode(struct inode *inode) |
| 6967 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6968 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6969 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6970 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6971 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 6972 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6973 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 6974 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6975 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 6976 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6977 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6978 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 6979 | * This can happen where we create an inode, but somebody else also |
| 6980 | * created the same inode and we need to destroy the one we already |
| 6981 | * created. |
| 6982 | */ |
| 6983 | if (!root) |
| 6984 | goto free; |
| 6985 | |
| 6986 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 6987 | * Make sure we're properly removed from the ordered operation |
| 6988 | * lists. |
| 6989 | */ |
| 6990 | smp_mb(); |
| 6991 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
| 6992 | spin_lock(&root->fs_info->ordered_extent_lock); |
| 6993 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
| 6994 | spin_unlock(&root->fs_info->ordered_extent_lock); |
| 6995 | } |
| 6996 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6997 | spin_lock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 6998 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6999 | printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", |
| 7000 | (unsigned long long)btrfs_ino(inode)); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7001 | list_del_init(&BTRFS_I(inode)->i_orphan); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7002 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7003 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7004 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7005 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7006 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 7007 | if (!ordered) |
| 7008 | break; |
| 7009 | else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7010 | printk(KERN_ERR "btrfs found ordered " |
| 7011 | "extent %llu %llu on inode cleanup\n", |
| 7012 | (unsigned long long)ordered->file_offset, |
| 7013 | (unsigned long long)ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7014 | btrfs_remove_ordered_extent(inode, ordered); |
| 7015 | btrfs_put_ordered_extent(ordered); |
| 7016 | btrfs_put_ordered_extent(ordered); |
| 7017 | } |
| 7018 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7019 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7020 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 7021 | free: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7022 | btrfs_remove_delayed_node(inode); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 7023 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7024 | } |
| 7025 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7026 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7027 | { |
| 7028 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7029 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7030 | if (btrfs_root_refs(&root->root_item) == 0 && |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 7031 | !btrfs_is_free_space_inode(root, inode)) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7032 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7033 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 7034 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7035 | } |
| 7036 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 7037 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7038 | { |
| 7039 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 7040 | |
| 7041 | inode_init_once(&ei->vfs_inode); |
| 7042 | } |
| 7043 | |
| 7044 | void btrfs_destroy_cachep(void) |
| 7045 | { |
| 7046 | if (btrfs_inode_cachep) |
| 7047 | kmem_cache_destroy(btrfs_inode_cachep); |
| 7048 | if (btrfs_trans_handle_cachep) |
| 7049 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 7050 | if (btrfs_transaction_cachep) |
| 7051 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7052 | if (btrfs_path_cachep) |
| 7053 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7054 | if (btrfs_free_space_cachep) |
| 7055 | kmem_cache_destroy(btrfs_free_space_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | int btrfs_init_cachep(void) |
| 7059 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7060 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 7061 | sizeof(struct btrfs_inode), 0, |
| 7062 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7063 | if (!btrfs_inode_cachep) |
| 7064 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7065 | |
| 7066 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 7067 | sizeof(struct btrfs_trans_handle), 0, |
| 7068 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7069 | if (!btrfs_trans_handle_cachep) |
| 7070 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7071 | |
| 7072 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 7073 | sizeof(struct btrfs_transaction), 0, |
| 7074 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7075 | if (!btrfs_transaction_cachep) |
| 7076 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7077 | |
| 7078 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 7079 | sizeof(struct btrfs_path), 0, |
| 7080 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7081 | if (!btrfs_path_cachep) |
| 7082 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 7083 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 7084 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", |
| 7085 | sizeof(struct btrfs_free_space), 0, |
| 7086 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 7087 | if (!btrfs_free_space_cachep) |
| 7088 | goto fail; |
| 7089 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7090 | return 0; |
| 7091 | fail: |
| 7092 | btrfs_destroy_cachep(); |
| 7093 | return -ENOMEM; |
| 7094 | } |
| 7095 | |
| 7096 | static int btrfs_getattr(struct vfsmount *mnt, |
| 7097 | struct dentry *dentry, struct kstat *stat) |
| 7098 | { |
| 7099 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7100 | u32 blocksize = inode->i_sb->s_blocksize; |
| 7101 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7102 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 7103 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 7104 | stat->blksize = PAGE_CACHE_SIZE; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 7105 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
| 7106 | ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7107 | return 0; |
| 7108 | } |
| 7109 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7110 | /* |
| 7111 | * If a file is moved, it will inherit the cow and compression flags of the new |
| 7112 | * directory. |
| 7113 | */ |
| 7114 | static void fixup_inode_flags(struct inode *dir, struct inode *inode) |
| 7115 | { |
| 7116 | struct btrfs_inode *b_dir = BTRFS_I(dir); |
| 7117 | struct btrfs_inode *b_inode = BTRFS_I(inode); |
| 7118 | |
| 7119 | if (b_dir->flags & BTRFS_INODE_NODATACOW) |
| 7120 | b_inode->flags |= BTRFS_INODE_NODATACOW; |
| 7121 | else |
| 7122 | b_inode->flags &= ~BTRFS_INODE_NODATACOW; |
| 7123 | |
| 7124 | if (b_dir->flags & BTRFS_INODE_COMPRESS) |
| 7125 | b_inode->flags |= BTRFS_INODE_COMPRESS; |
| 7126 | else |
| 7127 | b_inode->flags &= ~BTRFS_INODE_COMPRESS; |
| 7128 | } |
| 7129 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7130 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 7131 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7132 | { |
| 7133 | struct btrfs_trans_handle *trans; |
| 7134 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7135 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7136 | struct inode *new_inode = new_dentry->d_inode; |
| 7137 | struct inode *old_inode = old_dentry->d_inode; |
| 7138 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7139 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7140 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7141 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7142 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7143 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7144 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7145 | return -EPERM; |
| 7146 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7147 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7148 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7149 | return -EXDEV; |
| 7150 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7151 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 7152 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7153 | return -ENOTEMPTY; |
| 7154 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7155 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7156 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7157 | return -ENOTEMPTY; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7158 | /* |
| 7159 | * we're using rename to replace one file with another. |
| 7160 | * and the replacement file is large. Start IO on it now so |
| 7161 | * we don't add too much work to the end of the transaction |
| 7162 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 7163 | 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] | 7164 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 7165 | filemap_flush(old_inode->i_mapping); |
| 7166 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7167 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7168 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7169 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7170 | /* |
| 7171 | * We want to reserve the absolute worst case amount of items. So if |
| 7172 | * both inodes are subvols and we need to unlink them then that would |
| 7173 | * require 4 item modifications, but if they are both normal inodes it |
| 7174 | * would require 5 item modifications, so we'll assume their normal |
| 7175 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 7176 | * should cover the worst case number of items we'll modify. |
| 7177 | */ |
| 7178 | trans = btrfs_start_transaction(root, 20); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7179 | if (IS_ERR(trans)) { |
| 7180 | ret = PTR_ERR(trans); |
| 7181 | goto out_notrans; |
| 7182 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7183 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7184 | if (dest != root) |
| 7185 | btrfs_record_root_in_trans(trans, dest); |
| 7186 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7187 | ret = btrfs_set_inode_index(new_dir, &index); |
| 7188 | if (ret) |
| 7189 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7190 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7191 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7192 | /* force full log commit if subvolume involved. */ |
| 7193 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 7194 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7195 | ret = btrfs_insert_inode_ref(trans, dest, |
| 7196 | new_dentry->d_name.name, |
| 7197 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7198 | old_ino, |
| 7199 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7200 | if (ret) |
| 7201 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7202 | /* |
| 7203 | * this is an ugly little race, but the rename is required |
| 7204 | * to make sure that if we crash, the inode is either at the |
| 7205 | * old name or the new one. pinning the log transaction lets |
| 7206 | * us make sure we don't allow a log commit to come in after |
| 7207 | * we unlink the name but before we add the new name back in. |
| 7208 | */ |
| 7209 | btrfs_pin_log_trans(root); |
| 7210 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7211 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7212 | * make sure the inode gets flushed if it is replacing |
| 7213 | * something. |
| 7214 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7215 | 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] | 7216 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7217 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7218 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 7219 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 7220 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7221 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7222 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 7223 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 7224 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7225 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7226 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 7227 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 7228 | old_dentry->d_name.name, |
| 7229 | old_dentry->d_name.len); |
| 7230 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 7231 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 7232 | old_dentry->d_inode, |
| 7233 | old_dentry->d_name.name, |
| 7234 | old_dentry->d_name.len); |
| 7235 | if (!ret) |
| 7236 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7237 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7238 | if (ret) { |
| 7239 | btrfs_abort_transaction(trans, root, ret); |
| 7240 | goto out_fail; |
| 7241 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7242 | |
| 7243 | if (new_inode) { |
| 7244 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7245 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7246 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 7247 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 7248 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 7249 | root_objectid, |
| 7250 | new_dentry->d_name.name, |
| 7251 | new_dentry->d_name.len); |
| 7252 | BUG_ON(new_inode->i_nlink == 0); |
| 7253 | } else { |
| 7254 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 7255 | new_dentry->d_inode, |
| 7256 | new_dentry->d_name.name, |
| 7257 | new_dentry->d_name.len); |
| 7258 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7259 | if (!ret && new_inode->i_nlink == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7260 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7261 | BUG_ON(ret); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7262 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7263 | if (ret) { |
| 7264 | btrfs_abort_transaction(trans, root, ret); |
| 7265 | goto out_fail; |
| 7266 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7267 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7268 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 7269 | fixup_inode_flags(new_dir, old_inode); |
| 7270 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7271 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 7272 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 7273 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7274 | if (ret) { |
| 7275 | btrfs_abort_transaction(trans, root, ret); |
| 7276 | goto out_fail; |
| 7277 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7278 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7279 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 7280 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 7281 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 7282 | btrfs_end_log_trans(root); |
| 7283 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7284 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7285 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 7286 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7287 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7288 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7289 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7290 | return ret; |
| 7291 | } |
| 7292 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7293 | /* |
| 7294 | * some fairly slow code that needs optimization. This walks the list |
| 7295 | * of all the inodes with pending delalloc and forces them to disk. |
| 7296 | */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7297 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7298 | { |
| 7299 | struct list_head *head = &root->fs_info->delalloc_inodes; |
| 7300 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7301 | struct inode *inode; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7302 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7303 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 7304 | return -EROFS; |
| 7305 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7306 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7307 | while (!list_empty(head)) { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7308 | binode = list_entry(head->next, struct btrfs_inode, |
| 7309 | delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7310 | inode = igrab(&binode->vfs_inode); |
| 7311 | if (!inode) |
| 7312 | list_del_init(&binode->delalloc_inodes); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7313 | spin_unlock(&root->fs_info->delalloc_lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7314 | if (inode) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7315 | filemap_flush(inode->i_mapping); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 7316 | if (delay_iput) |
| 7317 | btrfs_add_delayed_iput(inode); |
| 7318 | else |
| 7319 | iput(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 7320 | } |
| 7321 | cond_resched(); |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7322 | spin_lock(&root->fs_info->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7323 | } |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 7324 | spin_unlock(&root->fs_info->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7325 | |
| 7326 | /* the filemap_flush will queue IO into the worker threads, but |
| 7327 | * we have to make sure the IO is actually started and that |
| 7328 | * ordered extents get created before we return |
| 7329 | */ |
| 7330 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7331 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7332 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7333 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7334 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 7335 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 7336 | } |
| 7337 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7338 | return 0; |
| 7339 | } |
| 7340 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7341 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 7342 | const char *symname) |
| 7343 | { |
| 7344 | struct btrfs_trans_handle *trans; |
| 7345 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 7346 | struct btrfs_path *path; |
| 7347 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7348 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7349 | int err; |
| 7350 | int drop_inode = 0; |
| 7351 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7352 | u64 index = 0 ; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7353 | int name_len; |
| 7354 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7355 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7356 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7357 | struct extent_buffer *leaf; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7358 | unsigned long nr = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7359 | |
| 7360 | name_len = strlen(symname) + 1; |
| 7361 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 7362 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7363 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7364 | /* |
| 7365 | * 2 items for inode item and ref |
| 7366 | * 2 items for dir items |
| 7367 | * 1 item for xattr if selinux is on |
| 7368 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7369 | trans = btrfs_start_transaction(root, 5); |
| 7370 | if (IS_ERR(trans)) |
| 7371 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7372 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 7373 | err = btrfs_find_free_ino(root, &objectid); |
| 7374 | if (err) |
| 7375 | goto out_unlock; |
| 7376 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 7377 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7378 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7379 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7380 | if (IS_ERR(inode)) { |
| 7381 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7382 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 7383 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7384 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 7385 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7386 | if (err) { |
| 7387 | drop_inode = 1; |
| 7388 | goto out_unlock; |
| 7389 | } |
| 7390 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 7391 | /* |
| 7392 | * If the active LSM wants to access the inode during |
| 7393 | * d_instantiate it needs these. Smack checks to see |
| 7394 | * if the filesystem supports xattrs by looking at the |
| 7395 | * ops vector. |
| 7396 | */ |
| 7397 | inode->i_fop = &btrfs_file_operations; |
| 7398 | inode->i_op = &btrfs_file_inode_operations; |
| 7399 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 7400 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7401 | if (err) |
| 7402 | drop_inode = 1; |
| 7403 | else { |
| 7404 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7405 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7406 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7407 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7408 | if (drop_inode) |
| 7409 | goto out_unlock; |
| 7410 | |
| 7411 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7412 | if (!path) { |
| 7413 | err = -ENOMEM; |
| 7414 | drop_inode = 1; |
| 7415 | goto out_unlock; |
| 7416 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 7417 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7418 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7419 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 7420 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 7421 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 7422 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7423 | if (err) { |
| 7424 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 7425 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7426 | goto out_unlock; |
| 7427 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7428 | leaf = path->nodes[0]; |
| 7429 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 7430 | struct btrfs_file_extent_item); |
| 7431 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 7432 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7433 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7434 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 7435 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 7436 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 7437 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 7438 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7439 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7440 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 7441 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7442 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7443 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7444 | inode->i_op = &btrfs_symlink_inode_operations; |
| 7445 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 7446 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7447 | inode_set_bytes(inode, name_len); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7448 | btrfs_i_size_write(inode, name_len - 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7449 | err = btrfs_update_inode(trans, root, inode); |
| 7450 | if (err) |
| 7451 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7452 | |
| 7453 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 7454 | if (!err) |
| 7455 | d_instantiate(dentry, inode); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7456 | nr = trans->blocks_used; |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7457 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7458 | if (drop_inode) { |
| 7459 | inode_dec_link_count(inode); |
| 7460 | iput(inode); |
| 7461 | } |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 7462 | btrfs_btree_balance_dirty(root, nr); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7463 | return err; |
| 7464 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7465 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7466 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7467 | u64 start, u64 num_bytes, u64 min_size, |
| 7468 | loff_t actual_len, u64 *alloc_hint, |
| 7469 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7470 | { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7471 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7472 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7473 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7474 | u64 i_size; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7475 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7476 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7477 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7478 | if (trans) |
| 7479 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7480 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7481 | if (own_trans) { |
| 7482 | trans = btrfs_start_transaction(root, 3); |
| 7483 | if (IS_ERR(trans)) { |
| 7484 | ret = PTR_ERR(trans); |
| 7485 | break; |
| 7486 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7487 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7488 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7489 | ret = btrfs_reserve_extent(trans, root, num_bytes, min_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7490 | 0, *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7491 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7492 | if (own_trans) |
| 7493 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7494 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7497 | ret = insert_reserved_file_extent(trans, inode, |
| 7498 | cur_offset, ins.objectid, |
| 7499 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 7500 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7501 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7502 | if (ret) { |
| 7503 | btrfs_abort_transaction(trans, root, ret); |
| 7504 | if (own_trans) |
| 7505 | btrfs_end_transaction(trans, root); |
| 7506 | break; |
| 7507 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 7508 | btrfs_drop_extent_cache(inode, cur_offset, |
| 7509 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7510 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7511 | num_bytes -= ins.offset; |
| 7512 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7513 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7514 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7515 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 7516 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7517 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7518 | (actual_len > inode->i_size) && |
| 7519 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7520 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7521 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 7522 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 7523 | i_size = cur_offset; |
| 7524 | i_size_write(inode, i_size); |
| 7525 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7526 | } |
| 7527 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7528 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7529 | |
| 7530 | if (ret) { |
| 7531 | btrfs_abort_transaction(trans, root, ret); |
| 7532 | if (own_trans) |
| 7533 | btrfs_end_transaction(trans, root); |
| 7534 | break; |
| 7535 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7536 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7537 | if (own_trans) |
| 7538 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 7539 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7540 | return ret; |
| 7541 | } |
| 7542 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7543 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 7544 | u64 start, u64 num_bytes, u64 min_size, |
| 7545 | loff_t actual_len, u64 *alloc_hint) |
| 7546 | { |
| 7547 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7548 | min_size, actual_len, alloc_hint, |
| 7549 | NULL); |
| 7550 | } |
| 7551 | |
| 7552 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 7553 | struct btrfs_trans_handle *trans, int mode, |
| 7554 | u64 start, u64 num_bytes, u64 min_size, |
| 7555 | loff_t actual_len, u64 *alloc_hint) |
| 7556 | { |
| 7557 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 7558 | min_size, actual_len, alloc_hint, trans); |
| 7559 | } |
| 7560 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7561 | static int btrfs_set_page_dirty(struct page *page) |
| 7562 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7563 | return __set_page_dirty_nobuffers(page); |
| 7564 | } |
| 7565 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 7566 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7567 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7568 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7569 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 7570 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 7571 | if (mask & MAY_WRITE && |
| 7572 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 7573 | if (btrfs_root_readonly(root)) |
| 7574 | return -EROFS; |
| 7575 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 7576 | return -EACCES; |
| 7577 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 7578 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7579 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7580 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7581 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 7582 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7583 | .lookup = btrfs_lookup, |
| 7584 | .create = btrfs_create, |
| 7585 | .unlink = btrfs_unlink, |
| 7586 | .link = btrfs_link, |
| 7587 | .mkdir = btrfs_mkdir, |
| 7588 | .rmdir = btrfs_rmdir, |
| 7589 | .rename = btrfs_rename, |
| 7590 | .symlink = btrfs_symlink, |
| 7591 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7592 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7593 | .setxattr = btrfs_setxattr, |
| 7594 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7595 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7596 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7597 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7598 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7599 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7600 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7601 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7602 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7603 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7604 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7605 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 7606 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7607 | .llseek = generic_file_llseek, |
| 7608 | .read = generic_read_dir, |
David Woodhouse | cbdf5a2 | 2008-08-06 19:42:33 +0100 | [diff] [blame] | 7609 | .readdir = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7610 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7611 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 7612 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7613 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 7614 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7615 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7616 | }; |
| 7617 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7618 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7619 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 7620 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7621 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7622 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7623 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7624 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 7625 | .set_bit_hook = btrfs_set_bit_hook, |
| 7626 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7627 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 7628 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 7629 | }; |
| 7630 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 7631 | /* |
| 7632 | * btrfs doesn't support the bmap operation because swapfiles |
| 7633 | * use bmap to make a mapping of extents in the file. They assume |
| 7634 | * these extents won't change over the life of the file and they |
| 7635 | * use the bmap result to do IO directly to the drive. |
| 7636 | * |
| 7637 | * the btrfs bmap call would return logical addresses that aren't |
| 7638 | * suitable for IO and they also will change frequently as COW |
| 7639 | * operations happen. So, swapfile + btrfs == corruption. |
| 7640 | * |
| 7641 | * For now we're avoiding this by dropping bmap. |
| 7642 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7643 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7644 | .readpage = btrfs_readpage, |
| 7645 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7646 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7647 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7648 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7649 | .invalidatepage = btrfs_invalidatepage, |
| 7650 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7651 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 7652 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7653 | }; |
| 7654 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 7655 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7656 | .readpage = btrfs_readpage, |
| 7657 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 7658 | .invalidatepage = btrfs_invalidatepage, |
| 7659 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7660 | }; |
| 7661 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7662 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7663 | .getattr = btrfs_getattr, |
| 7664 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7665 | .setxattr = btrfs_setxattr, |
| 7666 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 7667 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7668 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7669 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7670 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7671 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7672 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7673 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7674 | .getattr = btrfs_getattr, |
| 7675 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7676 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7677 | .setxattr = btrfs_setxattr, |
| 7678 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 7679 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 7680 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7681 | .get_acl = btrfs_get_acl, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 7682 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 7683 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7684 | .readlink = generic_readlink, |
| 7685 | .follow_link = page_follow_link_light, |
| 7686 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 7687 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 7688 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 7689 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 7690 | .setxattr = btrfs_setxattr, |
| 7691 | .getxattr = btrfs_getxattr, |
| 7692 | .listxattr = btrfs_listxattr, |
| 7693 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 7694 | .get_acl = btrfs_get_acl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7695 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7696 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 7697 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7698 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 7699 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7700 | }; |