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> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 35 | #include <linux/aio.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 36 | #include <linux/bit_spinlock.h> |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 37 | #include <linux/xattr.h> |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 38 | #include <linux/posix_acl.h> |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 39 | #include <linux/falloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
David Sterba | 7a36dde | 2011-05-06 15:33:15 +0200 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 42 | #include <linux/mount.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 43 | #include <linux/btrfs.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 44 | #include <linux/blkdev.h> |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 45 | #include <linux/posix_acl_xattr.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 46 | #include "ctree.h" |
| 47 | #include "disk-io.h" |
| 48 | #include "transaction.h" |
| 49 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | #include "print-tree.h" |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 51 | #include "ordered-data.h" |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 52 | #include "xattr.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 53 | #include "tree-log.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 54 | #include "volumes.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 55 | #include "compression.h" |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 56 | #include "locking.h" |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 57 | #include "free-space-cache.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 58 | #include "inode-map.h" |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 59 | #include "backref.h" |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 60 | #include "hash.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 61 | #include "props.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 62 | |
| 63 | struct btrfs_iget_args { |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 64 | struct btrfs_key *location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 65 | struct btrfs_root *root; |
| 66 | }; |
| 67 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 68 | static const struct inode_operations btrfs_dir_inode_operations; |
| 69 | static const struct inode_operations btrfs_symlink_inode_operations; |
| 70 | static const struct inode_operations btrfs_dir_ro_inode_operations; |
| 71 | static const struct inode_operations btrfs_special_inode_operations; |
| 72 | static const struct inode_operations btrfs_file_inode_operations; |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 73 | static const struct address_space_operations btrfs_aops; |
| 74 | static const struct address_space_operations btrfs_symlink_aops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 75 | static const struct file_operations btrfs_dir_file_operations; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 76 | static struct extent_io_ops btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 77 | |
| 78 | static struct kmem_cache *btrfs_inode_cachep; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 79 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 80 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 81 | struct kmem_cache *btrfs_transaction_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 82 | struct kmem_cache *btrfs_path_cachep; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 83 | struct kmem_cache *btrfs_free_space_cachep; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 84 | |
| 85 | #define S_SHIFT 12 |
| 86 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 87 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 88 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 89 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 90 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 91 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 92 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 93 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 94 | }; |
| 95 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 96 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 97 | static int btrfs_truncate(struct inode *inode); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 98 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 99 | static noinline int cow_file_range(struct inode *inode, |
| 100 | struct page *locked_page, |
| 101 | u64 start, u64 end, int *page_started, |
| 102 | unsigned long *nr_written, int unlock); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 103 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 104 | u64 len, u64 orig_start, |
| 105 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 106 | u64 orig_block_len, u64 ram_bytes, |
| 107 | int type); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 108 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 109 | static int btrfs_dirty_inode(struct inode *inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 110 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 111 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 112 | struct inode *inode, struct inode *dir, |
| 113 | const struct qstr *qstr) |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 114 | { |
| 115 | int err; |
| 116 | |
Yan, Zheng | f34f57a | 2009-11-12 09:35:27 +0000 | [diff] [blame] | 117 | err = btrfs_init_acl(trans, inode, dir); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 118 | if (!err) |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 119 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 120 | return err; |
| 121 | } |
| 122 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 123 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 124 | * this does all the hard work for inserting an inline extent into |
| 125 | * the btree. The caller should have done a btrfs_drop_extents so that |
| 126 | * no overlapping inline items exist in the btree |
| 127 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 128 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 129 | struct btrfs_path *path, int extent_inserted, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 130 | struct btrfs_root *root, struct inode *inode, |
| 131 | u64 start, size_t size, size_t compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 132 | int compress_type, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 133 | struct page **compressed_pages) |
| 134 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 135 | struct extent_buffer *leaf; |
| 136 | struct page *page = NULL; |
| 137 | char *kaddr; |
| 138 | unsigned long ptr; |
| 139 | struct btrfs_file_extent_item *ei; |
| 140 | int err = 0; |
| 141 | int ret; |
| 142 | size_t cur_size = size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 143 | unsigned long offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 144 | |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 145 | if (compressed_size && compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 146 | cur_size = compressed_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 147 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 148 | inode_add_bytes(inode, size); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 149 | |
| 150 | if (!extent_inserted) { |
| 151 | struct btrfs_key key; |
| 152 | size_t datasize; |
| 153 | |
| 154 | key.objectid = btrfs_ino(inode); |
| 155 | key.offset = start; |
| 156 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 157 | |
| 158 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
| 159 | path->leave_spinning = 1; |
| 160 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 161 | datasize); |
| 162 | if (ret) { |
| 163 | err = ret; |
| 164 | goto fail; |
| 165 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 166 | } |
| 167 | leaf = path->nodes[0]; |
| 168 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 169 | struct btrfs_file_extent_item); |
| 170 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 171 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 172 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 173 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 174 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); |
| 175 | ptr = btrfs_file_extent_inline_start(ei); |
| 176 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 177 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | struct page *cpage; |
| 179 | int i = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 180 | while (compressed_size > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 181 | cpage = compressed_pages[i]; |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 182 | cur_size = min_t(unsigned long, compressed_size, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 183 | PAGE_CACHE_SIZE); |
| 184 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 185 | kaddr = kmap_atomic(cpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 186 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 187 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 188 | |
| 189 | i++; |
| 190 | ptr += cur_size; |
| 191 | compressed_size -= cur_size; |
| 192 | } |
| 193 | btrfs_set_file_extent_compression(leaf, ei, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 194 | compress_type); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | } else { |
| 196 | page = find_get_page(inode->i_mapping, |
| 197 | start >> PAGE_CACHE_SHIFT); |
| 198 | btrfs_set_file_extent_compression(leaf, ei, 0); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 199 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 200 | offset = start & (PAGE_CACHE_SIZE - 1); |
| 201 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 202 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 203 | page_cache_release(page); |
| 204 | } |
| 205 | btrfs_mark_buffer_dirty(leaf); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 206 | btrfs_release_path(path); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 208 | /* |
| 209 | * we're an inline extent, so nobody can |
| 210 | * extend the file past i_size without locking |
| 211 | * a page we already have locked. |
| 212 | * |
| 213 | * We must do any isize and inode updates |
| 214 | * before we unlock the pages. Otherwise we |
| 215 | * could end up racing with unlink. |
| 216 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 217 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 218 | ret = btrfs_update_inode(trans, root, inode); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 219 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 220 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 221 | fail: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | return err; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* |
| 227 | * conditionally insert an inline extent into the file. This |
| 228 | * does the checks required to make sure the data is small enough |
| 229 | * to fit as an inline extent. |
| 230 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 231 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
| 232 | struct inode *inode, u64 start, |
| 233 | u64 end, size_t compressed_size, |
| 234 | int compress_type, |
| 235 | struct page **compressed_pages) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 236 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 237 | struct btrfs_trans_handle *trans; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 238 | u64 isize = i_size_read(inode); |
| 239 | u64 actual_end = min(end + 1, isize); |
| 240 | u64 inline_len = actual_end - start; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 241 | u64 aligned_end = ALIGN(end, root->sectorsize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 242 | u64 data_len = inline_len; |
| 243 | int ret; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 244 | struct btrfs_path *path; |
| 245 | int extent_inserted = 0; |
| 246 | u32 extent_item_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 247 | |
| 248 | if (compressed_size) |
| 249 | data_len = compressed_size; |
| 250 | |
| 251 | if (start > 0 || |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 252 | actual_end >= PAGE_CACHE_SIZE || |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 253 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 254 | (!compressed_size && |
| 255 | (actual_end & (root->sectorsize - 1)) == 0) || |
| 256 | end + 1 < isize || |
| 257 | data_len > root->fs_info->max_inline) { |
| 258 | return 1; |
| 259 | } |
| 260 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 261 | path = btrfs_alloc_path(); |
| 262 | if (!path) |
| 263 | return -ENOMEM; |
| 264 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 265 | trans = btrfs_join_transaction(root); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 266 | if (IS_ERR(trans)) { |
| 267 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 268 | return PTR_ERR(trans); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 269 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 270 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 271 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 272 | if (compressed_size && compressed_pages) |
| 273 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 274 | compressed_size); |
| 275 | else |
| 276 | extent_item_size = btrfs_file_extent_calc_inline_size( |
| 277 | inline_len); |
| 278 | |
| 279 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 280 | start, aligned_end, NULL, |
| 281 | 1, 1, extent_item_size, &extent_inserted); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 282 | if (ret) { |
| 283 | btrfs_abort_transaction(trans, root, ret); |
| 284 | goto out; |
| 285 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 286 | |
| 287 | if (isize > actual_end) |
| 288 | inline_len = min_t(u64, isize, actual_end); |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 289 | ret = insert_inline_extent(trans, path, extent_inserted, |
| 290 | root, inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 291 | inline_len, compressed_size, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 292 | compress_type, compressed_pages); |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 293 | if (ret && ret != -ENOSPC) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 294 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 295 | goto out; |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 296 | } else if (ret == -ENOSPC) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 297 | ret = 1; |
| 298 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 299 | } |
Josef Bacik | 2adcac1 | 2012-05-23 16:10:14 -0400 | [diff] [blame] | 300 | |
Josef Bacik | bdc20e6 | 2013-02-28 13:23:38 -0500 | [diff] [blame] | 301 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 302 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 303 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 304 | out: |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 305 | btrfs_free_path(path); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 306 | btrfs_end_transaction(trans, root); |
| 307 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 310 | struct async_extent { |
| 311 | u64 start; |
| 312 | u64 ram_size; |
| 313 | u64 compressed_size; |
| 314 | struct page **pages; |
| 315 | unsigned long nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 316 | int compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 317 | struct list_head list; |
| 318 | }; |
| 319 | |
| 320 | struct async_cow { |
| 321 | struct inode *inode; |
| 322 | struct btrfs_root *root; |
| 323 | struct page *locked_page; |
| 324 | u64 start; |
| 325 | u64 end; |
| 326 | struct list_head extents; |
| 327 | struct btrfs_work work; |
| 328 | }; |
| 329 | |
| 330 | static noinline int add_async_extent(struct async_cow *cow, |
| 331 | u64 start, u64 ram_size, |
| 332 | u64 compressed_size, |
| 333 | struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 334 | unsigned long nr_pages, |
| 335 | int compress_type) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 336 | { |
| 337 | struct async_extent *async_extent; |
| 338 | |
| 339 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 340 | BUG_ON(!async_extent); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 341 | async_extent->start = start; |
| 342 | async_extent->ram_size = ram_size; |
| 343 | async_extent->compressed_size = compressed_size; |
| 344 | async_extent->pages = pages; |
| 345 | async_extent->nr_pages = nr_pages; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 346 | async_extent->compress_type = compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 347 | list_add_tail(&async_extent->list, &cow->extents); |
| 348 | return 0; |
| 349 | } |
| 350 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 351 | /* |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 352 | * we create compressed extents in two phases. The first |
| 353 | * phase compresses a range of pages that have already been |
| 354 | * locked (both pages and state bits are locked). |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 355 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 356 | * This is done inside an ordered work queue, and the compression |
| 357 | * is spread across many cpus. The actual IO submission is step |
| 358 | * two, and the ordered work queue takes care of making sure that |
| 359 | * happens in the same order things were put onto the queue by |
| 360 | * writepages and friends. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 361 | * |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 362 | * If this code finds it can't get good compression, it puts an |
| 363 | * entry onto the work queue to write the uncompressed bytes. This |
| 364 | * makes sure that both compressed inodes and uncompressed inodes |
Artem Bityutskiy | b257031 | 2012-07-25 18:12:06 +0300 | [diff] [blame] | 365 | * are written in the same order that the flusher thread sent them |
| 366 | * down. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 367 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 368 | static noinline int compress_file_range(struct inode *inode, |
| 369 | struct page *locked_page, |
| 370 | u64 start, u64 end, |
| 371 | struct async_cow *async_cow, |
| 372 | int *num_added) |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 373 | { |
| 374 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 375 | u64 num_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 376 | u64 blocksize = root->sectorsize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 377 | u64 actual_end; |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 378 | u64 isize = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 379 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 380 | struct page **pages = NULL; |
| 381 | unsigned long nr_pages; |
| 382 | unsigned long nr_pages_ret = 0; |
| 383 | unsigned long total_compressed = 0; |
| 384 | unsigned long total_in = 0; |
| 385 | unsigned long max_compressed = 128 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 386 | unsigned long max_uncompressed = 128 * 1024; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 387 | int i; |
| 388 | int will_compress; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 389 | int compress_type = root->fs_info->compress_type; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 390 | int redirty = 0; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 391 | |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 392 | /* if this is a small write inside eof, kick off a defrag */ |
| 393 | if ((end - start + 1) < 16 * 1024 && |
| 394 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 395 | btrfs_add_inode_defrag(NULL, inode); |
| 396 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 397 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 398 | again: |
| 399 | will_compress = 0; |
| 400 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 401 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 402 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 403 | /* |
| 404 | * we don't want to send crud past the end of i_size through |
| 405 | * compression, that's just a waste of CPU time. So, if the |
| 406 | * end of the file is before the start of our current |
| 407 | * requested range of bytes, we bail out to the uncompressed |
| 408 | * cleanup code that can deal with all of this. |
| 409 | * |
| 410 | * It isn't really the fastest way to fix things, but this is a |
| 411 | * very uncommon corner. |
| 412 | */ |
| 413 | if (actual_end <= start) |
| 414 | goto cleanup_and_bail_uncompressed; |
| 415 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 416 | total_compressed = actual_end - start; |
| 417 | |
| 418 | /* we want to make sure that amount of ram required to uncompress |
| 419 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 420 | * of a compressed extent to 128k. This is a crucial number |
| 421 | * because it also controls how easily we can spread reads across |
| 422 | * cpus for decompression. |
| 423 | * |
| 424 | * We also want to make sure the amount of IO required to do |
| 425 | * a random read is reasonably small, so we limit the size of |
| 426 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 427 | */ |
| 428 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 429 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 430 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 431 | total_in = 0; |
| 432 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 433 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 434 | /* |
| 435 | * we do compression for mount -o compress and when the |
| 436 | * inode has not been flagged as nocompress. This flag can |
| 437 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 439 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 440 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 441 | (BTRFS_I(inode)->force_compress) || |
| 442 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 443 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 444 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 445 | if (!pages) { |
| 446 | /* just bail out to the uncompressed code */ |
| 447 | goto cont; |
| 448 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 449 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 450 | if (BTRFS_I(inode)->force_compress) |
| 451 | compress_type = BTRFS_I(inode)->force_compress; |
| 452 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 453 | /* |
| 454 | * we need to call clear_page_dirty_for_io on each |
| 455 | * page in the range. Otherwise applications with the file |
| 456 | * mmap'd can wander in and change the page contents while |
| 457 | * we are compressing them. |
| 458 | * |
| 459 | * If the compression fails for any reason, we set the pages |
| 460 | * dirty again later on. |
| 461 | */ |
| 462 | extent_range_clear_dirty_for_io(inode, start, end); |
| 463 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 464 | ret = btrfs_compress_pages(compress_type, |
| 465 | inode->i_mapping, start, |
| 466 | total_compressed, pages, |
| 467 | nr_pages, &nr_pages_ret, |
| 468 | &total_in, |
| 469 | &total_compressed, |
| 470 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 471 | |
| 472 | if (!ret) { |
| 473 | unsigned long offset = total_compressed & |
| 474 | (PAGE_CACHE_SIZE - 1); |
| 475 | struct page *page = pages[nr_pages_ret - 1]; |
| 476 | char *kaddr; |
| 477 | |
| 478 | /* zero the tail end of the last page, we might be |
| 479 | * sending it down to disk |
| 480 | */ |
| 481 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 482 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 483 | memset(kaddr + offset, 0, |
| 484 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 485 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 486 | } |
| 487 | will_compress = 1; |
| 488 | } |
| 489 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 490 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 491 | if (start == 0) { |
| 492 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 493 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 494 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 495 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 496 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 497 | ret = cow_file_range_inline(root, inode, start, end, |
| 498 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 499 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 500 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 501 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 502 | total_compressed, |
| 503 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 504 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 505 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 506 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 507 | EXTENT_DEFRAG; |
| 508 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
| 509 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 510 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 511 | * inline extent creation worked or returned error, |
| 512 | * we don't need to create any more async work items. |
| 513 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 514 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 515 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 516 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 517 | PAGE_CLEAR_DIRTY | |
| 518 | PAGE_SET_WRITEBACK | |
| 519 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 520 | goto free_pages_out; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (will_compress) { |
| 525 | /* |
| 526 | * we aren't doing an inline extent round the compressed size |
| 527 | * up to a block size boundary so the allocator does sane |
| 528 | * things |
| 529 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 530 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * one last check to make sure the compression is really a |
| 534 | * win, compare the page count read with the blocks on disk |
| 535 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 536 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 537 | if (total_compressed >= total_in) { |
| 538 | will_compress = 0; |
| 539 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 540 | num_bytes = total_in; |
| 541 | } |
| 542 | } |
| 543 | if (!will_compress && pages) { |
| 544 | /* |
| 545 | * the compression code ran but failed to make things smaller, |
| 546 | * free any pages it allocated and our page pointer array |
| 547 | */ |
| 548 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 549 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 550 | page_cache_release(pages[i]); |
| 551 | } |
| 552 | kfree(pages); |
| 553 | pages = NULL; |
| 554 | total_compressed = 0; |
| 555 | nr_pages_ret = 0; |
| 556 | |
| 557 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 558 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 559 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 560 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 561 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 562 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 563 | if (will_compress) { |
| 564 | *num_added += 1; |
| 565 | |
| 566 | /* the async work queues will take care of doing actual |
| 567 | * allocation on disk for these compressed pages, |
| 568 | * and will submit them to the elevator. |
| 569 | */ |
| 570 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 571 | total_compressed, pages, nr_pages_ret, |
| 572 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 573 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 574 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 575 | start += num_bytes; |
| 576 | pages = NULL; |
| 577 | cond_resched(); |
| 578 | goto again; |
| 579 | } |
| 580 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 581 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 582 | /* |
| 583 | * No compression, but we still need to write the pages in |
| 584 | * the file we've been given so far. redirty the locked |
| 585 | * page if it corresponds to our extent and set things up |
| 586 | * for the async work queue to run cow_file_range to do |
| 587 | * the normal delalloc dance |
| 588 | */ |
| 589 | if (page_offset(locked_page) >= start && |
| 590 | page_offset(locked_page) <= end) { |
| 591 | __set_page_dirty_nobuffers(locked_page); |
| 592 | /* unlocked later on in the async handlers */ |
| 593 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 594 | if (redirty) |
| 595 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 596 | add_async_extent(async_cow, start, end - start + 1, |
| 597 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 598 | *num_added += 1; |
| 599 | } |
| 600 | |
| 601 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 602 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 603 | |
| 604 | free_pages_out: |
| 605 | for (i = 0; i < nr_pages_ret; i++) { |
| 606 | WARN_ON(pages[i]->mapping); |
| 607 | page_cache_release(pages[i]); |
| 608 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 609 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 610 | |
| 611 | goto out; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * phase two of compressed writeback. This is the ordered portion |
| 616 | * of the code, which only gets called in the order the work was |
| 617 | * queued. We walk all the async extents created by compress_file_range |
| 618 | * and send them down to the disk. |
| 619 | */ |
| 620 | static noinline int submit_compressed_extents(struct inode *inode, |
| 621 | struct async_cow *async_cow) |
| 622 | { |
| 623 | struct async_extent *async_extent; |
| 624 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 625 | struct btrfs_key ins; |
| 626 | struct extent_map *em; |
| 627 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 628 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 629 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 630 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | |
| 632 | if (list_empty(&async_cow->extents)) |
| 633 | return 0; |
| 634 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 635 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 636 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 637 | async_extent = list_entry(async_cow->extents.next, |
| 638 | struct async_extent, list); |
| 639 | list_del(&async_extent->list); |
| 640 | |
| 641 | io_tree = &BTRFS_I(inode)->io_tree; |
| 642 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 643 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 644 | /* did the compression code fall back to uncompressed IO? */ |
| 645 | if (!async_extent->pages) { |
| 646 | int page_started = 0; |
| 647 | unsigned long nr_written = 0; |
| 648 | |
| 649 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 650 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 651 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 652 | |
| 653 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 654 | ret = cow_file_range(inode, async_cow->locked_page, |
| 655 | async_extent->start, |
| 656 | async_extent->start + |
| 657 | async_extent->ram_size - 1, |
| 658 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 659 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 660 | /* JDM XXX */ |
| 661 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 662 | /* |
| 663 | * if page_started, cow_file_range inserted an |
| 664 | * inline extent and took care of all the unlocking |
| 665 | * and IO for us. Otherwise, we need to submit |
| 666 | * all those pages down to the drive. |
| 667 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 668 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 669 | extent_write_locked_range(io_tree, |
| 670 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 671 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 672 | async_extent->ram_size - 1, |
| 673 | btrfs_get_extent, |
| 674 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 675 | else if (ret) |
| 676 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | kfree(async_extent); |
| 678 | cond_resched(); |
| 679 | continue; |
| 680 | } |
| 681 | |
| 682 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 683 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 684 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 685 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 686 | async_extent->compressed_size, |
| 687 | async_extent->compressed_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 688 | 0, alloc_hint, &ins, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 689 | if (ret) { |
| 690 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 691 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 692 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 693 | WARN_ON(async_extent->pages[i]->mapping); |
| 694 | page_cache_release(async_extent->pages[i]); |
| 695 | } |
| 696 | kfree(async_extent->pages); |
| 697 | async_extent->nr_pages = 0; |
| 698 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 699 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 700 | if (ret == -ENOSPC) { |
| 701 | unlock_extent(io_tree, async_extent->start, |
| 702 | async_extent->start + |
| 703 | async_extent->ram_size - 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 704 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 705 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 706 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 709 | /* |
| 710 | * here we're doing allocation and writeback of the |
| 711 | * compressed pages |
| 712 | */ |
| 713 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 714 | async_extent->start + |
| 715 | async_extent->ram_size - 1, 0); |
| 716 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 717 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 718 | if (!em) { |
| 719 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 720 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 721 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 722 | em->start = async_extent->start; |
| 723 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 724 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 725 | em->mod_start = em->start; |
| 726 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 727 | |
| 728 | em->block_start = ins.objectid; |
| 729 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 730 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 731 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 732 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 733 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 734 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 735 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 736 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 737 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 738 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 739 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 740 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 741 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 742 | if (ret != -EEXIST) { |
| 743 | free_extent_map(em); |
| 744 | break; |
| 745 | } |
| 746 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 747 | async_extent->start + |
| 748 | async_extent->ram_size - 1, 0); |
| 749 | } |
| 750 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 751 | if (ret) |
| 752 | goto out_free_reserve; |
| 753 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 754 | ret = btrfs_add_ordered_extent_compress(inode, |
| 755 | async_extent->start, |
| 756 | ins.objectid, |
| 757 | async_extent->ram_size, |
| 758 | ins.offset, |
| 759 | BTRFS_ORDERED_COMPRESSED, |
| 760 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 761 | if (ret) |
| 762 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 763 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 764 | /* |
| 765 | * clear dirty, set writeback and unlock the pages. |
| 766 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 767 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 768 | async_extent->start + |
| 769 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 770 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 771 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 772 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 773 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 774 | async_extent->start, |
| 775 | async_extent->ram_size, |
| 776 | ins.objectid, |
| 777 | ins.offset, async_extent->pages, |
| 778 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 779 | alloc_hint = ins.objectid + ins.offset; |
| 780 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 781 | if (ret) |
| 782 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 783 | cond_resched(); |
| 784 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 785 | ret = 0; |
| 786 | out: |
| 787 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 788 | out_free_reserve: |
| 789 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 790 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 791 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 792 | async_extent->start + |
| 793 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 794 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 795 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 796 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 797 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 798 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 799 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 800 | } |
| 801 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 802 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 803 | u64 num_bytes) |
| 804 | { |
| 805 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 806 | struct extent_map *em; |
| 807 | u64 alloc_hint = 0; |
| 808 | |
| 809 | read_lock(&em_tree->lock); |
| 810 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 811 | if (em) { |
| 812 | /* |
| 813 | * if block start isn't an actual block number then find the |
| 814 | * first block in this inode and use that as a hint. If that |
| 815 | * block is also bogus then just don't worry about it. |
| 816 | */ |
| 817 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 818 | free_extent_map(em); |
| 819 | em = search_extent_mapping(em_tree, 0, 0); |
| 820 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 821 | alloc_hint = em->block_start; |
| 822 | if (em) |
| 823 | free_extent_map(em); |
| 824 | } else { |
| 825 | alloc_hint = em->block_start; |
| 826 | free_extent_map(em); |
| 827 | } |
| 828 | } |
| 829 | read_unlock(&em_tree->lock); |
| 830 | |
| 831 | return alloc_hint; |
| 832 | } |
| 833 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | /* |
| 835 | * when extent_io.c finds a delayed allocation range in the file, |
| 836 | * the call backs end up in this code. The basic idea is to |
| 837 | * allocate extents on disk for the range, and create ordered data structs |
| 838 | * in ram to track those extents. |
| 839 | * |
| 840 | * locked_page is the page that writepage had locked already. We use |
| 841 | * it to make sure we don't do extra locks or unlocks. |
| 842 | * |
| 843 | * *page_started is set to one if we unlock locked_page and do everything |
| 844 | * required to start IO on it. It may be clean and already done with |
| 845 | * IO when we return. |
| 846 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 847 | static noinline int cow_file_range(struct inode *inode, |
| 848 | struct page *locked_page, |
| 849 | u64 start, u64 end, int *page_started, |
| 850 | unsigned long *nr_written, |
| 851 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 852 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 853 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 854 | u64 alloc_hint = 0; |
| 855 | u64 num_bytes; |
| 856 | unsigned long ram_size; |
| 857 | u64 disk_num_bytes; |
| 858 | u64 cur_alloc_size; |
| 859 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 860 | struct btrfs_key ins; |
| 861 | struct extent_map *em; |
| 862 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 863 | int ret = 0; |
| 864 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 865 | if (btrfs_is_free_space_inode(inode)) { |
| 866 | WARN_ON_ONCE(1); |
| 867 | return -EINVAL; |
| 868 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 869 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 870 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 871 | num_bytes = max(blocksize, num_bytes); |
| 872 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 873 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 874 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 875 | if (num_bytes < 64 * 1024 && |
| 876 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 877 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 878 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 879 | if (start == 0) { |
| 880 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 881 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 882 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 883 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 884 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 885 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 886 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 887 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 888 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 889 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 890 | *nr_written = *nr_written + |
| 891 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 892 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 893 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 894 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 895 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 896 | } |
| 897 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 898 | |
| 899 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 900 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 901 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 902 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 903 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 904 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 905 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 906 | unsigned long op; |
| 907 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 908 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 909 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 910 | root->sectorsize, 0, alloc_hint, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 911 | &ins, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 912 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 913 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 914 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 915 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 916 | if (!em) { |
| 917 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 918 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 919 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 920 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 921 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 922 | ram_size = ins.offset; |
| 923 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 924 | em->mod_start = em->start; |
| 925 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 926 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 927 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 928 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 929 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 930 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 931 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 932 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 933 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 934 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 935 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 936 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 937 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 938 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 939 | if (ret != -EEXIST) { |
| 940 | free_extent_map(em); |
| 941 | break; |
| 942 | } |
| 943 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 944 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 945 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 946 | if (ret) |
| 947 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 948 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 949 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 950 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 951 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 952 | if (ret) |
| 953 | goto out_reserve; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 954 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 955 | if (root->root_key.objectid == |
| 956 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 957 | ret = btrfs_reloc_clone_csums(inode, start, |
| 958 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 959 | if (ret) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 960 | goto out_reserve; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 961 | } |
| 962 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 963 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 964 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 965 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 966 | /* we're not doing compressed IO, don't unlock the first |
| 967 | * page (which the caller expects to stay locked), don't |
| 968 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 969 | * |
| 970 | * Do set the Private2 bit so we know this page was properly |
| 971 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 972 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 973 | op = unlock ? PAGE_UNLOCK : 0; |
| 974 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 975 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 976 | extent_clear_unlock_delalloc(inode, start, |
| 977 | start + ram_size - 1, locked_page, |
| 978 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 979 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 980 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 981 | num_bytes -= cur_alloc_size; |
| 982 | alloc_hint = ins.objectid + ins.offset; |
| 983 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 984 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 985 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 986 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 987 | |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 988 | out_reserve: |
| 989 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 990 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 991 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 992 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 993 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 994 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 995 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 996 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 997 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 998 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 999 | /* |
| 1000 | * work queue call back to started compression on a file and pages |
| 1001 | */ |
| 1002 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1003 | { |
| 1004 | struct async_cow *async_cow; |
| 1005 | int num_added = 0; |
| 1006 | async_cow = container_of(work, struct async_cow, work); |
| 1007 | |
| 1008 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1009 | async_cow->start, async_cow->end, async_cow, |
| 1010 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1011 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1012 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1013 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1014 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * work queue call back to submit previously compressed pages |
| 1019 | */ |
| 1020 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1021 | { |
| 1022 | struct async_cow *async_cow; |
| 1023 | struct btrfs_root *root; |
| 1024 | unsigned long nr_pages; |
| 1025 | |
| 1026 | async_cow = container_of(work, struct async_cow, work); |
| 1027 | |
| 1028 | root = async_cow->root; |
| 1029 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1030 | PAGE_CACHE_SHIFT; |
| 1031 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1032 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1033 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1034 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1035 | wake_up(&root->fs_info->async_submit_wait); |
| 1036 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1037 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1038 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1039 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1040 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1041 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1042 | { |
| 1043 | struct async_cow *async_cow; |
| 1044 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1045 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1046 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1047 | kfree(async_cow); |
| 1048 | } |
| 1049 | |
| 1050 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1051 | u64 start, u64 end, int *page_started, |
| 1052 | unsigned long *nr_written) |
| 1053 | { |
| 1054 | struct async_cow *async_cow; |
| 1055 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1056 | unsigned long nr_pages; |
| 1057 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1058 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1059 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1060 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1061 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1062 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1063 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1064 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1065 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1066 | async_cow->root = root; |
| 1067 | async_cow->locked_page = locked_page; |
| 1068 | async_cow->start = start; |
| 1069 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1070 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1071 | cur_end = end; |
| 1072 | else |
| 1073 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1074 | |
| 1075 | async_cow->end = cur_end; |
| 1076 | INIT_LIST_HEAD(&async_cow->extents); |
| 1077 | |
| 1078 | async_cow->work.func = async_cow_start; |
| 1079 | async_cow->work.ordered_func = async_cow_submit; |
| 1080 | async_cow->work.ordered_free = async_cow_free; |
| 1081 | async_cow->work.flags = 0; |
| 1082 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1083 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1084 | PAGE_CACHE_SHIFT; |
| 1085 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1086 | |
| 1087 | btrfs_queue_worker(&root->fs_info->delalloc_workers, |
| 1088 | &async_cow->work); |
| 1089 | |
| 1090 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1091 | wait_event(root->fs_info->async_submit_wait, |
| 1092 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1093 | limit)); |
| 1094 | } |
| 1095 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1096 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1097 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1098 | wait_event(root->fs_info->async_submit_wait, |
| 1099 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1100 | 0)); |
| 1101 | } |
| 1102 | |
| 1103 | *nr_written += nr_pages; |
| 1104 | start = cur_end + 1; |
| 1105 | } |
| 1106 | *page_started = 1; |
| 1107 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1108 | } |
| 1109 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1110 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1111 | u64 bytenr, u64 num_bytes) |
| 1112 | { |
| 1113 | int ret; |
| 1114 | struct btrfs_ordered_sum *sums; |
| 1115 | LIST_HEAD(list); |
| 1116 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1117 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1118 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1119 | if (ret == 0 && list_empty(&list)) |
| 1120 | return 0; |
| 1121 | |
| 1122 | while (!list_empty(&list)) { |
| 1123 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1124 | list_del(&sums->list); |
| 1125 | kfree(sums); |
| 1126 | } |
| 1127 | return 1; |
| 1128 | } |
| 1129 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1130 | /* |
| 1131 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1132 | * of the extents that exist in the file, and COWs the file as required. |
| 1133 | * |
| 1134 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1135 | * blocks on disk |
| 1136 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1137 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1138 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1139 | u64 start, u64 end, int *page_started, int force, |
| 1140 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1141 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1142 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1143 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1144 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1145 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1146 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1147 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1148 | u64 cow_start; |
| 1149 | u64 cur_offset; |
| 1150 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1151 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1152 | u64 disk_bytenr; |
| 1153 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1154 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1155 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1156 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1157 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1158 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1159 | int nocow; |
| 1160 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1161 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1162 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1163 | |
| 1164 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1165 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1166 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1167 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1168 | EXTENT_DO_ACCOUNTING | |
| 1169 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1170 | PAGE_CLEAR_DIRTY | |
| 1171 | PAGE_SET_WRITEBACK | |
| 1172 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1173 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1174 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1175 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1176 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1177 | |
| 1178 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1179 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1180 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1181 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1182 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1183 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1184 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1185 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1186 | EXTENT_DO_ACCOUNTING | |
| 1187 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1188 | PAGE_CLEAR_DIRTY | |
| 1189 | PAGE_SET_WRITEBACK | |
| 1190 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1191 | btrfs_free_path(path); |
| 1192 | return PTR_ERR(trans); |
| 1193 | } |
| 1194 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1195 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1196 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1197 | cow_start = (u64)-1; |
| 1198 | cur_offset = start; |
| 1199 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1200 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1201 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1202 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1203 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1204 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1205 | leaf = path->nodes[0]; |
| 1206 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1207 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1208 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1209 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1210 | path->slots[0]--; |
| 1211 | } |
| 1212 | check_prev = 0; |
| 1213 | next_slot: |
| 1214 | leaf = path->nodes[0]; |
| 1215 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1216 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1217 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1218 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1219 | if (ret > 0) |
| 1220 | break; |
| 1221 | leaf = path->nodes[0]; |
| 1222 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1223 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1224 | nocow = 0; |
| 1225 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1226 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1227 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1228 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1229 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1230 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1231 | found_key.offset > end) |
| 1232 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1233 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1234 | if (found_key.offset > cur_offset) { |
| 1235 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1236 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1237 | goto out_check; |
| 1238 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1239 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1240 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1241 | struct btrfs_file_extent_item); |
| 1242 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1243 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1244 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1245 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1246 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1247 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1248 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1249 | extent_end = found_key.offset + |
| 1250 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1251 | disk_num_bytes = |
| 1252 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1253 | if (extent_end <= start) { |
| 1254 | path->slots[0]++; |
| 1255 | goto next_slot; |
| 1256 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1257 | if (disk_bytenr == 0) |
| 1258 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1259 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1260 | btrfs_file_extent_encryption(leaf, fi) || |
| 1261 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1262 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1263 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1264 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1265 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1266 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1267 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1268 | found_key.offset - |
| 1269 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1270 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1271 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1272 | disk_bytenr += cur_offset - found_key.offset; |
| 1273 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1274 | /* |
| 1275 | * force cow if csum exists in the range. |
| 1276 | * this ensure that csum for a given extent are |
| 1277 | * either valid or do not exist. |
| 1278 | */ |
| 1279 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1280 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1281 | nocow = 1; |
| 1282 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1283 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1284 | btrfs_file_extent_inline_len(leaf, |
| 1285 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1286 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1287 | } else { |
| 1288 | BUG_ON(1); |
| 1289 | } |
| 1290 | out_check: |
| 1291 | if (extent_end <= start) { |
| 1292 | path->slots[0]++; |
| 1293 | goto next_slot; |
| 1294 | } |
| 1295 | if (!nocow) { |
| 1296 | if (cow_start == (u64)-1) |
| 1297 | cow_start = cur_offset; |
| 1298 | cur_offset = extent_end; |
| 1299 | if (cur_offset > end) |
| 1300 | break; |
| 1301 | path->slots[0]++; |
| 1302 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1303 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1304 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1305 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1306 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1307 | ret = cow_file_range(inode, locked_page, |
| 1308 | cow_start, found_key.offset - 1, |
| 1309 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1310 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1311 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1312 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1313 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1314 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1315 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1316 | struct extent_map *em; |
| 1317 | struct extent_map_tree *em_tree; |
| 1318 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1319 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1320 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1321 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1322 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1323 | em->len = num_bytes; |
| 1324 | em->block_len = num_bytes; |
| 1325 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1326 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1327 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1328 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1329 | em->mod_start = em->start; |
| 1330 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1331 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1332 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1333 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1334 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1335 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1336 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1337 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1338 | if (ret != -EEXIST) { |
| 1339 | free_extent_map(em); |
| 1340 | break; |
| 1341 | } |
| 1342 | btrfs_drop_extent_cache(inode, em->start, |
| 1343 | em->start + em->len - 1, 0); |
| 1344 | } |
| 1345 | type = BTRFS_ORDERED_PREALLOC; |
| 1346 | } else { |
| 1347 | type = BTRFS_ORDERED_NOCOW; |
| 1348 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1349 | |
| 1350 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1351 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1352 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1353 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1354 | if (root->root_key.objectid == |
| 1355 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1356 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1357 | num_bytes); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1358 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1359 | goto error; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1362 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1363 | cur_offset + num_bytes - 1, |
| 1364 | locked_page, EXTENT_LOCKED | |
| 1365 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1366 | PAGE_SET_PRIVATE2); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1367 | cur_offset = extent_end; |
| 1368 | if (cur_offset > end) |
| 1369 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1370 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1371 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1372 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1373 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1374 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1375 | cur_offset = end; |
| 1376 | } |
| 1377 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1378 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1379 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1380 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1381 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1382 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1385 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1386 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1387 | if (!ret) |
| 1388 | ret = err; |
| 1389 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1390 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1391 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1392 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1393 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1394 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1395 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1396 | PAGE_SET_WRITEBACK | |
| 1397 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1398 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1399 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1400 | } |
| 1401 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1402 | /* |
| 1403 | * extent_io.c call back to do delayed allocation processing |
| 1404 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1405 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1406 | u64 start, u64 end, int *page_started, |
| 1407 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1408 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1409 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1410 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1411 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1412 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1413 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1414 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1415 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1416 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1417 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1418 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1419 | !(BTRFS_I(inode)->force_compress) && |
| 1420 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1421 | ret = cow_file_range(inode, locked_page, start, end, |
| 1422 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1423 | } else { |
| 1424 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1425 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1426 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1427 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1428 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1429 | return ret; |
| 1430 | } |
| 1431 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1432 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1433 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1434 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1435 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1436 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1437 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1438 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1439 | spin_lock(&BTRFS_I(inode)->lock); |
| 1440 | BTRFS_I(inode)->outstanding_extents++; |
| 1441 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1446 | * extents so we can keep track of new extents that are just merged onto old |
| 1447 | * extents, such as when we are doing sequential writes, so we can properly |
| 1448 | * account for the metadata space we'll need. |
| 1449 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1450 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1451 | struct extent_state *new, |
| 1452 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1453 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1454 | /* not delalloc, ignore it */ |
| 1455 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1456 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1457 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1458 | spin_lock(&BTRFS_I(inode)->lock); |
| 1459 | BTRFS_I(inode)->outstanding_extents--; |
| 1460 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1461 | } |
| 1462 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1463 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1464 | struct inode *inode) |
| 1465 | { |
| 1466 | spin_lock(&root->delalloc_lock); |
| 1467 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1468 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1469 | &root->delalloc_inodes); |
| 1470 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1471 | &BTRFS_I(inode)->runtime_flags); |
| 1472 | root->nr_delalloc_inodes++; |
| 1473 | if (root->nr_delalloc_inodes == 1) { |
| 1474 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1475 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1476 | list_add_tail(&root->delalloc_root, |
| 1477 | &root->fs_info->delalloc_roots); |
| 1478 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1479 | } |
| 1480 | } |
| 1481 | spin_unlock(&root->delalloc_lock); |
| 1482 | } |
| 1483 | |
| 1484 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1485 | struct inode *inode) |
| 1486 | { |
| 1487 | spin_lock(&root->delalloc_lock); |
| 1488 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1489 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1490 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1491 | &BTRFS_I(inode)->runtime_flags); |
| 1492 | root->nr_delalloc_inodes--; |
| 1493 | if (!root->nr_delalloc_inodes) { |
| 1494 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1495 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1496 | list_del_init(&root->delalloc_root); |
| 1497 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1498 | } |
| 1499 | } |
| 1500 | spin_unlock(&root->delalloc_lock); |
| 1501 | } |
| 1502 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1503 | /* |
| 1504 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1505 | * bytes in this file, and to maintain the list of inodes that |
| 1506 | * have pending delalloc work to be done. |
| 1507 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1508 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1509 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1510 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1511 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1512 | /* |
| 1513 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1514 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1515 | * bit, which is only set or cleared with irqs on |
| 1516 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1517 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1518 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1519 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1520 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1521 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1522 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1523 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1524 | } else { |
| 1525 | spin_lock(&BTRFS_I(inode)->lock); |
| 1526 | BTRFS_I(inode)->outstanding_extents++; |
| 1527 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1528 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1529 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1530 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1531 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1532 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1533 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1534 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1535 | &BTRFS_I(inode)->runtime_flags)) |
| 1536 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1537 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1538 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1539 | } |
| 1540 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1541 | /* |
| 1542 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1543 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1544 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1545 | struct extent_state *state, |
| 1546 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1547 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1548 | /* |
| 1549 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1550 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1551 | * bit, which is only set or cleared with irqs on |
| 1552 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1553 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1554 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1555 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1556 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1557 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1558 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1559 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1560 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1561 | spin_lock(&BTRFS_I(inode)->lock); |
| 1562 | BTRFS_I(inode)->outstanding_extents--; |
| 1563 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1564 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1565 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1566 | /* |
| 1567 | * We don't reserve metadata space for space cache inodes so we |
| 1568 | * don't need to call dellalloc_release_metadata if there is an |
| 1569 | * error. |
| 1570 | */ |
| 1571 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1572 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1573 | btrfs_delalloc_release_metadata(inode, len); |
| 1574 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1575 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1576 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1577 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1578 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1579 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1580 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1581 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1582 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1583 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1584 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1585 | &BTRFS_I(inode)->runtime_flags)) |
| 1586 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1587 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1588 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1589 | } |
| 1590 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1591 | /* |
| 1592 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1593 | * we don't create bios that span stripes or chunks |
| 1594 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1595 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1596 | size_t size, struct bio *bio, |
| 1597 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1598 | { |
| 1599 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1600 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1601 | u64 length = 0; |
| 1602 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1603 | int ret; |
| 1604 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1605 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1606 | return 0; |
| 1607 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1608 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1609 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1610 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1611 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1612 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1613 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1614 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1615 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1616 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1617 | } |
| 1618 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1619 | /* |
| 1620 | * in order to insert checksums into the metadata in large chunks, |
| 1621 | * we wait until bio submission time. All the pages in the bio are |
| 1622 | * checksummed and sums are attached onto the ordered extent record. |
| 1623 | * |
| 1624 | * At IO completion time the cums attached on the ordered extent record |
| 1625 | * are inserted into the btree |
| 1626 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1627 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1628 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1629 | unsigned long bio_flags, |
| 1630 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1631 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1632 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1633 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1634 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1635 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1636 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1639 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1640 | /* |
| 1641 | * in order to insert checksums into the metadata in large chunks, |
| 1642 | * we wait until bio submission time. All the pages in the bio are |
| 1643 | * checksummed and sums are attached onto the ordered extent record. |
| 1644 | * |
| 1645 | * At IO completion time the cums attached on the ordered extent record |
| 1646 | * are inserted into the btree |
| 1647 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1648 | 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] | 1649 | int mirror_num, unsigned long bio_flags, |
| 1650 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1651 | { |
| 1652 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1653 | int ret; |
| 1654 | |
| 1655 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1656 | if (ret) |
| 1657 | bio_endio(bio, ret); |
| 1658 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1659 | } |
| 1660 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1661 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1662 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1663 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1664 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1665 | 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] | 1666 | int mirror_num, unsigned long bio_flags, |
| 1667 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1668 | { |
| 1669 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1670 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1671 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1672 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1673 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1674 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1675 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1676 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1677 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1678 | metadata = 2; |
| 1679 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1680 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1681 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1682 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1683 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1684 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1685 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1686 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1687 | mirror_num, |
| 1688 | bio_flags); |
| 1689 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1690 | } else if (!skip_sum) { |
| 1691 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1692 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1693 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1694 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1695 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1696 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1697 | /* csum items have already been cloned */ |
| 1698 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1699 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1700 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1701 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1702 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1703 | bio_flags, bio_offset, |
| 1704 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1705 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1706 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1707 | } else if (!skip_sum) { |
| 1708 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1709 | if (ret) |
| 1710 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1711 | } |
| 1712 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1713 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1714 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1715 | |
| 1716 | out: |
| 1717 | if (ret < 0) |
| 1718 | bio_endio(bio, ret); |
| 1719 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1720 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1721 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1722 | /* |
| 1723 | * given a list of ordered sums record them in the inode. This happens |
| 1724 | * at IO completion time based on sums calculated at bio submission time. |
| 1725 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1726 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1727 | struct inode *inode, u64 file_offset, |
| 1728 | struct list_head *list) |
| 1729 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1730 | struct btrfs_ordered_sum *sum; |
| 1731 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1732 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1733 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1734 | btrfs_csum_file_blocks(trans, |
| 1735 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1736 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1737 | } |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1741 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1742 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1743 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1744 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1745 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1746 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1747 | } |
| 1748 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1749 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1750 | struct btrfs_writepage_fixup { |
| 1751 | struct page *page; |
| 1752 | struct btrfs_work work; |
| 1753 | }; |
| 1754 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1755 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1756 | { |
| 1757 | struct btrfs_writepage_fixup *fixup; |
| 1758 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1759 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1760 | struct page *page; |
| 1761 | struct inode *inode; |
| 1762 | u64 page_start; |
| 1763 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1764 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1765 | |
| 1766 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1767 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1768 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1769 | lock_page(page); |
| 1770 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1771 | ClearPageChecked(page); |
| 1772 | goto out_page; |
| 1773 | } |
| 1774 | |
| 1775 | inode = page->mapping->host; |
| 1776 | page_start = page_offset(page); |
| 1777 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1778 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1779 | 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] | 1780 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1781 | |
| 1782 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1783 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1784 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1785 | |
| 1786 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1787 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1788 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1789 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1790 | unlock_page(page); |
| 1791 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1792 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1793 | goto again; |
| 1794 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1795 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1796 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1797 | if (ret) { |
| 1798 | mapping_set_error(page->mapping, ret); |
| 1799 | end_extent_writepage(page, ret, page_start, page_end); |
| 1800 | ClearPageChecked(page); |
| 1801 | goto out; |
| 1802 | } |
| 1803 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1804 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1805 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1806 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1807 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1808 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1809 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1810 | out_page: |
| 1811 | unlock_page(page); |
| 1812 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1813 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * There are a few paths in the higher layers of the kernel that directly |
| 1818 | * set the page dirty bit without asking the filesystem if it is a |
| 1819 | * good idea. This causes problems because we want to make sure COW |
| 1820 | * properly happens and the data=ordered rules are followed. |
| 1821 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1822 | * 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] | 1823 | * hasn't been properly setup for IO. We kick off an async process |
| 1824 | * to fix it up. The async helper will wait for ordered extents, set |
| 1825 | * the delalloc bit and make it safe to write the page. |
| 1826 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1827 | 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] | 1828 | { |
| 1829 | struct inode *inode = page->mapping->host; |
| 1830 | struct btrfs_writepage_fixup *fixup; |
| 1831 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1832 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1833 | /* this page is properly in the ordered list */ |
| 1834 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1835 | return 0; |
| 1836 | |
| 1837 | if (PageChecked(page)) |
| 1838 | return -EAGAIN; |
| 1839 | |
| 1840 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1841 | if (!fixup) |
| 1842 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1843 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1844 | SetPageChecked(page); |
| 1845 | page_cache_get(page); |
| 1846 | fixup->work.func = btrfs_writepage_fixup_worker; |
| 1847 | fixup->page = page; |
| 1848 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1849 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1852 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1853 | struct inode *inode, u64 file_pos, |
| 1854 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1855 | u64 num_bytes, u64 ram_bytes, |
| 1856 | u8 compression, u8 encryption, |
| 1857 | u16 other_encoding, int extent_type) |
| 1858 | { |
| 1859 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1860 | struct btrfs_file_extent_item *fi; |
| 1861 | struct btrfs_path *path; |
| 1862 | struct extent_buffer *leaf; |
| 1863 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1864 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1865 | int ret; |
| 1866 | |
| 1867 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1868 | if (!path) |
| 1869 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1870 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1871 | /* |
| 1872 | * we may be replacing one extent in the tree with another. |
| 1873 | * The new extent is pinned in the extent map, and we don't want |
| 1874 | * to drop it from the cache until it is completely in the btree. |
| 1875 | * |
| 1876 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1877 | * the caller is expected to unpin it and allow it to be merged |
| 1878 | * with the others. |
| 1879 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1880 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 1881 | file_pos + num_bytes, NULL, 0, |
| 1882 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1883 | if (ret) |
| 1884 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1885 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1886 | if (!extent_inserted) { |
| 1887 | ins.objectid = btrfs_ino(inode); |
| 1888 | ins.offset = file_pos; |
| 1889 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1890 | |
| 1891 | path->leave_spinning = 1; |
| 1892 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 1893 | sizeof(*fi)); |
| 1894 | if (ret) |
| 1895 | goto out; |
| 1896 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1897 | leaf = path->nodes[0]; |
| 1898 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1899 | struct btrfs_file_extent_item); |
| 1900 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1901 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1902 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1903 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1904 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1905 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1906 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1907 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1908 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1909 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1910 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1911 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1912 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1913 | |
| 1914 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1915 | |
| 1916 | ins.objectid = disk_bytenr; |
| 1917 | ins.offset = disk_num_bytes; |
| 1918 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1919 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1920 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1921 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1922 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1923 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1924 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1925 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1928 | /* snapshot-aware defrag */ |
| 1929 | struct sa_defrag_extent_backref { |
| 1930 | struct rb_node node; |
| 1931 | struct old_sa_defrag_extent *old; |
| 1932 | u64 root_id; |
| 1933 | u64 inum; |
| 1934 | u64 file_pos; |
| 1935 | u64 extent_offset; |
| 1936 | u64 num_bytes; |
| 1937 | u64 generation; |
| 1938 | }; |
| 1939 | |
| 1940 | struct old_sa_defrag_extent { |
| 1941 | struct list_head list; |
| 1942 | struct new_sa_defrag_extent *new; |
| 1943 | |
| 1944 | u64 extent_offset; |
| 1945 | u64 bytenr; |
| 1946 | u64 offset; |
| 1947 | u64 len; |
| 1948 | int count; |
| 1949 | }; |
| 1950 | |
| 1951 | struct new_sa_defrag_extent { |
| 1952 | struct rb_root root; |
| 1953 | struct list_head head; |
| 1954 | struct btrfs_path *path; |
| 1955 | struct inode *inode; |
| 1956 | u64 file_pos; |
| 1957 | u64 len; |
| 1958 | u64 bytenr; |
| 1959 | u64 disk_len; |
| 1960 | u8 compress_type; |
| 1961 | }; |
| 1962 | |
| 1963 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 1964 | struct sa_defrag_extent_backref *b2) |
| 1965 | { |
| 1966 | if (b1->root_id < b2->root_id) |
| 1967 | return -1; |
| 1968 | else if (b1->root_id > b2->root_id) |
| 1969 | return 1; |
| 1970 | |
| 1971 | if (b1->inum < b2->inum) |
| 1972 | return -1; |
| 1973 | else if (b1->inum > b2->inum) |
| 1974 | return 1; |
| 1975 | |
| 1976 | if (b1->file_pos < b2->file_pos) |
| 1977 | return -1; |
| 1978 | else if (b1->file_pos > b2->file_pos) |
| 1979 | return 1; |
| 1980 | |
| 1981 | /* |
| 1982 | * [------------------------------] ===> (a range of space) |
| 1983 | * |<--->| |<---->| =============> (fs/file tree A) |
| 1984 | * |<---------------------------->| ===> (fs/file tree B) |
| 1985 | * |
| 1986 | * A range of space can refer to two file extents in one tree while |
| 1987 | * refer to only one file extent in another tree. |
| 1988 | * |
| 1989 | * So we may process a disk offset more than one time(two extents in A) |
| 1990 | * and locate at the same extent(one extent in B), then insert two same |
| 1991 | * backrefs(both refer to the extent in B). |
| 1992 | */ |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
| 1996 | static void backref_insert(struct rb_root *root, |
| 1997 | struct sa_defrag_extent_backref *backref) |
| 1998 | { |
| 1999 | struct rb_node **p = &root->rb_node; |
| 2000 | struct rb_node *parent = NULL; |
| 2001 | struct sa_defrag_extent_backref *entry; |
| 2002 | int ret; |
| 2003 | |
| 2004 | while (*p) { |
| 2005 | parent = *p; |
| 2006 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2007 | |
| 2008 | ret = backref_comp(backref, entry); |
| 2009 | if (ret < 0) |
| 2010 | p = &(*p)->rb_left; |
| 2011 | else |
| 2012 | p = &(*p)->rb_right; |
| 2013 | } |
| 2014 | |
| 2015 | rb_link_node(&backref->node, parent, p); |
| 2016 | rb_insert_color(&backref->node, root); |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Note the backref might has changed, and in this case we just return 0. |
| 2021 | */ |
| 2022 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2023 | void *ctx) |
| 2024 | { |
| 2025 | struct btrfs_file_extent_item *extent; |
| 2026 | struct btrfs_fs_info *fs_info; |
| 2027 | struct old_sa_defrag_extent *old = ctx; |
| 2028 | struct new_sa_defrag_extent *new = old->new; |
| 2029 | struct btrfs_path *path = new->path; |
| 2030 | struct btrfs_key key; |
| 2031 | struct btrfs_root *root; |
| 2032 | struct sa_defrag_extent_backref *backref; |
| 2033 | struct extent_buffer *leaf; |
| 2034 | struct inode *inode = new->inode; |
| 2035 | int slot; |
| 2036 | int ret; |
| 2037 | u64 extent_offset; |
| 2038 | u64 num_bytes; |
| 2039 | |
| 2040 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2041 | inum == btrfs_ino(inode)) |
| 2042 | return 0; |
| 2043 | |
| 2044 | key.objectid = root_id; |
| 2045 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2046 | key.offset = (u64)-1; |
| 2047 | |
| 2048 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2049 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2050 | if (IS_ERR(root)) { |
| 2051 | if (PTR_ERR(root) == -ENOENT) |
| 2052 | return 0; |
| 2053 | WARN_ON(1); |
| 2054 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2055 | inum, offset, root_id); |
| 2056 | return PTR_ERR(root); |
| 2057 | } |
| 2058 | |
| 2059 | key.objectid = inum; |
| 2060 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2061 | if (offset > (u64)-1 << 32) |
| 2062 | key.offset = 0; |
| 2063 | else |
| 2064 | key.offset = offset; |
| 2065 | |
| 2066 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2067 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2068 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2069 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2070 | |
| 2071 | while (1) { |
| 2072 | cond_resched(); |
| 2073 | |
| 2074 | leaf = path->nodes[0]; |
| 2075 | slot = path->slots[0]; |
| 2076 | |
| 2077 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2078 | ret = btrfs_next_leaf(root, path); |
| 2079 | if (ret < 0) { |
| 2080 | goto out; |
| 2081 | } else if (ret > 0) { |
| 2082 | ret = 0; |
| 2083 | goto out; |
| 2084 | } |
| 2085 | continue; |
| 2086 | } |
| 2087 | |
| 2088 | path->slots[0]++; |
| 2089 | |
| 2090 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2091 | |
| 2092 | if (key.objectid > inum) |
| 2093 | goto out; |
| 2094 | |
| 2095 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2096 | continue; |
| 2097 | |
| 2098 | extent = btrfs_item_ptr(leaf, slot, |
| 2099 | struct btrfs_file_extent_item); |
| 2100 | |
| 2101 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2102 | continue; |
| 2103 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2104 | /* |
| 2105 | * 'offset' refers to the exact key.offset, |
| 2106 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2107 | * (key.offset - extent_offset). |
| 2108 | */ |
| 2109 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2110 | continue; |
| 2111 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2112 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2113 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2114 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2115 | if (extent_offset >= old->extent_offset + old->offset + |
| 2116 | old->len || extent_offset + num_bytes <= |
| 2117 | old->extent_offset + old->offset) |
| 2118 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2119 | break; |
| 2120 | } |
| 2121 | |
| 2122 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2123 | if (!backref) { |
| 2124 | ret = -ENOENT; |
| 2125 | goto out; |
| 2126 | } |
| 2127 | |
| 2128 | backref->root_id = root_id; |
| 2129 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2130 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2131 | backref->num_bytes = num_bytes; |
| 2132 | backref->extent_offset = extent_offset; |
| 2133 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2134 | backref->old = old; |
| 2135 | backref_insert(&new->root, backref); |
| 2136 | old->count++; |
| 2137 | out: |
| 2138 | btrfs_release_path(path); |
| 2139 | WARN_ON(ret); |
| 2140 | return ret; |
| 2141 | } |
| 2142 | |
| 2143 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2144 | struct new_sa_defrag_extent *new) |
| 2145 | { |
| 2146 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2147 | struct old_sa_defrag_extent *old, *tmp; |
| 2148 | int ret; |
| 2149 | |
| 2150 | new->path = path; |
| 2151 | |
| 2152 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2153 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2154 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2155 | path, record_one_backref, |
| 2156 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2157 | if (ret < 0 && ret != -ENOENT) |
| 2158 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2159 | |
| 2160 | /* no backref to be processed for this extent */ |
| 2161 | if (!old->count) { |
| 2162 | list_del(&old->list); |
| 2163 | kfree(old); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | if (list_empty(&new->head)) |
| 2168 | return false; |
| 2169 | |
| 2170 | return true; |
| 2171 | } |
| 2172 | |
| 2173 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2174 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2175 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2176 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2177 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2178 | return 0; |
| 2179 | |
| 2180 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2181 | return 0; |
| 2182 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2183 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2184 | return 0; |
| 2185 | |
| 2186 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2187 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2188 | return 0; |
| 2189 | |
| 2190 | return 1; |
| 2191 | } |
| 2192 | |
| 2193 | /* |
| 2194 | * Note the backref might has changed, and in this case we just return 0. |
| 2195 | */ |
| 2196 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2197 | struct sa_defrag_extent_backref *prev, |
| 2198 | struct sa_defrag_extent_backref *backref) |
| 2199 | { |
| 2200 | struct btrfs_file_extent_item *extent; |
| 2201 | struct btrfs_file_extent_item *item; |
| 2202 | struct btrfs_ordered_extent *ordered; |
| 2203 | struct btrfs_trans_handle *trans; |
| 2204 | struct btrfs_fs_info *fs_info; |
| 2205 | struct btrfs_root *root; |
| 2206 | struct btrfs_key key; |
| 2207 | struct extent_buffer *leaf; |
| 2208 | struct old_sa_defrag_extent *old = backref->old; |
| 2209 | struct new_sa_defrag_extent *new = old->new; |
| 2210 | struct inode *src_inode = new->inode; |
| 2211 | struct inode *inode; |
| 2212 | struct extent_state *cached = NULL; |
| 2213 | int ret = 0; |
| 2214 | u64 start; |
| 2215 | u64 len; |
| 2216 | u64 lock_start; |
| 2217 | u64 lock_end; |
| 2218 | bool merge = false; |
| 2219 | int index; |
| 2220 | |
| 2221 | if (prev && prev->root_id == backref->root_id && |
| 2222 | prev->inum == backref->inum && |
| 2223 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2224 | merge = true; |
| 2225 | |
| 2226 | /* step 1: get root */ |
| 2227 | key.objectid = backref->root_id; |
| 2228 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2229 | key.offset = (u64)-1; |
| 2230 | |
| 2231 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2232 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2233 | |
| 2234 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2235 | if (IS_ERR(root)) { |
| 2236 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2237 | if (PTR_ERR(root) == -ENOENT) |
| 2238 | return 0; |
| 2239 | return PTR_ERR(root); |
| 2240 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2241 | |
| 2242 | /* step 2: get inode */ |
| 2243 | key.objectid = backref->inum; |
| 2244 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2245 | key.offset = 0; |
| 2246 | |
| 2247 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2248 | if (IS_ERR(inode)) { |
| 2249 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2250 | return 0; |
| 2251 | } |
| 2252 | |
| 2253 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2254 | |
| 2255 | /* step 3: relink backref */ |
| 2256 | lock_start = backref->file_pos; |
| 2257 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2258 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2259 | 0, &cached); |
| 2260 | |
| 2261 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2262 | if (ordered) { |
| 2263 | btrfs_put_ordered_extent(ordered); |
| 2264 | goto out_unlock; |
| 2265 | } |
| 2266 | |
| 2267 | trans = btrfs_join_transaction(root); |
| 2268 | if (IS_ERR(trans)) { |
| 2269 | ret = PTR_ERR(trans); |
| 2270 | goto out_unlock; |
| 2271 | } |
| 2272 | |
| 2273 | key.objectid = backref->inum; |
| 2274 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2275 | key.offset = backref->file_pos; |
| 2276 | |
| 2277 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2278 | if (ret < 0) { |
| 2279 | goto out_free_path; |
| 2280 | } else if (ret > 0) { |
| 2281 | ret = 0; |
| 2282 | goto out_free_path; |
| 2283 | } |
| 2284 | |
| 2285 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2286 | struct btrfs_file_extent_item); |
| 2287 | |
| 2288 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2289 | backref->generation) |
| 2290 | goto out_free_path; |
| 2291 | |
| 2292 | btrfs_release_path(path); |
| 2293 | |
| 2294 | start = backref->file_pos; |
| 2295 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2296 | start += old->extent_offset + old->offset - |
| 2297 | backref->extent_offset; |
| 2298 | |
| 2299 | len = min(backref->extent_offset + backref->num_bytes, |
| 2300 | old->extent_offset + old->offset + old->len); |
| 2301 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2302 | |
| 2303 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2304 | start + len, 1); |
| 2305 | if (ret) |
| 2306 | goto out_free_path; |
| 2307 | again: |
| 2308 | key.objectid = btrfs_ino(inode); |
| 2309 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2310 | key.offset = start; |
| 2311 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2312 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2313 | if (merge) { |
| 2314 | struct btrfs_file_extent_item *fi; |
| 2315 | u64 extent_len; |
| 2316 | struct btrfs_key found_key; |
| 2317 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2318 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2319 | if (ret < 0) |
| 2320 | goto out_free_path; |
| 2321 | |
| 2322 | path->slots[0]--; |
| 2323 | leaf = path->nodes[0]; |
| 2324 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2325 | |
| 2326 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2327 | struct btrfs_file_extent_item); |
| 2328 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2329 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2330 | if (extent_len + found_key.offset == start && |
| 2331 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2332 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2333 | extent_len + len); |
| 2334 | btrfs_mark_buffer_dirty(leaf); |
| 2335 | inode_add_bytes(inode, len); |
| 2336 | |
| 2337 | ret = 1; |
| 2338 | goto out_free_path; |
| 2339 | } else { |
| 2340 | merge = false; |
| 2341 | btrfs_release_path(path); |
| 2342 | goto again; |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2347 | sizeof(*extent)); |
| 2348 | if (ret) { |
| 2349 | btrfs_abort_transaction(trans, root, ret); |
| 2350 | goto out_free_path; |
| 2351 | } |
| 2352 | |
| 2353 | leaf = path->nodes[0]; |
| 2354 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2355 | struct btrfs_file_extent_item); |
| 2356 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2357 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2358 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2359 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2360 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2361 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2362 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2363 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2364 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2365 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2366 | |
| 2367 | btrfs_mark_buffer_dirty(leaf); |
| 2368 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2369 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2370 | |
| 2371 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2372 | new->disk_len, 0, |
| 2373 | backref->root_id, backref->inum, |
| 2374 | new->file_pos, 0); /* start - extent_offset */ |
| 2375 | if (ret) { |
| 2376 | btrfs_abort_transaction(trans, root, ret); |
| 2377 | goto out_free_path; |
| 2378 | } |
| 2379 | |
| 2380 | ret = 1; |
| 2381 | out_free_path: |
| 2382 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2383 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2384 | btrfs_end_transaction(trans, root); |
| 2385 | out_unlock: |
| 2386 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2387 | &cached, GFP_NOFS); |
| 2388 | iput(inode); |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2392 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2393 | { |
| 2394 | struct old_sa_defrag_extent *old, *tmp; |
| 2395 | |
| 2396 | if (!new) |
| 2397 | return; |
| 2398 | |
| 2399 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2400 | list_del(&old->list); |
| 2401 | kfree(old); |
| 2402 | } |
| 2403 | kfree(new); |
| 2404 | } |
| 2405 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2406 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2407 | { |
| 2408 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2409 | struct sa_defrag_extent_backref *backref; |
| 2410 | struct sa_defrag_extent_backref *prev = NULL; |
| 2411 | struct inode *inode; |
| 2412 | struct btrfs_root *root; |
| 2413 | struct rb_node *node; |
| 2414 | int ret; |
| 2415 | |
| 2416 | inode = new->inode; |
| 2417 | root = BTRFS_I(inode)->root; |
| 2418 | |
| 2419 | path = btrfs_alloc_path(); |
| 2420 | if (!path) |
| 2421 | return; |
| 2422 | |
| 2423 | if (!record_extent_backrefs(path, new)) { |
| 2424 | btrfs_free_path(path); |
| 2425 | goto out; |
| 2426 | } |
| 2427 | btrfs_release_path(path); |
| 2428 | |
| 2429 | while (1) { |
| 2430 | node = rb_first(&new->root); |
| 2431 | if (!node) |
| 2432 | break; |
| 2433 | rb_erase(node, &new->root); |
| 2434 | |
| 2435 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2436 | |
| 2437 | ret = relink_extent_backref(path, prev, backref); |
| 2438 | WARN_ON(ret < 0); |
| 2439 | |
| 2440 | kfree(prev); |
| 2441 | |
| 2442 | if (ret == 1) |
| 2443 | prev = backref; |
| 2444 | else |
| 2445 | prev = NULL; |
| 2446 | cond_resched(); |
| 2447 | } |
| 2448 | kfree(prev); |
| 2449 | |
| 2450 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2451 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2452 | free_sa_defrag_extent(new); |
| 2453 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2454 | atomic_dec(&root->fs_info->defrag_running); |
| 2455 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | static struct new_sa_defrag_extent * |
| 2459 | record_old_file_extents(struct inode *inode, |
| 2460 | struct btrfs_ordered_extent *ordered) |
| 2461 | { |
| 2462 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2463 | struct btrfs_path *path; |
| 2464 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2465 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2466 | struct new_sa_defrag_extent *new; |
| 2467 | int ret; |
| 2468 | |
| 2469 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2470 | if (!new) |
| 2471 | return NULL; |
| 2472 | |
| 2473 | new->inode = inode; |
| 2474 | new->file_pos = ordered->file_offset; |
| 2475 | new->len = ordered->len; |
| 2476 | new->bytenr = ordered->start; |
| 2477 | new->disk_len = ordered->disk_len; |
| 2478 | new->compress_type = ordered->compress_type; |
| 2479 | new->root = RB_ROOT; |
| 2480 | INIT_LIST_HEAD(&new->head); |
| 2481 | |
| 2482 | path = btrfs_alloc_path(); |
| 2483 | if (!path) |
| 2484 | goto out_kfree; |
| 2485 | |
| 2486 | key.objectid = btrfs_ino(inode); |
| 2487 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2488 | key.offset = new->file_pos; |
| 2489 | |
| 2490 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2491 | if (ret < 0) |
| 2492 | goto out_free_path; |
| 2493 | if (ret > 0 && path->slots[0] > 0) |
| 2494 | path->slots[0]--; |
| 2495 | |
| 2496 | /* find out all the old extents for the file range */ |
| 2497 | while (1) { |
| 2498 | struct btrfs_file_extent_item *extent; |
| 2499 | struct extent_buffer *l; |
| 2500 | int slot; |
| 2501 | u64 num_bytes; |
| 2502 | u64 offset; |
| 2503 | u64 end; |
| 2504 | u64 disk_bytenr; |
| 2505 | u64 extent_offset; |
| 2506 | |
| 2507 | l = path->nodes[0]; |
| 2508 | slot = path->slots[0]; |
| 2509 | |
| 2510 | if (slot >= btrfs_header_nritems(l)) { |
| 2511 | ret = btrfs_next_leaf(root, path); |
| 2512 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2513 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2514 | else if (ret > 0) |
| 2515 | break; |
| 2516 | continue; |
| 2517 | } |
| 2518 | |
| 2519 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2520 | |
| 2521 | if (key.objectid != btrfs_ino(inode)) |
| 2522 | break; |
| 2523 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2524 | break; |
| 2525 | if (key.offset >= new->file_pos + new->len) |
| 2526 | break; |
| 2527 | |
| 2528 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2529 | |
| 2530 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2531 | if (key.offset + num_bytes < new->file_pos) |
| 2532 | goto next; |
| 2533 | |
| 2534 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2535 | if (!disk_bytenr) |
| 2536 | goto next; |
| 2537 | |
| 2538 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2539 | |
| 2540 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2541 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2542 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2543 | |
| 2544 | offset = max(new->file_pos, key.offset); |
| 2545 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2546 | |
| 2547 | old->bytenr = disk_bytenr; |
| 2548 | old->extent_offset = extent_offset; |
| 2549 | old->offset = offset - key.offset; |
| 2550 | old->len = end - offset; |
| 2551 | old->new = new; |
| 2552 | old->count = 0; |
| 2553 | list_add_tail(&old->list, &new->head); |
| 2554 | next: |
| 2555 | path->slots[0]++; |
| 2556 | cond_resched(); |
| 2557 | } |
| 2558 | |
| 2559 | btrfs_free_path(path); |
| 2560 | atomic_inc(&root->fs_info->defrag_running); |
| 2561 | |
| 2562 | return new; |
| 2563 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2564 | out_free_path: |
| 2565 | btrfs_free_path(path); |
| 2566 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2567 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2568 | return NULL; |
| 2569 | } |
| 2570 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2571 | /* as ordered data IO finishes, this gets called so we can finish |
| 2572 | * an ordered extent if the range of bytes in the file it covers are |
| 2573 | * fully written. |
| 2574 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2575 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2576 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2577 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2578 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2579 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2580 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2581 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2582 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2583 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2584 | int ret = 0; |
| 2585 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2586 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2587 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2588 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2589 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2590 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2591 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2592 | ret = -EIO; |
| 2593 | goto out; |
| 2594 | } |
| 2595 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2596 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2597 | truncated = true; |
| 2598 | logical_len = ordered_extent->truncated_len; |
| 2599 | /* Truncated the entire extent, don't bother adding */ |
| 2600 | if (!logical_len) |
| 2601 | goto out; |
| 2602 | } |
| 2603 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2604 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2605 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2606 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2607 | if (nolock) |
| 2608 | trans = btrfs_join_transaction_nolock(root); |
| 2609 | else |
| 2610 | trans = btrfs_join_transaction(root); |
| 2611 | if (IS_ERR(trans)) { |
| 2612 | ret = PTR_ERR(trans); |
| 2613 | trans = NULL; |
| 2614 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2615 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2616 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2617 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2618 | if (ret) /* -ENOMEM or corruption */ |
| 2619 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2620 | goto out; |
| 2621 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2622 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2623 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2624 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2625 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2626 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2627 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2628 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2629 | EXTENT_DEFRAG, 1, cached_state); |
| 2630 | if (ret) { |
| 2631 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2632 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2633 | /* the inode is shared */ |
| 2634 | new = record_old_file_extents(inode, ordered_extent); |
| 2635 | |
| 2636 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2637 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2638 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2639 | } |
| 2640 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2641 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2642 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2643 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2644 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2645 | if (IS_ERR(trans)) { |
| 2646 | ret = PTR_ERR(trans); |
| 2647 | trans = NULL; |
| 2648 | goto out_unlock; |
| 2649 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2650 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2651 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2652 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2653 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2654 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2655 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2656 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2657 | ordered_extent->file_offset, |
| 2658 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2659 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2660 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2661 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2662 | ret = insert_reserved_file_extent(trans, inode, |
| 2663 | ordered_extent->file_offset, |
| 2664 | ordered_extent->start, |
| 2665 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2666 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2667 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2668 | BTRFS_FILE_EXTENT_REG); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2669 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2670 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2671 | ordered_extent->file_offset, ordered_extent->len, |
| 2672 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2673 | if (ret < 0) { |
| 2674 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2675 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2676 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2677 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2678 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2679 | &ordered_extent->list); |
| 2680 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2681 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2682 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2683 | if (ret) { /* -ENOMEM or corruption */ |
| 2684 | btrfs_abort_transaction(trans, root, ret); |
| 2685 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2686 | } |
| 2687 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2688 | out_unlock: |
| 2689 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2690 | ordered_extent->file_offset + |
| 2691 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2692 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2693 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2694 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2695 | if (trans) |
| 2696 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2697 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2698 | if (ret || truncated) { |
| 2699 | u64 start, end; |
| 2700 | |
| 2701 | if (truncated) |
| 2702 | start = ordered_extent->file_offset + logical_len; |
| 2703 | else |
| 2704 | start = ordered_extent->file_offset; |
| 2705 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2706 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2707 | |
| 2708 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2709 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2710 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2711 | /* |
| 2712 | * If the ordered extent had an IOERR or something else went |
| 2713 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2714 | * back to the allocator. We only free the extent in the |
| 2715 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2716 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2717 | if ((ret || !logical_len) && |
| 2718 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2719 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2720 | btrfs_free_reserved_extent(root, ordered_extent->start, |
| 2721 | ordered_extent->disk_len); |
| 2722 | } |
| 2723 | |
| 2724 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2725 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2726 | * This needs to be done to make sure anybody waiting knows we are done |
| 2727 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2728 | */ |
| 2729 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2730 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2731 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2732 | if (new) { |
| 2733 | if (ret) { |
| 2734 | free_sa_defrag_extent(new); |
| 2735 | atomic_dec(&root->fs_info->defrag_running); |
| 2736 | } else { |
| 2737 | relink_file_extents(new); |
| 2738 | } |
| 2739 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2740 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2741 | /* once for us */ |
| 2742 | btrfs_put_ordered_extent(ordered_extent); |
| 2743 | /* once for the tree */ |
| 2744 | btrfs_put_ordered_extent(ordered_extent); |
| 2745 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2746 | return ret; |
| 2747 | } |
| 2748 | |
| 2749 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2750 | { |
| 2751 | struct btrfs_ordered_extent *ordered_extent; |
| 2752 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2753 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2754 | } |
| 2755 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2756 | 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] | 2757 | struct extent_state *state, int uptodate) |
| 2758 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2759 | struct inode *inode = page->mapping->host; |
| 2760 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2761 | struct btrfs_ordered_extent *ordered_extent = NULL; |
| 2762 | struct btrfs_workers *workers; |
| 2763 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2764 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2765 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2766 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2767 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2768 | end - start + 1, uptodate)) |
| 2769 | return 0; |
| 2770 | |
| 2771 | ordered_extent->work.func = finish_ordered_fn; |
| 2772 | ordered_extent->work.flags = 0; |
| 2773 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2774 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2775 | workers = &root->fs_info->endio_freespace_worker; |
| 2776 | else |
| 2777 | workers = &root->fs_info->endio_write_workers; |
| 2778 | btrfs_queue_worker(workers, &ordered_extent->work); |
| 2779 | |
| 2780 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2781 | } |
| 2782 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2783 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2784 | * 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] | 2785 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2786 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2787 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2788 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2789 | u64 phy_offset, struct page *page, |
| 2790 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2791 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2792 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2793 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2794 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2795 | char *kaddr; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2796 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2797 | u32 csum_expected; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2798 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2799 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2800 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2801 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2802 | if (PageChecked(page)) { |
| 2803 | ClearPageChecked(page); |
| 2804 | goto good; |
| 2805 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2806 | |
| 2807 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2808 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2809 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2810 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2811 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2812 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2813 | GFP_NOFS); |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2817 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2818 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2819 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2820 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2821 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2822 | btrfs_csum_final(csum, (char *)&csum); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2823 | if (csum != csum_expected) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2824 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2825 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2826 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2827 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2828 | return 0; |
| 2829 | |
| 2830 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2831 | if (__ratelimit(&_rs)) |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2832 | btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2833 | btrfs_ino(page->mapping->host), start, csum, csum_expected); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2834 | memset(kaddr + offset, 1, end - start + 1); |
| 2835 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2836 | kunmap_atomic(kaddr); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2837 | if (csum_expected == 0) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2838 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2839 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2840 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2841 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2842 | struct delayed_iput { |
| 2843 | struct list_head list; |
| 2844 | struct inode *inode; |
| 2845 | }; |
| 2846 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2847 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2848 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2849 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2850 | { |
| 2851 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2852 | struct delayed_iput *delayed; |
| 2853 | |
| 2854 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2855 | return; |
| 2856 | |
| 2857 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2858 | delayed->inode = inode; |
| 2859 | |
| 2860 | spin_lock(&fs_info->delayed_iput_lock); |
| 2861 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2862 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2863 | } |
| 2864 | |
| 2865 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2866 | { |
| 2867 | LIST_HEAD(list); |
| 2868 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2869 | struct delayed_iput *delayed; |
| 2870 | int empty; |
| 2871 | |
| 2872 | spin_lock(&fs_info->delayed_iput_lock); |
| 2873 | empty = list_empty(&fs_info->delayed_iputs); |
| 2874 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2875 | if (empty) |
| 2876 | return; |
| 2877 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2878 | spin_lock(&fs_info->delayed_iput_lock); |
| 2879 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2880 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2881 | |
| 2882 | while (!list_empty(&list)) { |
| 2883 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2884 | list_del(&delayed->list); |
| 2885 | iput(delayed->inode); |
| 2886 | kfree(delayed); |
| 2887 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2890 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2891 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2892 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2893 | * structure. |
| 2894 | */ |
| 2895 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2896 | struct btrfs_root *root) |
| 2897 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2898 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2899 | int ret; |
| 2900 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2901 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2902 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2903 | return; |
| 2904 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2905 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2906 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2907 | spin_unlock(&root->orphan_lock); |
| 2908 | return; |
| 2909 | } |
| 2910 | |
| 2911 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2912 | spin_unlock(&root->orphan_lock); |
| 2913 | return; |
| 2914 | } |
| 2915 | |
| 2916 | block_rsv = root->orphan_block_rsv; |
| 2917 | root->orphan_block_rsv = NULL; |
| 2918 | spin_unlock(&root->orphan_lock); |
| 2919 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2920 | if (root->orphan_item_inserted && |
| 2921 | btrfs_root_refs(&root->root_item) > 0) { |
| 2922 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2923 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2924 | if (ret) |
| 2925 | btrfs_abort_transaction(trans, root, ret); |
| 2926 | else |
| 2927 | root->orphan_item_inserted = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2928 | } |
| 2929 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2930 | if (block_rsv) { |
| 2931 | WARN_ON(block_rsv->size > 0); |
| 2932 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2937 | * This creates an orphan entry for the given inode in case something goes |
| 2938 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2939 | * |
| 2940 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 2941 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2942 | */ |
| 2943 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 2944 | { |
| 2945 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2946 | struct btrfs_block_rsv *block_rsv = NULL; |
| 2947 | int reserve = 0; |
| 2948 | int insert = 0; |
| 2949 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2950 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2951 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 2952 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 2953 | if (!block_rsv) |
| 2954 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2955 | } |
| 2956 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2957 | spin_lock(&root->orphan_lock); |
| 2958 | if (!root->orphan_block_rsv) { |
| 2959 | root->orphan_block_rsv = block_rsv; |
| 2960 | } else if (block_rsv) { |
| 2961 | btrfs_free_block_rsv(root, block_rsv); |
| 2962 | block_rsv = NULL; |
| 2963 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2964 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2965 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 2966 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2967 | #if 0 |
| 2968 | /* |
| 2969 | * For proper ENOSPC handling, we should do orphan |
| 2970 | * cleanup when mounting. But this introduces backward |
| 2971 | * compatibility issue. |
| 2972 | */ |
| 2973 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 2974 | insert = 2; |
| 2975 | else |
| 2976 | insert = 1; |
| 2977 | #endif |
| 2978 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 2979 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2980 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2981 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 2982 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 2983 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2984 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2985 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 2986 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2987 | /* grab metadata reservation from transaction handle */ |
| 2988 | if (reserve) { |
| 2989 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2990 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | /* insert an orphan item to track this unlinked/truncated file */ |
| 2994 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2995 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2996 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 2997 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2998 | if (reserve) { |
| 2999 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3000 | &BTRFS_I(inode)->runtime_flags); |
| 3001 | btrfs_orphan_release_metadata(inode); |
| 3002 | } |
| 3003 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3004 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3005 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3006 | btrfs_abort_transaction(trans, root, ret); |
| 3007 | return ret; |
| 3008 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3009 | } |
| 3010 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3014 | if (insert >= 2) { |
| 3015 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3016 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3017 | if (ret && ret != -EEXIST) { |
| 3018 | btrfs_abort_transaction(trans, root, ret); |
| 3019 | return ret; |
| 3020 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3021 | } |
| 3022 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /* |
| 3026 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3027 | * item for this particular inode. |
| 3028 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3029 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3030 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3031 | { |
| 3032 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3033 | int delete_item = 0; |
| 3034 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3035 | int ret = 0; |
| 3036 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3037 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3038 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3039 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3040 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3041 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3042 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3043 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3044 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3045 | spin_unlock(&root->orphan_lock); |
| 3046 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3047 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3048 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3049 | if (trans) |
| 3050 | ret = btrfs_del_orphan_item(trans, root, |
| 3051 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3052 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3053 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3054 | if (release_rsv) |
| 3055 | btrfs_orphan_release_metadata(inode); |
| 3056 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3057 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * this cleans up any orphans that may be left on the list from the last use |
| 3062 | * of this root. |
| 3063 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3064 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3065 | { |
| 3066 | struct btrfs_path *path; |
| 3067 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3068 | struct btrfs_key key, found_key; |
| 3069 | struct btrfs_trans_handle *trans; |
| 3070 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3071 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3072 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3073 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3074 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3075 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3076 | |
| 3077 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3078 | if (!path) { |
| 3079 | ret = -ENOMEM; |
| 3080 | goto out; |
| 3081 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3082 | path->reada = -1; |
| 3083 | |
| 3084 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3085 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3086 | key.offset = (u64)-1; |
| 3087 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3088 | while (1) { |
| 3089 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3090 | if (ret < 0) |
| 3091 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | * 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] | 3095 | * 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] | 3096 | * find the key and see if we have stuff that matches |
| 3097 | */ |
| 3098 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3099 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3100 | if (path->slots[0] == 0) |
| 3101 | break; |
| 3102 | path->slots[0]--; |
| 3103 | } |
| 3104 | |
| 3105 | /* pull out the item */ |
| 3106 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3107 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3108 | |
| 3109 | /* make sure the item matches what we want */ |
| 3110 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3111 | break; |
| 3112 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3113 | break; |
| 3114 | |
| 3115 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3116 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3117 | |
| 3118 | /* |
| 3119 | * this is where we are basically btrfs_lookup, without the |
| 3120 | * crossing root thing. we store the inode number in the |
| 3121 | * offset of the orphan item. |
| 3122 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3123 | |
| 3124 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3125 | btrfs_err(root->fs_info, |
| 3126 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3127 | ret = -EINVAL; |
| 3128 | goto out; |
| 3129 | } |
| 3130 | |
| 3131 | last_objectid = found_key.offset; |
| 3132 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3133 | found_key.objectid = found_key.offset; |
| 3134 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3135 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3136 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3137 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3138 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3139 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3140 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3141 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3142 | struct btrfs_root *dead_root; |
| 3143 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3144 | int is_dead_root = 0; |
| 3145 | |
| 3146 | /* |
| 3147 | * this is an orphan in the tree root. Currently these |
| 3148 | * could come from 2 sources: |
| 3149 | * a) a snapshot deletion in progress |
| 3150 | * b) a free space cache inode |
| 3151 | * We need to distinguish those two, as the snapshot |
| 3152 | * orphan must not get deleted. |
| 3153 | * find_dead_roots already ran before us, so if this |
| 3154 | * is a snapshot deletion, we should find the root |
| 3155 | * in the dead_roots list |
| 3156 | */ |
| 3157 | spin_lock(&fs_info->trans_lock); |
| 3158 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3159 | root_list) { |
| 3160 | if (dead_root->root_key.objectid == |
| 3161 | found_key.objectid) { |
| 3162 | is_dead_root = 1; |
| 3163 | break; |
| 3164 | } |
| 3165 | } |
| 3166 | spin_unlock(&fs_info->trans_lock); |
| 3167 | if (is_dead_root) { |
| 3168 | /* prevent this orphan from being found again */ |
| 3169 | key.offset = found_key.objectid - 1; |
| 3170 | continue; |
| 3171 | } |
| 3172 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3173 | /* |
| 3174 | * Inode is already gone but the orphan item is still there, |
| 3175 | * kill the orphan item. |
| 3176 | */ |
| 3177 | if (ret == -ESTALE) { |
| 3178 | trans = btrfs_start_transaction(root, 1); |
| 3179 | if (IS_ERR(trans)) { |
| 3180 | ret = PTR_ERR(trans); |
| 3181 | goto out; |
| 3182 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3183 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3184 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3185 | ret = btrfs_del_orphan_item(trans, root, |
| 3186 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3187 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3188 | if (ret) |
| 3189 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3190 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3191 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3192 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3193 | /* |
| 3194 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3195 | * the proper thing when we hit it |
| 3196 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3197 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3198 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3199 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3200 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3201 | /* if we have links, this was a truncate, lets do that */ |
| 3202 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3203 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3204 | iput(inode); |
| 3205 | continue; |
| 3206 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3207 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3208 | |
| 3209 | /* 1 for the orphan item deletion. */ |
| 3210 | trans = btrfs_start_transaction(root, 1); |
| 3211 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3212 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3213 | ret = PTR_ERR(trans); |
| 3214 | goto out; |
| 3215 | } |
| 3216 | ret = btrfs_orphan_add(trans, inode); |
| 3217 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3218 | if (ret) { |
| 3219 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3220 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3221 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3222 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3223 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3224 | if (ret) |
| 3225 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3226 | } else { |
| 3227 | nr_unlink++; |
| 3228 | } |
| 3229 | |
| 3230 | /* this will do delete_inode and everything for us */ |
| 3231 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3232 | if (ret) |
| 3233 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3234 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3235 | /* release the path since we're done with it */ |
| 3236 | btrfs_release_path(path); |
| 3237 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3238 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3239 | |
| 3240 | if (root->orphan_block_rsv) |
| 3241 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3242 | (u64)-1); |
| 3243 | |
| 3244 | if (root->orphan_block_rsv || root->orphan_item_inserted) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3245 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3246 | if (!IS_ERR(trans)) |
| 3247 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3248 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3249 | |
| 3250 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3251 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3252 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3253 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3254 | |
| 3255 | out: |
| 3256 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3257 | btrfs_crit(root->fs_info, |
| 3258 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3259 | btrfs_free_path(path); |
| 3260 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3261 | } |
| 3262 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3263 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3264 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3265 | * don't find any xattrs, we know there can't be any acls. |
| 3266 | * |
| 3267 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3268 | */ |
| 3269 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3270 | int slot, u64 objectid, |
| 3271 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3272 | { |
| 3273 | u32 nritems = btrfs_header_nritems(leaf); |
| 3274 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3275 | static u64 xattr_access = 0; |
| 3276 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3277 | int scanned = 0; |
| 3278 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3279 | if (!xattr_access) { |
| 3280 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3281 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3282 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3283 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3284 | } |
| 3285 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3286 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3287 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3288 | while (slot < nritems) { |
| 3289 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3290 | |
| 3291 | /* we found a different objectid, there must not be acls */ |
| 3292 | if (found_key.objectid != objectid) |
| 3293 | return 0; |
| 3294 | |
| 3295 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3296 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3297 | if (*first_xattr_slot == -1) |
| 3298 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3299 | if (found_key.offset == xattr_access || |
| 3300 | found_key.offset == xattr_default) |
| 3301 | return 1; |
| 3302 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3303 | |
| 3304 | /* |
| 3305 | * we found a key greater than an xattr key, there can't |
| 3306 | * be any acls later on |
| 3307 | */ |
| 3308 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3309 | return 0; |
| 3310 | |
| 3311 | slot++; |
| 3312 | scanned++; |
| 3313 | |
| 3314 | /* |
| 3315 | * it goes inode, inode backrefs, xattrs, extents, |
| 3316 | * so if there are a ton of hard links to an inode there can |
| 3317 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3318 | * this is just an optimization |
| 3319 | */ |
| 3320 | if (scanned >= 8) |
| 3321 | break; |
| 3322 | } |
| 3323 | /* we hit the end of the leaf before we found an xattr or |
| 3324 | * something larger than an xattr. We have to assume the inode |
| 3325 | * has acls |
| 3326 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3327 | if (*first_xattr_slot == -1) |
| 3328 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3329 | return 1; |
| 3330 | } |
| 3331 | |
| 3332 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3333 | * read an inode from the btree into the in-memory inode |
| 3334 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3335 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3336 | { |
| 3337 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3338 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3339 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3340 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3341 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3342 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3343 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3344 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3345 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3346 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3347 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3348 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3349 | |
| 3350 | ret = btrfs_fill_inode(inode, &rdev); |
| 3351 | if (!ret) |
| 3352 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3353 | |
| 3354 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3355 | if (!path) |
| 3356 | goto make_bad; |
| 3357 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3358 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3359 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3360 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3361 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3362 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3363 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3364 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3365 | |
| 3366 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3367 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3368 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3369 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3370 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3371 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3372 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3373 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3374 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3375 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3376 | |
| 3377 | tspec = btrfs_inode_atime(inode_item); |
| 3378 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3379 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3380 | |
| 3381 | tspec = btrfs_inode_mtime(inode_item); |
| 3382 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3383 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3384 | |
| 3385 | tspec = btrfs_inode_ctime(inode_item); |
| 3386 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3387 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3388 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3389 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3390 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3391 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3392 | |
| 3393 | /* |
| 3394 | * If we were modified in the current generation and evicted from memory |
| 3395 | * and then re-read we need to do a full sync since we don't have any |
| 3396 | * idea about which extents were modified before we were evicted from |
| 3397 | * cache. |
| 3398 | */ |
| 3399 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3400 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3401 | &BTRFS_I(inode)->runtime_flags); |
| 3402 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3403 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3404 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3405 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3406 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3407 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3408 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3409 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3410 | |
| 3411 | cache_index: |
| 3412 | path->slots[0]++; |
| 3413 | if (inode->i_nlink != 1 || |
| 3414 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3415 | goto cache_acl; |
| 3416 | |
| 3417 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3418 | if (location.objectid != btrfs_ino(inode)) |
| 3419 | goto cache_acl; |
| 3420 | |
| 3421 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3422 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3423 | struct btrfs_inode_ref *ref; |
| 3424 | |
| 3425 | ref = (struct btrfs_inode_ref *)ptr; |
| 3426 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3427 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3428 | struct btrfs_inode_extref *extref; |
| 3429 | |
| 3430 | extref = (struct btrfs_inode_extref *)ptr; |
| 3431 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3432 | extref); |
| 3433 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3434 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3435 | /* |
| 3436 | * try to precache a NULL acl entry for files that don't have |
| 3437 | * any xattrs or acls |
| 3438 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3439 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3440 | btrfs_ino(inode), &first_xattr_slot); |
| 3441 | if (first_xattr_slot != -1) { |
| 3442 | path->slots[0] = first_xattr_slot; |
| 3443 | ret = btrfs_load_inode_props(inode, path); |
| 3444 | if (ret) |
| 3445 | btrfs_err(root->fs_info, |
| 3446 | "error loading props for ino %llu (root %llu): %d\n", |
| 3447 | btrfs_ino(inode), |
| 3448 | root->root_key.objectid, ret); |
| 3449 | } |
| 3450 | btrfs_free_path(path); |
| 3451 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3452 | if (!maybe_acls) |
| 3453 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3454 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3455 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3456 | case S_IFREG: |
| 3457 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3458 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3459 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3460 | inode->i_fop = &btrfs_file_operations; |
| 3461 | inode->i_op = &btrfs_file_inode_operations; |
| 3462 | break; |
| 3463 | case S_IFDIR: |
| 3464 | inode->i_fop = &btrfs_dir_file_operations; |
| 3465 | if (root == root->fs_info->tree_root) |
| 3466 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3467 | else |
| 3468 | inode->i_op = &btrfs_dir_inode_operations; |
| 3469 | break; |
| 3470 | case S_IFLNK: |
| 3471 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3472 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3473 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3474 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3475 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3476 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3477 | init_special_inode(inode, inode->i_mode, rdev); |
| 3478 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3479 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3480 | |
| 3481 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3482 | return; |
| 3483 | |
| 3484 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3485 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3486 | make_bad_inode(inode); |
| 3487 | } |
| 3488 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3489 | /* |
| 3490 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3491 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3492 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3493 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3494 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3495 | struct inode *inode) |
| 3496 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3497 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3498 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3499 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3500 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3501 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3502 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3503 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3504 | &token); |
| 3505 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3506 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3507 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3508 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3509 | inode->i_atime.tv_sec, &token); |
| 3510 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3511 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3512 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3513 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3514 | inode->i_mtime.tv_sec, &token); |
| 3515 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3516 | inode->i_mtime.tv_nsec, &token); |
| 3517 | |
| 3518 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3519 | inode->i_ctime.tv_sec, &token); |
| 3520 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3521 | inode->i_ctime.tv_nsec, &token); |
| 3522 | |
| 3523 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3524 | &token); |
| 3525 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3526 | &token); |
| 3527 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3528 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3529 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3530 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3531 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3532 | } |
| 3533 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3534 | /* |
| 3535 | * copy everything in the in-memory inode into the btree. |
| 3536 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3537 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3538 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3539 | { |
| 3540 | struct btrfs_inode_item *inode_item; |
| 3541 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3542 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3543 | int ret; |
| 3544 | |
| 3545 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3546 | if (!path) |
| 3547 | return -ENOMEM; |
| 3548 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3549 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3550 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3551 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3552 | if (ret) { |
| 3553 | if (ret > 0) |
| 3554 | ret = -ENOENT; |
| 3555 | goto failed; |
| 3556 | } |
| 3557 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3558 | leaf = path->nodes[0]; |
| 3559 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3560 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3561 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3562 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3563 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3564 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3565 | ret = 0; |
| 3566 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3567 | btrfs_free_path(path); |
| 3568 | return ret; |
| 3569 | } |
| 3570 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3571 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3572 | * copy everything in the in-memory inode into the btree. |
| 3573 | */ |
| 3574 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3575 | struct btrfs_root *root, struct inode *inode) |
| 3576 | { |
| 3577 | int ret; |
| 3578 | |
| 3579 | /* |
| 3580 | * If the inode is a free space inode, we can deadlock during commit |
| 3581 | * if we put it into the delayed code. |
| 3582 | * |
| 3583 | * The data relocation inode should also be directly updated |
| 3584 | * without delay |
| 3585 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3586 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3587 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3588 | btrfs_update_root_times(trans, root); |
| 3589 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3590 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3591 | if (!ret) |
| 3592 | btrfs_set_inode_last_trans(trans, inode); |
| 3593 | return ret; |
| 3594 | } |
| 3595 | |
| 3596 | return btrfs_update_inode_item(trans, root, inode); |
| 3597 | } |
| 3598 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3599 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3600 | struct btrfs_root *root, |
| 3601 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3602 | { |
| 3603 | int ret; |
| 3604 | |
| 3605 | ret = btrfs_update_inode(trans, root, inode); |
| 3606 | if (ret == -ENOSPC) |
| 3607 | return btrfs_update_inode_item(trans, root, inode); |
| 3608 | return ret; |
| 3609 | } |
| 3610 | |
| 3611 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3612 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3613 | * recovery code. It remove a link in a directory with a given name, and |
| 3614 | * also drops the back refs in the inode to the directory |
| 3615 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3616 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3617 | struct btrfs_root *root, |
| 3618 | struct inode *dir, struct inode *inode, |
| 3619 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3620 | { |
| 3621 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3622 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3623 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3624 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3625 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3626 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3627 | u64 ino = btrfs_ino(inode); |
| 3628 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3629 | |
| 3630 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3631 | if (!path) { |
| 3632 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3633 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3634 | } |
| 3635 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3636 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3637 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3638 | name, name_len, -1); |
| 3639 | if (IS_ERR(di)) { |
| 3640 | ret = PTR_ERR(di); |
| 3641 | goto err; |
| 3642 | } |
| 3643 | if (!di) { |
| 3644 | ret = -ENOENT; |
| 3645 | goto err; |
| 3646 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3647 | leaf = path->nodes[0]; |
| 3648 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3649 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3650 | if (ret) |
| 3651 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3652 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3653 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3654 | /* |
| 3655 | * If we don't have dir index, we have to get it by looking up |
| 3656 | * the inode ref, since we get the inode ref, remove it directly, |
| 3657 | * it is unnecessary to do delayed deletion. |
| 3658 | * |
| 3659 | * But if we have dir index, needn't search inode ref to get it. |
| 3660 | * Since the inode ref is close to the inode item, it is better |
| 3661 | * that we delay to delete it, and just do this deletion when |
| 3662 | * we update the inode item. |
| 3663 | */ |
| 3664 | if (BTRFS_I(inode)->dir_index) { |
| 3665 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3666 | if (!ret) { |
| 3667 | index = BTRFS_I(inode)->dir_index; |
| 3668 | goto skip_backref; |
| 3669 | } |
| 3670 | } |
| 3671 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3672 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3673 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3674 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3675 | btrfs_info(root->fs_info, |
| 3676 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3677 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3678 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3679 | goto err; |
| 3680 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3681 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3682 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3683 | if (ret) { |
| 3684 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3685 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3686 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3687 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3688 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3689 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3690 | if (ret != 0 && ret != -ENOENT) { |
| 3691 | btrfs_abort_transaction(trans, root, ret); |
| 3692 | goto err; |
| 3693 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3694 | |
| 3695 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3696 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3697 | if (ret == -ENOENT) |
| 3698 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3699 | else if (ret) |
| 3700 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3701 | err: |
| 3702 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3703 | if (ret) |
| 3704 | goto out; |
| 3705 | |
| 3706 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3707 | inode_inc_iversion(inode); |
| 3708 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3709 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3710 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3711 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3712 | return ret; |
| 3713 | } |
| 3714 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3715 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3716 | struct btrfs_root *root, |
| 3717 | struct inode *dir, struct inode *inode, |
| 3718 | const char *name, int name_len) |
| 3719 | { |
| 3720 | int ret; |
| 3721 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3722 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3723 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3724 | ret = btrfs_update_inode(trans, root, inode); |
| 3725 | } |
| 3726 | return ret; |
| 3727 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3728 | |
| 3729 | /* |
| 3730 | * helper to start transaction for unlink and rmdir. |
| 3731 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3732 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3733 | * if we cannot make our reservations the normal way try and see if there is |
| 3734 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3735 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3736 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3737 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3738 | { |
| 3739 | struct btrfs_trans_handle *trans; |
| 3740 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3741 | int ret; |
| 3742 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3743 | /* |
| 3744 | * 1 for the possible orphan item |
| 3745 | * 1 for the dir item |
| 3746 | * 1 for the dir index |
| 3747 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3748 | * 1 for the inode |
| 3749 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3750 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3751 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3752 | return trans; |
| 3753 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3754 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3755 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3756 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3757 | trans = btrfs_start_transaction(root, 0); |
| 3758 | if (IS_ERR(trans)) |
| 3759 | return trans; |
| 3760 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3761 | &root->fs_info->trans_block_rsv, |
| 3762 | num_bytes, 5); |
| 3763 | if (ret) { |
| 3764 | btrfs_end_transaction(trans, root); |
| 3765 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3766 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3767 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3768 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3769 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3770 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3771 | } |
| 3772 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3773 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3774 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3775 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3776 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3777 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3778 | int ret; |
| 3779 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3780 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3781 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3782 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3783 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3784 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3785 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3786 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3787 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3788 | if (ret) |
| 3789 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3790 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3791 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3792 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3793 | if (ret) |
| 3794 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3795 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3796 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3797 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3798 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3799 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3800 | return ret; |
| 3801 | } |
| 3802 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3803 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3804 | struct btrfs_root *root, |
| 3805 | struct inode *dir, u64 objectid, |
| 3806 | const char *name, int name_len) |
| 3807 | { |
| 3808 | struct btrfs_path *path; |
| 3809 | struct extent_buffer *leaf; |
| 3810 | struct btrfs_dir_item *di; |
| 3811 | struct btrfs_key key; |
| 3812 | u64 index; |
| 3813 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3814 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3815 | |
| 3816 | path = btrfs_alloc_path(); |
| 3817 | if (!path) |
| 3818 | return -ENOMEM; |
| 3819 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3820 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3821 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3822 | if (IS_ERR_OR_NULL(di)) { |
| 3823 | if (!di) |
| 3824 | ret = -ENOENT; |
| 3825 | else |
| 3826 | ret = PTR_ERR(di); |
| 3827 | goto out; |
| 3828 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3829 | |
| 3830 | leaf = path->nodes[0]; |
| 3831 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3832 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3833 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3834 | if (ret) { |
| 3835 | btrfs_abort_transaction(trans, root, ret); |
| 3836 | goto out; |
| 3837 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3838 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3839 | |
| 3840 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3841 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3842 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3843 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3844 | if (ret != -ENOENT) { |
| 3845 | btrfs_abort_transaction(trans, root, ret); |
| 3846 | goto out; |
| 3847 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3848 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3849 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3850 | if (IS_ERR_OR_NULL(di)) { |
| 3851 | if (!di) |
| 3852 | ret = -ENOENT; |
| 3853 | else |
| 3854 | ret = PTR_ERR(di); |
| 3855 | btrfs_abort_transaction(trans, root, ret); |
| 3856 | goto out; |
| 3857 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3858 | |
| 3859 | leaf = path->nodes[0]; |
| 3860 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3861 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3862 | index = key.offset; |
| 3863 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3864 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3865 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3866 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3867 | if (ret) { |
| 3868 | btrfs_abort_transaction(trans, root, ret); |
| 3869 | goto out; |
| 3870 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3871 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3872 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3873 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3874 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3875 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3876 | if (ret) |
| 3877 | btrfs_abort_transaction(trans, root, ret); |
| 3878 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3879 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3880 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3881 | } |
| 3882 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3883 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3884 | { |
| 3885 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3886 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3887 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3888 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3889 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3890 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3891 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3892 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3893 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3894 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3895 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3896 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3897 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3898 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3899 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3900 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3901 | BTRFS_I(inode)->location.objectid, |
| 3902 | dentry->d_name.name, |
| 3903 | dentry->d_name.len); |
| 3904 | goto out; |
| 3905 | } |
| 3906 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3907 | err = btrfs_orphan_add(trans, inode); |
| 3908 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3909 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3910 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3911 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3912 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3913 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3914 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3915 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3916 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3917 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3918 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3919 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3920 | return err; |
| 3921 | } |
| 3922 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3923 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3924 | * this can truncate away extent items, csum items and directory items. |
| 3925 | * 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] | 3926 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3927 | * |
| 3928 | * csum items that cross the new i_size are truncated to the new size |
| 3929 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3930 | * |
| 3931 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3932 | * 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] | 3933 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3934 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3935 | struct btrfs_root *root, |
| 3936 | struct inode *inode, |
| 3937 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3938 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3939 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3940 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3941 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3942 | struct btrfs_key key; |
| 3943 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3944 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3945 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3946 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3947 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 3948 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3949 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3950 | int found_extent; |
| 3951 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3952 | int pending_del_nr = 0; |
| 3953 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 3954 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3955 | int ret; |
| 3956 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3957 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3958 | |
| 3959 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3960 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 3961 | path = btrfs_alloc_path(); |
| 3962 | if (!path) |
| 3963 | return -ENOMEM; |
| 3964 | path->reada = -1; |
| 3965 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3966 | /* |
| 3967 | * We want to drop from the next block forward in case this new size is |
| 3968 | * not block aligned since we will be keeping the last block of the |
| 3969 | * extent just the way it is. |
| 3970 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3971 | if (root->ref_cows || root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3972 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 3973 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3974 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3975 | /* |
| 3976 | * This function is also used to drop the items in the log tree before |
| 3977 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 3978 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 3979 | * items. |
| 3980 | */ |
| 3981 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 3982 | btrfs_kill_delayed_inode_items(inode); |
| 3983 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3984 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3985 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3986 | key.type = (u8)-1; |
| 3987 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3988 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3989 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3990 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3991 | if (ret < 0) { |
| 3992 | err = ret; |
| 3993 | goto out; |
| 3994 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3995 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 3996 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3997 | /* there are no items in the tree for us to truncate, we're |
| 3998 | * done |
| 3999 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4000 | if (path->slots[0] == 0) |
| 4001 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4002 | path->slots[0]--; |
| 4003 | } |
| 4004 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4005 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4006 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4007 | leaf = path->nodes[0]; |
| 4008 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4009 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4010 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4011 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4012 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4013 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4014 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4015 | break; |
| 4016 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4017 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4018 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4019 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4020 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4021 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4022 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4023 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4024 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4025 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4026 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4027 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4029 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4030 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4031 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4032 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4033 | } else { |
| 4034 | if (item_end < new_size) |
| 4035 | break; |
| 4036 | if (found_key.offset >= new_size) |
| 4037 | del_item = 1; |
| 4038 | else |
| 4039 | del_item = 0; |
| 4040 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4041 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4042 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4043 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4044 | goto delete; |
| 4045 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4046 | if (del_item) |
| 4047 | last_size = found_key.offset; |
| 4048 | else |
| 4049 | last_size = new_size; |
| 4050 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4051 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4052 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4053 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4054 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4055 | u64 orig_num_bytes = |
| 4056 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4057 | extent_num_bytes = ALIGN(new_size - |
| 4058 | found_key.offset, |
| 4059 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4060 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4061 | extent_num_bytes); |
| 4062 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4063 | extent_num_bytes); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4064 | if (root->ref_cows && extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4065 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4066 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4067 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4068 | extent_num_bytes = |
| 4069 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4070 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4071 | extent_offset = found_key.offset - |
| 4072 | btrfs_file_extent_offset(leaf, fi); |
| 4073 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4074 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4075 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4076 | if (extent_start != 0) { |
| 4077 | found_extent = 1; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4078 | if (root->ref_cows) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4079 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4080 | } |
| 4081 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4082 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4083 | /* |
| 4084 | * we can't truncate inline items that have had |
| 4085 | * special encodings |
| 4086 | */ |
| 4087 | if (!del_item && |
| 4088 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4089 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4090 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4091 | u32 size = new_size - found_key.offset; |
| 4092 | |
| 4093 | if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4094 | inode_sub_bytes(inode, item_end + 1 - |
| 4095 | new_size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4096 | } |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4097 | |
| 4098 | /* |
| 4099 | * update the ram bytes to properly reflect |
| 4100 | * the new size of our item |
| 4101 | */ |
| 4102 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4103 | size = |
| 4104 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4105 | btrfs_truncate_item(root, path, size, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4106 | } else if (root->ref_cows) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4107 | inode_sub_bytes(inode, item_end + 1 - |
| 4108 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4109 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4110 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4111 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4112 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4113 | if (!pending_del_nr) { |
| 4114 | /* no pending yet, add ourselves */ |
| 4115 | pending_del_slot = path->slots[0]; |
| 4116 | pending_del_nr = 1; |
| 4117 | } else if (pending_del_nr && |
| 4118 | path->slots[0] + 1 == pending_del_slot) { |
| 4119 | /* hop on the pending chunk */ |
| 4120 | pending_del_nr++; |
| 4121 | pending_del_slot = path->slots[0]; |
| 4122 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4123 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4124 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4125 | } else { |
| 4126 | break; |
| 4127 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4128 | if (found_extent && (root->ref_cows || |
| 4129 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4130 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4132 | extent_num_bytes, 0, |
| 4133 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4134 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4135 | BUG_ON(ret); |
| 4136 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4137 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4138 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4139 | break; |
| 4140 | |
| 4141 | if (path->slots[0] == 0 || |
| 4142 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4143 | if (pending_del_nr) { |
| 4144 | ret = btrfs_del_items(trans, root, path, |
| 4145 | pending_del_slot, |
| 4146 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4147 | if (ret) { |
| 4148 | btrfs_abort_transaction(trans, |
| 4149 | root, ret); |
| 4150 | goto error; |
| 4151 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4152 | pending_del_nr = 0; |
| 4153 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4154 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4155 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4156 | } else { |
| 4157 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4158 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4159 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4160 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4161 | if (pending_del_nr) { |
| 4162 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4163 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4164 | if (ret) |
| 4165 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4166 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4167 | error: |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4168 | if (last_size != (u64)-1) |
| 4169 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4170 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4171 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4172 | } |
| 4173 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4174 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4175 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4176 | * @inode - inode that we're zeroing |
| 4177 | * @from - the offset to start zeroing |
| 4178 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4179 | * offset |
| 4180 | * @front - zero up to the offset instead of from the offset on |
| 4181 | * |
| 4182 | * This will find the page for the "from" offset and cow the page and zero the |
| 4183 | * part we want to zero. This is used with truncate and hole punching. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4184 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4185 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4186 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4187 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4188 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4189 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4190 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4191 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4192 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4193 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4194 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4195 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4196 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4197 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4198 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4199 | int ret = 0; |
| 4200 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4201 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4202 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4203 | if ((offset & (blocksize - 1)) == 0 && |
| 4204 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4205 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4206 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4207 | if (ret) |
| 4208 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4209 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4210 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4211 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4212 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4213 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4214 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4215 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4216 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4217 | |
| 4218 | page_start = page_offset(page); |
| 4219 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4220 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4221 | if (!PageUptodate(page)) { |
| 4222 | ret = btrfs_readpage(NULL, page); |
| 4223 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4224 | if (page->mapping != mapping) { |
| 4225 | unlock_page(page); |
| 4226 | page_cache_release(page); |
| 4227 | goto again; |
| 4228 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4229 | if (!PageUptodate(page)) { |
| 4230 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4231 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4232 | } |
| 4233 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4234 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4235 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4236 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4237 | set_page_extent_mapped(page); |
| 4238 | |
| 4239 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4240 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4241 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4242 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4243 | unlock_page(page); |
| 4244 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4245 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4246 | btrfs_put_ordered_extent(ordered); |
| 4247 | goto again; |
| 4248 | } |
| 4249 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4250 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4251 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4252 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4253 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4254 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4255 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4256 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4257 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4258 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4259 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4260 | goto out_unlock; |
| 4261 | } |
| 4262 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4263 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4264 | if (!len) |
| 4265 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4266 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4267 | if (front) |
| 4268 | memset(kaddr, 0, offset); |
| 4269 | else |
| 4270 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4271 | flush_dcache_page(page); |
| 4272 | kunmap(page); |
| 4273 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4274 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4275 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4276 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4277 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4278 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4279 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4280 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4281 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4282 | unlock_page(page); |
| 4283 | page_cache_release(page); |
| 4284 | out: |
| 4285 | return ret; |
| 4286 | } |
| 4287 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4288 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4289 | u64 offset, u64 len) |
| 4290 | { |
| 4291 | struct btrfs_trans_handle *trans; |
| 4292 | int ret; |
| 4293 | |
| 4294 | /* |
| 4295 | * Still need to make sure the inode looks like it's been updated so |
| 4296 | * that any holes get logged if we fsync. |
| 4297 | */ |
| 4298 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4299 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4300 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4301 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4302 | return 0; |
| 4303 | } |
| 4304 | |
| 4305 | /* |
| 4306 | * 1 - for the one we're dropping |
| 4307 | * 1 - for the one we're adding |
| 4308 | * 1 - for updating the inode. |
| 4309 | */ |
| 4310 | trans = btrfs_start_transaction(root, 3); |
| 4311 | if (IS_ERR(trans)) |
| 4312 | return PTR_ERR(trans); |
| 4313 | |
| 4314 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4315 | if (ret) { |
| 4316 | btrfs_abort_transaction(trans, root, ret); |
| 4317 | btrfs_end_transaction(trans, root); |
| 4318 | return ret; |
| 4319 | } |
| 4320 | |
| 4321 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4322 | 0, 0, len, 0, len, 0, 0, 0); |
| 4323 | if (ret) |
| 4324 | btrfs_abort_transaction(trans, root, ret); |
| 4325 | else |
| 4326 | btrfs_update_inode(trans, root, inode); |
| 4327 | btrfs_end_transaction(trans, root); |
| 4328 | return ret; |
| 4329 | } |
| 4330 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4331 | /* |
| 4332 | * This function puts in dummy file extents for the area we're creating a hole |
| 4333 | * for. So if we are truncating this file to a larger size we need to insert |
| 4334 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4335 | * the range between oldsize and size |
| 4336 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4337 | 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] | 4338 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4339 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4340 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4341 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4342 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4343 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4344 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4345 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4346 | u64 last_byte; |
| 4347 | u64 cur_offset; |
| 4348 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4349 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4350 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4351 | /* |
| 4352 | * If our size started in the middle of a page we need to zero out the |
| 4353 | * rest of the page before we expand the i_size, otherwise we could |
| 4354 | * expose stale data. |
| 4355 | */ |
| 4356 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4357 | if (err) |
| 4358 | return err; |
| 4359 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4360 | if (size <= hole_start) |
| 4361 | return 0; |
| 4362 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4363 | while (1) { |
| 4364 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4365 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4366 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4367 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4368 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4369 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4370 | if (!ordered) |
| 4371 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4372 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4373 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4374 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4375 | btrfs_put_ordered_extent(ordered); |
| 4376 | } |
| 4377 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4378 | cur_offset = hole_start; |
| 4379 | while (1) { |
| 4380 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4381 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4382 | if (IS_ERR(em)) { |
| 4383 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4384 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4385 | break; |
| 4386 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4387 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4388 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4389 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4390 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4391 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4392 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4393 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4394 | hole_size); |
| 4395 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4396 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4397 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4398 | cur_offset + hole_size - 1, 0); |
| 4399 | hole_em = alloc_extent_map(); |
| 4400 | if (!hole_em) { |
| 4401 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4402 | &BTRFS_I(inode)->runtime_flags); |
| 4403 | goto next; |
| 4404 | } |
| 4405 | hole_em->start = cur_offset; |
| 4406 | hole_em->len = hole_size; |
| 4407 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4408 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4409 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4410 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4411 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4412 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4413 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4414 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4415 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4416 | |
| 4417 | while (1) { |
| 4418 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4419 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4420 | write_unlock(&em_tree->lock); |
| 4421 | if (err != -EEXIST) |
| 4422 | break; |
| 4423 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4424 | cur_offset + |
| 4425 | hole_size - 1, 0); |
| 4426 | } |
| 4427 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4428 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4429 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4430 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4431 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4432 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4433 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4434 | break; |
| 4435 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4436 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4437 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4438 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4439 | return err; |
| 4440 | } |
| 4441 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4442 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4443 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4444 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4445 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4446 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4447 | loff_t newsize = attr->ia_size; |
| 4448 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4449 | int ret; |
| 4450 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4451 | /* |
| 4452 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4453 | * special case where we need to update the times despite not having |
| 4454 | * these flags set. For all other operations the VFS set these flags |
| 4455 | * explicitly if it wants a timestamp update. |
| 4456 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4457 | if (newsize != oldsize) { |
| 4458 | inode_inc_iversion(inode); |
| 4459 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4460 | inode->i_ctime = inode->i_mtime = |
| 4461 | current_fs_time(inode->i_sb); |
| 4462 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4463 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4464 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4465 | truncate_pagecache(inode, newsize); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4466 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4467 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4468 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4469 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4470 | trans = btrfs_start_transaction(root, 1); |
| 4471 | if (IS_ERR(trans)) |
| 4472 | return PTR_ERR(trans); |
| 4473 | |
| 4474 | i_size_write(inode, newsize); |
| 4475 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4476 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4477 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4478 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4479 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4480 | /* |
| 4481 | * We're truncating a file that used to have good data down to |
| 4482 | * zero. Make sure it gets into the ordered flush list so that |
| 4483 | * any new writes get down to disk quickly. |
| 4484 | */ |
| 4485 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4486 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4487 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4488 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4489 | /* |
| 4490 | * 1 for the orphan item we're going to add |
| 4491 | * 1 for the orphan item deletion. |
| 4492 | */ |
| 4493 | trans = btrfs_start_transaction(root, 2); |
| 4494 | if (IS_ERR(trans)) |
| 4495 | return PTR_ERR(trans); |
| 4496 | |
| 4497 | /* |
| 4498 | * We need to do this in case we fail at _any_ point during the |
| 4499 | * actual truncate. Once we do the truncate_setsize we could |
| 4500 | * invalidate pages which forces any outstanding ordered io to |
| 4501 | * be instantly completed which will give us extents that need |
| 4502 | * to be truncated. If we fail to get an orphan inode down we |
| 4503 | * could have left over extents that were never meant to live, |
| 4504 | * so we need to garuntee from this point on that everything |
| 4505 | * will be consistent. |
| 4506 | */ |
| 4507 | ret = btrfs_orphan_add(trans, inode); |
| 4508 | btrfs_end_transaction(trans, root); |
| 4509 | if (ret) |
| 4510 | return ret; |
| 4511 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4512 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4513 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4514 | |
| 4515 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4516 | btrfs_inode_block_unlocked_dio(inode); |
| 4517 | inode_dio_wait(inode); |
| 4518 | btrfs_inode_resume_unlocked_dio(inode); |
| 4519 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4520 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4521 | if (ret && inode->i_nlink) { |
| 4522 | int err; |
| 4523 | |
| 4524 | /* |
| 4525 | * failed to truncate, disk_i_size is only adjusted down |
| 4526 | * as we remove extents, so it should represent the true |
| 4527 | * size of the inode, so reset the in memory size and |
| 4528 | * delete our orphan entry. |
| 4529 | */ |
| 4530 | trans = btrfs_join_transaction(root); |
| 4531 | if (IS_ERR(trans)) { |
| 4532 | btrfs_orphan_del(NULL, inode); |
| 4533 | return ret; |
| 4534 | } |
| 4535 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4536 | err = btrfs_orphan_del(trans, inode); |
| 4537 | if (err) |
| 4538 | btrfs_abort_transaction(trans, root, err); |
| 4539 | btrfs_end_transaction(trans, root); |
| 4540 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4543 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4546 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4547 | { |
| 4548 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4549 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4550 | int err; |
| 4551 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4552 | if (btrfs_root_readonly(root)) |
| 4553 | return -EROFS; |
| 4554 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4555 | err = inode_change_ok(inode, attr); |
| 4556 | if (err) |
| 4557 | return err; |
| 4558 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4559 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4560 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4561 | if (err) |
| 4562 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4563 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4564 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4565 | if (attr->ia_valid) { |
| 4566 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4567 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4568 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4569 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4570 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4571 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4572 | } |
| 4573 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4574 | return err; |
| 4575 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4576 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4577 | /* |
| 4578 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4579 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4580 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4581 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4582 | * extent_state structures over and over, wasting lots of time. |
| 4583 | * |
| 4584 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4585 | * those expensive operations on a per page basis and do only the ordered io |
| 4586 | * finishing, while we release here the extent_map and extent_state structures, |
| 4587 | * without the excessive merging and splitting. |
| 4588 | */ |
| 4589 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4590 | { |
| 4591 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4592 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4593 | struct rb_node *node; |
| 4594 | |
| 4595 | ASSERT(inode->i_state & I_FREEING); |
| 4596 | truncate_inode_pages(&inode->i_data, 0); |
| 4597 | |
| 4598 | write_lock(&map_tree->lock); |
| 4599 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4600 | struct extent_map *em; |
| 4601 | |
| 4602 | node = rb_first(&map_tree->map); |
| 4603 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4604 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4605 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4606 | remove_extent_mapping(map_tree, em); |
| 4607 | free_extent_map(em); |
| 4608 | } |
| 4609 | write_unlock(&map_tree->lock); |
| 4610 | |
| 4611 | spin_lock(&io_tree->lock); |
| 4612 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4613 | struct extent_state *state; |
| 4614 | struct extent_state *cached_state = NULL; |
| 4615 | |
| 4616 | node = rb_first(&io_tree->state); |
| 4617 | state = rb_entry(node, struct extent_state, rb_node); |
| 4618 | atomic_inc(&state->refs); |
| 4619 | spin_unlock(&io_tree->lock); |
| 4620 | |
| 4621 | lock_extent_bits(io_tree, state->start, state->end, |
| 4622 | 0, &cached_state); |
| 4623 | clear_extent_bit(io_tree, state->start, state->end, |
| 4624 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4625 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4626 | EXTENT_DEFRAG, 1, 1, |
| 4627 | &cached_state, GFP_NOFS); |
| 4628 | free_extent_state(state); |
| 4629 | |
| 4630 | spin_lock(&io_tree->lock); |
| 4631 | } |
| 4632 | spin_unlock(&io_tree->lock); |
| 4633 | } |
| 4634 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4635 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4636 | { |
| 4637 | struct btrfs_trans_handle *trans; |
| 4638 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4639 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4640 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4641 | int ret; |
| 4642 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4643 | trace_btrfs_inode_evict(inode); |
| 4644 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4645 | evict_inode_truncate_pages(inode); |
| 4646 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4647 | if (inode->i_nlink && |
| 4648 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4649 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4650 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4651 | goto no_delete; |
| 4652 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4653 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4654 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4655 | goto no_delete; |
| 4656 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4657 | /* 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] | 4658 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4659 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4660 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4661 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4662 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4663 | goto no_delete; |
| 4664 | } |
| 4665 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4666 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4667 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4668 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4669 | goto no_delete; |
| 4670 | } |
| 4671 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4672 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4673 | if (ret) { |
| 4674 | btrfs_orphan_del(NULL, inode); |
| 4675 | goto no_delete; |
| 4676 | } |
| 4677 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4678 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4679 | if (!rsv) { |
| 4680 | btrfs_orphan_del(NULL, inode); |
| 4681 | goto no_delete; |
| 4682 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4683 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4684 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4685 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4686 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4687 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4688 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4689 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4690 | * This is a bit simpler than btrfs_truncate since we've already |
| 4691 | * reserved our space for our orphan item in the unlink, so we just |
| 4692 | * need to reserve some slack space in case we add bytes and update |
| 4693 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4694 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4695 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4696 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4697 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4698 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4699 | /* |
| 4700 | * Try and steal from the global reserve since we will |
| 4701 | * likely not use this space anyway, we want to try as |
| 4702 | * hard as possible to get this to work. |
| 4703 | */ |
| 4704 | if (ret) |
| 4705 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4706 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4707 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4708 | btrfs_warn(root->fs_info, |
| 4709 | "Could not get space for a delete, will truncate on mount %d", |
| 4710 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4711 | btrfs_orphan_del(NULL, inode); |
| 4712 | btrfs_free_block_rsv(root, rsv); |
| 4713 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4714 | } |
| 4715 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4716 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4717 | if (IS_ERR(trans)) { |
| 4718 | btrfs_orphan_del(NULL, inode); |
| 4719 | btrfs_free_block_rsv(root, rsv); |
| 4720 | goto no_delete; |
| 4721 | } |
| 4722 | |
| 4723 | trans->block_rsv = rsv; |
| 4724 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4725 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4726 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4727 | break; |
| 4728 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4729 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4730 | btrfs_end_transaction(trans, root); |
| 4731 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4732 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4733 | } |
| 4734 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4735 | btrfs_free_block_rsv(root, rsv); |
| 4736 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4737 | /* |
| 4738 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4739 | * in the tree. They will be cleaned up on the next mount. |
| 4740 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4741 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4742 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4743 | btrfs_orphan_del(trans, inode); |
| 4744 | } else { |
| 4745 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4746 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4747 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4748 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4749 | if (!(root == root->fs_info->tree_root || |
| 4750 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4751 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4752 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4753 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4754 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4755 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4756 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4757 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4758 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | /* |
| 4762 | * this returns the key found in the dir entry in the location pointer. |
| 4763 | * If no dir entries were found, location->objectid is 0. |
| 4764 | */ |
| 4765 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4766 | struct btrfs_key *location) |
| 4767 | { |
| 4768 | const char *name = dentry->d_name.name; |
| 4769 | int namelen = dentry->d_name.len; |
| 4770 | struct btrfs_dir_item *di; |
| 4771 | struct btrfs_path *path; |
| 4772 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4773 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4774 | |
| 4775 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4776 | if (!path) |
| 4777 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4778 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4779 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4780 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4781 | if (IS_ERR(di)) |
| 4782 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4783 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4784 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4785 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4786 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4787 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4788 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4789 | btrfs_free_path(path); |
| 4790 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4791 | out_err: |
| 4792 | location->objectid = 0; |
| 4793 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | /* |
| 4797 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4798 | * needs to be changed to reflect the root directory of the tree root. This |
| 4799 | * is kind of like crossing a mount point. |
| 4800 | */ |
| 4801 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4802 | struct inode *dir, |
| 4803 | struct dentry *dentry, |
| 4804 | struct btrfs_key *location, |
| 4805 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4806 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4807 | struct btrfs_path *path; |
| 4808 | struct btrfs_root *new_root; |
| 4809 | struct btrfs_root_ref *ref; |
| 4810 | struct extent_buffer *leaf; |
| 4811 | int ret; |
| 4812 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4813 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4814 | path = btrfs_alloc_path(); |
| 4815 | if (!path) { |
| 4816 | err = -ENOMEM; |
| 4817 | goto out; |
| 4818 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4819 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4820 | err = -ENOENT; |
Kelley Nielsen | 75ac2dd | 2013-11-04 19:35:58 -0800 | [diff] [blame] | 4821 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
| 4822 | BTRFS_I(dir)->root->root_key.objectid, |
| 4823 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4824 | if (ret) { |
| 4825 | if (ret < 0) |
| 4826 | err = ret; |
| 4827 | goto out; |
| 4828 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4829 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4830 | leaf = path->nodes[0]; |
| 4831 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4832 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4833 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4834 | goto out; |
| 4835 | |
| 4836 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4837 | (unsigned long)(ref + 1), |
| 4838 | dentry->d_name.len); |
| 4839 | if (ret) |
| 4840 | goto out; |
| 4841 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4842 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4843 | |
| 4844 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4845 | if (IS_ERR(new_root)) { |
| 4846 | err = PTR_ERR(new_root); |
| 4847 | goto out; |
| 4848 | } |
| 4849 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4850 | *sub_root = new_root; |
| 4851 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4852 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4853 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4854 | err = 0; |
| 4855 | out: |
| 4856 | btrfs_free_path(path); |
| 4857 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4858 | } |
| 4859 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4860 | static void inode_tree_add(struct inode *inode) |
| 4861 | { |
| 4862 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4863 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4864 | struct rb_node **p; |
| 4865 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4866 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4867 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4868 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4869 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4870 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4871 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4872 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4873 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4874 | while (*p) { |
| 4875 | parent = *p; |
| 4876 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4877 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4878 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4879 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4880 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4881 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4882 | else { |
| 4883 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4884 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4885 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4886 | RB_CLEAR_NODE(parent); |
| 4887 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4888 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4889 | } |
| 4890 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4891 | rb_link_node(new, parent, p); |
| 4892 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4893 | spin_unlock(&root->inode_lock); |
| 4894 | } |
| 4895 | |
| 4896 | static void inode_tree_del(struct inode *inode) |
| 4897 | { |
| 4898 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4899 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4900 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4901 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4902 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4903 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4904 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4905 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4906 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4907 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4908 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4909 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4910 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4911 | spin_lock(&root->inode_lock); |
| 4912 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4913 | spin_unlock(&root->inode_lock); |
| 4914 | if (empty) |
| 4915 | btrfs_add_dead_root(root); |
| 4916 | } |
| 4917 | } |
| 4918 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4919 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4920 | { |
| 4921 | struct rb_node *node; |
| 4922 | struct rb_node *prev; |
| 4923 | struct btrfs_inode *entry; |
| 4924 | struct inode *inode; |
| 4925 | u64 objectid = 0; |
| 4926 | |
| 4927 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
| 4928 | |
| 4929 | spin_lock(&root->inode_lock); |
| 4930 | again: |
| 4931 | node = root->inode_tree.rb_node; |
| 4932 | prev = NULL; |
| 4933 | while (node) { |
| 4934 | prev = node; |
| 4935 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 4936 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4937 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4938 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4939 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4940 | node = node->rb_right; |
| 4941 | else |
| 4942 | break; |
| 4943 | } |
| 4944 | if (!node) { |
| 4945 | while (prev) { |
| 4946 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4947 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4948 | node = prev; |
| 4949 | break; |
| 4950 | } |
| 4951 | prev = rb_next(prev); |
| 4952 | } |
| 4953 | } |
| 4954 | while (node) { |
| 4955 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4956 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4957 | inode = igrab(&entry->vfs_inode); |
| 4958 | if (inode) { |
| 4959 | spin_unlock(&root->inode_lock); |
| 4960 | if (atomic_read(&inode->i_count) > 1) |
| 4961 | d_prune_aliases(inode); |
| 4962 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 4963 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4964 | * the inode cache when its usage count |
| 4965 | * hits zero. |
| 4966 | */ |
| 4967 | iput(inode); |
| 4968 | cond_resched(); |
| 4969 | spin_lock(&root->inode_lock); |
| 4970 | goto again; |
| 4971 | } |
| 4972 | |
| 4973 | if (cond_resched_lock(&root->inode_lock)) |
| 4974 | goto again; |
| 4975 | |
| 4976 | node = rb_next(node); |
| 4977 | } |
| 4978 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4979 | } |
| 4980 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4981 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 4982 | { |
| 4983 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 4984 | inode->i_ino = args->location->objectid; |
| 4985 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 4986 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4987 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 4992 | { |
| 4993 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 4994 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4995 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4996 | } |
| 4997 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4998 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 4999 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5000 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5001 | { |
| 5002 | struct inode *inode; |
| 5003 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5004 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5005 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5006 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5007 | args.root = root; |
| 5008 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5009 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5010 | btrfs_init_locked_inode, |
| 5011 | (void *)&args); |
| 5012 | return inode; |
| 5013 | } |
| 5014 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5015 | /* Get an inode object given its location and corresponding root. |
| 5016 | * Returns in *is_new if the inode was read from disk |
| 5017 | */ |
| 5018 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5019 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5020 | { |
| 5021 | struct inode *inode; |
| 5022 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5023 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5024 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5025 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5026 | |
| 5027 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5028 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5029 | if (!is_bad_inode(inode)) { |
| 5030 | inode_tree_add(inode); |
| 5031 | unlock_new_inode(inode); |
| 5032 | if (new) |
| 5033 | *new = 1; |
| 5034 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5035 | unlock_new_inode(inode); |
| 5036 | iput(inode); |
| 5037 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5041 | return inode; |
| 5042 | } |
| 5043 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5044 | static struct inode *new_simple_dir(struct super_block *s, |
| 5045 | struct btrfs_key *key, |
| 5046 | struct btrfs_root *root) |
| 5047 | { |
| 5048 | struct inode *inode = new_inode(s); |
| 5049 | |
| 5050 | if (!inode) |
| 5051 | return ERR_PTR(-ENOMEM); |
| 5052 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5053 | BTRFS_I(inode)->root = root; |
| 5054 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5055 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5056 | |
| 5057 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5058 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5059 | inode->i_fop = &simple_dir_operations; |
| 5060 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5061 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5062 | |
| 5063 | return inode; |
| 5064 | } |
| 5065 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5066 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5067 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5068 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5069 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5070 | struct btrfs_root *sub_root = root; |
| 5071 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5072 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5073 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5074 | |
| 5075 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5076 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5077 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5078 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5079 | if (ret < 0) |
| 5080 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5081 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5082 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5083 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5084 | |
| 5085 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5086 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5087 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5088 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5089 | |
| 5090 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5091 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5092 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5093 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5094 | &location, &sub_root); |
| 5095 | if (ret < 0) { |
| 5096 | if (ret != -ENOENT) |
| 5097 | inode = ERR_PTR(ret); |
| 5098 | else |
| 5099 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5100 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5101 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5102 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5103 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5104 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5105 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5106 | down_read(&root->fs_info->cleanup_work_sem); |
| 5107 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5108 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5109 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5110 | if (ret) { |
| 5111 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5112 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5113 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5116 | return inode; |
| 5117 | } |
| 5118 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5119 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5120 | { |
| 5121 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5122 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5123 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5124 | if (!inode && !IS_ROOT(dentry)) |
| 5125 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5126 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5127 | if (inode) { |
| 5128 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5129 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5130 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5131 | |
| 5132 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5133 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5134 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5135 | return 0; |
| 5136 | } |
| 5137 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5138 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5139 | { |
| 5140 | if (dentry->d_fsdata) |
| 5141 | kfree(dentry->d_fsdata); |
| 5142 | } |
| 5143 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5144 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5145 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5146 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5147 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5148 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5149 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5150 | if (IS_ERR(inode)) { |
| 5151 | if (PTR_ERR(inode) == -ENOENT) |
| 5152 | inode = NULL; |
| 5153 | else |
| 5154 | return ERR_CAST(inode); |
| 5155 | } |
| 5156 | |
Josef Bacik | 3a0dfa6 | 2014-02-14 13:43:48 -0500 | [diff] [blame] | 5157 | return d_materialise_unique(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5158 | } |
| 5159 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5160 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5161 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5162 | }; |
| 5163 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5164 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5165 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5166 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5167 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5168 | struct btrfs_item *item; |
| 5169 | struct btrfs_dir_item *di; |
| 5170 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5171 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5172 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5173 | struct list_head ins_list; |
| 5174 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5175 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5176 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5177 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5178 | unsigned char d_type; |
| 5179 | int over = 0; |
| 5180 | u32 di_cur; |
| 5181 | u32 di_total; |
| 5182 | u32 di_len; |
| 5183 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5184 | char tmp_name[32]; |
| 5185 | char *name_ptr; |
| 5186 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5187 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5188 | |
| 5189 | /* FIXME, use a real flag for deciding about the key type */ |
| 5190 | if (root->fs_info->tree_root == root) |
| 5191 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5192 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5193 | if (!dir_emit_dots(file, ctx)) |
| 5194 | return 0; |
| 5195 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5196 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5197 | if (!path) |
| 5198 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5199 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5200 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5201 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5202 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5203 | INIT_LIST_HEAD(&ins_list); |
| 5204 | INIT_LIST_HEAD(&del_list); |
| 5205 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5206 | } |
| 5207 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5208 | btrfs_set_key_type(&key, key_type); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5209 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5210 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5211 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5212 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5213 | if (ret < 0) |
| 5214 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5215 | |
| 5216 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5217 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5218 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5219 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5220 | ret = btrfs_next_leaf(root, path); |
| 5221 | if (ret < 0) |
| 5222 | goto err; |
| 5223 | else if (ret > 0) |
| 5224 | break; |
| 5225 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5226 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5227 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5228 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5229 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5230 | |
| 5231 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5232 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5233 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5234 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5235 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5236 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5237 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5238 | btrfs_should_delete_dir_index(&del_list, |
| 5239 | found_key.offset)) |
| 5240 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5241 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5242 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5243 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5244 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5245 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5246 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5247 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5248 | |
| 5249 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5250 | struct btrfs_key location; |
| 5251 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5252 | if (verify_dir_item(root, leaf, di)) |
| 5253 | break; |
| 5254 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5255 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5256 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5257 | name_ptr = tmp_name; |
| 5258 | } else { |
| 5259 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5260 | if (!name_ptr) { |
| 5261 | ret = -ENOMEM; |
| 5262 | goto err; |
| 5263 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5264 | } |
| 5265 | read_extent_buffer(leaf, name_ptr, |
| 5266 | (unsigned long)(di + 1), name_len); |
| 5267 | |
| 5268 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5269 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5270 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5271 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5272 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5273 | * skip it. |
| 5274 | * |
| 5275 | * In contrast to old kernels, we insert the snapshot's |
| 5276 | * dir item and dir index after it has been created, so |
| 5277 | * we won't find a reference to our own snapshot. We |
| 5278 | * still keep the following code for backward |
| 5279 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5280 | */ |
| 5281 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5282 | location.objectid == root->root_key.objectid) { |
| 5283 | over = 0; |
| 5284 | goto skip; |
| 5285 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5286 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5287 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5288 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5289 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5290 | if (name_ptr != tmp_name) |
| 5291 | kfree(name_ptr); |
| 5292 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5293 | if (over) |
| 5294 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5295 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5296 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5297 | di_cur += di_len; |
| 5298 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5299 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5300 | next: |
| 5301 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5302 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5303 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5304 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5305 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5306 | ctx->pos++; |
| 5307 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5308 | if (ret) |
| 5309 | goto nopos; |
| 5310 | } |
| 5311 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5312 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5313 | ctx->pos++; |
| 5314 | |
| 5315 | /* |
| 5316 | * Stop new entries from being returned after we return the last |
| 5317 | * entry. |
| 5318 | * |
| 5319 | * New directory entries are assigned a strictly increasing |
| 5320 | * offset. This means that new entries created during readdir |
| 5321 | * are *guaranteed* to be seen in the future by that readdir. |
| 5322 | * This has broken buggy programs which operate on names as |
| 5323 | * they're returned by readdir. Until we re-use freed offsets |
| 5324 | * we have this hack to stop new entries from being returned |
| 5325 | * under the assumption that they'll never reach this huge |
| 5326 | * offset. |
| 5327 | * |
| 5328 | * This is being careful not to overflow 32bit loff_t unless the |
| 5329 | * last entry requires it because doing so has broken 32bit apps |
| 5330 | * in the past. |
| 5331 | */ |
| 5332 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5333 | if (ctx->pos >= INT_MAX) |
| 5334 | ctx->pos = LLONG_MAX; |
| 5335 | else |
| 5336 | ctx->pos = INT_MAX; |
| 5337 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5338 | nopos: |
| 5339 | ret = 0; |
| 5340 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5341 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5342 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5343 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5344 | return ret; |
| 5345 | } |
| 5346 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5347 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5348 | { |
| 5349 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5350 | struct btrfs_trans_handle *trans; |
| 5351 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5352 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5353 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5354 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5355 | return 0; |
| 5356 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5357 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 5358 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5359 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5360 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5361 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5362 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5363 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5364 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5365 | if (IS_ERR(trans)) |
| 5366 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5367 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5368 | } |
| 5369 | return ret; |
| 5370 | } |
| 5371 | |
| 5372 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5373 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5374 | * inode changes. But, it is most likely to find the inode in cache. |
| 5375 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5376 | * to keep or drop this code. |
| 5377 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5378 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5379 | { |
| 5380 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5381 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5382 | int ret; |
| 5383 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5384 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5385 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5386 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5387 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5388 | if (IS_ERR(trans)) |
| 5389 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5390 | |
| 5391 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5392 | if (ret && ret == -ENOSPC) { |
| 5393 | /* whoops, lets try again with the full transaction */ |
| 5394 | btrfs_end_transaction(trans, root); |
| 5395 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5396 | if (IS_ERR(trans)) |
| 5397 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5398 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5399 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5400 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5401 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5402 | if (BTRFS_I(inode)->delayed_node) |
| 5403 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5404 | |
| 5405 | return ret; |
| 5406 | } |
| 5407 | |
| 5408 | /* |
| 5409 | * This is a copy of file_update_time. We need this so we can return error on |
| 5410 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5411 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5412 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5413 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5414 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5415 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5416 | |
| 5417 | if (btrfs_root_readonly(root)) |
| 5418 | return -EROFS; |
| 5419 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5420 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5421 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5422 | if (flags & S_CTIME) |
| 5423 | inode->i_ctime = *now; |
| 5424 | if (flags & S_MTIME) |
| 5425 | inode->i_mtime = *now; |
| 5426 | if (flags & S_ATIME) |
| 5427 | inode->i_atime = *now; |
| 5428 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5429 | } |
| 5430 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5431 | /* |
| 5432 | * find the highest existing sequence number in a directory |
| 5433 | * and then set the in-memory index_cnt variable to reflect |
| 5434 | * free sequence numbers |
| 5435 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5436 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5437 | { |
| 5438 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5439 | struct btrfs_key key, found_key; |
| 5440 | struct btrfs_path *path; |
| 5441 | struct extent_buffer *leaf; |
| 5442 | int ret; |
| 5443 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5444 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5445 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5446 | key.offset = (u64)-1; |
| 5447 | |
| 5448 | path = btrfs_alloc_path(); |
| 5449 | if (!path) |
| 5450 | return -ENOMEM; |
| 5451 | |
| 5452 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5453 | if (ret < 0) |
| 5454 | goto out; |
| 5455 | /* FIXME: we should be able to handle this */ |
| 5456 | if (ret == 0) |
| 5457 | goto out; |
| 5458 | ret = 0; |
| 5459 | |
| 5460 | /* |
| 5461 | * MAGIC NUMBER EXPLANATION: |
| 5462 | * since we search a directory based on f_pos we have to start at 2 |
| 5463 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5464 | * else has to start at 2 |
| 5465 | */ |
| 5466 | if (path->slots[0] == 0) { |
| 5467 | BTRFS_I(inode)->index_cnt = 2; |
| 5468 | goto out; |
| 5469 | } |
| 5470 | |
| 5471 | path->slots[0]--; |
| 5472 | |
| 5473 | leaf = path->nodes[0]; |
| 5474 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5475 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5476 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5477 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5478 | BTRFS_I(inode)->index_cnt = 2; |
| 5479 | goto out; |
| 5480 | } |
| 5481 | |
| 5482 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5483 | out: |
| 5484 | btrfs_free_path(path); |
| 5485 | return ret; |
| 5486 | } |
| 5487 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5488 | /* |
| 5489 | * helper to find a free sequence number in a given directory. This current |
| 5490 | * code is very simple, later versions will do smarter things in the btree |
| 5491 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5492 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5493 | { |
| 5494 | int ret = 0; |
| 5495 | |
| 5496 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5497 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5498 | if (ret) { |
| 5499 | ret = btrfs_set_inode_index_count(dir); |
| 5500 | if (ret) |
| 5501 | return ret; |
| 5502 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5503 | } |
| 5504 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5505 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5506 | BTRFS_I(dir)->index_cnt++; |
| 5507 | |
| 5508 | return ret; |
| 5509 | } |
| 5510 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5511 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5512 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5513 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5514 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5515 | u64 ref_objectid, u64 objectid, |
| 5516 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5517 | { |
| 5518 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5519 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5520 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5521 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5522 | struct btrfs_inode_ref *ref; |
| 5523 | struct btrfs_key key[2]; |
| 5524 | u32 sizes[2]; |
| 5525 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5526 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5527 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5528 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5529 | if (!path) |
| 5530 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5531 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5532 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5533 | if (!inode) { |
| 5534 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5535 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5536 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5537 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5538 | /* |
| 5539 | * we have to initialize this early, so we can reclaim the inode |
| 5540 | * number if we fail afterwards in this function. |
| 5541 | */ |
| 5542 | inode->i_ino = objectid; |
| 5543 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5544 | if (dir) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5545 | trace_btrfs_inode_request(dir); |
| 5546 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5547 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5548 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5549 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5550 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5551 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5552 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5553 | } |
| 5554 | /* |
| 5555 | * index_cnt is ignored for everything but a dir, |
| 5556 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5557 | * number |
| 5558 | */ |
| 5559 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5560 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5561 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5562 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5563 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5564 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5565 | /* |
| 5566 | * We could have gotten an inode number from somebody who was fsynced |
| 5567 | * and then removed in this same transaction, so let's just set full |
| 5568 | * sync since it will be a full sync anyway and this will blow away the |
| 5569 | * old info in the log. |
| 5570 | */ |
| 5571 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5572 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5573 | key[0].objectid = objectid; |
| 5574 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5575 | key[0].offset = 0; |
| 5576 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5577 | /* |
| 5578 | * Start new inodes with an inode_ref. This is slightly more |
| 5579 | * efficient for small numbers of hard links since they will |
| 5580 | * be packed into one item. Extended refs will kick in if we |
| 5581 | * add more hard links than can fit in the ref item. |
| 5582 | */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5583 | key[1].objectid = objectid; |
| 5584 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5585 | key[1].offset = ref_objectid; |
| 5586 | |
| 5587 | sizes[0] = sizeof(struct btrfs_inode_item); |
| 5588 | sizes[1] = name_len + sizeof(*ref); |
| 5589 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5590 | path->leave_spinning = 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5591 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
| 5592 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5593 | goto fail; |
| 5594 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5595 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5596 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5597 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5598 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5599 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5600 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5601 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5602 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5603 | |
| 5604 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5605 | struct btrfs_inode_ref); |
| 5606 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5607 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5608 | ptr = (unsigned long)(ref + 1); |
| 5609 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5610 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5611 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5612 | btrfs_free_path(path); |
| 5613 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5614 | location = &BTRFS_I(inode)->location; |
| 5615 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5616 | location->offset = 0; |
| 5617 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5618 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5619 | btrfs_inherit_iflags(inode, dir); |
| 5620 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5621 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5622 | if (btrfs_test_opt(root, NODATASUM)) |
| 5623 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5624 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5625 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5626 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5627 | } |
| 5628 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5629 | btrfs_insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5630 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5631 | |
| 5632 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5633 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5634 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5635 | btrfs_update_root_times(trans, root); |
| 5636 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5637 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5638 | if (ret) |
| 5639 | btrfs_err(root->fs_info, |
| 5640 | "error inheriting props for ino %llu (root %llu): %d", |
| 5641 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5642 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5643 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5644 | fail: |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5645 | if (dir) |
| 5646 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5647 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5648 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5649 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5653 | { |
| 5654 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5655 | } |
| 5656 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5657 | /* |
| 5658 | * utility function to add 'inode' into 'parent_inode' with |
| 5659 | * a give name and a given sequence number. |
| 5660 | * if 'add_backref' is true, also insert a backref from the |
| 5661 | * inode to the parent directory. |
| 5662 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5663 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5664 | struct inode *parent_inode, struct inode *inode, |
| 5665 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5666 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5667 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5668 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5669 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5670 | u64 ino = btrfs_ino(inode); |
| 5671 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5672 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5673 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5674 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5675 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5676 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5677 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5678 | key.offset = 0; |
| 5679 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5680 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5681 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5682 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5683 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5684 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5685 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5686 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5687 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5688 | } |
| 5689 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5690 | /* Nothing to clean up yet */ |
| 5691 | if (ret) |
| 5692 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5693 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5694 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5695 | parent_inode, &key, |
| 5696 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5697 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5698 | goto fail_dir_item; |
| 5699 | else if (ret) { |
| 5700 | btrfs_abort_transaction(trans, root, ret); |
| 5701 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5702 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5703 | |
| 5704 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5705 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5706 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5707 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5708 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5709 | if (ret) |
| 5710 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5711 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5712 | |
| 5713 | fail_dir_item: |
| 5714 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5715 | u64 local_index; |
| 5716 | int err; |
| 5717 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5718 | key.objectid, root->root_key.objectid, |
| 5719 | parent_ino, &local_index, name, name_len); |
| 5720 | |
| 5721 | } else if (add_backref) { |
| 5722 | u64 local_index; |
| 5723 | int err; |
| 5724 | |
| 5725 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5726 | ino, parent_ino, &local_index); |
| 5727 | } |
| 5728 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5732 | struct inode *dir, struct dentry *dentry, |
| 5733 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5734 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5735 | int err = btrfs_add_link(trans, dir, inode, |
| 5736 | dentry->d_name.name, dentry->d_name.len, |
| 5737 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5738 | if (err > 0) |
| 5739 | err = -EEXIST; |
| 5740 | return err; |
| 5741 | } |
| 5742 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5743 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5744 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5745 | { |
| 5746 | struct btrfs_trans_handle *trans; |
| 5747 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5748 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5749 | int err; |
| 5750 | int drop_inode = 0; |
| 5751 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5752 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5753 | |
| 5754 | if (!new_valid_dev(rdev)) |
| 5755 | return -EINVAL; |
| 5756 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5757 | /* |
| 5758 | * 2 for inode item and ref |
| 5759 | * 2 for dir items |
| 5760 | * 1 for xattr if selinux is on |
| 5761 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5762 | trans = btrfs_start_transaction(root, 5); |
| 5763 | if (IS_ERR(trans)) |
| 5764 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5765 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5766 | err = btrfs_find_free_ino(root, &objectid); |
| 5767 | if (err) |
| 5768 | goto out_unlock; |
| 5769 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5770 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5771 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5772 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5773 | if (IS_ERR(inode)) { |
| 5774 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5775 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5776 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5777 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5778 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5779 | if (err) { |
| 5780 | drop_inode = 1; |
| 5781 | goto out_unlock; |
| 5782 | } |
| 5783 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5784 | /* |
| 5785 | * If the active LSM wants to access the inode during |
| 5786 | * d_instantiate it needs these. Smack checks to see |
| 5787 | * if the filesystem supports xattrs by looking at the |
| 5788 | * ops vector. |
| 5789 | */ |
| 5790 | |
| 5791 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5792 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5793 | if (err) |
| 5794 | drop_inode = 1; |
| 5795 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5796 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5797 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5798 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5799 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5800 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5801 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5802 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5803 | if (drop_inode) { |
| 5804 | inode_dec_link_count(inode); |
| 5805 | iput(inode); |
| 5806 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5807 | return err; |
| 5808 | } |
| 5809 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5810 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5811 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5812 | { |
| 5813 | struct btrfs_trans_handle *trans; |
| 5814 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5815 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5816 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5817 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5818 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5819 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5820 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5821 | /* |
| 5822 | * 2 for inode item and ref |
| 5823 | * 2 for dir items |
| 5824 | * 1 for xattr if selinux is on |
| 5825 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5826 | trans = btrfs_start_transaction(root, 5); |
| 5827 | if (IS_ERR(trans)) |
| 5828 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5829 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5830 | err = btrfs_find_free_ino(root, &objectid); |
| 5831 | if (err) |
| 5832 | goto out_unlock; |
| 5833 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5834 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5835 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5836 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5837 | if (IS_ERR(inode)) { |
| 5838 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5839 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5840 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5841 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5842 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5843 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5844 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5845 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5846 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5847 | err = btrfs_update_inode(trans, root, inode); |
| 5848 | if (err) |
| 5849 | goto out_unlock; |
| 5850 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5851 | /* |
| 5852 | * If the active LSM wants to access the inode during |
| 5853 | * d_instantiate it needs these. Smack checks to see |
| 5854 | * if the filesystem supports xattrs by looking at the |
| 5855 | * ops vector. |
| 5856 | */ |
| 5857 | inode->i_fop = &btrfs_file_operations; |
| 5858 | inode->i_op = &btrfs_file_inode_operations; |
| 5859 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5860 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5861 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5862 | goto out_unlock; |
| 5863 | |
| 5864 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5865 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5866 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5867 | d_instantiate(dentry, inode); |
| 5868 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5869 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5870 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5871 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5872 | inode_dec_link_count(inode); |
| 5873 | iput(inode); |
| 5874 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5875 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5876 | return err; |
| 5877 | } |
| 5878 | |
| 5879 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5880 | struct dentry *dentry) |
| 5881 | { |
| 5882 | struct btrfs_trans_handle *trans; |
| 5883 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5884 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5885 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5886 | int err; |
| 5887 | int drop_inode = 0; |
| 5888 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5889 | /* do not allow sys_link's with other subvols of the same device */ |
| 5890 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5891 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5892 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5893 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5894 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5895 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5896 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5897 | if (err) |
| 5898 | goto fail; |
| 5899 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5900 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5901 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5902 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5903 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5904 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5905 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5906 | if (IS_ERR(trans)) { |
| 5907 | err = PTR_ERR(trans); |
| 5908 | goto fail; |
| 5909 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5910 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5911 | /* There are several dir indexes for this inode, clear the cache. */ |
| 5912 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 5913 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5914 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5915 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5916 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5917 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5918 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5919 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5920 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5921 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5922 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5923 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 5924 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5925 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5926 | if (err) |
| 5927 | goto fail; |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5928 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 5929 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 5930 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5931 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5932 | btrfs_end_transaction(trans, root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5933 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5934 | if (drop_inode) { |
| 5935 | inode_dec_link_count(inode); |
| 5936 | iput(inode); |
| 5937 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5938 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5939 | return err; |
| 5940 | } |
| 5941 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 5942 | 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] | 5943 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5944 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5945 | struct btrfs_trans_handle *trans; |
| 5946 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5947 | int err = 0; |
| 5948 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 5949 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5950 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5951 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5952 | /* |
| 5953 | * 2 items for inode and ref |
| 5954 | * 2 items for dir items |
| 5955 | * 1 for xattr if selinux is on |
| 5956 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5957 | trans = btrfs_start_transaction(root, 5); |
| 5958 | if (IS_ERR(trans)) |
| 5959 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5960 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5961 | err = btrfs_find_free_ino(root, &objectid); |
| 5962 | if (err) |
| 5963 | goto out_fail; |
| 5964 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5965 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5966 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5967 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5968 | if (IS_ERR(inode)) { |
| 5969 | err = PTR_ERR(inode); |
| 5970 | goto out_fail; |
| 5971 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5972 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5973 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5974 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5975 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5976 | if (err) |
| 5977 | goto out_fail; |
| 5978 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5979 | inode->i_op = &btrfs_dir_inode_operations; |
| 5980 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5981 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 5982 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5983 | err = btrfs_update_inode(trans, root, inode); |
| 5984 | if (err) |
| 5985 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5986 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5987 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 5988 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5989 | if (err) |
| 5990 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5991 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5992 | d_instantiate(dentry, inode); |
| 5993 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5994 | |
| 5995 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5996 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5997 | if (drop_on_err) |
| 5998 | iput(inode); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5999 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6000 | return err; |
| 6001 | } |
| 6002 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6003 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 6004 | * and an extent that you want to insert, deal with overlap and insert |
| 6005 | * the new extent into the tree. |
| 6006 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6007 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6008 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6009 | struct extent_map *em, |
| 6010 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6011 | { |
| 6012 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6013 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6014 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 6015 | start_diff = map_start - em->start; |
| 6016 | em->start = map_start; |
| 6017 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6018 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6019 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6020 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6021 | em->block_len -= start_diff; |
| 6022 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6023 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6024 | } |
| 6025 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6026 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6027 | struct inode *inode, struct page *page, |
| 6028 | size_t pg_offset, u64 extent_offset, |
| 6029 | struct btrfs_file_extent_item *item) |
| 6030 | { |
| 6031 | int ret; |
| 6032 | struct extent_buffer *leaf = path->nodes[0]; |
| 6033 | char *tmp; |
| 6034 | size_t max_size; |
| 6035 | unsigned long inline_size; |
| 6036 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6037 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6038 | |
| 6039 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6040 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6041 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6042 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6043 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6044 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6045 | if (!tmp) |
| 6046 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6047 | ptr = btrfs_file_extent_inline_start(item); |
| 6048 | |
| 6049 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6050 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6051 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6052 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6053 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6054 | if (ret) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6055 | char *kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6056 | unsigned long copy_size = min_t(u64, |
| 6057 | PAGE_CACHE_SIZE - pg_offset, |
| 6058 | max_size - extent_offset); |
| 6059 | memset(kaddr + pg_offset, 0, copy_size); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6060 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6061 | } |
| 6062 | kfree(tmp); |
| 6063 | return 0; |
| 6064 | } |
| 6065 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6066 | /* |
| 6067 | * 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] | 6068 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6069 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6070 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6071 | * |
| 6072 | * This also copies inline extents directly into the page. |
| 6073 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6074 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6075 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6076 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6077 | int create) |
| 6078 | { |
| 6079 | int ret; |
| 6080 | int err = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6081 | u64 bytenr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6082 | u64 extent_start = 0; |
| 6083 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6084 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6085 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6086 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6087 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6088 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6089 | struct extent_buffer *leaf; |
| 6090 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6091 | struct extent_map *em = NULL; |
| 6092 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6093 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6094 | struct btrfs_trans_handle *trans = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6095 | int compress_type; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6096 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6097 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6098 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6099 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6100 | if (em) |
| 6101 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6102 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6103 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6104 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6105 | if (em->start > start || em->start + em->len <= start) |
| 6106 | free_extent_map(em); |
| 6107 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6108 | free_extent_map(em); |
| 6109 | else |
| 6110 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6111 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6112 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6113 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6114 | err = -ENOMEM; |
| 6115 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6116 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6117 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6118 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6119 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6120 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6121 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6122 | |
| 6123 | if (!path) { |
| 6124 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6125 | if (!path) { |
| 6126 | err = -ENOMEM; |
| 6127 | goto out; |
| 6128 | } |
| 6129 | /* |
| 6130 | * Chances are we'll be called again, so go ahead and do |
| 6131 | * readahead |
| 6132 | */ |
| 6133 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6134 | } |
| 6135 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6136 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6137 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6138 | if (ret < 0) { |
| 6139 | err = ret; |
| 6140 | goto out; |
| 6141 | } |
| 6142 | |
| 6143 | if (ret != 0) { |
| 6144 | if (path->slots[0] == 0) |
| 6145 | goto not_found; |
| 6146 | path->slots[0]--; |
| 6147 | } |
| 6148 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6149 | leaf = path->nodes[0]; |
| 6150 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6151 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6152 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6153 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6154 | found_type = btrfs_key_type(&found_key); |
| 6155 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6156 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6157 | /* |
| 6158 | * If we backup past the first extent we want to move forward |
| 6159 | * and see if there is an extent in front of us, otherwise we'll |
| 6160 | * say there is a hole for our whole search range which can |
| 6161 | * cause problems. |
| 6162 | */ |
| 6163 | extent_end = start; |
| 6164 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6165 | } |
| 6166 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6167 | found_type = btrfs_file_extent_type(leaf, item); |
| 6168 | extent_start = found_key.offset; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6169 | compress_type = btrfs_file_extent_compression(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6170 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6171 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6172 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6173 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6174 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6175 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6176 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6177 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6178 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6179 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6180 | if (start >= extent_end) { |
| 6181 | path->slots[0]++; |
| 6182 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6183 | ret = btrfs_next_leaf(root, path); |
| 6184 | if (ret < 0) { |
| 6185 | err = ret; |
| 6186 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6187 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6188 | if (ret > 0) |
| 6189 | goto not_found; |
| 6190 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6191 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6192 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6193 | if (found_key.objectid != objectid || |
| 6194 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6195 | goto not_found; |
| 6196 | if (start + len <= found_key.offset) |
| 6197 | goto not_found; |
| 6198 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6199 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6200 | em->len = found_key.offset - start; |
| 6201 | goto not_found_em; |
| 6202 | } |
| 6203 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6204 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6205 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6206 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6207 | em->start = extent_start; |
| 6208 | em->len = extent_end - extent_start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 6209 | em->orig_start = extent_start - |
| 6210 | btrfs_file_extent_offset(leaf, item); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6211 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
| 6212 | item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6213 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
| 6214 | if (bytenr == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6215 | em->block_start = EXTENT_MAP_HOLE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6216 | goto insert; |
| 6217 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6218 | if (compress_type != BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6219 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6220 | em->compress_type = compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6221 | em->block_start = bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6222 | em->block_len = em->orig_block_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6223 | } else { |
| 6224 | bytenr += btrfs_file_extent_offset(leaf, item); |
| 6225 | em->block_start = bytenr; |
| 6226 | em->block_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6227 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
| 6228 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6229 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6230 | goto insert; |
| 6231 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6232 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6233 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6234 | size_t size; |
| 6235 | size_t extent_offset; |
| 6236 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6237 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6238 | em->block_start = EXTENT_MAP_INLINE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6239 | if (!page || create) { |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6240 | em->start = extent_start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6241 | em->len = extent_end - extent_start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6242 | goto out; |
| 6243 | } |
| 6244 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6245 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6246 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6247 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6248 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6249 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6250 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6251 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6252 | em->orig_start = em->start; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6253 | if (compress_type) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6254 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6255 | em->compress_type = compress_type; |
| 6256 | } |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6257 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6258 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6259 | if (btrfs_file_extent_compression(leaf, item) != |
| 6260 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6261 | ret = uncompress_inline(path, inode, page, |
| 6262 | pg_offset, |
| 6263 | extent_offset, item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6264 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6265 | } else { |
| 6266 | map = kmap(page); |
| 6267 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6268 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6269 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6270 | memset(map + pg_offset + copy_size, 0, |
| 6271 | PAGE_CACHE_SIZE - pg_offset - |
| 6272 | copy_size); |
| 6273 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6274 | kunmap(page); |
| 6275 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6276 | flush_dcache_page(page); |
| 6277 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6278 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6279 | if (!trans) { |
| 6280 | kunmap(page); |
| 6281 | free_extent_map(em); |
| 6282 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6283 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6284 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6285 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6286 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6287 | if (IS_ERR(trans)) |
| 6288 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6289 | goto again; |
| 6290 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6291 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6292 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6293 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6294 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6295 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6296 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6297 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6298 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6299 | goto insert; |
| 6300 | } else { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 6301 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6302 | } |
| 6303 | not_found: |
| 6304 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6305 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6306 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6307 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6308 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6309 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6310 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6311 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6312 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6313 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6314 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6315 | err = -EIO; |
| 6316 | goto out; |
| 6317 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6318 | |
| 6319 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6320 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6321 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6322 | /* it is possible that someone inserted the extent into the tree |
| 6323 | * while we had the lock dropped. It is also possible that |
| 6324 | * an overlapping map exists in the tree |
| 6325 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6326 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6327 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6328 | |
| 6329 | ret = 0; |
| 6330 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6331 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6332 | if (existing && (existing->start > start || |
| 6333 | existing->start + existing->len <= start)) { |
| 6334 | free_extent_map(existing); |
| 6335 | existing = NULL; |
| 6336 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6337 | if (!existing) { |
| 6338 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6339 | em->len); |
| 6340 | if (existing) { |
| 6341 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6342 | em, start, |
| 6343 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6344 | free_extent_map(existing); |
| 6345 | if (err) { |
| 6346 | free_extent_map(em); |
| 6347 | em = NULL; |
| 6348 | } |
| 6349 | } else { |
| 6350 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6351 | free_extent_map(em); |
| 6352 | em = NULL; |
| 6353 | } |
| 6354 | } else { |
| 6355 | free_extent_map(em); |
| 6356 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6357 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6358 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6359 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6360 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6361 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6362 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6363 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6364 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6365 | if (path) |
| 6366 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6367 | if (trans) { |
| 6368 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6369 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6370 | err = ret; |
| 6371 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6372 | if (err) { |
| 6373 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6374 | return ERR_PTR(err); |
| 6375 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6376 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6377 | return em; |
| 6378 | } |
| 6379 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6380 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6381 | size_t pg_offset, u64 start, u64 len, |
| 6382 | int create) |
| 6383 | { |
| 6384 | struct extent_map *em; |
| 6385 | struct extent_map *hole_em = NULL; |
| 6386 | u64 range_start = start; |
| 6387 | u64 end; |
| 6388 | u64 found; |
| 6389 | u64 found_end; |
| 6390 | int err = 0; |
| 6391 | |
| 6392 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6393 | if (IS_ERR(em)) |
| 6394 | return em; |
| 6395 | if (em) { |
| 6396 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6397 | * if our em maps to |
| 6398 | * - a hole or |
| 6399 | * - a pre-alloc extent, |
| 6400 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6401 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6402 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6403 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6404 | return em; |
| 6405 | else |
| 6406 | hole_em = em; |
| 6407 | } |
| 6408 | |
| 6409 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6410 | end = start + len; |
| 6411 | if (end < start) |
| 6412 | end = (u64)-1; |
| 6413 | else |
| 6414 | end -= 1; |
| 6415 | |
| 6416 | em = NULL; |
| 6417 | |
| 6418 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6419 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6420 | end, len, EXTENT_DELALLOC, 1); |
| 6421 | found_end = range_start + found; |
| 6422 | if (found_end < range_start) |
| 6423 | found_end = (u64)-1; |
| 6424 | |
| 6425 | /* |
| 6426 | * we didn't find anything useful, return |
| 6427 | * the original results from get_extent() |
| 6428 | */ |
| 6429 | if (range_start > end || found_end <= start) { |
| 6430 | em = hole_em; |
| 6431 | hole_em = NULL; |
| 6432 | goto out; |
| 6433 | } |
| 6434 | |
| 6435 | /* adjust the range_start to make sure it doesn't |
| 6436 | * go backwards from the start they passed in |
| 6437 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6438 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6439 | found = found_end - range_start; |
| 6440 | |
| 6441 | if (found > 0) { |
| 6442 | u64 hole_start = start; |
| 6443 | u64 hole_len = len; |
| 6444 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6445 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6446 | if (!em) { |
| 6447 | err = -ENOMEM; |
| 6448 | goto out; |
| 6449 | } |
| 6450 | /* |
| 6451 | * when btrfs_get_extent can't find anything it |
| 6452 | * returns one huge hole |
| 6453 | * |
| 6454 | * make sure what it found really fits our range, and |
| 6455 | * adjust to make sure it is based on the start from |
| 6456 | * the caller |
| 6457 | */ |
| 6458 | if (hole_em) { |
| 6459 | u64 calc_end = extent_map_end(hole_em); |
| 6460 | |
| 6461 | if (calc_end <= start || (hole_em->start > end)) { |
| 6462 | free_extent_map(hole_em); |
| 6463 | hole_em = NULL; |
| 6464 | } else { |
| 6465 | hole_start = max(hole_em->start, start); |
| 6466 | hole_len = calc_end - hole_start; |
| 6467 | } |
| 6468 | } |
| 6469 | em->bdev = NULL; |
| 6470 | if (hole_em && range_start > hole_start) { |
| 6471 | /* our hole starts before our delalloc, so we |
| 6472 | * have to return just the parts of the hole |
| 6473 | * that go until the delalloc starts |
| 6474 | */ |
| 6475 | em->len = min(hole_len, |
| 6476 | range_start - hole_start); |
| 6477 | em->start = hole_start; |
| 6478 | em->orig_start = hole_start; |
| 6479 | /* |
| 6480 | * don't adjust block start at all, |
| 6481 | * it is fixed at EXTENT_MAP_HOLE |
| 6482 | */ |
| 6483 | em->block_start = hole_em->block_start; |
| 6484 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6485 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6486 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6487 | } else { |
| 6488 | em->start = range_start; |
| 6489 | em->len = found; |
| 6490 | em->orig_start = range_start; |
| 6491 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6492 | em->block_len = found; |
| 6493 | } |
| 6494 | } else if (hole_em) { |
| 6495 | return hole_em; |
| 6496 | } |
| 6497 | out: |
| 6498 | |
| 6499 | free_extent_map(hole_em); |
| 6500 | if (err) { |
| 6501 | free_extent_map(em); |
| 6502 | return ERR_PTR(err); |
| 6503 | } |
| 6504 | return em; |
| 6505 | } |
| 6506 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6507 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6508 | u64 start, u64 len) |
| 6509 | { |
| 6510 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6511 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6512 | struct btrfs_key ins; |
| 6513 | u64 alloc_hint; |
| 6514 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6515 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6516 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6517 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6518 | alloc_hint, &ins, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6519 | if (ret) |
| 6520 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6521 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6522 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6523 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6524 | if (IS_ERR(em)) { |
| 6525 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
| 6526 | return em; |
| 6527 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6528 | |
| 6529 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6530 | ins.offset, ins.offset, 0); |
| 6531 | if (ret) { |
| 6532 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6533 | free_extent_map(em); |
| 6534 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6535 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6536 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6537 | return em; |
| 6538 | } |
| 6539 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6540 | /* |
| 6541 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6542 | * block must be cow'd |
| 6543 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6544 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6545 | u64 *orig_start, u64 *orig_block_len, |
| 6546 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6547 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6548 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6549 | struct btrfs_path *path; |
| 6550 | int ret; |
| 6551 | struct extent_buffer *leaf; |
| 6552 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6553 | struct btrfs_file_extent_item *fi; |
| 6554 | struct btrfs_key key; |
| 6555 | u64 disk_bytenr; |
| 6556 | u64 backref_offset; |
| 6557 | u64 extent_end; |
| 6558 | u64 num_bytes; |
| 6559 | int slot; |
| 6560 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6561 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6562 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6563 | path = btrfs_alloc_path(); |
| 6564 | if (!path) |
| 6565 | return -ENOMEM; |
| 6566 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6567 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6568 | offset, 0); |
| 6569 | if (ret < 0) |
| 6570 | goto out; |
| 6571 | |
| 6572 | slot = path->slots[0]; |
| 6573 | if (ret == 1) { |
| 6574 | if (slot == 0) { |
| 6575 | /* can't find the item, must cow */ |
| 6576 | ret = 0; |
| 6577 | goto out; |
| 6578 | } |
| 6579 | slot--; |
| 6580 | } |
| 6581 | ret = 0; |
| 6582 | leaf = path->nodes[0]; |
| 6583 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6584 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6585 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6586 | /* not our file or wrong item type, must cow */ |
| 6587 | goto out; |
| 6588 | } |
| 6589 | |
| 6590 | if (key.offset > offset) { |
| 6591 | /* Wrong offset, must cow */ |
| 6592 | goto out; |
| 6593 | } |
| 6594 | |
| 6595 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6596 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6597 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6598 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6599 | /* not a regular extent, must cow */ |
| 6600 | goto out; |
| 6601 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6602 | |
| 6603 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6604 | goto out; |
| 6605 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6606 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6607 | if (extent_end <= offset) |
| 6608 | goto out; |
| 6609 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6610 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6611 | if (disk_bytenr == 0) |
| 6612 | goto out; |
| 6613 | |
| 6614 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6615 | btrfs_file_extent_encryption(leaf, fi) || |
| 6616 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6617 | goto out; |
| 6618 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6619 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6620 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6621 | if (orig_start) { |
| 6622 | *orig_start = key.offset - backref_offset; |
| 6623 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6624 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6625 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6626 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6627 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6628 | goto out; |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6629 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6630 | |
| 6631 | /* |
| 6632 | * look for other files referencing this extent, if we |
| 6633 | * find any we must cow |
| 6634 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6635 | trans = btrfs_join_transaction(root); |
| 6636 | if (IS_ERR(trans)) { |
| 6637 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6638 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6639 | } |
| 6640 | |
| 6641 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6642 | key.offset - backref_offset, disk_bytenr); |
| 6643 | btrfs_end_transaction(trans, root); |
| 6644 | if (ret) { |
| 6645 | ret = 0; |
| 6646 | goto out; |
| 6647 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6648 | |
| 6649 | /* |
| 6650 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6651 | * in this extent we are about to write. If there |
| 6652 | * are any csums in that range we have to cow in order |
| 6653 | * to keep the csums correct |
| 6654 | */ |
| 6655 | disk_bytenr += backref_offset; |
| 6656 | disk_bytenr += offset - key.offset; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6657 | num_bytes = min(offset + *len, extent_end) - offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6658 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6659 | goto out; |
| 6660 | /* |
| 6661 | * all of the above have passed, it is safe to overwrite this extent |
| 6662 | * without cow |
| 6663 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6664 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6665 | ret = 1; |
| 6666 | out: |
| 6667 | btrfs_free_path(path); |
| 6668 | return ret; |
| 6669 | } |
| 6670 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6671 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6672 | struct extent_state **cached_state, int writing) |
| 6673 | { |
| 6674 | struct btrfs_ordered_extent *ordered; |
| 6675 | int ret = 0; |
| 6676 | |
| 6677 | while (1) { |
| 6678 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6679 | 0, cached_state); |
| 6680 | /* |
| 6681 | * We're concerned with the entire range that we're going to be |
| 6682 | * doing DIO to, so we need to make sure theres no ordered |
| 6683 | * extents in this range. |
| 6684 | */ |
| 6685 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6686 | lockend - lockstart + 1); |
| 6687 | |
| 6688 | /* |
| 6689 | * We need to make sure there are no buffered pages in this |
| 6690 | * range either, we could have raced between the invalidate in |
| 6691 | * generic_file_direct_write and locking the extent. The |
| 6692 | * invalidate needs to happen so that reads after a write do not |
| 6693 | * get stale data. |
| 6694 | */ |
| 6695 | if (!ordered && (!writing || |
| 6696 | !test_range_bit(&BTRFS_I(inode)->io_tree, |
| 6697 | lockstart, lockend, EXTENT_UPTODATE, 0, |
| 6698 | *cached_state))) |
| 6699 | break; |
| 6700 | |
| 6701 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6702 | cached_state, GFP_NOFS); |
| 6703 | |
| 6704 | if (ordered) { |
| 6705 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6706 | btrfs_put_ordered_extent(ordered); |
| 6707 | } else { |
| 6708 | /* Screw you mmap */ |
| 6709 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6710 | lockstart, |
| 6711 | lockend); |
| 6712 | if (ret) |
| 6713 | break; |
| 6714 | |
| 6715 | /* |
| 6716 | * If we found a page that couldn't be invalidated just |
| 6717 | * fall back to buffered. |
| 6718 | */ |
| 6719 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6720 | lockstart >> PAGE_CACHE_SHIFT, |
| 6721 | lockend >> PAGE_CACHE_SHIFT); |
| 6722 | if (ret) |
| 6723 | break; |
| 6724 | } |
| 6725 | |
| 6726 | cond_resched(); |
| 6727 | } |
| 6728 | |
| 6729 | return ret; |
| 6730 | } |
| 6731 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6732 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6733 | u64 len, u64 orig_start, |
| 6734 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6735 | u64 orig_block_len, u64 ram_bytes, |
| 6736 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6737 | { |
| 6738 | struct extent_map_tree *em_tree; |
| 6739 | struct extent_map *em; |
| 6740 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6741 | int ret; |
| 6742 | |
| 6743 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6744 | em = alloc_extent_map(); |
| 6745 | if (!em) |
| 6746 | return ERR_PTR(-ENOMEM); |
| 6747 | |
| 6748 | em->start = start; |
| 6749 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6750 | em->mod_start = start; |
| 6751 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6752 | em->len = len; |
| 6753 | em->block_len = block_len; |
| 6754 | em->block_start = block_start; |
| 6755 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6756 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6757 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6758 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6759 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6760 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6761 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6762 | |
| 6763 | do { |
| 6764 | btrfs_drop_extent_cache(inode, em->start, |
| 6765 | em->start + em->len - 1, 0); |
| 6766 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6767 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6768 | write_unlock(&em_tree->lock); |
| 6769 | } while (ret == -EEXIST); |
| 6770 | |
| 6771 | if (ret) { |
| 6772 | free_extent_map(em); |
| 6773 | return ERR_PTR(ret); |
| 6774 | } |
| 6775 | |
| 6776 | return em; |
| 6777 | } |
| 6778 | |
| 6779 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6780 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6781 | struct buffer_head *bh_result, int create) |
| 6782 | { |
| 6783 | struct extent_map *em; |
| 6784 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6785 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6786 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6787 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6788 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6789 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6790 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6791 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6792 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6793 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6794 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6795 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6796 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6797 | lockstart = start; |
| 6798 | lockend = start + len - 1; |
| 6799 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6800 | /* |
| 6801 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6802 | * this range and we need to fallback to buffered. |
| 6803 | */ |
| 6804 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6805 | return -ENOTBLK; |
| 6806 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6807 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6808 | if (IS_ERR(em)) { |
| 6809 | ret = PTR_ERR(em); |
| 6810 | goto unlock_err; |
| 6811 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6812 | |
| 6813 | /* |
| 6814 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6815 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6816 | * it's still buffered so for safety lets just fall back to the generic |
| 6817 | * buffered path. |
| 6818 | * |
| 6819 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6820 | * decompress it, so there will be buffering required no matter what we |
| 6821 | * do, so go ahead and fallback to buffered. |
| 6822 | * |
| 6823 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6824 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6825 | * the generic code. |
| 6826 | */ |
| 6827 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6828 | em->block_start == EXTENT_MAP_INLINE) { |
| 6829 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6830 | ret = -ENOTBLK; |
| 6831 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6832 | } |
| 6833 | |
| 6834 | /* Just a good old fashioned hole, return */ |
| 6835 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6836 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6837 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6838 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6839 | } |
| 6840 | |
| 6841 | /* |
| 6842 | * We don't allocate a new extent in the following cases |
| 6843 | * |
| 6844 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6845 | * existing extent. |
| 6846 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6847 | * just use the extent. |
| 6848 | * |
| 6849 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6850 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6851 | len = min(len, em->len - (start - em->start)); |
| 6852 | lockstart = start + len; |
| 6853 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6854 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6855 | |
| 6856 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6857 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6858 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6859 | int type; |
| 6860 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6861 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6862 | |
| 6863 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6864 | type = BTRFS_ORDERED_PREALLOC; |
| 6865 | else |
| 6866 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6867 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6868 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6869 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6870 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6871 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6872 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 6873 | free_extent_map(em); |
| 6874 | em = create_pinned_em(inode, start, len, |
| 6875 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6876 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6877 | orig_block_len, |
| 6878 | ram_bytes, type); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6879 | if (IS_ERR(em)) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6880 | goto unlock_err; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6881 | } |
| 6882 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6883 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 6884 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6885 | if (ret) { |
| 6886 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6887 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6888 | } |
| 6889 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6890 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6891 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6892 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6893 | /* |
| 6894 | * this will cow the extent, reset the len in case we changed |
| 6895 | * it above |
| 6896 | */ |
| 6897 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6898 | free_extent_map(em); |
| 6899 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6900 | if (IS_ERR(em)) { |
| 6901 | ret = PTR_ERR(em); |
| 6902 | goto unlock_err; |
| 6903 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6904 | len = min(len, em->len - (start - em->start)); |
| 6905 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6906 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 6907 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6908 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6909 | bh_result->b_bdev = em->bdev; |
| 6910 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6911 | if (create) { |
| 6912 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 6913 | set_buffer_new(bh_result); |
| 6914 | |
| 6915 | /* |
| 6916 | * Need to update the i_size under the extent lock so buffered |
| 6917 | * readers will get the updated i_size when we unlock. |
| 6918 | */ |
| 6919 | if (start + len > i_size_read(inode)) |
| 6920 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6921 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6922 | spin_lock(&BTRFS_I(inode)->lock); |
| 6923 | BTRFS_I(inode)->outstanding_extents++; |
| 6924 | spin_unlock(&BTRFS_I(inode)->lock); |
| 6925 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6926 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6927 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 6928 | &cached_state, GFP_NOFS); |
| 6929 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 6930 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6931 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6932 | /* |
| 6933 | * In the case of write we need to clear and unlock the entire range, |
| 6934 | * in the case of read we need to unlock only the end area that we |
| 6935 | * aren't using if there is any left over space. |
| 6936 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6937 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6938 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 6939 | lockend, unlock_bits, 1, 0, |
| 6940 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6941 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6942 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 6943 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6944 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6945 | free_extent_map(em); |
| 6946 | |
| 6947 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6948 | |
| 6949 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6950 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6951 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 6952 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6953 | } |
| 6954 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6955 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 6956 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 6957 | struct btrfs_dio_private *dip = bio->bi_private; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 6958 | struct bio_vec *bvec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6959 | struct inode *inode = dip->inode; |
| 6960 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6961 | struct bio *dio_bio; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6962 | u32 *csums = (u32 *)dip->csum; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6963 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 6964 | int i; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6965 | |
| 6966 | start = dip->logical_offset; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 6967 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6968 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 6969 | struct page *page = bvec->bv_page; |
| 6970 | char *kaddr; |
| 6971 | u32 csum = ~(u32)0; |
| 6972 | unsigned long flags; |
| 6973 | |
| 6974 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6975 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 6976 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6977 | csum, bvec->bv_len); |
| 6978 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 6979 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6980 | local_irq_restore(flags); |
| 6981 | |
| 6982 | flush_dcache_page(bvec->bv_page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 6983 | if (csum != csums[i]) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 6984 | btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6985 | btrfs_ino(inode), start, csum, |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 6986 | csums[i]); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6987 | err = -EIO; |
| 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 6992 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6993 | |
| 6994 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 6995 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6996 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6997 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6998 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 6999 | |
| 7000 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7001 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7002 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7003 | dio_end_io(dio_bio, err); |
| 7004 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7005 | } |
| 7006 | |
| 7007 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7008 | { |
| 7009 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7010 | struct inode *inode = dip->inode; |
| 7011 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7012 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7013 | u64 ordered_offset = dip->logical_offset; |
| 7014 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7015 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7016 | int ret; |
| 7017 | |
| 7018 | if (err) |
| 7019 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7020 | again: |
| 7021 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7022 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7023 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7024 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7025 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7026 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7027 | ordered->work.func = finish_ordered_fn; |
| 7028 | ordered->work.flags = 0; |
| 7029 | btrfs_queue_worker(&root->fs_info->endio_write_workers, |
| 7030 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7031 | out_test: |
| 7032 | /* |
| 7033 | * our bio might span multiple ordered extents. If we haven't |
| 7034 | * completed the accounting for the whole dio, go back and try again |
| 7035 | */ |
| 7036 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7037 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7038 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7039 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7040 | goto again; |
| 7041 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7042 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7043 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7044 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7045 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7046 | |
| 7047 | /* If we had an error make sure to clear the uptodate flag */ |
| 7048 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7049 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7050 | dio_end_io(dio_bio, err); |
| 7051 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7052 | } |
| 7053 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7054 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7055 | struct bio *bio, int mirror_num, |
| 7056 | unsigned long bio_flags, u64 offset) |
| 7057 | { |
| 7058 | int ret; |
| 7059 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7060 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7061 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7062 | return 0; |
| 7063 | } |
| 7064 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7065 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7066 | { |
| 7067 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7068 | |
| 7069 | if (err) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7070 | btrfs_err(BTRFS_I(dip->inode)->root->fs_info, |
| 7071 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7072 | btrfs_ino(dip->inode), bio->bi_rw, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7073 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7074 | bio->bi_iter.bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7075 | dip->errors = 1; |
| 7076 | |
| 7077 | /* |
| 7078 | * before atomic variable goto zero, we must make sure |
| 7079 | * dip->errors is perceived to be set. |
| 7080 | */ |
| 7081 | smp_mb__before_atomic_dec(); |
| 7082 | } |
| 7083 | |
| 7084 | /* if there are more bios still pending for this dio, just exit */ |
| 7085 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7086 | goto out; |
| 7087 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7088 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7089 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7090 | } else { |
| 7091 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7092 | bio_endio(dip->orig_bio, 0); |
| 7093 | } |
| 7094 | out: |
| 7095 | bio_put(bio); |
| 7096 | } |
| 7097 | |
| 7098 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7099 | u64 first_sector, gfp_t gfp_flags) |
| 7100 | { |
| 7101 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7102 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7103 | } |
| 7104 | |
| 7105 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7106 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7107 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7108 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7109 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7110 | int write = rw & REQ_WRITE; |
| 7111 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7112 | int ret; |
| 7113 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7114 | if (async_submit) |
| 7115 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7116 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7117 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7118 | |
| 7119 | if (!write) { |
| 7120 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 7121 | if (ret) |
| 7122 | goto err; |
| 7123 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7124 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7125 | if (skip_sum) |
| 7126 | goto map; |
| 7127 | |
| 7128 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7129 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7130 | inode, rw, bio, 0, 0, |
| 7131 | file_offset, |
| 7132 | __btrfs_submit_bio_start_direct_io, |
| 7133 | __btrfs_submit_bio_done); |
| 7134 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7135 | } else if (write) { |
| 7136 | /* |
| 7137 | * If we aren't doing async submit, calculate the csum of the |
| 7138 | * bio now. |
| 7139 | */ |
| 7140 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7141 | if (ret) |
| 7142 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7143 | } else if (!skip_sum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7144 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
| 7145 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7146 | if (ret) |
| 7147 | goto err; |
| 7148 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7149 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7150 | map: |
| 7151 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7152 | err: |
| 7153 | bio_put(bio); |
| 7154 | return ret; |
| 7155 | } |
| 7156 | |
| 7157 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7158 | int skip_sum) |
| 7159 | { |
| 7160 | struct inode *inode = dip->inode; |
| 7161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7162 | struct bio *bio; |
| 7163 | struct bio *orig_bio = dip->orig_bio; |
| 7164 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7165 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7166 | u64 file_offset = dip->logical_offset; |
| 7167 | u64 submit_len = 0; |
| 7168 | u64 map_length; |
| 7169 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7170 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7171 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7172 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7173 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7174 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7175 | &map_length, NULL, 0); |
| 7176 | if (ret) { |
Josef Bacik | 64728bb | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 7177 | bio_put(orig_bio); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7178 | return -EIO; |
| 7179 | } |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7180 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7181 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7182 | bio = orig_bio; |
| 7183 | goto submit; |
| 7184 | } |
| 7185 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7186 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7187 | if (btrfs_get_alloc_profile(root, 1) & |
| 7188 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7189 | async_submit = 0; |
| 7190 | else |
| 7191 | async_submit = 1; |
| 7192 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7193 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7194 | if (!bio) |
| 7195 | return -ENOMEM; |
| 7196 | bio->bi_private = dip; |
| 7197 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7198 | atomic_inc(&dip->pending_bios); |
| 7199 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7200 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7201 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7202 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7203 | bvec->bv_offset) < bvec->bv_len)) { |
| 7204 | /* |
| 7205 | * inc the count before we submit the bio so |
| 7206 | * we know the end IO handler won't happen before |
| 7207 | * we inc the count. Otherwise, the dip might get freed |
| 7208 | * before we're done setting it up |
| 7209 | */ |
| 7210 | atomic_inc(&dip->pending_bios); |
| 7211 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7212 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7213 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7214 | if (ret) { |
| 7215 | bio_put(bio); |
| 7216 | atomic_dec(&dip->pending_bios); |
| 7217 | goto out_err; |
| 7218 | } |
| 7219 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7220 | start_sector += submit_len >> 9; |
| 7221 | file_offset += submit_len; |
| 7222 | |
| 7223 | submit_len = 0; |
| 7224 | nr_pages = 0; |
| 7225 | |
| 7226 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7227 | start_sector, GFP_NOFS); |
| 7228 | if (!bio) |
| 7229 | goto out_err; |
| 7230 | bio->bi_private = dip; |
| 7231 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7232 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7233 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7234 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7235 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7236 | &map_length, NULL, 0); |
| 7237 | if (ret) { |
| 7238 | bio_put(bio); |
| 7239 | goto out_err; |
| 7240 | } |
| 7241 | } else { |
| 7242 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7243 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7244 | bvec++; |
| 7245 | } |
| 7246 | } |
| 7247 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7248 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7249 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7250 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7251 | if (!ret) |
| 7252 | return 0; |
| 7253 | |
| 7254 | bio_put(bio); |
| 7255 | out_err: |
| 7256 | dip->errors = 1; |
| 7257 | /* |
| 7258 | * before atomic variable goto zero, we must |
| 7259 | * make sure dip->errors is perceived to be set. |
| 7260 | */ |
| 7261 | smp_mb__before_atomic_dec(); |
| 7262 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7263 | bio_io_error(dip->orig_bio); |
| 7264 | |
| 7265 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7266 | return 0; |
| 7267 | } |
| 7268 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7269 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7270 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7271 | { |
| 7272 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7273 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7274 | struct bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7275 | int skip_sum; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7276 | int sum_len; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7277 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7278 | int ret = 0; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7279 | u16 csum_size; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7280 | |
| 7281 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7282 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7283 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7284 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7285 | ret = -ENOMEM; |
| 7286 | goto free_ordered; |
| 7287 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7288 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7289 | if (!skip_sum && !write) { |
| 7290 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7291 | sum_len = dio_bio->bi_iter.bi_size >> |
| 7292 | inode->i_sb->s_blocksize_bits; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7293 | sum_len *= csum_size; |
| 7294 | } else { |
| 7295 | sum_len = 0; |
| 7296 | } |
| 7297 | |
| 7298 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7299 | if (!dip) { |
| 7300 | ret = -ENOMEM; |
| 7301 | goto free_io_bio; |
| 7302 | } |
| 7303 | |
| 7304 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7305 | dip->inode = inode; |
| 7306 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7307 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7308 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7309 | io_bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7310 | dip->errors = 0; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7311 | dip->orig_bio = io_bio; |
| 7312 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7313 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7314 | |
| 7315 | if (write) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7316 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7317 | else |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7318 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7319 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7320 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7321 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7322 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7323 | |
| 7324 | free_io_bio: |
| 7325 | bio_put(io_bio); |
| 7326 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7327 | free_ordered: |
| 7328 | /* |
| 7329 | * If this is a write, we need to clean up the reserved space and kill |
| 7330 | * the ordered extent. |
| 7331 | */ |
| 7332 | if (write) { |
| 7333 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7334 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7335 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7336 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7337 | btrfs_free_reserved_extent(root, ordered->start, |
| 7338 | ordered->disk_len); |
| 7339 | btrfs_put_ordered_extent(ordered); |
| 7340 | btrfs_put_ordered_extent(ordered); |
| 7341 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7342 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7343 | } |
| 7344 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7345 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
| 7346 | const struct iovec *iov, loff_t offset, |
| 7347 | unsigned long nr_segs) |
| 7348 | { |
| 7349 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7350 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7351 | size_t size; |
| 7352 | unsigned long addr; |
| 7353 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7354 | ssize_t retval = -EINVAL; |
| 7355 | loff_t end = offset; |
| 7356 | |
| 7357 | if (offset & blocksize_mask) |
| 7358 | goto out; |
| 7359 | |
| 7360 | /* Check the memory alignment. Blocks cannot straddle pages */ |
| 7361 | for (seg = 0; seg < nr_segs; seg++) { |
| 7362 | addr = (unsigned long)iov[seg].iov_base; |
| 7363 | size = iov[seg].iov_len; |
| 7364 | end += size; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7365 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7366 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7367 | |
| 7368 | /* If this is a write we don't need to check anymore */ |
| 7369 | if (rw & WRITE) |
| 7370 | continue; |
| 7371 | |
| 7372 | /* |
| 7373 | * Check to make sure we don't have duplicate iov_base's in this |
| 7374 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7375 | * when reading back. |
| 7376 | */ |
| 7377 | for (i = seg + 1; i < nr_segs; i++) { |
| 7378 | if (iov[seg].iov_base == iov[i].iov_base) |
| 7379 | goto out; |
| 7380 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7381 | } |
| 7382 | retval = 0; |
| 7383 | out: |
| 7384 | return retval; |
| 7385 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7386 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7387 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
| 7388 | const struct iovec *iov, loff_t offset, |
| 7389 | unsigned long nr_segs) |
| 7390 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7391 | struct file *file = iocb->ki_filp; |
| 7392 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7393 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7394 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7395 | bool wakeup = true; |
| 7396 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7397 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7398 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7399 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7400 | offset, nr_segs)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7401 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7402 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7403 | atomic_inc(&inode->i_dio_count); |
| 7404 | smp_mb__after_atomic_inc(); |
| 7405 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7406 | /* |
| 7407 | * The generic stuff only does filemap_write_and_wait_range, which isn't |
| 7408 | * enough if we've written compressed pages to this area, so we need to |
| 7409 | * call btrfs_wait_ordered_range to make absolutely sure that any |
| 7410 | * outstanding dirty pages are on disk. |
| 7411 | */ |
| 7412 | count = iov_length(iov, nr_segs); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 7413 | ret = btrfs_wait_ordered_range(inode, offset, count); |
| 7414 | if (ret) |
| 7415 | return ret; |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7416 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7417 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7418 | /* |
| 7419 | * If the write DIO is beyond the EOF, we need update |
| 7420 | * the isize, but it is protected by i_mutex. So we can |
| 7421 | * not unlock the i_mutex at this case. |
| 7422 | */ |
| 7423 | if (offset + count <= inode->i_size) { |
| 7424 | mutex_unlock(&inode->i_mutex); |
| 7425 | relock = true; |
| 7426 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7427 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7428 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7429 | goto out; |
| 7430 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7431 | &BTRFS_I(inode)->runtime_flags))) { |
| 7432 | inode_dio_done(inode); |
| 7433 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7434 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7435 | } |
| 7436 | |
| 7437 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7438 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
| 7439 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7440 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7441 | if (rw & WRITE) { |
| 7442 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7443 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7444 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7445 | btrfs_delalloc_release_space(inode, |
| 7446 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7447 | else |
| 7448 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7449 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7450 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7451 | if (wakeup) |
| 7452 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7453 | if (relock) |
| 7454 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7455 | |
| 7456 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7457 | } |
| 7458 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7459 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7460 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7461 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7462 | __u64 start, __u64 len) |
| 7463 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7464 | int ret; |
| 7465 | |
| 7466 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7467 | if (ret) |
| 7468 | return ret; |
| 7469 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7470 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7471 | } |
| 7472 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7473 | int btrfs_readpage(struct file *file, struct page *page) |
| 7474 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7475 | struct extent_io_tree *tree; |
| 7476 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7477 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7478 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7479 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7480 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7481 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7482 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7483 | |
| 7484 | |
| 7485 | if (current->flags & PF_MEMALLOC) { |
| 7486 | redirty_page_for_writepage(wbc, page); |
| 7487 | unlock_page(page); |
| 7488 | return 0; |
| 7489 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7490 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7491 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7492 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7493 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7494 | static int btrfs_writepages(struct address_space *mapping, |
| 7495 | struct writeback_control *wbc) |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7496 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7497 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7498 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7499 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7500 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7501 | } |
| 7502 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7503 | static int |
| 7504 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7505 | struct list_head *pages, unsigned nr_pages) |
| 7506 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7507 | struct extent_io_tree *tree; |
| 7508 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7509 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7510 | btrfs_get_extent); |
| 7511 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7512 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7513 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7514 | struct extent_io_tree *tree; |
| 7515 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7516 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7517 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7518 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7519 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7520 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7521 | if (ret == 1) { |
| 7522 | ClearPagePrivate(page); |
| 7523 | set_page_private(page, 0); |
| 7524 | page_cache_release(page); |
| 7525 | } |
| 7526 | return ret; |
| 7527 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7528 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7529 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7530 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7531 | if (PageWriteback(page) || PageDirty(page)) |
| 7532 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7533 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7534 | } |
| 7535 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 7536 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 7537 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7538 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7539 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7540 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7541 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7542 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7543 | u64 page_start = page_offset(page); |
| 7544 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7545 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7546 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7547 | /* |
| 7548 | * we have the page locked, so new writeback can't start, |
| 7549 | * and the dirty bit won't be cleared while we are here. |
| 7550 | * |
| 7551 | * Wait for IO on this page so that we can safely clear |
| 7552 | * the PagePrivate2 bit and do ordered accounting |
| 7553 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7554 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7555 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7556 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7557 | if (offset) { |
| 7558 | btrfs_releasepage(page, GFP_NOFS); |
| 7559 | return; |
| 7560 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7561 | |
| 7562 | if (!inode_evicting) |
| 7563 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 7564 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7565 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7566 | /* |
| 7567 | * IO on this page will never be started, so we need |
| 7568 | * to account for any ordered extents now |
| 7569 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7570 | if (!inode_evicting) |
| 7571 | clear_extent_bit(tree, page_start, page_end, |
| 7572 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7573 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7574 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 7575 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7576 | /* |
| 7577 | * whoever cleared the private bit is responsible |
| 7578 | * for the finish_ordered_io |
| 7579 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 7580 | if (TestClearPagePrivate2(page)) { |
| 7581 | struct btrfs_ordered_inode_tree *tree; |
| 7582 | u64 new_len; |
| 7583 | |
| 7584 | tree = &BTRFS_I(inode)->ordered_tree; |
| 7585 | |
| 7586 | spin_lock_irq(&tree->lock); |
| 7587 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 7588 | new_len = page_start - ordered->file_offset; |
| 7589 | if (new_len < ordered->truncated_len) |
| 7590 | ordered->truncated_len = new_len; |
| 7591 | spin_unlock_irq(&tree->lock); |
| 7592 | |
| 7593 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 7594 | page_start, |
| 7595 | PAGE_CACHE_SIZE, 1)) |
| 7596 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7597 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7598 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7599 | if (!inode_evicting) { |
| 7600 | cached_state = NULL; |
| 7601 | lock_extent_bits(tree, page_start, page_end, 0, |
| 7602 | &cached_state); |
| 7603 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7604 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7605 | |
| 7606 | if (!inode_evicting) { |
| 7607 | clear_extent_bit(tree, page_start, page_end, |
| 7608 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 7609 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 7610 | EXTENT_DEFRAG, 1, 1, |
| 7611 | &cached_state, GFP_NOFS); |
| 7612 | |
| 7613 | __btrfs_releasepage(page, GFP_NOFS); |
| 7614 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7615 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7616 | ClearPageChecked(page); |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7617 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7b | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7618 | ClearPagePrivate(page); |
| 7619 | set_page_private(page, 0); |
| 7620 | page_cache_release(page); |
| 7621 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7622 | } |
| 7623 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7624 | /* |
| 7625 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7626 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7627 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7628 | * for a written page which means we get ENOSPC checking when writing into |
| 7629 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7630 | * support these features. |
| 7631 | * |
| 7632 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7633 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7634 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7635 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7636 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7637 | * unlock the page. |
| 7638 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7639 | 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] | 7640 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7641 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7642 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7643 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7644 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7645 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7646 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7647 | char *kaddr; |
| 7648 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7649 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7650 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7651 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7652 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7653 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7654 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7655 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7656 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7657 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7658 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7659 | reserved = 1; |
| 7660 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7661 | if (ret) { |
| 7662 | if (ret == -ENOMEM) |
| 7663 | ret = VM_FAULT_OOM; |
| 7664 | else /* -ENOSPC, -EIO, etc */ |
| 7665 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7666 | if (reserved) |
| 7667 | goto out; |
| 7668 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7669 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7670 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7671 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7672 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7673 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7674 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7675 | page_start = page_offset(page); |
| 7676 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7677 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7678 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7679 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7680 | /* page got truncated out from underneath us */ |
| 7681 | goto out_unlock; |
| 7682 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7683 | wait_on_page_writeback(page); |
| 7684 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7685 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7686 | set_page_extent_mapped(page); |
| 7687 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7688 | /* |
| 7689 | * we can't set the delalloc bits if there are pending ordered |
| 7690 | * extents. Drop our locks and wait for them to finish |
| 7691 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7692 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7693 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7694 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7695 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7696 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7697 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7698 | btrfs_put_ordered_extent(ordered); |
| 7699 | goto again; |
| 7700 | } |
| 7701 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7702 | /* |
| 7703 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7704 | * if it was already dirty, so for space accounting reasons we need to |
| 7705 | * clear any delalloc bits for the range we are fixing to save. There |
| 7706 | * is probably a better way to do this, but for now keep consistent with |
| 7707 | * prepare_pages in the normal write path. |
| 7708 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7709 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7710 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7711 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7712 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7713 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7714 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7715 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7716 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7717 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7718 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7719 | ret = VM_FAULT_SIGBUS; |
| 7720 | goto out_unlock; |
| 7721 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7722 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7723 | |
| 7724 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7725 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7726 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7727 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7728 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7729 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7730 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7731 | kaddr = kmap(page); |
| 7732 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7733 | flush_dcache_page(page); |
| 7734 | kunmap(page); |
| 7735 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7736 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7737 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7738 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7739 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7740 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7741 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7742 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7743 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7744 | 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] | 7745 | |
| 7746 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7747 | if (!ret) { |
| 7748 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7749 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7750 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7751 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7752 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7753 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7754 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7755 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7756 | return ret; |
| 7757 | } |
| 7758 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7759 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7760 | { |
| 7761 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7762 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 7763 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7764 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7765 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7766 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7767 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7768 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 7769 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 7770 | (u64)-1); |
| 7771 | if (ret) |
| 7772 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7773 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7774 | /* |
| 7775 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7776 | * 3 things going on here |
| 7777 | * |
| 7778 | * 1) We need to reserve space for our orphan item and the space to |
| 7779 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7780 | * orphan item because we didn't reserve space to remove it. |
| 7781 | * |
| 7782 | * 2) We need to reserve space to update our inode. |
| 7783 | * |
| 7784 | * 3) We need to have something to cache all the space that is going to |
| 7785 | * be free'd up by the truncate operation, but also have some slack |
| 7786 | * space reserved in case it uses space during the truncate (thank you |
| 7787 | * very much snapshotting). |
| 7788 | * |
| 7789 | * And we need these to all be seperate. The fact is we can use alot of |
| 7790 | * space doing the truncate, and we have no earthly idea how much space |
| 7791 | * we will use, so we need the truncate reservation to be seperate so it |
| 7792 | * doesn't end up using space reserved for updating the inode or |
| 7793 | * removing the orphan item. We also need to be able to stop the |
| 7794 | * transaction and start a new one, which means we need to be able to |
| 7795 | * update the inode several times, and we have no idea of knowing how |
| 7796 | * many times that will be, so we can't just reserve 1 item for the |
| 7797 | * entirety of the opration, so that has to be done seperately as well. |
| 7798 | * Then there is the orphan item, which does indeed need to be held on |
| 7799 | * to for the whole operation, and we need nobody to touch this reserved |
| 7800 | * space except the orphan code. |
| 7801 | * |
| 7802 | * So that leaves us with |
| 7803 | * |
| 7804 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7805 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7806 | * transaction reservation. |
| 7807 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7808 | * updating the inode. |
| 7809 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7810 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7811 | if (!rsv) |
| 7812 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7813 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7814 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7815 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7816 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7817 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7818 | * 1 for updating the inode. |
| 7819 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7820 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7821 | if (IS_ERR(trans)) { |
| 7822 | err = PTR_ERR(trans); |
| 7823 | goto out; |
| 7824 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7825 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7826 | /* Migrate the slack space for the truncate to our reserve */ |
| 7827 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7828 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7829 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7830 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7831 | /* |
| 7832 | * setattr is responsible for setting the ordered_data_close flag, |
| 7833 | * but that is only tested during the last file release. That |
| 7834 | * could happen well after the next commit, leaving a great big |
| 7835 | * window where new writes may get lost if someone chooses to write |
| 7836 | * to this file after truncating to zero |
| 7837 | * |
| 7838 | * The inode doesn't have any dirty data here, and so if we commit |
| 7839 | * this is a noop. If someone immediately starts writing to the inode |
| 7840 | * it is very likely we'll catch some of their writes in this |
| 7841 | * transaction, and the commit will find this file on the ordered |
| 7842 | * data list with good things to send down. |
| 7843 | * |
| 7844 | * This is a best effort solution, there is still a window where |
| 7845 | * using truncate to replace the contents of the file will |
| 7846 | * end up with a zero length file after a crash. |
| 7847 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7848 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 7849 | &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7850 | btrfs_add_ordered_operation(trans, root, inode); |
| 7851 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7852 | /* |
| 7853 | * So if we truncate and then write and fsync we normally would just |
| 7854 | * write the extents that changed, which is a problem if we need to |
| 7855 | * first truncate that entire inode. So set this flag so we write out |
| 7856 | * all of the extents in the inode to the sync log so we're completely |
| 7857 | * safe. |
| 7858 | */ |
| 7859 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7860 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7861 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7862 | while (1) { |
| 7863 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7864 | inode->i_size, |
| 7865 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7866 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7867 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7868 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7869 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7870 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7871 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7872 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7873 | if (ret) { |
| 7874 | err = ret; |
| 7875 | break; |
| 7876 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7877 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7878 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7879 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7880 | |
| 7881 | trans = btrfs_start_transaction(root, 2); |
| 7882 | if (IS_ERR(trans)) { |
| 7883 | ret = err = PTR_ERR(trans); |
| 7884 | trans = NULL; |
| 7885 | break; |
| 7886 | } |
| 7887 | |
| 7888 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7889 | rsv, min_size); |
| 7890 | BUG_ON(ret); /* shouldn't happen */ |
| 7891 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7895 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7896 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7897 | if (ret) |
| 7898 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7899 | } |
| 7900 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7901 | if (trans) { |
| 7902 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 7903 | ret = btrfs_update_inode(trans, root, inode); |
| 7904 | if (ret && !err) |
| 7905 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 7906 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7907 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7908 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 7909 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7910 | |
| 7911 | out: |
| 7912 | btrfs_free_block_rsv(root, rsv); |
| 7913 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7914 | if (ret && !err) |
| 7915 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7916 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7917 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7918 | } |
| 7919 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7920 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 7921 | * create a new subvolume directory/inode (helper for the ioctl). |
| 7922 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7923 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 7924 | struct btrfs_root *new_root, |
| 7925 | struct btrfs_root *parent_root, |
| 7926 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7927 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7928 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7929 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 7930 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7931 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 7932 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 7933 | new_dirid, new_dirid, |
| 7934 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 7935 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7936 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 7937 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7938 | inode->i_op = &btrfs_dir_inode_operations; |
| 7939 | inode->i_fop = &btrfs_dir_file_operations; |
| 7940 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 7941 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7942 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 7943 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 7944 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 7945 | if (err) |
| 7946 | btrfs_err(new_root->fs_info, |
| 7947 | "error inheriting subvolume %llu properties: %d\n", |
| 7948 | new_root->root_key.objectid, err); |
| 7949 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7950 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 7951 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7952 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 7953 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7954 | } |
| 7955 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7956 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 7957 | { |
| 7958 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7959 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7960 | |
| 7961 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 7962 | if (!ei) |
| 7963 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7964 | |
| 7965 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7966 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 7967 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7968 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7969 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7970 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7971 | ei->disk_i_size = 0; |
| 7972 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 7973 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7974 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 7975 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7976 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7977 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7978 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 7979 | spin_lock_init(&ei->lock); |
| 7980 | ei->outstanding_extents = 0; |
| 7981 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7982 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 7983 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 7984 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7985 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 7986 | ei->delayed_node = NULL; |
| 7987 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7988 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 7989 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 7990 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 7991 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7992 | ei->io_tree.track_uptodate = 1; |
| 7993 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7994 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7995 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 7996 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7997 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7998 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7999 | INIT_LIST_HEAD(&ei->ordered_operations); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8000 | RB_CLEAR_NODE(&ei->rb_node); |
| 8001 | |
| 8002 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8003 | } |
| 8004 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8005 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8006 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8007 | { |
| 8008 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8009 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8010 | } |
| 8011 | #endif |
| 8012 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8013 | static void btrfs_i_callback(struct rcu_head *head) |
| 8014 | { |
| 8015 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8016 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8017 | } |
| 8018 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8019 | void btrfs_destroy_inode(struct inode *inode) |
| 8020 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8021 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8022 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8023 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8024 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8025 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8026 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8027 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8028 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8029 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8030 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8031 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8032 | * This can happen where we create an inode, but somebody else also |
| 8033 | * created the same inode and we need to destroy the one we already |
| 8034 | * created. |
| 8035 | */ |
| 8036 | if (!root) |
| 8037 | goto free; |
| 8038 | |
| 8039 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8040 | * Make sure we're properly removed from the ordered operation |
| 8041 | * lists. |
| 8042 | */ |
| 8043 | smp_mb(); |
| 8044 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 8045 | spin_lock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8046 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 8047 | spin_unlock(&root->fs_info->ordered_root_lock); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8048 | } |
| 8049 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8050 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8051 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8052 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8053 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8054 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8055 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8056 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8057 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8058 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8059 | if (!ordered) |
| 8060 | break; |
| 8061 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8062 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8063 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8064 | btrfs_remove_ordered_extent(inode, ordered); |
| 8065 | btrfs_put_ordered_extent(ordered); |
| 8066 | btrfs_put_ordered_extent(ordered); |
| 8067 | } |
| 8068 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8069 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8070 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8071 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8072 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8073 | } |
| 8074 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8075 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8076 | { |
| 8077 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8078 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8079 | if (root == NULL) |
| 8080 | return 1; |
| 8081 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8082 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8083 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8084 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8085 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8086 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8087 | } |
| 8088 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8089 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8090 | { |
| 8091 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8092 | |
| 8093 | inode_init_once(&ei->vfs_inode); |
| 8094 | } |
| 8095 | |
| 8096 | void btrfs_destroy_cachep(void) |
| 8097 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8098 | /* |
| 8099 | * Make sure all delayed rcu free inodes are flushed before we |
| 8100 | * destroy cache. |
| 8101 | */ |
| 8102 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8103 | if (btrfs_inode_cachep) |
| 8104 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8105 | if (btrfs_trans_handle_cachep) |
| 8106 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8107 | if (btrfs_transaction_cachep) |
| 8108 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8109 | if (btrfs_path_cachep) |
| 8110 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8111 | if (btrfs_free_space_cachep) |
| 8112 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8113 | if (btrfs_delalloc_work_cachep) |
| 8114 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8115 | } |
| 8116 | |
| 8117 | int btrfs_init_cachep(void) |
| 8118 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8119 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8120 | sizeof(struct btrfs_inode), 0, |
| 8121 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8122 | if (!btrfs_inode_cachep) |
| 8123 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8124 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8125 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8126 | sizeof(struct btrfs_trans_handle), 0, |
| 8127 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8128 | if (!btrfs_trans_handle_cachep) |
| 8129 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8130 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8131 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8132 | sizeof(struct btrfs_transaction), 0, |
| 8133 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8134 | if (!btrfs_transaction_cachep) |
| 8135 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8136 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8137 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8138 | sizeof(struct btrfs_path), 0, |
| 8139 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8140 | if (!btrfs_path_cachep) |
| 8141 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8142 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8143 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8144 | sizeof(struct btrfs_free_space), 0, |
| 8145 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8146 | if (!btrfs_free_space_cachep) |
| 8147 | goto fail; |
| 8148 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8149 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8150 | sizeof(struct btrfs_delalloc_work), 0, |
| 8151 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8152 | NULL); |
| 8153 | if (!btrfs_delalloc_work_cachep) |
| 8154 | goto fail; |
| 8155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8156 | return 0; |
| 8157 | fail: |
| 8158 | btrfs_destroy_cachep(); |
| 8159 | return -ENOMEM; |
| 8160 | } |
| 8161 | |
| 8162 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8163 | struct dentry *dentry, struct kstat *stat) |
| 8164 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8165 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8166 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8167 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8168 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8169 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8170 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8171 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8172 | |
| 8173 | spin_lock(&BTRFS_I(inode)->lock); |
| 8174 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8175 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8176 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8177 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8178 | return 0; |
| 8179 | } |
| 8180 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8181 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8182 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8183 | { |
| 8184 | struct btrfs_trans_handle *trans; |
| 8185 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8186 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8187 | struct inode *new_inode = new_dentry->d_inode; |
| 8188 | struct inode *old_inode = old_dentry->d_inode; |
| 8189 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8190 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8191 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8192 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8193 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8194 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8195 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8196 | return -EPERM; |
| 8197 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8198 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8199 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8200 | return -EXDEV; |
| 8201 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8202 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8203 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8204 | return -ENOTEMPTY; |
| 8205 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8206 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8207 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8208 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8209 | |
| 8210 | |
| 8211 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8212 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8213 | new_dentry->d_name.name, |
| 8214 | new_dentry->d_name.len); |
| 8215 | |
| 8216 | if (ret) { |
| 8217 | if (ret == -EEXIST) { |
| 8218 | /* we shouldn't get |
| 8219 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8220 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8221 | return ret; |
| 8222 | } |
| 8223 | } else { |
| 8224 | /* maybe -EOVERFLOW */ |
| 8225 | return ret; |
| 8226 | } |
| 8227 | } |
| 8228 | ret = 0; |
| 8229 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8230 | /* |
| 8231 | * we're using rename to replace one file with another. |
| 8232 | * and the replacement file is large. Start IO on it now so |
| 8233 | * we don't add too much work to the end of the transaction |
| 8234 | */ |
Bartlomiej Zolnierkiewicz | 4baf8c9 | 2009-08-07 13:47:08 -0400 | [diff] [blame] | 8235 | 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] | 8236 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 8237 | filemap_flush(old_inode->i_mapping); |
| 8238 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8239 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8240 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8241 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8242 | /* |
| 8243 | * We want to reserve the absolute worst case amount of items. So if |
| 8244 | * both inodes are subvols and we need to unlink them then that would |
| 8245 | * require 4 item modifications, but if they are both normal inodes it |
| 8246 | * would require 5 item modifications, so we'll assume their normal |
| 8247 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8248 | * should cover the worst case number of items we'll modify. |
| 8249 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8250 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8251 | if (IS_ERR(trans)) { |
| 8252 | ret = PTR_ERR(trans); |
| 8253 | goto out_notrans; |
| 8254 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8255 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8256 | if (dest != root) |
| 8257 | btrfs_record_root_in_trans(trans, dest); |
| 8258 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8259 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8260 | if (ret) |
| 8261 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8262 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8263 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8264 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8265 | /* force full log commit if subvolume involved. */ |
| 8266 | root->fs_info->last_trans_log_full_commit = trans->transid; |
| 8267 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8268 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8269 | new_dentry->d_name.name, |
| 8270 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8271 | old_ino, |
| 8272 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8273 | if (ret) |
| 8274 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8275 | /* |
| 8276 | * this is an ugly little race, but the rename is required |
| 8277 | * to make sure that if we crash, the inode is either at the |
| 8278 | * old name or the new one. pinning the log transaction lets |
| 8279 | * us make sure we don't allow a log commit to come in after |
| 8280 | * we unlink the name but before we add the new name back in. |
| 8281 | */ |
| 8282 | btrfs_pin_log_trans(root); |
| 8283 | } |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8284 | /* |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8285 | * make sure the inode gets flushed if it is replacing |
| 8286 | * something. |
| 8287 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8288 | 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] | 8289 | btrfs_add_ordered_operation(trans, root, old_inode); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8290 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8291 | inode_inc_iversion(old_dir); |
| 8292 | inode_inc_iversion(new_dir); |
| 8293 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8294 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8295 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8296 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8297 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8298 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8299 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8300 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8301 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8302 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8303 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8304 | old_dentry->d_name.name, |
| 8305 | old_dentry->d_name.len); |
| 8306 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8307 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8308 | old_dentry->d_inode, |
| 8309 | old_dentry->d_name.name, |
| 8310 | old_dentry->d_name.len); |
| 8311 | if (!ret) |
| 8312 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8313 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8314 | if (ret) { |
| 8315 | btrfs_abort_transaction(trans, root, ret); |
| 8316 | goto out_fail; |
| 8317 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8318 | |
| 8319 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8320 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8321 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8322 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8323 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8324 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8325 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8326 | root_objectid, |
| 8327 | new_dentry->d_name.name, |
| 8328 | new_dentry->d_name.len); |
| 8329 | BUG_ON(new_inode->i_nlink == 0); |
| 8330 | } else { |
| 8331 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8332 | new_dentry->d_inode, |
| 8333 | new_dentry->d_name.name, |
| 8334 | new_dentry->d_name.len); |
| 8335 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8336 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8337 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8338 | if (ret) { |
| 8339 | btrfs_abort_transaction(trans, root, ret); |
| 8340 | goto out_fail; |
| 8341 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8342 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8343 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8344 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8345 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8346 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8347 | if (ret) { |
| 8348 | btrfs_abort_transaction(trans, root, ret); |
| 8349 | goto out_fail; |
| 8350 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8351 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8352 | if (old_inode->i_nlink == 1) |
| 8353 | BTRFS_I(old_inode)->dir_index = index; |
| 8354 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8355 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8356 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8357 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8358 | btrfs_end_log_trans(root); |
| 8359 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8360 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8361 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8362 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8363 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8364 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8365 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8366 | return ret; |
| 8367 | } |
| 8368 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8369 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8370 | { |
| 8371 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8372 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8373 | |
| 8374 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8375 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8376 | inode = delalloc_work->inode; |
| 8377 | if (delalloc_work->wait) { |
| 8378 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 8379 | } else { |
| 8380 | filemap_flush(inode->i_mapping); |
| 8381 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8382 | &BTRFS_I(inode)->runtime_flags)) |
| 8383 | filemap_flush(inode->i_mapping); |
| 8384 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8385 | |
| 8386 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8387 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8388 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8389 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8390 | complete(&delalloc_work->completion); |
| 8391 | } |
| 8392 | |
| 8393 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8394 | int wait, int delay_iput) |
| 8395 | { |
| 8396 | struct btrfs_delalloc_work *work; |
| 8397 | |
| 8398 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8399 | if (!work) |
| 8400 | return NULL; |
| 8401 | |
| 8402 | init_completion(&work->completion); |
| 8403 | INIT_LIST_HEAD(&work->list); |
| 8404 | work->inode = inode; |
| 8405 | work->wait = wait; |
| 8406 | work->delay_iput = delay_iput; |
| 8407 | work->work.func = btrfs_run_delalloc_work; |
| 8408 | |
| 8409 | return work; |
| 8410 | } |
| 8411 | |
| 8412 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8413 | { |
| 8414 | wait_for_completion(&work->completion); |
| 8415 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8416 | } |
| 8417 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8418 | /* |
| 8419 | * some fairly slow code that needs optimization. This walks the list |
| 8420 | * of all the inodes with pending delalloc and forces them to disk. |
| 8421 | */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8422 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8423 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8424 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8425 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8426 | struct btrfs_delalloc_work *work, *next; |
| 8427 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8428 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8429 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8430 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8431 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8432 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8433 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8434 | spin_lock(&root->delalloc_lock); |
| 8435 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8436 | while (!list_empty(&splice)) { |
| 8437 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8438 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8439 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8440 | list_move_tail(&binode->delalloc_inodes, |
| 8441 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8442 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8443 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8444 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8445 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8446 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8447 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8448 | |
| 8449 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8450 | if (unlikely(!work)) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 8451 | if (delay_iput) |
| 8452 | btrfs_add_delayed_iput(inode); |
| 8453 | else |
| 8454 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8455 | ret = -ENOMEM; |
| 8456 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8457 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8458 | list_add_tail(&work->list, &works); |
| 8459 | btrfs_queue_worker(&root->fs_info->flush_workers, |
| 8460 | &work->work); |
| 8461 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8462 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8463 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8464 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8465 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8466 | |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8467 | list_for_each_entry_safe(work, next, &works, list) { |
| 8468 | list_del_init(&work->list); |
| 8469 | btrfs_wait_and_free_delalloc_work(work); |
| 8470 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8471 | return 0; |
| 8472 | out: |
| 8473 | list_for_each_entry_safe(work, next, &works, list) { |
| 8474 | list_del_init(&work->list); |
| 8475 | btrfs_wait_and_free_delalloc_work(work); |
| 8476 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8477 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8478 | if (!list_empty_careful(&splice)) { |
| 8479 | spin_lock(&root->delalloc_lock); |
| 8480 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 8481 | spin_unlock(&root->delalloc_lock); |
| 8482 | } |
| 8483 | return ret; |
| 8484 | } |
| 8485 | |
| 8486 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 8487 | { |
| 8488 | int ret; |
| 8489 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 8490 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8491 | return -EROFS; |
| 8492 | |
| 8493 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8494 | /* |
| 8495 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8496 | * we have to make sure the IO is actually started and that |
| 8497 | * ordered extents get created before we return |
| 8498 | */ |
| 8499 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8500 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8501 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8502 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8503 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8504 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8505 | } |
| 8506 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8507 | return ret; |
| 8508 | } |
| 8509 | |
Miao Xie | 91aef86 | 2013-11-04 23:13:26 +0800 | [diff] [blame] | 8510 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8511 | { |
| 8512 | struct btrfs_root *root; |
| 8513 | struct list_head splice; |
| 8514 | int ret; |
| 8515 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 8516 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8517 | return -EROFS; |
| 8518 | |
| 8519 | INIT_LIST_HEAD(&splice); |
| 8520 | |
| 8521 | spin_lock(&fs_info->delalloc_root_lock); |
| 8522 | list_splice_init(&fs_info->delalloc_roots, &splice); |
| 8523 | while (!list_empty(&splice)) { |
| 8524 | root = list_first_entry(&splice, struct btrfs_root, |
| 8525 | delalloc_root); |
| 8526 | root = btrfs_grab_fs_root(root); |
| 8527 | BUG_ON(!root); |
| 8528 | list_move_tail(&root->delalloc_root, |
| 8529 | &fs_info->delalloc_roots); |
| 8530 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8531 | |
| 8532 | ret = __start_delalloc_inodes(root, delay_iput); |
| 8533 | btrfs_put_fs_root(root); |
| 8534 | if (ret) |
| 8535 | goto out; |
| 8536 | |
| 8537 | spin_lock(&fs_info->delalloc_root_lock); |
| 8538 | } |
| 8539 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8540 | |
| 8541 | atomic_inc(&fs_info->async_submit_draining); |
| 8542 | while (atomic_read(&fs_info->nr_async_submits) || |
| 8543 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 8544 | wait_event(fs_info->async_submit_wait, |
| 8545 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 8546 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 8547 | } |
| 8548 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8549 | return 0; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8550 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8551 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8552 | spin_lock(&fs_info->delalloc_root_lock); |
| 8553 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 8554 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8555 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8556 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8557 | } |
| 8558 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8559 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8560 | const char *symname) |
| 8561 | { |
| 8562 | struct btrfs_trans_handle *trans; |
| 8563 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8564 | struct btrfs_path *path; |
| 8565 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8566 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8567 | int err; |
| 8568 | int drop_inode = 0; |
| 8569 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8570 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8571 | int name_len; |
| 8572 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8573 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8574 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8575 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8576 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8577 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8578 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8579 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8580 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8581 | /* |
| 8582 | * 2 items for inode item and ref |
| 8583 | * 2 items for dir items |
| 8584 | * 1 item for xattr if selinux is on |
| 8585 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8586 | trans = btrfs_start_transaction(root, 5); |
| 8587 | if (IS_ERR(trans)) |
| 8588 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8589 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8590 | err = btrfs_find_free_ino(root, &objectid); |
| 8591 | if (err) |
| 8592 | goto out_unlock; |
| 8593 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8594 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8595 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8596 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8597 | if (IS_ERR(inode)) { |
| 8598 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8599 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8600 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8601 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8602 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8603 | if (err) { |
| 8604 | drop_inode = 1; |
| 8605 | goto out_unlock; |
| 8606 | } |
| 8607 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8608 | /* |
| 8609 | * If the active LSM wants to access the inode during |
| 8610 | * d_instantiate it needs these. Smack checks to see |
| 8611 | * if the filesystem supports xattrs by looking at the |
| 8612 | * ops vector. |
| 8613 | */ |
| 8614 | inode->i_fop = &btrfs_file_operations; |
| 8615 | inode->i_op = &btrfs_file_inode_operations; |
| 8616 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8617 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8618 | if (err) |
| 8619 | drop_inode = 1; |
| 8620 | else { |
| 8621 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8622 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8623 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8624 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8625 | if (drop_inode) |
| 8626 | goto out_unlock; |
| 8627 | |
| 8628 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8629 | if (!path) { |
| 8630 | err = -ENOMEM; |
| 8631 | drop_inode = 1; |
| 8632 | goto out_unlock; |
| 8633 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8634 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8635 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8636 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8637 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8638 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8639 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8640 | if (err) { |
| 8641 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8642 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8643 | goto out_unlock; |
| 8644 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8645 | leaf = path->nodes[0]; |
| 8646 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8647 | struct btrfs_file_extent_item); |
| 8648 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8649 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8650 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8651 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8652 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8653 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8654 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8655 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8656 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8657 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8658 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8659 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8660 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8661 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8662 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8663 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8664 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8665 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8666 | err = btrfs_update_inode(trans, root, inode); |
| 8667 | if (err) |
| 8668 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8669 | |
| 8670 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8671 | if (!err) |
| 8672 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8673 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8674 | if (drop_inode) { |
| 8675 | inode_dec_link_count(inode); |
| 8676 | iput(inode); |
| 8677 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8678 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8679 | return err; |
| 8680 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8681 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8682 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8683 | u64 start, u64 num_bytes, u64 min_size, |
| 8684 | loff_t actual_len, u64 *alloc_hint, |
| 8685 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8686 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8687 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8688 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8689 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8690 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8691 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8692 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8693 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8694 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8695 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8696 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8697 | if (trans) |
| 8698 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8699 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8700 | if (own_trans) { |
| 8701 | trans = btrfs_start_transaction(root, 3); |
| 8702 | if (IS_ERR(trans)) { |
| 8703 | ret = PTR_ERR(trans); |
| 8704 | break; |
| 8705 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8706 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8707 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8708 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8709 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 8710 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
| 8711 | *alloc_hint, &ins, 1); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8712 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8713 | if (own_trans) |
| 8714 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8715 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8716 | } |
| 8717 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8718 | ret = insert_reserved_file_extent(trans, inode, |
| 8719 | cur_offset, ins.objectid, |
| 8720 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8721 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8722 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8723 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8724 | btrfs_free_reserved_extent(root, ins.objectid, |
| 8725 | ins.offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8726 | btrfs_abort_transaction(trans, root, ret); |
| 8727 | if (own_trans) |
| 8728 | btrfs_end_transaction(trans, root); |
| 8729 | break; |
| 8730 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8731 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8732 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8733 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8734 | em = alloc_extent_map(); |
| 8735 | if (!em) { |
| 8736 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8737 | &BTRFS_I(inode)->runtime_flags); |
| 8738 | goto next; |
| 8739 | } |
| 8740 | |
| 8741 | em->start = cur_offset; |
| 8742 | em->orig_start = cur_offset; |
| 8743 | em->len = ins.offset; |
| 8744 | em->block_start = ins.objectid; |
| 8745 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8746 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8747 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8748 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8749 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8750 | em->generation = trans->transid; |
| 8751 | |
| 8752 | while (1) { |
| 8753 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8754 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8755 | write_unlock(&em_tree->lock); |
| 8756 | if (ret != -EEXIST) |
| 8757 | break; |
| 8758 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8759 | cur_offset + ins.offset - 1, |
| 8760 | 0); |
| 8761 | } |
| 8762 | free_extent_map(em); |
| 8763 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8764 | num_bytes -= ins.offset; |
| 8765 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8766 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8767 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8768 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8769 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8770 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8771 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8772 | (actual_len > inode->i_size) && |
| 8773 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8774 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8775 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8776 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8777 | i_size = cur_offset; |
| 8778 | i_size_write(inode, i_size); |
| 8779 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8780 | } |
| 8781 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8782 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8783 | |
| 8784 | if (ret) { |
| 8785 | btrfs_abort_transaction(trans, root, ret); |
| 8786 | if (own_trans) |
| 8787 | btrfs_end_transaction(trans, root); |
| 8788 | break; |
| 8789 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8790 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8791 | if (own_trans) |
| 8792 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8793 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8794 | return ret; |
| 8795 | } |
| 8796 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8797 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8798 | u64 start, u64 num_bytes, u64 min_size, |
| 8799 | loff_t actual_len, u64 *alloc_hint) |
| 8800 | { |
| 8801 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8802 | min_size, actual_len, alloc_hint, |
| 8803 | NULL); |
| 8804 | } |
| 8805 | |
| 8806 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8807 | struct btrfs_trans_handle *trans, int mode, |
| 8808 | u64 start, u64 num_bytes, u64 min_size, |
| 8809 | loff_t actual_len, u64 *alloc_hint) |
| 8810 | { |
| 8811 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8812 | min_size, actual_len, alloc_hint, trans); |
| 8813 | } |
| 8814 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8815 | static int btrfs_set_page_dirty(struct page *page) |
| 8816 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8817 | return __set_page_dirty_nobuffers(page); |
| 8818 | } |
| 8819 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8820 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8821 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8822 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8823 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8824 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8825 | if (mask & MAY_WRITE && |
| 8826 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8827 | if (btrfs_root_readonly(root)) |
| 8828 | return -EROFS; |
| 8829 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8830 | return -EACCES; |
| 8831 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8832 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8833 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8834 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8835 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8836 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8837 | .lookup = btrfs_lookup, |
| 8838 | .create = btrfs_create, |
| 8839 | .unlink = btrfs_unlink, |
| 8840 | .link = btrfs_link, |
| 8841 | .mkdir = btrfs_mkdir, |
| 8842 | .rmdir = btrfs_rmdir, |
| 8843 | .rename = btrfs_rename, |
| 8844 | .symlink = btrfs_symlink, |
| 8845 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8846 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8847 | .setxattr = btrfs_setxattr, |
| 8848 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8849 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8850 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8851 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8852 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 8853 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 8854 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8855 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8856 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8857 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8858 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8859 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 8860 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 8861 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8862 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8863 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 8864 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8865 | .llseek = generic_file_llseek, |
| 8866 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 8867 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8868 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8869 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 8870 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8871 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 8872 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8873 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8874 | }; |
| 8875 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8876 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8877 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8878 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 8879 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8880 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8881 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 8882 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 8883 | .set_bit_hook = btrfs_set_bit_hook, |
| 8884 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8885 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 8886 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 8887 | }; |
| 8888 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 8889 | /* |
| 8890 | * btrfs doesn't support the bmap operation because swapfiles |
| 8891 | * use bmap to make a mapping of extents in the file. They assume |
| 8892 | * these extents won't change over the life of the file and they |
| 8893 | * use the bmap result to do IO directly to the drive. |
| 8894 | * |
| 8895 | * the btrfs bmap call would return logical addresses that aren't |
| 8896 | * suitable for IO and they also will change frequently as COW |
| 8897 | * operations happen. So, swapfile + btrfs == corruption. |
| 8898 | * |
| 8899 | * For now we're avoiding this by dropping bmap. |
| 8900 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8901 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8902 | .readpage = btrfs_readpage, |
| 8903 | .writepage = btrfs_writepage, |
Chris Mason | b293f02 | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 8904 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 8905 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8906 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8907 | .invalidatepage = btrfs_invalidatepage, |
| 8908 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8909 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 8910 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8911 | }; |
| 8912 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 8913 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8914 | .readpage = btrfs_readpage, |
| 8915 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 8916 | .invalidatepage = btrfs_invalidatepage, |
| 8917 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8918 | }; |
| 8919 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8920 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8921 | .getattr = btrfs_getattr, |
| 8922 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8923 | .setxattr = btrfs_setxattr, |
| 8924 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 8925 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8926 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8927 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 8928 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8929 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 8930 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8931 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8932 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8933 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8934 | .getattr = btrfs_getattr, |
| 8935 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8936 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8937 | .setxattr = btrfs_setxattr, |
| 8938 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8939 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8940 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 8941 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 8942 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8943 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8944 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8945 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8946 | .readlink = generic_readlink, |
| 8947 | .follow_link = page_follow_link_light, |
| 8948 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 8949 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 8950 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8951 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 8952 | .setxattr = btrfs_setxattr, |
| 8953 | .getxattr = btrfs_getxattr, |
| 8954 | .listxattr = btrfs_listxattr, |
| 8955 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 8956 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8957 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8958 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 8959 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8960 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 8961 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8962 | }; |