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 | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 128 | static 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 | |
Wang Shilong | 68bb462 | 2014-04-01 18:01:42 +0800 | [diff] [blame] | 397 | /* |
| 398 | * skip compression for a small file range(<=blocksize) that |
| 399 | * isn't an inline extent, since it dosen't save disk space at all. |
| 400 | */ |
| 401 | if ((end - start + 1) <= blocksize && |
| 402 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
| 403 | goto cleanup_and_bail_uncompressed; |
| 404 | |
Chris Mason | 42dc7ba | 2008-12-15 11:44:56 -0500 | [diff] [blame] | 405 | actual_end = min_t(u64, isize, end + 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 406 | again: |
| 407 | will_compress = 0; |
| 408 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; |
| 409 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); |
| 410 | |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 411 | /* |
| 412 | * we don't want to send crud past the end of i_size through |
| 413 | * compression, that's just a waste of CPU time. So, if the |
| 414 | * end of the file is before the start of our current |
| 415 | * requested range of bytes, we bail out to the uncompressed |
| 416 | * cleanup code that can deal with all of this. |
| 417 | * |
| 418 | * It isn't really the fastest way to fix things, but this is a |
| 419 | * very uncommon corner. |
| 420 | */ |
| 421 | if (actual_end <= start) |
| 422 | goto cleanup_and_bail_uncompressed; |
| 423 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 424 | total_compressed = actual_end - start; |
| 425 | |
| 426 | /* we want to make sure that amount of ram required to uncompress |
| 427 | * an extent is reasonable, so we limit the total size in ram |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 428 | * of a compressed extent to 128k. This is a crucial number |
| 429 | * because it also controls how easily we can spread reads across |
| 430 | * cpus for decompression. |
| 431 | * |
| 432 | * We also want to make sure the amount of IO required to do |
| 433 | * a random read is reasonably small, so we limit the size of |
| 434 | * a compressed extent to 128k. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 435 | */ |
| 436 | total_compressed = min(total_compressed, max_uncompressed); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 437 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 438 | num_bytes = max(blocksize, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 439 | total_in = 0; |
| 440 | ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 441 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 442 | /* |
| 443 | * we do compression for mount -o compress and when the |
| 444 | * inode has not been flagged as nocompress. This flag can |
| 445 | * change at any time if we discover bad compression ratios. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 446 | */ |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 447 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 448 | (btrfs_test_opt(root, COMPRESS) || |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 449 | (BTRFS_I(inode)->force_compress) || |
| 450 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 451 | WARN_ON(pages); |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 452 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 453 | if (!pages) { |
| 454 | /* just bail out to the uncompressed code */ |
| 455 | goto cont; |
| 456 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 457 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 458 | if (BTRFS_I(inode)->force_compress) |
| 459 | compress_type = BTRFS_I(inode)->force_compress; |
| 460 | |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 461 | /* |
| 462 | * we need to call clear_page_dirty_for_io on each |
| 463 | * page in the range. Otherwise applications with the file |
| 464 | * mmap'd can wander in and change the page contents while |
| 465 | * we are compressing them. |
| 466 | * |
| 467 | * If the compression fails for any reason, we set the pages |
| 468 | * dirty again later on. |
| 469 | */ |
| 470 | extent_range_clear_dirty_for_io(inode, start, end); |
| 471 | redirty = 1; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 472 | ret = btrfs_compress_pages(compress_type, |
| 473 | inode->i_mapping, start, |
| 474 | total_compressed, pages, |
| 475 | nr_pages, &nr_pages_ret, |
| 476 | &total_in, |
| 477 | &total_compressed, |
| 478 | max_compressed); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 479 | |
| 480 | if (!ret) { |
| 481 | unsigned long offset = total_compressed & |
| 482 | (PAGE_CACHE_SIZE - 1); |
| 483 | struct page *page = pages[nr_pages_ret - 1]; |
| 484 | char *kaddr; |
| 485 | |
| 486 | /* zero the tail end of the last page, we might be |
| 487 | * sending it down to disk |
| 488 | */ |
| 489 | if (offset) { |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 490 | kaddr = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 491 | memset(kaddr + offset, 0, |
| 492 | PAGE_CACHE_SIZE - offset); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 493 | kunmap_atomic(kaddr); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 494 | } |
| 495 | will_compress = 1; |
| 496 | } |
| 497 | } |
Li Zefan | 560f7d7 | 2011-09-08 10:22:01 +0800 | [diff] [blame] | 498 | cont: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 499 | if (start == 0) { |
| 500 | /* lets try to make an inline extent */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 501 | if (ret || total_in < (actual_end - start)) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 502 | /* we didn't compress the entire range, try |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 503 | * to make an uncompressed inline extent. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 504 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 505 | ret = cow_file_range_inline(root, inode, start, end, |
| 506 | 0, 0, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 507 | } else { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 508 | /* try making a compressed inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 509 | ret = cow_file_range_inline(root, inode, start, end, |
Li Zefan | fe3f566 | 2011-03-28 08:30:38 +0000 | [diff] [blame] | 510 | total_compressed, |
| 511 | compress_type, pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 512 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 513 | if (ret <= 0) { |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 514 | unsigned long clear_flags = EXTENT_DELALLOC | |
| 515 | EXTENT_DEFRAG; |
| 516 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
| 517 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 518 | /* |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 519 | * inline extent creation worked or returned error, |
| 520 | * we don't need to create any more async work items. |
| 521 | * Unlock and free up our temp pages. |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 522 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 523 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 524 | clear_flags, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 525 | PAGE_CLEAR_DIRTY | |
| 526 | PAGE_SET_WRITEBACK | |
| 527 | PAGE_END_WRITEBACK); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 528 | goto free_pages_out; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (will_compress) { |
| 533 | /* |
| 534 | * we aren't doing an inline extent round the compressed size |
| 535 | * up to a block size boundary so the allocator does sane |
| 536 | * things |
| 537 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 538 | total_compressed = ALIGN(total_compressed, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * one last check to make sure the compression is really a |
| 542 | * win, compare the page count read with the blocks on disk |
| 543 | */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 544 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 545 | if (total_compressed >= total_in) { |
| 546 | will_compress = 0; |
| 547 | } else { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 548 | num_bytes = total_in; |
| 549 | } |
| 550 | } |
| 551 | if (!will_compress && pages) { |
| 552 | /* |
| 553 | * the compression code ran but failed to make things smaller, |
| 554 | * free any pages it allocated and our page pointer array |
| 555 | */ |
| 556 | for (i = 0; i < nr_pages_ret; i++) { |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 557 | WARN_ON(pages[i]->mapping); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 558 | page_cache_release(pages[i]); |
| 559 | } |
| 560 | kfree(pages); |
| 561 | pages = NULL; |
| 562 | total_compressed = 0; |
| 563 | nr_pages_ret = 0; |
| 564 | |
| 565 | /* flag the file so we don't compress in the future */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 566 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
| 567 | !(BTRFS_I(inode)->force_compress)) { |
Chris Mason | a555f81 | 2010-01-28 16:18:15 -0500 | [diff] [blame] | 568 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 569 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 570 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 571 | if (will_compress) { |
| 572 | *num_added += 1; |
| 573 | |
| 574 | /* the async work queues will take care of doing actual |
| 575 | * allocation on disk for these compressed pages, |
| 576 | * and will submit them to the elevator. |
| 577 | */ |
| 578 | add_async_extent(async_cow, start, num_bytes, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 579 | total_compressed, pages, nr_pages_ret, |
| 580 | compress_type); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 581 | |
Yan, Zheng | 24ae636 | 2010-12-06 07:02:36 +0000 | [diff] [blame] | 582 | if (start + num_bytes < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 583 | start += num_bytes; |
| 584 | pages = NULL; |
| 585 | cond_resched(); |
| 586 | goto again; |
| 587 | } |
| 588 | } else { |
Chris Mason | f03d9301f | 2009-02-04 09:31:06 -0500 | [diff] [blame] | 589 | cleanup_and_bail_uncompressed: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 590 | /* |
| 591 | * No compression, but we still need to write the pages in |
| 592 | * the file we've been given so far. redirty the locked |
| 593 | * page if it corresponds to our extent and set things up |
| 594 | * for the async work queue to run cow_file_range to do |
| 595 | * the normal delalloc dance |
| 596 | */ |
| 597 | if (page_offset(locked_page) >= start && |
| 598 | page_offset(locked_page) <= end) { |
| 599 | __set_page_dirty_nobuffers(locked_page); |
| 600 | /* unlocked later on in the async handlers */ |
| 601 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 602 | if (redirty) |
| 603 | extent_range_redirty_for_io(inode, start, end); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 604 | add_async_extent(async_cow, start, end - start + 1, |
| 605 | 0, NULL, 0, BTRFS_COMPRESS_NONE); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 606 | *num_added += 1; |
| 607 | } |
| 608 | |
| 609 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 610 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 611 | |
| 612 | free_pages_out: |
| 613 | for (i = 0; i < nr_pages_ret; i++) { |
| 614 | WARN_ON(pages[i]->mapping); |
| 615 | page_cache_release(pages[i]); |
| 616 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 617 | kfree(pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 618 | |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * phase two of compressed writeback. This is the ordered portion |
| 624 | * of the code, which only gets called in the order the work was |
| 625 | * queued. We walk all the async extents created by compress_file_range |
| 626 | * and send them down to the disk. |
| 627 | */ |
| 628 | static noinline int submit_compressed_extents(struct inode *inode, |
| 629 | struct async_cow *async_cow) |
| 630 | { |
| 631 | struct async_extent *async_extent; |
| 632 | u64 alloc_hint = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 633 | struct btrfs_key ins; |
| 634 | struct extent_map *em; |
| 635 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 636 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 637 | struct extent_io_tree *io_tree; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 638 | int ret = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 639 | |
| 640 | if (list_empty(&async_cow->extents)) |
| 641 | return 0; |
| 642 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 643 | again: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 644 | while (!list_empty(&async_cow->extents)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 645 | async_extent = list_entry(async_cow->extents.next, |
| 646 | struct async_extent, list); |
| 647 | list_del(&async_extent->list); |
| 648 | |
| 649 | io_tree = &BTRFS_I(inode)->io_tree; |
| 650 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 651 | retry: |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 652 | /* did the compression code fall back to uncompressed IO? */ |
| 653 | if (!async_extent->pages) { |
| 654 | int page_started = 0; |
| 655 | unsigned long nr_written = 0; |
| 656 | |
| 657 | lock_extent(io_tree, async_extent->start, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 658 | async_extent->start + |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 659 | async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 660 | |
| 661 | /* allocate blocks */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 662 | ret = cow_file_range(inode, async_cow->locked_page, |
| 663 | async_extent->start, |
| 664 | async_extent->start + |
| 665 | async_extent->ram_size - 1, |
| 666 | &page_started, &nr_written, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 667 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 668 | /* JDM XXX */ |
| 669 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 670 | /* |
| 671 | * if page_started, cow_file_range inserted an |
| 672 | * inline extent and took care of all the unlocking |
| 673 | * and IO for us. Otherwise, we need to submit |
| 674 | * all those pages down to the drive. |
| 675 | */ |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 676 | if (!page_started && !ret) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | extent_write_locked_range(io_tree, |
| 678 | inode, async_extent->start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 679 | async_extent->start + |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 680 | async_extent->ram_size - 1, |
| 681 | btrfs_get_extent, |
| 682 | WB_SYNC_ALL); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 683 | else if (ret) |
| 684 | unlock_page(async_cow->locked_page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 685 | kfree(async_extent); |
| 686 | cond_resched(); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | lock_extent(io_tree, async_extent->start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 691 | async_extent->start + async_extent->ram_size - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 692 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 693 | ret = btrfs_reserve_extent(root, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 694 | async_extent->compressed_size, |
| 695 | async_extent->compressed_size, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 696 | 0, alloc_hint, &ins, 1, 1); |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 697 | if (ret) { |
| 698 | int i; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 699 | |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 700 | for (i = 0; i < async_extent->nr_pages; i++) { |
| 701 | WARN_ON(async_extent->pages[i]->mapping); |
| 702 | page_cache_release(async_extent->pages[i]); |
| 703 | } |
| 704 | kfree(async_extent->pages); |
| 705 | async_extent->nr_pages = 0; |
| 706 | async_extent->pages = NULL; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 707 | |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 708 | if (ret == -ENOSPC) { |
| 709 | unlock_extent(io_tree, async_extent->start, |
| 710 | async_extent->start + |
| 711 | async_extent->ram_size - 1); |
Liu Bo | ce62003 | 2014-07-24 22:48:05 +0800 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * we need to redirty the pages if we decide to |
| 715 | * fallback to uncompressed IO, otherwise we |
| 716 | * will not submit these pages down to lower |
| 717 | * layers. |
| 718 | */ |
| 719 | extent_range_redirty_for_io(inode, |
| 720 | async_extent->start, |
| 721 | async_extent->start + |
| 722 | async_extent->ram_size - 1); |
| 723 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 724 | goto retry; |
Josef Bacik | fdf8e2e | 2013-06-14 16:58:23 -0400 | [diff] [blame] | 725 | } |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 726 | goto out_free; |
Josef Bacik | f5a84ee3 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 729 | /* |
| 730 | * here we're doing allocation and writeback of the |
| 731 | * compressed pages |
| 732 | */ |
| 733 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 734 | async_extent->start + |
| 735 | async_extent->ram_size - 1, 0); |
| 736 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 737 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 738 | if (!em) { |
| 739 | ret = -ENOMEM; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 740 | goto out_free_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 741 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 742 | em->start = async_extent->start; |
| 743 | em->len = async_extent->ram_size; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 744 | em->orig_start = em->start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 745 | em->mod_start = em->start; |
| 746 | em->mod_len = em->len; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 747 | |
| 748 | em->block_start = ins.objectid; |
| 749 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 750 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 751 | em->ram_bytes = async_extent->ram_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 752 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 753 | em->compress_type = async_extent->compress_type; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 754 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 755 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 756 | em->generation = -1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 757 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 758 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 759 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 760 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 761 | write_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 762 | if (ret != -EEXIST) { |
| 763 | free_extent_map(em); |
| 764 | break; |
| 765 | } |
| 766 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 767 | async_extent->start + |
| 768 | async_extent->ram_size - 1, 0); |
| 769 | } |
| 770 | |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 771 | if (ret) |
| 772 | goto out_free_reserve; |
| 773 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 774 | ret = btrfs_add_ordered_extent_compress(inode, |
| 775 | async_extent->start, |
| 776 | ins.objectid, |
| 777 | async_extent->ram_size, |
| 778 | ins.offset, |
| 779 | BTRFS_ORDERED_COMPRESSED, |
| 780 | async_extent->compress_type); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 781 | if (ret) |
| 782 | goto out_free_reserve; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 783 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 784 | /* |
| 785 | * clear dirty, set writeback and unlock the pages. |
| 786 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 787 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 788 | async_extent->start + |
| 789 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 790 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 791 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 792 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 793 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 794 | async_extent->start, |
| 795 | async_extent->ram_size, |
| 796 | ins.objectid, |
| 797 | ins.offset, async_extent->pages, |
| 798 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 799 | alloc_hint = ins.objectid + ins.offset; |
| 800 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 801 | if (ret) |
| 802 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | cond_resched(); |
| 804 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 805 | ret = 0; |
| 806 | out: |
| 807 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 808 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 809 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 810 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 811 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 812 | async_extent->start + |
| 813 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 814 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 815 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 816 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 817 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 818 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 819 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 820 | } |
| 821 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 822 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 823 | u64 num_bytes) |
| 824 | { |
| 825 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 826 | struct extent_map *em; |
| 827 | u64 alloc_hint = 0; |
| 828 | |
| 829 | read_lock(&em_tree->lock); |
| 830 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 831 | if (em) { |
| 832 | /* |
| 833 | * if block start isn't an actual block number then find the |
| 834 | * first block in this inode and use that as a hint. If that |
| 835 | * block is also bogus then just don't worry about it. |
| 836 | */ |
| 837 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 838 | free_extent_map(em); |
| 839 | em = search_extent_mapping(em_tree, 0, 0); |
| 840 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 841 | alloc_hint = em->block_start; |
| 842 | if (em) |
| 843 | free_extent_map(em); |
| 844 | } else { |
| 845 | alloc_hint = em->block_start; |
| 846 | free_extent_map(em); |
| 847 | } |
| 848 | } |
| 849 | read_unlock(&em_tree->lock); |
| 850 | |
| 851 | return alloc_hint; |
| 852 | } |
| 853 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 854 | /* |
| 855 | * when extent_io.c finds a delayed allocation range in the file, |
| 856 | * the call backs end up in this code. The basic idea is to |
| 857 | * allocate extents on disk for the range, and create ordered data structs |
| 858 | * in ram to track those extents. |
| 859 | * |
| 860 | * locked_page is the page that writepage had locked already. We use |
| 861 | * it to make sure we don't do extra locks or unlocks. |
| 862 | * |
| 863 | * *page_started is set to one if we unlock locked_page and do everything |
| 864 | * required to start IO on it. It may be clean and already done with |
| 865 | * IO when we return. |
| 866 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 867 | static noinline int cow_file_range(struct inode *inode, |
| 868 | struct page *locked_page, |
| 869 | u64 start, u64 end, int *page_started, |
| 870 | unsigned long *nr_written, |
| 871 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 872 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 873 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 874 | u64 alloc_hint = 0; |
| 875 | u64 num_bytes; |
| 876 | unsigned long ram_size; |
| 877 | u64 disk_num_bytes; |
| 878 | u64 cur_alloc_size; |
| 879 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 880 | struct btrfs_key ins; |
| 881 | struct extent_map *em; |
| 882 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 883 | int ret = 0; |
| 884 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 885 | if (btrfs_is_free_space_inode(inode)) { |
| 886 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 887 | ret = -EINVAL; |
| 888 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 889 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 890 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 891 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 892 | num_bytes = max(blocksize, num_bytes); |
| 893 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 894 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 895 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 896 | if (num_bytes < 64 * 1024 && |
| 897 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 898 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 899 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 900 | if (start == 0) { |
| 901 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 902 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 903 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 904 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 905 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 906 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 907 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 908 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 909 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 910 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 911 | *nr_written = *nr_written + |
| 912 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 913 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 914 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 915 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 916 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 917 | } |
| 918 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 919 | |
| 920 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 921 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 922 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 923 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 924 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 925 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 926 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 927 | unsigned long op; |
| 928 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 929 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 930 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 931 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 932 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 933 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 934 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 935 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 936 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 937 | if (!em) { |
| 938 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 939 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 940 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 941 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 942 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 943 | ram_size = ins.offset; |
| 944 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 945 | em->mod_start = em->start; |
| 946 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 947 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 948 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 949 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 950 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 951 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 952 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 953 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 954 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 955 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 956 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 957 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 958 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 959 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 960 | if (ret != -EEXIST) { |
| 961 | free_extent_map(em); |
| 962 | break; |
| 963 | } |
| 964 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 965 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 966 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 967 | if (ret) |
| 968 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 969 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 970 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 971 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 972 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 973 | if (ret) |
| 974 | goto out_reserve; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 975 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 976 | if (root->root_key.objectid == |
| 977 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 978 | ret = btrfs_reloc_clone_csums(inode, start, |
| 979 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 980 | if (ret) |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 981 | goto out_reserve; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 982 | } |
| 983 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 984 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 985 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 986 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 987 | /* we're not doing compressed IO, don't unlock the first |
| 988 | * page (which the caller expects to stay locked), don't |
| 989 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 990 | * |
| 991 | * Do set the Private2 bit so we know this page was properly |
| 992 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 993 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 994 | op = unlock ? PAGE_UNLOCK : 0; |
| 995 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 996 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 997 | extent_clear_unlock_delalloc(inode, start, |
| 998 | start + ram_size - 1, locked_page, |
| 999 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1000 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1001 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1002 | num_bytes -= cur_alloc_size; |
| 1003 | alloc_hint = ins.objectid + ins.offset; |
| 1004 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1005 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1006 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1007 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1008 | |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1009 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1010 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1011 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1012 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1013 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1014 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1015 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1016 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1017 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1018 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1019 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1020 | /* |
| 1021 | * work queue call back to started compression on a file and pages |
| 1022 | */ |
| 1023 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1024 | { |
| 1025 | struct async_cow *async_cow; |
| 1026 | int num_added = 0; |
| 1027 | async_cow = container_of(work, struct async_cow, work); |
| 1028 | |
| 1029 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1030 | async_cow->start, async_cow->end, async_cow, |
| 1031 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1032 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1033 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1034 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1035 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * work queue call back to submit previously compressed pages |
| 1040 | */ |
| 1041 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1042 | { |
| 1043 | struct async_cow *async_cow; |
| 1044 | struct btrfs_root *root; |
| 1045 | unsigned long nr_pages; |
| 1046 | |
| 1047 | async_cow = container_of(work, struct async_cow, work); |
| 1048 | |
| 1049 | root = async_cow->root; |
| 1050 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1051 | PAGE_CACHE_SHIFT; |
| 1052 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1053 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1054 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1055 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1056 | wake_up(&root->fs_info->async_submit_wait); |
| 1057 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1058 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1059 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1060 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1061 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1062 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1063 | { |
| 1064 | struct async_cow *async_cow; |
| 1065 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1066 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1067 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1068 | kfree(async_cow); |
| 1069 | } |
| 1070 | |
| 1071 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1072 | u64 start, u64 end, int *page_started, |
| 1073 | unsigned long *nr_written) |
| 1074 | { |
| 1075 | struct async_cow *async_cow; |
| 1076 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1077 | unsigned long nr_pages; |
| 1078 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1079 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1080 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1081 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1082 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1083 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1084 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1085 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1086 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1087 | async_cow->root = root; |
| 1088 | async_cow->locked_page = locked_page; |
| 1089 | async_cow->start = start; |
| 1090 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1091 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1092 | cur_end = end; |
| 1093 | else |
| 1094 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1095 | |
| 1096 | async_cow->end = cur_end; |
| 1097 | INIT_LIST_HEAD(&async_cow->extents); |
| 1098 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1099 | btrfs_init_work(&async_cow->work, async_cow_start, |
| 1100 | async_cow_submit, async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1101 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1102 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1103 | PAGE_CACHE_SHIFT; |
| 1104 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1105 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1106 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1107 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1108 | |
| 1109 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1110 | wait_event(root->fs_info->async_submit_wait, |
| 1111 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1112 | limit)); |
| 1113 | } |
| 1114 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1115 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1116 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1117 | wait_event(root->fs_info->async_submit_wait, |
| 1118 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1119 | 0)); |
| 1120 | } |
| 1121 | |
| 1122 | *nr_written += nr_pages; |
| 1123 | start = cur_end + 1; |
| 1124 | } |
| 1125 | *page_started = 1; |
| 1126 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1127 | } |
| 1128 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1129 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1130 | u64 bytenr, u64 num_bytes) |
| 1131 | { |
| 1132 | int ret; |
| 1133 | struct btrfs_ordered_sum *sums; |
| 1134 | LIST_HEAD(list); |
| 1135 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1136 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1137 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1138 | if (ret == 0 && list_empty(&list)) |
| 1139 | return 0; |
| 1140 | |
| 1141 | while (!list_empty(&list)) { |
| 1142 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1143 | list_del(&sums->list); |
| 1144 | kfree(sums); |
| 1145 | } |
| 1146 | return 1; |
| 1147 | } |
| 1148 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1149 | /* |
| 1150 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1151 | * of the extents that exist in the file, and COWs the file as required. |
| 1152 | * |
| 1153 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1154 | * blocks on disk |
| 1155 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1156 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1157 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1158 | u64 start, u64 end, int *page_started, int force, |
| 1159 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1160 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1162 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1163 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1164 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1165 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1166 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1167 | u64 cow_start; |
| 1168 | u64 cur_offset; |
| 1169 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1170 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1171 | u64 disk_bytenr; |
| 1172 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1173 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1174 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1175 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1176 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1177 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1178 | int nocow; |
| 1179 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1180 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1181 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1182 | |
| 1183 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1184 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1185 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1186 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1187 | EXTENT_DO_ACCOUNTING | |
| 1188 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1189 | PAGE_CLEAR_DIRTY | |
| 1190 | PAGE_SET_WRITEBACK | |
| 1191 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1192 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1193 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1194 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1195 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1196 | |
| 1197 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1198 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1199 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1200 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1201 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1202 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1203 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1204 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1205 | EXTENT_DO_ACCOUNTING | |
| 1206 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1207 | PAGE_CLEAR_DIRTY | |
| 1208 | PAGE_SET_WRITEBACK | |
| 1209 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1210 | btrfs_free_path(path); |
| 1211 | return PTR_ERR(trans); |
| 1212 | } |
| 1213 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1214 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1215 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1216 | cow_start = (u64)-1; |
| 1217 | cur_offset = start; |
| 1218 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1219 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1220 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1221 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1222 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1223 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1224 | leaf = path->nodes[0]; |
| 1225 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1226 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1227 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1228 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1229 | path->slots[0]--; |
| 1230 | } |
| 1231 | check_prev = 0; |
| 1232 | next_slot: |
| 1233 | leaf = path->nodes[0]; |
| 1234 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1235 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1236 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1237 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1238 | if (ret > 0) |
| 1239 | break; |
| 1240 | leaf = path->nodes[0]; |
| 1241 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1242 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1243 | nocow = 0; |
| 1244 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1245 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1246 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1247 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1248 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1249 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1250 | found_key.offset > end) |
| 1251 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1252 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1253 | if (found_key.offset > cur_offset) { |
| 1254 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1255 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1256 | goto out_check; |
| 1257 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1258 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1259 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1260 | struct btrfs_file_extent_item); |
| 1261 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1262 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1263 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1264 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1265 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1266 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1267 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1268 | extent_end = found_key.offset + |
| 1269 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1270 | disk_num_bytes = |
| 1271 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1272 | if (extent_end <= start) { |
| 1273 | path->slots[0]++; |
| 1274 | goto next_slot; |
| 1275 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1276 | if (disk_bytenr == 0) |
| 1277 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1278 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1279 | btrfs_file_extent_encryption(leaf, fi) || |
| 1280 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1281 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1282 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1283 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1284 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1285 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1286 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1287 | found_key.offset - |
| 1288 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1289 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1290 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1291 | disk_bytenr += cur_offset - found_key.offset; |
| 1292 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1293 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1294 | * if there are pending snapshots for this root, |
| 1295 | * we fall into common COW way. |
| 1296 | */ |
| 1297 | if (!nolock) { |
| 1298 | err = btrfs_start_nocow_write(root); |
| 1299 | if (!err) |
| 1300 | goto out_check; |
| 1301 | } |
| 1302 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1303 | * force cow if csum exists in the range. |
| 1304 | * this ensure that csum for a given extent are |
| 1305 | * either valid or do not exist. |
| 1306 | */ |
| 1307 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1308 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1309 | nocow = 1; |
| 1310 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1311 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1312 | btrfs_file_extent_inline_len(leaf, |
| 1313 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1314 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1315 | } else { |
| 1316 | BUG_ON(1); |
| 1317 | } |
| 1318 | out_check: |
| 1319 | if (extent_end <= start) { |
| 1320 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1321 | if (!nolock && nocow) |
| 1322 | btrfs_end_nocow_write(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1323 | goto next_slot; |
| 1324 | } |
| 1325 | if (!nocow) { |
| 1326 | if (cow_start == (u64)-1) |
| 1327 | cow_start = cur_offset; |
| 1328 | cur_offset = extent_end; |
| 1329 | if (cur_offset > end) |
| 1330 | break; |
| 1331 | path->slots[0]++; |
| 1332 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1333 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1334 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1335 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1336 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1337 | ret = cow_file_range(inode, locked_page, |
| 1338 | cow_start, found_key.offset - 1, |
| 1339 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1340 | if (ret) { |
| 1341 | if (!nolock && nocow) |
| 1342 | btrfs_end_nocow_write(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1343 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1344 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1345 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1346 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1347 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1348 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1349 | struct extent_map *em; |
| 1350 | struct extent_map_tree *em_tree; |
| 1351 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1352 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1353 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1354 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1355 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1356 | em->len = num_bytes; |
| 1357 | em->block_len = num_bytes; |
| 1358 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1359 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1360 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1361 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1362 | em->mod_start = em->start; |
| 1363 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1364 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1365 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1366 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1367 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1368 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1369 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1370 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1371 | if (ret != -EEXIST) { |
| 1372 | free_extent_map(em); |
| 1373 | break; |
| 1374 | } |
| 1375 | btrfs_drop_extent_cache(inode, em->start, |
| 1376 | em->start + em->len - 1, 0); |
| 1377 | } |
| 1378 | type = BTRFS_ORDERED_PREALLOC; |
| 1379 | } else { |
| 1380 | type = BTRFS_ORDERED_NOCOW; |
| 1381 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1382 | |
| 1383 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1384 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1385 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1386 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1387 | if (root->root_key.objectid == |
| 1388 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1389 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1390 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1391 | if (ret) { |
| 1392 | if (!nolock && nocow) |
| 1393 | btrfs_end_nocow_write(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1394 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1395 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1396 | } |
| 1397 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1398 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1399 | cur_offset + num_bytes - 1, |
| 1400 | locked_page, EXTENT_LOCKED | |
| 1401 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1402 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1403 | if (!nolock && nocow) |
| 1404 | btrfs_end_nocow_write(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1405 | cur_offset = extent_end; |
| 1406 | if (cur_offset > end) |
| 1407 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1408 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1409 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1410 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1411 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1412 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1413 | cur_offset = end; |
| 1414 | } |
| 1415 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1416 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1417 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1418 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1419 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1420 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1421 | } |
| 1422 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1423 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1424 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1425 | if (!ret) |
| 1426 | ret = err; |
| 1427 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1428 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1429 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1430 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1431 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1432 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1433 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1434 | PAGE_SET_WRITEBACK | |
| 1435 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1436 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1437 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1438 | } |
| 1439 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1440 | /* |
| 1441 | * extent_io.c call back to do delayed allocation processing |
| 1442 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1443 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1444 | u64 start, u64 end, int *page_started, |
| 1445 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1446 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1447 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1448 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1449 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1450 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1451 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1452 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1453 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1454 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1455 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1456 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1457 | !(BTRFS_I(inode)->force_compress) && |
| 1458 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1459 | ret = cow_file_range(inode, locked_page, start, end, |
| 1460 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1461 | } else { |
| 1462 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1463 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1464 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1465 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1466 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1467 | return ret; |
| 1468 | } |
| 1469 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1470 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1471 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1472 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1473 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1474 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1475 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1476 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1477 | spin_lock(&BTRFS_I(inode)->lock); |
| 1478 | BTRFS_I(inode)->outstanding_extents++; |
| 1479 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1484 | * extents so we can keep track of new extents that are just merged onto old |
| 1485 | * extents, such as when we are doing sequential writes, so we can properly |
| 1486 | * account for the metadata space we'll need. |
| 1487 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1488 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1489 | struct extent_state *new, |
| 1490 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1491 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1492 | /* not delalloc, ignore it */ |
| 1493 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1494 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1495 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1496 | spin_lock(&BTRFS_I(inode)->lock); |
| 1497 | BTRFS_I(inode)->outstanding_extents--; |
| 1498 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1501 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1502 | struct inode *inode) |
| 1503 | { |
| 1504 | spin_lock(&root->delalloc_lock); |
| 1505 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1506 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1507 | &root->delalloc_inodes); |
| 1508 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1509 | &BTRFS_I(inode)->runtime_flags); |
| 1510 | root->nr_delalloc_inodes++; |
| 1511 | if (root->nr_delalloc_inodes == 1) { |
| 1512 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1513 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1514 | list_add_tail(&root->delalloc_root, |
| 1515 | &root->fs_info->delalloc_roots); |
| 1516 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1517 | } |
| 1518 | } |
| 1519 | spin_unlock(&root->delalloc_lock); |
| 1520 | } |
| 1521 | |
| 1522 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1523 | struct inode *inode) |
| 1524 | { |
| 1525 | spin_lock(&root->delalloc_lock); |
| 1526 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1527 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1528 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1529 | &BTRFS_I(inode)->runtime_flags); |
| 1530 | root->nr_delalloc_inodes--; |
| 1531 | if (!root->nr_delalloc_inodes) { |
| 1532 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1533 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1534 | list_del_init(&root->delalloc_root); |
| 1535 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1536 | } |
| 1537 | } |
| 1538 | spin_unlock(&root->delalloc_lock); |
| 1539 | } |
| 1540 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1541 | /* |
| 1542 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1543 | * bytes in this file, and to maintain the list of inodes that |
| 1544 | * have pending delalloc work to be done. |
| 1545 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1546 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1547 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1548 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1549 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1550 | /* |
| 1551 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1552 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1553 | * bit, which is only set or cleared with irqs on |
| 1554 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1555 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1556 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1557 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1558 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1559 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1560 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1561 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | spin_lock(&BTRFS_I(inode)->lock); |
| 1564 | BTRFS_I(inode)->outstanding_extents++; |
| 1565 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1566 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1567 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1568 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1569 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1570 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1571 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1572 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1573 | &BTRFS_I(inode)->runtime_flags)) |
| 1574 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1575 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1576 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1579 | /* |
| 1580 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1581 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1582 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1583 | struct extent_state *state, |
| 1584 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1585 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1586 | /* |
| 1587 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1588 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1589 | * bit, which is only set or cleared with irqs on |
| 1590 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1591 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1592 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1593 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1594 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1595 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1596 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1597 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1598 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1599 | spin_lock(&BTRFS_I(inode)->lock); |
| 1600 | BTRFS_I(inode)->outstanding_extents--; |
| 1601 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1602 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1603 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1604 | /* |
| 1605 | * We don't reserve metadata space for space cache inodes so we |
| 1606 | * don't need to call dellalloc_release_metadata if there is an |
| 1607 | * error. |
| 1608 | */ |
| 1609 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1610 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1611 | btrfs_delalloc_release_metadata(inode, len); |
| 1612 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1613 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1614 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1615 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1616 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1617 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1618 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1619 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1620 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1621 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1622 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1623 | &BTRFS_I(inode)->runtime_flags)) |
| 1624 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1625 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1626 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1627 | } |
| 1628 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1629 | /* |
| 1630 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1631 | * we don't create bios that span stripes or chunks |
| 1632 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1633 | 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] | 1634 | size_t size, struct bio *bio, |
| 1635 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1636 | { |
| 1637 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1638 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1639 | u64 length = 0; |
| 1640 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1641 | int ret; |
| 1642 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1643 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1644 | return 0; |
| 1645 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1646 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1647 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1648 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1649 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1650 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1651 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1652 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1653 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1654 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1655 | } |
| 1656 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1657 | /* |
| 1658 | * in order to insert checksums into the metadata in large chunks, |
| 1659 | * we wait until bio submission time. All the pages in the bio are |
| 1660 | * checksummed and sums are attached onto the ordered extent record. |
| 1661 | * |
| 1662 | * At IO completion time the cums attached on the ordered extent record |
| 1663 | * are inserted into the btree |
| 1664 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1665 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1666 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1667 | unsigned long bio_flags, |
| 1668 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1669 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1670 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1671 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1672 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1673 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1674 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1675 | return 0; |
| 1676 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1677 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1678 | /* |
| 1679 | * in order to insert checksums into the metadata in large chunks, |
| 1680 | * we wait until bio submission time. All the pages in the bio are |
| 1681 | * checksummed and sums are attached onto the ordered extent record. |
| 1682 | * |
| 1683 | * At IO completion time the cums attached on the ordered extent record |
| 1684 | * are inserted into the btree |
| 1685 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1686 | 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] | 1687 | int mirror_num, unsigned long bio_flags, |
| 1688 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1689 | { |
| 1690 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1691 | int ret; |
| 1692 | |
| 1693 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1694 | if (ret) |
| 1695 | bio_endio(bio, ret); |
| 1696 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1697 | } |
| 1698 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1699 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1700 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1701 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1702 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1703 | 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] | 1704 | int mirror_num, unsigned long bio_flags, |
| 1705 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1706 | { |
| 1707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1708 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1709 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1710 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1711 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1712 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1713 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1714 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1715 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1716 | metadata = 2; |
| 1717 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1718 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1719 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1720 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1721 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1722 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1723 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1724 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1725 | mirror_num, |
| 1726 | bio_flags); |
| 1727 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1728 | } else if (!skip_sum) { |
| 1729 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1730 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1731 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1732 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1733 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1734 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1735 | /* csum items have already been cloned */ |
| 1736 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1737 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1738 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1739 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1740 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1741 | bio_flags, bio_offset, |
| 1742 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1743 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1744 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1745 | } else if (!skip_sum) { |
| 1746 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1747 | if (ret) |
| 1748 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1749 | } |
| 1750 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1751 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1752 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1753 | |
| 1754 | out: |
| 1755 | if (ret < 0) |
| 1756 | bio_endio(bio, ret); |
| 1757 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1758 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1759 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1760 | /* |
| 1761 | * given a list of ordered sums record them in the inode. This happens |
| 1762 | * at IO completion time based on sums calculated at bio submission time. |
| 1763 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1764 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1765 | struct inode *inode, u64 file_offset, |
| 1766 | struct list_head *list) |
| 1767 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1768 | struct btrfs_ordered_sum *sum; |
| 1769 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1770 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1771 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1772 | btrfs_csum_file_blocks(trans, |
| 1773 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1774 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1775 | } |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1779 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1780 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1781 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1782 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1783 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1784 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1785 | } |
| 1786 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1787 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1788 | struct btrfs_writepage_fixup { |
| 1789 | struct page *page; |
| 1790 | struct btrfs_work work; |
| 1791 | }; |
| 1792 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1793 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1794 | { |
| 1795 | struct btrfs_writepage_fixup *fixup; |
| 1796 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1797 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1798 | struct page *page; |
| 1799 | struct inode *inode; |
| 1800 | u64 page_start; |
| 1801 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1802 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1803 | |
| 1804 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1805 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1806 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1807 | lock_page(page); |
| 1808 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1809 | ClearPageChecked(page); |
| 1810 | goto out_page; |
| 1811 | } |
| 1812 | |
| 1813 | inode = page->mapping->host; |
| 1814 | page_start = page_offset(page); |
| 1815 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1816 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1817 | 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] | 1818 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1819 | |
| 1820 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1821 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1822 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1823 | |
| 1824 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1825 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1826 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1827 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1828 | unlock_page(page); |
| 1829 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1830 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1831 | goto again; |
| 1832 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1833 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1834 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1835 | if (ret) { |
| 1836 | mapping_set_error(page->mapping, ret); |
| 1837 | end_extent_writepage(page, ret, page_start, page_end); |
| 1838 | ClearPageChecked(page); |
| 1839 | goto out; |
| 1840 | } |
| 1841 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1842 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1843 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1844 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1845 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1846 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1847 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1848 | out_page: |
| 1849 | unlock_page(page); |
| 1850 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1851 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * There are a few paths in the higher layers of the kernel that directly |
| 1856 | * set the page dirty bit without asking the filesystem if it is a |
| 1857 | * good idea. This causes problems because we want to make sure COW |
| 1858 | * properly happens and the data=ordered rules are followed. |
| 1859 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1860 | * 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] | 1861 | * hasn't been properly setup for IO. We kick off an async process |
| 1862 | * to fix it up. The async helper will wait for ordered extents, set |
| 1863 | * the delalloc bit and make it safe to write the page. |
| 1864 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1865 | 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] | 1866 | { |
| 1867 | struct inode *inode = page->mapping->host; |
| 1868 | struct btrfs_writepage_fixup *fixup; |
| 1869 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1870 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1871 | /* this page is properly in the ordered list */ |
| 1872 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1873 | return 0; |
| 1874 | |
| 1875 | if (PageChecked(page)) |
| 1876 | return -EAGAIN; |
| 1877 | |
| 1878 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1879 | if (!fixup) |
| 1880 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1881 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1882 | SetPageChecked(page); |
| 1883 | page_cache_get(page); |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 1884 | btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1885 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 1886 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1887 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1888 | } |
| 1889 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1890 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1891 | struct inode *inode, u64 file_pos, |
| 1892 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1893 | u64 num_bytes, u64 ram_bytes, |
| 1894 | u8 compression, u8 encryption, |
| 1895 | u16 other_encoding, int extent_type) |
| 1896 | { |
| 1897 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1898 | struct btrfs_file_extent_item *fi; |
| 1899 | struct btrfs_path *path; |
| 1900 | struct extent_buffer *leaf; |
| 1901 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1902 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1903 | int ret; |
| 1904 | |
| 1905 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1906 | if (!path) |
| 1907 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1908 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1909 | /* |
| 1910 | * we may be replacing one extent in the tree with another. |
| 1911 | * The new extent is pinned in the extent map, and we don't want |
| 1912 | * to drop it from the cache until it is completely in the btree. |
| 1913 | * |
| 1914 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1915 | * the caller is expected to unpin it and allow it to be merged |
| 1916 | * with the others. |
| 1917 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1918 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 1919 | file_pos + num_bytes, NULL, 0, |
| 1920 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1921 | if (ret) |
| 1922 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1923 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1924 | if (!extent_inserted) { |
| 1925 | ins.objectid = btrfs_ino(inode); |
| 1926 | ins.offset = file_pos; |
| 1927 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1928 | |
| 1929 | path->leave_spinning = 1; |
| 1930 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 1931 | sizeof(*fi)); |
| 1932 | if (ret) |
| 1933 | goto out; |
| 1934 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1935 | leaf = path->nodes[0]; |
| 1936 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1937 | struct btrfs_file_extent_item); |
| 1938 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1939 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1940 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1941 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1942 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1943 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1944 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1945 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1946 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1947 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1948 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1949 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1950 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1951 | |
| 1952 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1953 | |
| 1954 | ins.objectid = disk_bytenr; |
| 1955 | ins.offset = disk_num_bytes; |
| 1956 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1957 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1958 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1959 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1960 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1961 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1962 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1963 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1964 | } |
| 1965 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1966 | /* snapshot-aware defrag */ |
| 1967 | struct sa_defrag_extent_backref { |
| 1968 | struct rb_node node; |
| 1969 | struct old_sa_defrag_extent *old; |
| 1970 | u64 root_id; |
| 1971 | u64 inum; |
| 1972 | u64 file_pos; |
| 1973 | u64 extent_offset; |
| 1974 | u64 num_bytes; |
| 1975 | u64 generation; |
| 1976 | }; |
| 1977 | |
| 1978 | struct old_sa_defrag_extent { |
| 1979 | struct list_head list; |
| 1980 | struct new_sa_defrag_extent *new; |
| 1981 | |
| 1982 | u64 extent_offset; |
| 1983 | u64 bytenr; |
| 1984 | u64 offset; |
| 1985 | u64 len; |
| 1986 | int count; |
| 1987 | }; |
| 1988 | |
| 1989 | struct new_sa_defrag_extent { |
| 1990 | struct rb_root root; |
| 1991 | struct list_head head; |
| 1992 | struct btrfs_path *path; |
| 1993 | struct inode *inode; |
| 1994 | u64 file_pos; |
| 1995 | u64 len; |
| 1996 | u64 bytenr; |
| 1997 | u64 disk_len; |
| 1998 | u8 compress_type; |
| 1999 | }; |
| 2000 | |
| 2001 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2002 | struct sa_defrag_extent_backref *b2) |
| 2003 | { |
| 2004 | if (b1->root_id < b2->root_id) |
| 2005 | return -1; |
| 2006 | else if (b1->root_id > b2->root_id) |
| 2007 | return 1; |
| 2008 | |
| 2009 | if (b1->inum < b2->inum) |
| 2010 | return -1; |
| 2011 | else if (b1->inum > b2->inum) |
| 2012 | return 1; |
| 2013 | |
| 2014 | if (b1->file_pos < b2->file_pos) |
| 2015 | return -1; |
| 2016 | else if (b1->file_pos > b2->file_pos) |
| 2017 | return 1; |
| 2018 | |
| 2019 | /* |
| 2020 | * [------------------------------] ===> (a range of space) |
| 2021 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2022 | * |<---------------------------->| ===> (fs/file tree B) |
| 2023 | * |
| 2024 | * A range of space can refer to two file extents in one tree while |
| 2025 | * refer to only one file extent in another tree. |
| 2026 | * |
| 2027 | * So we may process a disk offset more than one time(two extents in A) |
| 2028 | * and locate at the same extent(one extent in B), then insert two same |
| 2029 | * backrefs(both refer to the extent in B). |
| 2030 | */ |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | static void backref_insert(struct rb_root *root, |
| 2035 | struct sa_defrag_extent_backref *backref) |
| 2036 | { |
| 2037 | struct rb_node **p = &root->rb_node; |
| 2038 | struct rb_node *parent = NULL; |
| 2039 | struct sa_defrag_extent_backref *entry; |
| 2040 | int ret; |
| 2041 | |
| 2042 | while (*p) { |
| 2043 | parent = *p; |
| 2044 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2045 | |
| 2046 | ret = backref_comp(backref, entry); |
| 2047 | if (ret < 0) |
| 2048 | p = &(*p)->rb_left; |
| 2049 | else |
| 2050 | p = &(*p)->rb_right; |
| 2051 | } |
| 2052 | |
| 2053 | rb_link_node(&backref->node, parent, p); |
| 2054 | rb_insert_color(&backref->node, root); |
| 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * Note the backref might has changed, and in this case we just return 0. |
| 2059 | */ |
| 2060 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2061 | void *ctx) |
| 2062 | { |
| 2063 | struct btrfs_file_extent_item *extent; |
| 2064 | struct btrfs_fs_info *fs_info; |
| 2065 | struct old_sa_defrag_extent *old = ctx; |
| 2066 | struct new_sa_defrag_extent *new = old->new; |
| 2067 | struct btrfs_path *path = new->path; |
| 2068 | struct btrfs_key key; |
| 2069 | struct btrfs_root *root; |
| 2070 | struct sa_defrag_extent_backref *backref; |
| 2071 | struct extent_buffer *leaf; |
| 2072 | struct inode *inode = new->inode; |
| 2073 | int slot; |
| 2074 | int ret; |
| 2075 | u64 extent_offset; |
| 2076 | u64 num_bytes; |
| 2077 | |
| 2078 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2079 | inum == btrfs_ino(inode)) |
| 2080 | return 0; |
| 2081 | |
| 2082 | key.objectid = root_id; |
| 2083 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2084 | key.offset = (u64)-1; |
| 2085 | |
| 2086 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2087 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2088 | if (IS_ERR(root)) { |
| 2089 | if (PTR_ERR(root) == -ENOENT) |
| 2090 | return 0; |
| 2091 | WARN_ON(1); |
| 2092 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2093 | inum, offset, root_id); |
| 2094 | return PTR_ERR(root); |
| 2095 | } |
| 2096 | |
| 2097 | key.objectid = inum; |
| 2098 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2099 | if (offset > (u64)-1 << 32) |
| 2100 | key.offset = 0; |
| 2101 | else |
| 2102 | key.offset = offset; |
| 2103 | |
| 2104 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2105 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2106 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2107 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2108 | |
| 2109 | while (1) { |
| 2110 | cond_resched(); |
| 2111 | |
| 2112 | leaf = path->nodes[0]; |
| 2113 | slot = path->slots[0]; |
| 2114 | |
| 2115 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2116 | ret = btrfs_next_leaf(root, path); |
| 2117 | if (ret < 0) { |
| 2118 | goto out; |
| 2119 | } else if (ret > 0) { |
| 2120 | ret = 0; |
| 2121 | goto out; |
| 2122 | } |
| 2123 | continue; |
| 2124 | } |
| 2125 | |
| 2126 | path->slots[0]++; |
| 2127 | |
| 2128 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2129 | |
| 2130 | if (key.objectid > inum) |
| 2131 | goto out; |
| 2132 | |
| 2133 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2134 | continue; |
| 2135 | |
| 2136 | extent = btrfs_item_ptr(leaf, slot, |
| 2137 | struct btrfs_file_extent_item); |
| 2138 | |
| 2139 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2140 | continue; |
| 2141 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2142 | /* |
| 2143 | * 'offset' refers to the exact key.offset, |
| 2144 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2145 | * (key.offset - extent_offset). |
| 2146 | */ |
| 2147 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2148 | continue; |
| 2149 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2150 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2151 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2152 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2153 | if (extent_offset >= old->extent_offset + old->offset + |
| 2154 | old->len || extent_offset + num_bytes <= |
| 2155 | old->extent_offset + old->offset) |
| 2156 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2157 | break; |
| 2158 | } |
| 2159 | |
| 2160 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2161 | if (!backref) { |
| 2162 | ret = -ENOENT; |
| 2163 | goto out; |
| 2164 | } |
| 2165 | |
| 2166 | backref->root_id = root_id; |
| 2167 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2168 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2169 | backref->num_bytes = num_bytes; |
| 2170 | backref->extent_offset = extent_offset; |
| 2171 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2172 | backref->old = old; |
| 2173 | backref_insert(&new->root, backref); |
| 2174 | old->count++; |
| 2175 | out: |
| 2176 | btrfs_release_path(path); |
| 2177 | WARN_ON(ret); |
| 2178 | return ret; |
| 2179 | } |
| 2180 | |
| 2181 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2182 | struct new_sa_defrag_extent *new) |
| 2183 | { |
| 2184 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2185 | struct old_sa_defrag_extent *old, *tmp; |
| 2186 | int ret; |
| 2187 | |
| 2188 | new->path = path; |
| 2189 | |
| 2190 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2191 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2192 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2193 | path, record_one_backref, |
| 2194 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2195 | if (ret < 0 && ret != -ENOENT) |
| 2196 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2197 | |
| 2198 | /* no backref to be processed for this extent */ |
| 2199 | if (!old->count) { |
| 2200 | list_del(&old->list); |
| 2201 | kfree(old); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | if (list_empty(&new->head)) |
| 2206 | return false; |
| 2207 | |
| 2208 | return true; |
| 2209 | } |
| 2210 | |
| 2211 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2212 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2213 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2214 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2215 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2216 | return 0; |
| 2217 | |
| 2218 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2219 | return 0; |
| 2220 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2221 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2222 | return 0; |
| 2223 | |
| 2224 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2225 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2226 | return 0; |
| 2227 | |
| 2228 | return 1; |
| 2229 | } |
| 2230 | |
| 2231 | /* |
| 2232 | * Note the backref might has changed, and in this case we just return 0. |
| 2233 | */ |
| 2234 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2235 | struct sa_defrag_extent_backref *prev, |
| 2236 | struct sa_defrag_extent_backref *backref) |
| 2237 | { |
| 2238 | struct btrfs_file_extent_item *extent; |
| 2239 | struct btrfs_file_extent_item *item; |
| 2240 | struct btrfs_ordered_extent *ordered; |
| 2241 | struct btrfs_trans_handle *trans; |
| 2242 | struct btrfs_fs_info *fs_info; |
| 2243 | struct btrfs_root *root; |
| 2244 | struct btrfs_key key; |
| 2245 | struct extent_buffer *leaf; |
| 2246 | struct old_sa_defrag_extent *old = backref->old; |
| 2247 | struct new_sa_defrag_extent *new = old->new; |
| 2248 | struct inode *src_inode = new->inode; |
| 2249 | struct inode *inode; |
| 2250 | struct extent_state *cached = NULL; |
| 2251 | int ret = 0; |
| 2252 | u64 start; |
| 2253 | u64 len; |
| 2254 | u64 lock_start; |
| 2255 | u64 lock_end; |
| 2256 | bool merge = false; |
| 2257 | int index; |
| 2258 | |
| 2259 | if (prev && prev->root_id == backref->root_id && |
| 2260 | prev->inum == backref->inum && |
| 2261 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2262 | merge = true; |
| 2263 | |
| 2264 | /* step 1: get root */ |
| 2265 | key.objectid = backref->root_id; |
| 2266 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2267 | key.offset = (u64)-1; |
| 2268 | |
| 2269 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2270 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2271 | |
| 2272 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2273 | if (IS_ERR(root)) { |
| 2274 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2275 | if (PTR_ERR(root) == -ENOENT) |
| 2276 | return 0; |
| 2277 | return PTR_ERR(root); |
| 2278 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2279 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2280 | if (btrfs_root_readonly(root)) { |
| 2281 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2285 | /* step 2: get inode */ |
| 2286 | key.objectid = backref->inum; |
| 2287 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2288 | key.offset = 0; |
| 2289 | |
| 2290 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2291 | if (IS_ERR(inode)) { |
| 2292 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
| 2296 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2297 | |
| 2298 | /* step 3: relink backref */ |
| 2299 | lock_start = backref->file_pos; |
| 2300 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2301 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2302 | 0, &cached); |
| 2303 | |
| 2304 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2305 | if (ordered) { |
| 2306 | btrfs_put_ordered_extent(ordered); |
| 2307 | goto out_unlock; |
| 2308 | } |
| 2309 | |
| 2310 | trans = btrfs_join_transaction(root); |
| 2311 | if (IS_ERR(trans)) { |
| 2312 | ret = PTR_ERR(trans); |
| 2313 | goto out_unlock; |
| 2314 | } |
| 2315 | |
| 2316 | key.objectid = backref->inum; |
| 2317 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2318 | key.offset = backref->file_pos; |
| 2319 | |
| 2320 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2321 | if (ret < 0) { |
| 2322 | goto out_free_path; |
| 2323 | } else if (ret > 0) { |
| 2324 | ret = 0; |
| 2325 | goto out_free_path; |
| 2326 | } |
| 2327 | |
| 2328 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2329 | struct btrfs_file_extent_item); |
| 2330 | |
| 2331 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2332 | backref->generation) |
| 2333 | goto out_free_path; |
| 2334 | |
| 2335 | btrfs_release_path(path); |
| 2336 | |
| 2337 | start = backref->file_pos; |
| 2338 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2339 | start += old->extent_offset + old->offset - |
| 2340 | backref->extent_offset; |
| 2341 | |
| 2342 | len = min(backref->extent_offset + backref->num_bytes, |
| 2343 | old->extent_offset + old->offset + old->len); |
| 2344 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2345 | |
| 2346 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2347 | start + len, 1); |
| 2348 | if (ret) |
| 2349 | goto out_free_path; |
| 2350 | again: |
| 2351 | key.objectid = btrfs_ino(inode); |
| 2352 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2353 | key.offset = start; |
| 2354 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2355 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2356 | if (merge) { |
| 2357 | struct btrfs_file_extent_item *fi; |
| 2358 | u64 extent_len; |
| 2359 | struct btrfs_key found_key; |
| 2360 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2361 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2362 | if (ret < 0) |
| 2363 | goto out_free_path; |
| 2364 | |
| 2365 | path->slots[0]--; |
| 2366 | leaf = path->nodes[0]; |
| 2367 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2368 | |
| 2369 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2370 | struct btrfs_file_extent_item); |
| 2371 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2372 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2373 | if (extent_len + found_key.offset == start && |
| 2374 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2375 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2376 | extent_len + len); |
| 2377 | btrfs_mark_buffer_dirty(leaf); |
| 2378 | inode_add_bytes(inode, len); |
| 2379 | |
| 2380 | ret = 1; |
| 2381 | goto out_free_path; |
| 2382 | } else { |
| 2383 | merge = false; |
| 2384 | btrfs_release_path(path); |
| 2385 | goto again; |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2390 | sizeof(*extent)); |
| 2391 | if (ret) { |
| 2392 | btrfs_abort_transaction(trans, root, ret); |
| 2393 | goto out_free_path; |
| 2394 | } |
| 2395 | |
| 2396 | leaf = path->nodes[0]; |
| 2397 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2398 | struct btrfs_file_extent_item); |
| 2399 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2400 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2401 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2402 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2403 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2404 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2405 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2406 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2407 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2408 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2409 | |
| 2410 | btrfs_mark_buffer_dirty(leaf); |
| 2411 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2412 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2413 | |
| 2414 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2415 | new->disk_len, 0, |
| 2416 | backref->root_id, backref->inum, |
| 2417 | new->file_pos, 0); /* start - extent_offset */ |
| 2418 | if (ret) { |
| 2419 | btrfs_abort_transaction(trans, root, ret); |
| 2420 | goto out_free_path; |
| 2421 | } |
| 2422 | |
| 2423 | ret = 1; |
| 2424 | out_free_path: |
| 2425 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2426 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2427 | btrfs_end_transaction(trans, root); |
| 2428 | out_unlock: |
| 2429 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2430 | &cached, GFP_NOFS); |
| 2431 | iput(inode); |
| 2432 | return ret; |
| 2433 | } |
| 2434 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2435 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2436 | { |
| 2437 | struct old_sa_defrag_extent *old, *tmp; |
| 2438 | |
| 2439 | if (!new) |
| 2440 | return; |
| 2441 | |
| 2442 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2443 | list_del(&old->list); |
| 2444 | kfree(old); |
| 2445 | } |
| 2446 | kfree(new); |
| 2447 | } |
| 2448 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2449 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2450 | { |
| 2451 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2452 | struct sa_defrag_extent_backref *backref; |
| 2453 | struct sa_defrag_extent_backref *prev = NULL; |
| 2454 | struct inode *inode; |
| 2455 | struct btrfs_root *root; |
| 2456 | struct rb_node *node; |
| 2457 | int ret; |
| 2458 | |
| 2459 | inode = new->inode; |
| 2460 | root = BTRFS_I(inode)->root; |
| 2461 | |
| 2462 | path = btrfs_alloc_path(); |
| 2463 | if (!path) |
| 2464 | return; |
| 2465 | |
| 2466 | if (!record_extent_backrefs(path, new)) { |
| 2467 | btrfs_free_path(path); |
| 2468 | goto out; |
| 2469 | } |
| 2470 | btrfs_release_path(path); |
| 2471 | |
| 2472 | while (1) { |
| 2473 | node = rb_first(&new->root); |
| 2474 | if (!node) |
| 2475 | break; |
| 2476 | rb_erase(node, &new->root); |
| 2477 | |
| 2478 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2479 | |
| 2480 | ret = relink_extent_backref(path, prev, backref); |
| 2481 | WARN_ON(ret < 0); |
| 2482 | |
| 2483 | kfree(prev); |
| 2484 | |
| 2485 | if (ret == 1) |
| 2486 | prev = backref; |
| 2487 | else |
| 2488 | prev = NULL; |
| 2489 | cond_resched(); |
| 2490 | } |
| 2491 | kfree(prev); |
| 2492 | |
| 2493 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2494 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2495 | free_sa_defrag_extent(new); |
| 2496 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2497 | atomic_dec(&root->fs_info->defrag_running); |
| 2498 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | static struct new_sa_defrag_extent * |
| 2502 | record_old_file_extents(struct inode *inode, |
| 2503 | struct btrfs_ordered_extent *ordered) |
| 2504 | { |
| 2505 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2506 | struct btrfs_path *path; |
| 2507 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2508 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2509 | struct new_sa_defrag_extent *new; |
| 2510 | int ret; |
| 2511 | |
| 2512 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2513 | if (!new) |
| 2514 | return NULL; |
| 2515 | |
| 2516 | new->inode = inode; |
| 2517 | new->file_pos = ordered->file_offset; |
| 2518 | new->len = ordered->len; |
| 2519 | new->bytenr = ordered->start; |
| 2520 | new->disk_len = ordered->disk_len; |
| 2521 | new->compress_type = ordered->compress_type; |
| 2522 | new->root = RB_ROOT; |
| 2523 | INIT_LIST_HEAD(&new->head); |
| 2524 | |
| 2525 | path = btrfs_alloc_path(); |
| 2526 | if (!path) |
| 2527 | goto out_kfree; |
| 2528 | |
| 2529 | key.objectid = btrfs_ino(inode); |
| 2530 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2531 | key.offset = new->file_pos; |
| 2532 | |
| 2533 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2534 | if (ret < 0) |
| 2535 | goto out_free_path; |
| 2536 | if (ret > 0 && path->slots[0] > 0) |
| 2537 | path->slots[0]--; |
| 2538 | |
| 2539 | /* find out all the old extents for the file range */ |
| 2540 | while (1) { |
| 2541 | struct btrfs_file_extent_item *extent; |
| 2542 | struct extent_buffer *l; |
| 2543 | int slot; |
| 2544 | u64 num_bytes; |
| 2545 | u64 offset; |
| 2546 | u64 end; |
| 2547 | u64 disk_bytenr; |
| 2548 | u64 extent_offset; |
| 2549 | |
| 2550 | l = path->nodes[0]; |
| 2551 | slot = path->slots[0]; |
| 2552 | |
| 2553 | if (slot >= btrfs_header_nritems(l)) { |
| 2554 | ret = btrfs_next_leaf(root, path); |
| 2555 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2556 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2557 | else if (ret > 0) |
| 2558 | break; |
| 2559 | continue; |
| 2560 | } |
| 2561 | |
| 2562 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2563 | |
| 2564 | if (key.objectid != btrfs_ino(inode)) |
| 2565 | break; |
| 2566 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2567 | break; |
| 2568 | if (key.offset >= new->file_pos + new->len) |
| 2569 | break; |
| 2570 | |
| 2571 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2572 | |
| 2573 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2574 | if (key.offset + num_bytes < new->file_pos) |
| 2575 | goto next; |
| 2576 | |
| 2577 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2578 | if (!disk_bytenr) |
| 2579 | goto next; |
| 2580 | |
| 2581 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2582 | |
| 2583 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2584 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2585 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2586 | |
| 2587 | offset = max(new->file_pos, key.offset); |
| 2588 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2589 | |
| 2590 | old->bytenr = disk_bytenr; |
| 2591 | old->extent_offset = extent_offset; |
| 2592 | old->offset = offset - key.offset; |
| 2593 | old->len = end - offset; |
| 2594 | old->new = new; |
| 2595 | old->count = 0; |
| 2596 | list_add_tail(&old->list, &new->head); |
| 2597 | next: |
| 2598 | path->slots[0]++; |
| 2599 | cond_resched(); |
| 2600 | } |
| 2601 | |
| 2602 | btrfs_free_path(path); |
| 2603 | atomic_inc(&root->fs_info->defrag_running); |
| 2604 | |
| 2605 | return new; |
| 2606 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2607 | out_free_path: |
| 2608 | btrfs_free_path(path); |
| 2609 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2610 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2611 | return NULL; |
| 2612 | } |
| 2613 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2614 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2615 | u64 start, u64 len) |
| 2616 | { |
| 2617 | struct btrfs_block_group_cache *cache; |
| 2618 | |
| 2619 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2620 | ASSERT(cache); |
| 2621 | |
| 2622 | spin_lock(&cache->lock); |
| 2623 | cache->delalloc_bytes -= len; |
| 2624 | spin_unlock(&cache->lock); |
| 2625 | |
| 2626 | btrfs_put_block_group(cache); |
| 2627 | } |
| 2628 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2629 | /* as ordered data IO finishes, this gets called so we can finish |
| 2630 | * an ordered extent if the range of bytes in the file it covers are |
| 2631 | * fully written. |
| 2632 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2633 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2634 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2635 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2636 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2637 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2638 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2639 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2640 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2641 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2642 | int ret = 0; |
| 2643 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2644 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2645 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2646 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2647 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2648 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2649 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2650 | ret = -EIO; |
| 2651 | goto out; |
| 2652 | } |
| 2653 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2654 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2655 | truncated = true; |
| 2656 | logical_len = ordered_extent->truncated_len; |
| 2657 | /* Truncated the entire extent, don't bother adding */ |
| 2658 | if (!logical_len) |
| 2659 | goto out; |
| 2660 | } |
| 2661 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2662 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2663 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2664 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2665 | if (nolock) |
| 2666 | trans = btrfs_join_transaction_nolock(root); |
| 2667 | else |
| 2668 | trans = btrfs_join_transaction(root); |
| 2669 | if (IS_ERR(trans)) { |
| 2670 | ret = PTR_ERR(trans); |
| 2671 | trans = NULL; |
| 2672 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2673 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2674 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2675 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2676 | if (ret) /* -ENOMEM or corruption */ |
| 2677 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2678 | goto out; |
| 2679 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2680 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2681 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2682 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2683 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2684 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2685 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2686 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2687 | EXTENT_DEFRAG, 1, cached_state); |
| 2688 | if (ret) { |
| 2689 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2690 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2691 | /* the inode is shared */ |
| 2692 | new = record_old_file_extents(inode, ordered_extent); |
| 2693 | |
| 2694 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2695 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2696 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2697 | } |
| 2698 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2699 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2700 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2701 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2702 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2703 | if (IS_ERR(trans)) { |
| 2704 | ret = PTR_ERR(trans); |
| 2705 | trans = NULL; |
| 2706 | goto out_unlock; |
| 2707 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2708 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2709 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2710 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2711 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2712 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2713 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2714 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2715 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2716 | ordered_extent->file_offset, |
| 2717 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2718 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2719 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2720 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2721 | ret = insert_reserved_file_extent(trans, inode, |
| 2722 | ordered_extent->file_offset, |
| 2723 | ordered_extent->start, |
| 2724 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2725 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2726 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2727 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2728 | if (!ret) |
| 2729 | btrfs_release_delalloc_bytes(root, |
| 2730 | ordered_extent->start, |
| 2731 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2732 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2733 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2734 | ordered_extent->file_offset, ordered_extent->len, |
| 2735 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2736 | if (ret < 0) { |
| 2737 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2738 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2739 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2740 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2741 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2742 | &ordered_extent->list); |
| 2743 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2744 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2745 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2746 | if (ret) { /* -ENOMEM or corruption */ |
| 2747 | btrfs_abort_transaction(trans, root, ret); |
| 2748 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2749 | } |
| 2750 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2751 | out_unlock: |
| 2752 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2753 | ordered_extent->file_offset + |
| 2754 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2755 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2756 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2757 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2758 | if (trans) |
| 2759 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2760 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2761 | if (ret || truncated) { |
| 2762 | u64 start, end; |
| 2763 | |
| 2764 | if (truncated) |
| 2765 | start = ordered_extent->file_offset + logical_len; |
| 2766 | else |
| 2767 | start = ordered_extent->file_offset; |
| 2768 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2769 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2770 | |
| 2771 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2772 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2773 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2774 | /* |
| 2775 | * If the ordered extent had an IOERR or something else went |
| 2776 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2777 | * back to the allocator. We only free the extent in the |
| 2778 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2779 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2780 | if ((ret || !logical_len) && |
| 2781 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2782 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2783 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2784 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2788 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2789 | * This needs to be done to make sure anybody waiting knows we are done |
| 2790 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2791 | */ |
| 2792 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2793 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2794 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2795 | if (new) { |
| 2796 | if (ret) { |
| 2797 | free_sa_defrag_extent(new); |
| 2798 | atomic_dec(&root->fs_info->defrag_running); |
| 2799 | } else { |
| 2800 | relink_file_extents(new); |
| 2801 | } |
| 2802 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2803 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2804 | /* once for us */ |
| 2805 | btrfs_put_ordered_extent(ordered_extent); |
| 2806 | /* once for the tree */ |
| 2807 | btrfs_put_ordered_extent(ordered_extent); |
| 2808 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2809 | return ret; |
| 2810 | } |
| 2811 | |
| 2812 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2813 | { |
| 2814 | struct btrfs_ordered_extent *ordered_extent; |
| 2815 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2816 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2817 | } |
| 2818 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2819 | 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] | 2820 | struct extent_state *state, int uptodate) |
| 2821 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2822 | struct inode *inode = page->mapping->host; |
| 2823 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2824 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 2825 | struct btrfs_workqueue *workers; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2826 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2827 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2828 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2829 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2830 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2831 | end - start + 1, uptodate)) |
| 2832 | return 0; |
| 2833 | |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 2834 | btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2835 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2836 | if (btrfs_is_free_space_inode(inode)) |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 2837 | workers = root->fs_info->endio_freespace_worker; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2838 | else |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 2839 | workers = root->fs_info->endio_write_workers; |
| 2840 | btrfs_queue_work(workers, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2841 | |
| 2842 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2843 | } |
| 2844 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2845 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2846 | * 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] | 2847 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2848 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2849 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2850 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2851 | u64 phy_offset, struct page *page, |
| 2852 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2853 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2854 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2855 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2856 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2857 | char *kaddr; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2858 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2859 | u32 csum_expected; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2860 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2861 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2862 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2863 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2864 | if (PageChecked(page)) { |
| 2865 | ClearPageChecked(page); |
| 2866 | goto good; |
| 2867 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2868 | |
| 2869 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2870 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2871 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2872 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2873 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2874 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2875 | GFP_NOFS); |
| 2876 | return 0; |
| 2877 | } |
| 2878 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2879 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2880 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2881 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2882 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2883 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2884 | btrfs_csum_final(csum, (char *)&csum); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2885 | if (csum != csum_expected) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2886 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2887 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2888 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2889 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2890 | return 0; |
| 2891 | |
| 2892 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2893 | if (__ratelimit(&_rs)) |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2894 | 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] | 2895 | btrfs_ino(page->mapping->host), start, csum, csum_expected); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2896 | memset(kaddr + offset, 1, end - start + 1); |
| 2897 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2898 | kunmap_atomic(kaddr); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2899 | if (csum_expected == 0) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2900 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2901 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2902 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2903 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2904 | struct delayed_iput { |
| 2905 | struct list_head list; |
| 2906 | struct inode *inode; |
| 2907 | }; |
| 2908 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2909 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2910 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2911 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2912 | { |
| 2913 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2914 | struct delayed_iput *delayed; |
| 2915 | |
| 2916 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2917 | return; |
| 2918 | |
| 2919 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2920 | delayed->inode = inode; |
| 2921 | |
| 2922 | spin_lock(&fs_info->delayed_iput_lock); |
| 2923 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2924 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2925 | } |
| 2926 | |
| 2927 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2928 | { |
| 2929 | LIST_HEAD(list); |
| 2930 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2931 | struct delayed_iput *delayed; |
| 2932 | int empty; |
| 2933 | |
| 2934 | spin_lock(&fs_info->delayed_iput_lock); |
| 2935 | empty = list_empty(&fs_info->delayed_iputs); |
| 2936 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2937 | if (empty) |
| 2938 | return; |
| 2939 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2940 | spin_lock(&fs_info->delayed_iput_lock); |
| 2941 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2942 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2943 | |
| 2944 | while (!list_empty(&list)) { |
| 2945 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2946 | list_del(&delayed->list); |
| 2947 | iput(delayed->inode); |
| 2948 | kfree(delayed); |
| 2949 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2952 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2953 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2954 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2955 | * structure. |
| 2956 | */ |
| 2957 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2958 | struct btrfs_root *root) |
| 2959 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2960 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2961 | int ret; |
| 2962 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2963 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2964 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2965 | return; |
| 2966 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2967 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2968 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2969 | spin_unlock(&root->orphan_lock); |
| 2970 | return; |
| 2971 | } |
| 2972 | |
| 2973 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2974 | spin_unlock(&root->orphan_lock); |
| 2975 | return; |
| 2976 | } |
| 2977 | |
| 2978 | block_rsv = root->orphan_block_rsv; |
| 2979 | root->orphan_block_rsv = NULL; |
| 2980 | spin_unlock(&root->orphan_lock); |
| 2981 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2982 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2983 | btrfs_root_refs(&root->root_item) > 0) { |
| 2984 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2985 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 2986 | if (ret) |
| 2987 | btrfs_abort_transaction(trans, root, ret); |
| 2988 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2989 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 2990 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2991 | } |
| 2992 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2993 | if (block_rsv) { |
| 2994 | WARN_ON(block_rsv->size > 0); |
| 2995 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3000 | * This creates an orphan entry for the given inode in case something goes |
| 3001 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3002 | * |
| 3003 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3004 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3005 | */ |
| 3006 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3007 | { |
| 3008 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3009 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3010 | int reserve = 0; |
| 3011 | int insert = 0; |
| 3012 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3013 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3014 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3015 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3016 | if (!block_rsv) |
| 3017 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3018 | } |
| 3019 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3020 | spin_lock(&root->orphan_lock); |
| 3021 | if (!root->orphan_block_rsv) { |
| 3022 | root->orphan_block_rsv = block_rsv; |
| 3023 | } else if (block_rsv) { |
| 3024 | btrfs_free_block_rsv(root, block_rsv); |
| 3025 | block_rsv = NULL; |
| 3026 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3027 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3028 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3029 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3030 | #if 0 |
| 3031 | /* |
| 3032 | * For proper ENOSPC handling, we should do orphan |
| 3033 | * cleanup when mounting. But this introduces backward |
| 3034 | * compatibility issue. |
| 3035 | */ |
| 3036 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3037 | insert = 2; |
| 3038 | else |
| 3039 | insert = 1; |
| 3040 | #endif |
| 3041 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3042 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3043 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3044 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3045 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3046 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3047 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3048 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3049 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3050 | /* grab metadata reservation from transaction handle */ |
| 3051 | if (reserve) { |
| 3052 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3053 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3057 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3058 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3059 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3060 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3061 | if (reserve) { |
| 3062 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3063 | &BTRFS_I(inode)->runtime_flags); |
| 3064 | btrfs_orphan_release_metadata(inode); |
| 3065 | } |
| 3066 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3067 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3068 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3069 | btrfs_abort_transaction(trans, root, ret); |
| 3070 | return ret; |
| 3071 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3072 | } |
| 3073 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3077 | if (insert >= 2) { |
| 3078 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3079 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3080 | if (ret && ret != -EEXIST) { |
| 3081 | btrfs_abort_transaction(trans, root, ret); |
| 3082 | return ret; |
| 3083 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3084 | } |
| 3085 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | /* |
| 3089 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3090 | * item for this particular inode. |
| 3091 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3092 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3093 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3094 | { |
| 3095 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3096 | int delete_item = 0; |
| 3097 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3098 | int ret = 0; |
| 3099 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3100 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3101 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3102 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3103 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3104 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3105 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3106 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3107 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3108 | spin_unlock(&root->orphan_lock); |
| 3109 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3110 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3111 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3112 | if (trans) |
| 3113 | ret = btrfs_del_orphan_item(trans, root, |
| 3114 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3115 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3116 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3117 | if (release_rsv) |
| 3118 | btrfs_orphan_release_metadata(inode); |
| 3119 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3120 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | /* |
| 3124 | * this cleans up any orphans that may be left on the list from the last use |
| 3125 | * of this root. |
| 3126 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3127 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3128 | { |
| 3129 | struct btrfs_path *path; |
| 3130 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3131 | struct btrfs_key key, found_key; |
| 3132 | struct btrfs_trans_handle *trans; |
| 3133 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3134 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3135 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3136 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3137 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3138 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3139 | |
| 3140 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3141 | if (!path) { |
| 3142 | ret = -ENOMEM; |
| 3143 | goto out; |
| 3144 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3145 | path->reada = -1; |
| 3146 | |
| 3147 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3148 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3149 | key.offset = (u64)-1; |
| 3150 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3151 | while (1) { |
| 3152 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3153 | if (ret < 0) |
| 3154 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3155 | |
| 3156 | /* |
| 3157 | * 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] | 3158 | * 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] | 3159 | * find the key and see if we have stuff that matches |
| 3160 | */ |
| 3161 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3162 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3163 | if (path->slots[0] == 0) |
| 3164 | break; |
| 3165 | path->slots[0]--; |
| 3166 | } |
| 3167 | |
| 3168 | /* pull out the item */ |
| 3169 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3170 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3171 | |
| 3172 | /* make sure the item matches what we want */ |
| 3173 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3174 | break; |
| 3175 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3176 | break; |
| 3177 | |
| 3178 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3179 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3180 | |
| 3181 | /* |
| 3182 | * this is where we are basically btrfs_lookup, without the |
| 3183 | * crossing root thing. we store the inode number in the |
| 3184 | * offset of the orphan item. |
| 3185 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3186 | |
| 3187 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3188 | btrfs_err(root->fs_info, |
| 3189 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3190 | ret = -EINVAL; |
| 3191 | goto out; |
| 3192 | } |
| 3193 | |
| 3194 | last_objectid = found_key.offset; |
| 3195 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3196 | found_key.objectid = found_key.offset; |
| 3197 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3198 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3199 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3200 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3201 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3202 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3203 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3204 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3205 | struct btrfs_root *dead_root; |
| 3206 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3207 | int is_dead_root = 0; |
| 3208 | |
| 3209 | /* |
| 3210 | * this is an orphan in the tree root. Currently these |
| 3211 | * could come from 2 sources: |
| 3212 | * a) a snapshot deletion in progress |
| 3213 | * b) a free space cache inode |
| 3214 | * We need to distinguish those two, as the snapshot |
| 3215 | * orphan must not get deleted. |
| 3216 | * find_dead_roots already ran before us, so if this |
| 3217 | * is a snapshot deletion, we should find the root |
| 3218 | * in the dead_roots list |
| 3219 | */ |
| 3220 | spin_lock(&fs_info->trans_lock); |
| 3221 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3222 | root_list) { |
| 3223 | if (dead_root->root_key.objectid == |
| 3224 | found_key.objectid) { |
| 3225 | is_dead_root = 1; |
| 3226 | break; |
| 3227 | } |
| 3228 | } |
| 3229 | spin_unlock(&fs_info->trans_lock); |
| 3230 | if (is_dead_root) { |
| 3231 | /* prevent this orphan from being found again */ |
| 3232 | key.offset = found_key.objectid - 1; |
| 3233 | continue; |
| 3234 | } |
| 3235 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3236 | /* |
| 3237 | * Inode is already gone but the orphan item is still there, |
| 3238 | * kill the orphan item. |
| 3239 | */ |
| 3240 | if (ret == -ESTALE) { |
| 3241 | trans = btrfs_start_transaction(root, 1); |
| 3242 | if (IS_ERR(trans)) { |
| 3243 | ret = PTR_ERR(trans); |
| 3244 | goto out; |
| 3245 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3246 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3247 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3248 | ret = btrfs_del_orphan_item(trans, root, |
| 3249 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3250 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3251 | if (ret) |
| 3252 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3253 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3254 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3255 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3256 | /* |
| 3257 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3258 | * the proper thing when we hit it |
| 3259 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3260 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3261 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3262 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3263 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3264 | /* if we have links, this was a truncate, lets do that */ |
| 3265 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3266 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3267 | iput(inode); |
| 3268 | continue; |
| 3269 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3270 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3271 | |
| 3272 | /* 1 for the orphan item deletion. */ |
| 3273 | trans = btrfs_start_transaction(root, 1); |
| 3274 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3275 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3276 | ret = PTR_ERR(trans); |
| 3277 | goto out; |
| 3278 | } |
| 3279 | ret = btrfs_orphan_add(trans, inode); |
| 3280 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3281 | if (ret) { |
| 3282 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3283 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3284 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3285 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3286 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3287 | if (ret) |
| 3288 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3289 | } else { |
| 3290 | nr_unlink++; |
| 3291 | } |
| 3292 | |
| 3293 | /* this will do delete_inode and everything for us */ |
| 3294 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3295 | if (ret) |
| 3296 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3297 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3298 | /* release the path since we're done with it */ |
| 3299 | btrfs_release_path(path); |
| 3300 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3301 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3302 | |
| 3303 | if (root->orphan_block_rsv) |
| 3304 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3305 | (u64)-1); |
| 3306 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3307 | if (root->orphan_block_rsv || |
| 3308 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3309 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3310 | if (!IS_ERR(trans)) |
| 3311 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3312 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3313 | |
| 3314 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3315 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3316 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3317 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3318 | |
| 3319 | out: |
| 3320 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3321 | btrfs_crit(root->fs_info, |
| 3322 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3323 | btrfs_free_path(path); |
| 3324 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3325 | } |
| 3326 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3327 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3328 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3329 | * don't find any xattrs, we know there can't be any acls. |
| 3330 | * |
| 3331 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3332 | */ |
| 3333 | 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] | 3334 | int slot, u64 objectid, |
| 3335 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3336 | { |
| 3337 | u32 nritems = btrfs_header_nritems(leaf); |
| 3338 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3339 | static u64 xattr_access = 0; |
| 3340 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3341 | int scanned = 0; |
| 3342 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3343 | if (!xattr_access) { |
| 3344 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3345 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3346 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3347 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3348 | } |
| 3349 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3350 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3351 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3352 | while (slot < nritems) { |
| 3353 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3354 | |
| 3355 | /* we found a different objectid, there must not be acls */ |
| 3356 | if (found_key.objectid != objectid) |
| 3357 | return 0; |
| 3358 | |
| 3359 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3360 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3361 | if (*first_xattr_slot == -1) |
| 3362 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3363 | if (found_key.offset == xattr_access || |
| 3364 | found_key.offset == xattr_default) |
| 3365 | return 1; |
| 3366 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3367 | |
| 3368 | /* |
| 3369 | * we found a key greater than an xattr key, there can't |
| 3370 | * be any acls later on |
| 3371 | */ |
| 3372 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3373 | return 0; |
| 3374 | |
| 3375 | slot++; |
| 3376 | scanned++; |
| 3377 | |
| 3378 | /* |
| 3379 | * it goes inode, inode backrefs, xattrs, extents, |
| 3380 | * so if there are a ton of hard links to an inode there can |
| 3381 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3382 | * this is just an optimization |
| 3383 | */ |
| 3384 | if (scanned >= 8) |
| 3385 | break; |
| 3386 | } |
| 3387 | /* we hit the end of the leaf before we found an xattr or |
| 3388 | * something larger than an xattr. We have to assume the inode |
| 3389 | * has acls |
| 3390 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3391 | if (*first_xattr_slot == -1) |
| 3392 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3393 | return 1; |
| 3394 | } |
| 3395 | |
| 3396 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3397 | * read an inode from the btree into the in-memory inode |
| 3398 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3399 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3400 | { |
| 3401 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3402 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3403 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3404 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3405 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3406 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3407 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3408 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3409 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3410 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3411 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3412 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3413 | |
| 3414 | ret = btrfs_fill_inode(inode, &rdev); |
| 3415 | if (!ret) |
| 3416 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | |
| 3418 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3419 | if (!path) |
| 3420 | goto make_bad; |
| 3421 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3422 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3423 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3424 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3425 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3426 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3427 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3428 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3429 | |
| 3430 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3431 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3432 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3433 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3434 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3435 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3436 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3437 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3438 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3439 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3440 | |
| 3441 | tspec = btrfs_inode_atime(inode_item); |
| 3442 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3443 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3444 | |
| 3445 | tspec = btrfs_inode_mtime(inode_item); |
| 3446 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3447 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3448 | |
| 3449 | tspec = btrfs_inode_ctime(inode_item); |
| 3450 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3451 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3452 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3453 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3454 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3455 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3456 | |
| 3457 | /* |
| 3458 | * If we were modified in the current generation and evicted from memory |
| 3459 | * and then re-read we need to do a full sync since we don't have any |
| 3460 | * idea about which extents were modified before we were evicted from |
| 3461 | * cache. |
| 3462 | */ |
| 3463 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3464 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3465 | &BTRFS_I(inode)->runtime_flags); |
| 3466 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3467 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3468 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3469 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3470 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3471 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3472 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3473 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3474 | |
| 3475 | cache_index: |
| 3476 | path->slots[0]++; |
| 3477 | if (inode->i_nlink != 1 || |
| 3478 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3479 | goto cache_acl; |
| 3480 | |
| 3481 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3482 | if (location.objectid != btrfs_ino(inode)) |
| 3483 | goto cache_acl; |
| 3484 | |
| 3485 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3486 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3487 | struct btrfs_inode_ref *ref; |
| 3488 | |
| 3489 | ref = (struct btrfs_inode_ref *)ptr; |
| 3490 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3491 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3492 | struct btrfs_inode_extref *extref; |
| 3493 | |
| 3494 | extref = (struct btrfs_inode_extref *)ptr; |
| 3495 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3496 | extref); |
| 3497 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3498 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3499 | /* |
| 3500 | * try to precache a NULL acl entry for files that don't have |
| 3501 | * any xattrs or acls |
| 3502 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3503 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3504 | btrfs_ino(inode), &first_xattr_slot); |
| 3505 | if (first_xattr_slot != -1) { |
| 3506 | path->slots[0] = first_xattr_slot; |
| 3507 | ret = btrfs_load_inode_props(inode, path); |
| 3508 | if (ret) |
| 3509 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3510 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3511 | btrfs_ino(inode), |
| 3512 | root->root_key.objectid, ret); |
| 3513 | } |
| 3514 | btrfs_free_path(path); |
| 3515 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3516 | if (!maybe_acls) |
| 3517 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3518 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3519 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3520 | case S_IFREG: |
| 3521 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3522 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3523 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3524 | inode->i_fop = &btrfs_file_operations; |
| 3525 | inode->i_op = &btrfs_file_inode_operations; |
| 3526 | break; |
| 3527 | case S_IFDIR: |
| 3528 | inode->i_fop = &btrfs_dir_file_operations; |
| 3529 | if (root == root->fs_info->tree_root) |
| 3530 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3531 | else |
| 3532 | inode->i_op = &btrfs_dir_inode_operations; |
| 3533 | break; |
| 3534 | case S_IFLNK: |
| 3535 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3536 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3537 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3538 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3539 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3540 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3541 | init_special_inode(inode, inode->i_mode, rdev); |
| 3542 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3543 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3544 | |
| 3545 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3546 | return; |
| 3547 | |
| 3548 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3549 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3550 | make_bad_inode(inode); |
| 3551 | } |
| 3552 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3553 | /* |
| 3554 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3555 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3556 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3557 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3558 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3559 | struct inode *inode) |
| 3560 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3561 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3562 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3563 | btrfs_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3564 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3565 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3566 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3567 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3568 | &token); |
| 3569 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3570 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3571 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3572 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3573 | inode->i_atime.tv_sec, &token); |
| 3574 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3575 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3576 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3577 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3578 | inode->i_mtime.tv_sec, &token); |
| 3579 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3580 | inode->i_mtime.tv_nsec, &token); |
| 3581 | |
| 3582 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3583 | inode->i_ctime.tv_sec, &token); |
| 3584 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3585 | inode->i_ctime.tv_nsec, &token); |
| 3586 | |
| 3587 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3588 | &token); |
| 3589 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3590 | &token); |
| 3591 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3592 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3593 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3594 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3595 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3596 | } |
| 3597 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3598 | /* |
| 3599 | * copy everything in the in-memory inode into the btree. |
| 3600 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3601 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3602 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3603 | { |
| 3604 | struct btrfs_inode_item *inode_item; |
| 3605 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3606 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3607 | int ret; |
| 3608 | |
| 3609 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3610 | if (!path) |
| 3611 | return -ENOMEM; |
| 3612 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3613 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3614 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3615 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3616 | if (ret) { |
| 3617 | if (ret > 0) |
| 3618 | ret = -ENOENT; |
| 3619 | goto failed; |
| 3620 | } |
| 3621 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3622 | leaf = path->nodes[0]; |
| 3623 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3624 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3625 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3626 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3627 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3628 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3629 | ret = 0; |
| 3630 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3631 | btrfs_free_path(path); |
| 3632 | return ret; |
| 3633 | } |
| 3634 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3635 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3636 | * copy everything in the in-memory inode into the btree. |
| 3637 | */ |
| 3638 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3639 | struct btrfs_root *root, struct inode *inode) |
| 3640 | { |
| 3641 | int ret; |
| 3642 | |
| 3643 | /* |
| 3644 | * If the inode is a free space inode, we can deadlock during commit |
| 3645 | * if we put it into the delayed code. |
| 3646 | * |
| 3647 | * The data relocation inode should also be directly updated |
| 3648 | * without delay |
| 3649 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3650 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3651 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3652 | btrfs_update_root_times(trans, root); |
| 3653 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3654 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3655 | if (!ret) |
| 3656 | btrfs_set_inode_last_trans(trans, inode); |
| 3657 | return ret; |
| 3658 | } |
| 3659 | |
| 3660 | return btrfs_update_inode_item(trans, root, inode); |
| 3661 | } |
| 3662 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3663 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3664 | struct btrfs_root *root, |
| 3665 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3666 | { |
| 3667 | int ret; |
| 3668 | |
| 3669 | ret = btrfs_update_inode(trans, root, inode); |
| 3670 | if (ret == -ENOSPC) |
| 3671 | return btrfs_update_inode_item(trans, root, inode); |
| 3672 | return ret; |
| 3673 | } |
| 3674 | |
| 3675 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3676 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3677 | * recovery code. It remove a link in a directory with a given name, and |
| 3678 | * also drops the back refs in the inode to the directory |
| 3679 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3680 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3681 | struct btrfs_root *root, |
| 3682 | struct inode *dir, struct inode *inode, |
| 3683 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3684 | { |
| 3685 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3686 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3687 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3688 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3689 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3690 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3691 | u64 ino = btrfs_ino(inode); |
| 3692 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3693 | |
| 3694 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3695 | if (!path) { |
| 3696 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3697 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3698 | } |
| 3699 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3700 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3701 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | name, name_len, -1); |
| 3703 | if (IS_ERR(di)) { |
| 3704 | ret = PTR_ERR(di); |
| 3705 | goto err; |
| 3706 | } |
| 3707 | if (!di) { |
| 3708 | ret = -ENOENT; |
| 3709 | goto err; |
| 3710 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3711 | leaf = path->nodes[0]; |
| 3712 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3713 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3714 | if (ret) |
| 3715 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3716 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3717 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3718 | /* |
| 3719 | * If we don't have dir index, we have to get it by looking up |
| 3720 | * the inode ref, since we get the inode ref, remove it directly, |
| 3721 | * it is unnecessary to do delayed deletion. |
| 3722 | * |
| 3723 | * But if we have dir index, needn't search inode ref to get it. |
| 3724 | * Since the inode ref is close to the inode item, it is better |
| 3725 | * that we delay to delete it, and just do this deletion when |
| 3726 | * we update the inode item. |
| 3727 | */ |
| 3728 | if (BTRFS_I(inode)->dir_index) { |
| 3729 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3730 | if (!ret) { |
| 3731 | index = BTRFS_I(inode)->dir_index; |
| 3732 | goto skip_backref; |
| 3733 | } |
| 3734 | } |
| 3735 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3736 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3737 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3738 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3739 | btrfs_info(root->fs_info, |
| 3740 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3741 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3742 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3743 | goto err; |
| 3744 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3745 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3746 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3747 | if (ret) { |
| 3748 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3749 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3750 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3751 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3752 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3753 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3754 | if (ret != 0 && ret != -ENOENT) { |
| 3755 | btrfs_abort_transaction(trans, root, ret); |
| 3756 | goto err; |
| 3757 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3758 | |
| 3759 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3760 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3761 | if (ret == -ENOENT) |
| 3762 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3763 | else if (ret) |
| 3764 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | err: |
| 3766 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3767 | if (ret) |
| 3768 | goto out; |
| 3769 | |
| 3770 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3771 | inode_inc_iversion(inode); |
| 3772 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3773 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3774 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3775 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3776 | return ret; |
| 3777 | } |
| 3778 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3779 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3780 | struct btrfs_root *root, |
| 3781 | struct inode *dir, struct inode *inode, |
| 3782 | const char *name, int name_len) |
| 3783 | { |
| 3784 | int ret; |
| 3785 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3786 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3787 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3788 | ret = btrfs_update_inode(trans, root, inode); |
| 3789 | } |
| 3790 | return ret; |
| 3791 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3792 | |
| 3793 | /* |
| 3794 | * helper to start transaction for unlink and rmdir. |
| 3795 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3796 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3797 | * if we cannot make our reservations the normal way try and see if there is |
| 3798 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3799 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3800 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3801 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3802 | { |
| 3803 | struct btrfs_trans_handle *trans; |
| 3804 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3805 | int ret; |
| 3806 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3807 | /* |
| 3808 | * 1 for the possible orphan item |
| 3809 | * 1 for the dir item |
| 3810 | * 1 for the dir index |
| 3811 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3812 | * 1 for the inode |
| 3813 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3814 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3815 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3816 | return trans; |
| 3817 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3818 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3819 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3820 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3821 | trans = btrfs_start_transaction(root, 0); |
| 3822 | if (IS_ERR(trans)) |
| 3823 | return trans; |
| 3824 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3825 | &root->fs_info->trans_block_rsv, |
| 3826 | num_bytes, 5); |
| 3827 | if (ret) { |
| 3828 | btrfs_end_transaction(trans, root); |
| 3829 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3830 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3831 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3832 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3833 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3834 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3835 | } |
| 3836 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3837 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3838 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3839 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3840 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3841 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3842 | int ret; |
| 3843 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3844 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3845 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3846 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3847 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3848 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3849 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3850 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3851 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3852 | if (ret) |
| 3853 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3854 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3855 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3856 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3857 | if (ret) |
| 3858 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3859 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3860 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3861 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3862 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3863 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3864 | return ret; |
| 3865 | } |
| 3866 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3867 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3868 | struct btrfs_root *root, |
| 3869 | struct inode *dir, u64 objectid, |
| 3870 | const char *name, int name_len) |
| 3871 | { |
| 3872 | struct btrfs_path *path; |
| 3873 | struct extent_buffer *leaf; |
| 3874 | struct btrfs_dir_item *di; |
| 3875 | struct btrfs_key key; |
| 3876 | u64 index; |
| 3877 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3878 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3879 | |
| 3880 | path = btrfs_alloc_path(); |
| 3881 | if (!path) |
| 3882 | return -ENOMEM; |
| 3883 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3884 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3885 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3886 | if (IS_ERR_OR_NULL(di)) { |
| 3887 | if (!di) |
| 3888 | ret = -ENOENT; |
| 3889 | else |
| 3890 | ret = PTR_ERR(di); |
| 3891 | goto out; |
| 3892 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3893 | |
| 3894 | leaf = path->nodes[0]; |
| 3895 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3896 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3897 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3898 | if (ret) { |
| 3899 | btrfs_abort_transaction(trans, root, ret); |
| 3900 | goto out; |
| 3901 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3902 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3903 | |
| 3904 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3905 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3906 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3907 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3908 | if (ret != -ENOENT) { |
| 3909 | btrfs_abort_transaction(trans, root, ret); |
| 3910 | goto out; |
| 3911 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3912 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3913 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3914 | if (IS_ERR_OR_NULL(di)) { |
| 3915 | if (!di) |
| 3916 | ret = -ENOENT; |
| 3917 | else |
| 3918 | ret = PTR_ERR(di); |
| 3919 | btrfs_abort_transaction(trans, root, ret); |
| 3920 | goto out; |
| 3921 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3922 | |
| 3923 | leaf = path->nodes[0]; |
| 3924 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3925 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3926 | index = key.offset; |
| 3927 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3928 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3929 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3930 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3931 | if (ret) { |
| 3932 | btrfs_abort_transaction(trans, root, ret); |
| 3933 | goto out; |
| 3934 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3935 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3936 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3937 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3938 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3939 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3940 | if (ret) |
| 3941 | btrfs_abort_transaction(trans, root, ret); |
| 3942 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3943 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3944 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3945 | } |
| 3946 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3947 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3948 | { |
| 3949 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3950 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3951 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3952 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3953 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3954 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3955 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3956 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3957 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3958 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3959 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3960 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3961 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3962 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3963 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3964 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3965 | BTRFS_I(inode)->location.objectid, |
| 3966 | dentry->d_name.name, |
| 3967 | dentry->d_name.len); |
| 3968 | goto out; |
| 3969 | } |
| 3970 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3971 | err = btrfs_orphan_add(trans, inode); |
| 3972 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3973 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3974 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3975 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3976 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3977 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3978 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3979 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3980 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3981 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3982 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3983 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3984 | return err; |
| 3985 | } |
| 3986 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 3987 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3988 | * this can truncate away extent items, csum items and directory items. |
| 3989 | * 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] | 3990 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3991 | * |
| 3992 | * csum items that cross the new i_size are truncated to the new size |
| 3993 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3994 | * |
| 3995 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 3996 | * 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] | 3997 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 3998 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 3999 | struct btrfs_root *root, |
| 4000 | struct inode *inode, |
| 4001 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4002 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4003 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4004 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4006 | struct btrfs_key key; |
| 4007 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4008 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4009 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4010 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4011 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4012 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4013 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4014 | int found_extent; |
| 4015 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4016 | int pending_del_nr = 0; |
| 4017 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4018 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4019 | int ret; |
| 4020 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4021 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4022 | |
| 4023 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4024 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4025 | path = btrfs_alloc_path(); |
| 4026 | if (!path) |
| 4027 | return -ENOMEM; |
| 4028 | path->reada = -1; |
| 4029 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4030 | /* |
| 4031 | * We want to drop from the next block forward in case this new size is |
| 4032 | * not block aligned since we will be keeping the last block of the |
| 4033 | * extent just the way it is. |
| 4034 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4035 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4036 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4037 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4038 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4039 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4040 | /* |
| 4041 | * This function is also used to drop the items in the log tree before |
| 4042 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4043 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4044 | * items. |
| 4045 | */ |
| 4046 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4047 | btrfs_kill_delayed_inode_items(inode); |
| 4048 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4049 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4050 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4051 | key.type = (u8)-1; |
| 4052 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4053 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4054 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4055 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4056 | if (ret < 0) { |
| 4057 | err = ret; |
| 4058 | goto out; |
| 4059 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4060 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4061 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4062 | /* there are no items in the tree for us to truncate, we're |
| 4063 | * done |
| 4064 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4065 | if (path->slots[0] == 0) |
| 4066 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4067 | path->slots[0]--; |
| 4068 | } |
| 4069 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4070 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4071 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4072 | leaf = path->nodes[0]; |
| 4073 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4074 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4075 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4076 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4077 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4078 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4079 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4080 | break; |
| 4081 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4082 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4083 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4084 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4085 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4086 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4087 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4088 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4089 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4090 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4091 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4092 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4093 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4094 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4095 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4096 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4097 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4098 | } else { |
| 4099 | if (item_end < new_size) |
| 4100 | break; |
| 4101 | if (found_key.offset >= new_size) |
| 4102 | del_item = 1; |
| 4103 | else |
| 4104 | del_item = 0; |
| 4105 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4106 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4107 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4108 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4109 | goto delete; |
| 4110 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4111 | if (del_item) |
| 4112 | last_size = found_key.offset; |
| 4113 | else |
| 4114 | last_size = new_size; |
| 4115 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4116 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4117 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4118 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4119 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4120 | u64 orig_num_bytes = |
| 4121 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4122 | extent_num_bytes = ALIGN(new_size - |
| 4123 | found_key.offset, |
| 4124 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4125 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4126 | extent_num_bytes); |
| 4127 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4128 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4129 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4130 | &root->state) && |
| 4131 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4132 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4133 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4134 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4135 | extent_num_bytes = |
| 4136 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4137 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4138 | extent_offset = found_key.offset - |
| 4139 | btrfs_file_extent_offset(leaf, fi); |
| 4140 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4141 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4142 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4143 | if (extent_start != 0) { |
| 4144 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4145 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4146 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4147 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4148 | } |
| 4149 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4150 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4151 | /* |
| 4152 | * we can't truncate inline items that have had |
| 4153 | * special encodings |
| 4154 | */ |
| 4155 | if (!del_item && |
| 4156 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4157 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4158 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4159 | u32 size = new_size - found_key.offset; |
| 4160 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4161 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4162 | inode_sub_bytes(inode, item_end + 1 - |
| 4163 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4164 | |
| 4165 | /* |
| 4166 | * update the ram bytes to properly reflect |
| 4167 | * the new size of our item |
| 4168 | */ |
| 4169 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4170 | size = |
| 4171 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4172 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4173 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4174 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4175 | inode_sub_bytes(inode, item_end + 1 - |
| 4176 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4177 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4178 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4179 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4180 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4181 | if (!pending_del_nr) { |
| 4182 | /* no pending yet, add ourselves */ |
| 4183 | pending_del_slot = path->slots[0]; |
| 4184 | pending_del_nr = 1; |
| 4185 | } else if (pending_del_nr && |
| 4186 | path->slots[0] + 1 == pending_del_slot) { |
| 4187 | /* hop on the pending chunk */ |
| 4188 | pending_del_nr++; |
| 4189 | pending_del_slot = path->slots[0]; |
| 4190 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4191 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4192 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4193 | } else { |
| 4194 | break; |
| 4195 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4196 | if (found_extent && |
| 4197 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4198 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4199 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4200 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4201 | extent_num_bytes, 0, |
| 4202 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4203 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4204 | BUG_ON(ret); |
| 4205 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4206 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4207 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4208 | break; |
| 4209 | |
| 4210 | if (path->slots[0] == 0 || |
| 4211 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4212 | if (pending_del_nr) { |
| 4213 | ret = btrfs_del_items(trans, root, path, |
| 4214 | pending_del_slot, |
| 4215 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4216 | if (ret) { |
| 4217 | btrfs_abort_transaction(trans, |
| 4218 | root, ret); |
| 4219 | goto error; |
| 4220 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4221 | pending_del_nr = 0; |
| 4222 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4223 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4224 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4225 | } else { |
| 4226 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4227 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4228 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4229 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4230 | if (pending_del_nr) { |
| 4231 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4232 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4233 | if (ret) |
| 4234 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4235 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4236 | error: |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4237 | if (last_size != (u64)-1) |
| 4238 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4239 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4240 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4241 | } |
| 4242 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4243 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4244 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4245 | * @inode - inode that we're zeroing |
| 4246 | * @from - the offset to start zeroing |
| 4247 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4248 | * offset |
| 4249 | * @front - zero up to the offset instead of from the offset on |
| 4250 | * |
| 4251 | * This will find the page for the "from" offset and cow the page and zero the |
| 4252 | * 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] | 4253 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4254 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4255 | int front) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4256 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4257 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4258 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4259 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4260 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4261 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4262 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4263 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4264 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4265 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4266 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4267 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4268 | int ret = 0; |
| 4269 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4270 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4271 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4272 | if ((offset & (blocksize - 1)) == 0 && |
| 4273 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4274 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4275 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4276 | if (ret) |
| 4277 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4278 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4279 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4280 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4281 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4282 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4283 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4284 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4285 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4286 | |
| 4287 | page_start = page_offset(page); |
| 4288 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4289 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4290 | if (!PageUptodate(page)) { |
| 4291 | ret = btrfs_readpage(NULL, page); |
| 4292 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4293 | if (page->mapping != mapping) { |
| 4294 | unlock_page(page); |
| 4295 | page_cache_release(page); |
| 4296 | goto again; |
| 4297 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4298 | if (!PageUptodate(page)) { |
| 4299 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4300 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4301 | } |
| 4302 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4303 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4304 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4305 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4306 | set_page_extent_mapped(page); |
| 4307 | |
| 4308 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4309 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4310 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4311 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4312 | unlock_page(page); |
| 4313 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4314 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4315 | btrfs_put_ordered_extent(ordered); |
| 4316 | goto again; |
| 4317 | } |
| 4318 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4319 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4320 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4321 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4322 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4323 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4324 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4325 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4326 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4327 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4328 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4329 | goto out_unlock; |
| 4330 | } |
| 4331 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4332 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4333 | if (!len) |
| 4334 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4335 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4336 | if (front) |
| 4337 | memset(kaddr, 0, offset); |
| 4338 | else |
| 4339 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4340 | flush_dcache_page(page); |
| 4341 | kunmap(page); |
| 4342 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4343 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4344 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4345 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4346 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4347 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4348 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4349 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4350 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4351 | unlock_page(page); |
| 4352 | page_cache_release(page); |
| 4353 | out: |
| 4354 | return ret; |
| 4355 | } |
| 4356 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4357 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4358 | u64 offset, u64 len) |
| 4359 | { |
| 4360 | struct btrfs_trans_handle *trans; |
| 4361 | int ret; |
| 4362 | |
| 4363 | /* |
| 4364 | * Still need to make sure the inode looks like it's been updated so |
| 4365 | * that any holes get logged if we fsync. |
| 4366 | */ |
| 4367 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4368 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4369 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4370 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4371 | return 0; |
| 4372 | } |
| 4373 | |
| 4374 | /* |
| 4375 | * 1 - for the one we're dropping |
| 4376 | * 1 - for the one we're adding |
| 4377 | * 1 - for updating the inode. |
| 4378 | */ |
| 4379 | trans = btrfs_start_transaction(root, 3); |
| 4380 | if (IS_ERR(trans)) |
| 4381 | return PTR_ERR(trans); |
| 4382 | |
| 4383 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4384 | if (ret) { |
| 4385 | btrfs_abort_transaction(trans, root, ret); |
| 4386 | btrfs_end_transaction(trans, root); |
| 4387 | return ret; |
| 4388 | } |
| 4389 | |
| 4390 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4391 | 0, 0, len, 0, len, 0, 0, 0); |
| 4392 | if (ret) |
| 4393 | btrfs_abort_transaction(trans, root, ret); |
| 4394 | else |
| 4395 | btrfs_update_inode(trans, root, inode); |
| 4396 | btrfs_end_transaction(trans, root); |
| 4397 | return ret; |
| 4398 | } |
| 4399 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4400 | /* |
| 4401 | * This function puts in dummy file extents for the area we're creating a hole |
| 4402 | * for. So if we are truncating this file to a larger size we need to insert |
| 4403 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4404 | * the range between oldsize and size |
| 4405 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4406 | 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] | 4407 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4408 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4409 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4410 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4411 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4412 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4413 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4414 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4415 | u64 last_byte; |
| 4416 | u64 cur_offset; |
| 4417 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4418 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4419 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4420 | /* |
| 4421 | * If our size started in the middle of a page we need to zero out the |
| 4422 | * rest of the page before we expand the i_size, otherwise we could |
| 4423 | * expose stale data. |
| 4424 | */ |
| 4425 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4426 | if (err) |
| 4427 | return err; |
| 4428 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4429 | if (size <= hole_start) |
| 4430 | return 0; |
| 4431 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4432 | while (1) { |
| 4433 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4434 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4435 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4436 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4437 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4438 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4439 | if (!ordered) |
| 4440 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4441 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4442 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4443 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4444 | btrfs_put_ordered_extent(ordered); |
| 4445 | } |
| 4446 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4447 | cur_offset = hole_start; |
| 4448 | while (1) { |
| 4449 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4450 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4451 | if (IS_ERR(em)) { |
| 4452 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4453 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4454 | break; |
| 4455 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4456 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4457 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4458 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4459 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4460 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4461 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4462 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4463 | hole_size); |
| 4464 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4465 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4466 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4467 | cur_offset + hole_size - 1, 0); |
| 4468 | hole_em = alloc_extent_map(); |
| 4469 | if (!hole_em) { |
| 4470 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4471 | &BTRFS_I(inode)->runtime_flags); |
| 4472 | goto next; |
| 4473 | } |
| 4474 | hole_em->start = cur_offset; |
| 4475 | hole_em->len = hole_size; |
| 4476 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4477 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4478 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4479 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4480 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4481 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4482 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4483 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4484 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4485 | |
| 4486 | while (1) { |
| 4487 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4488 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4489 | write_unlock(&em_tree->lock); |
| 4490 | if (err != -EEXIST) |
| 4491 | break; |
| 4492 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4493 | cur_offset + |
| 4494 | hole_size - 1, 0); |
| 4495 | } |
| 4496 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4497 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4498 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4499 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4500 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4501 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4502 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4503 | break; |
| 4504 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4505 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4506 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4507 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4508 | return err; |
| 4509 | } |
| 4510 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4511 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4512 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4513 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4514 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4515 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4516 | loff_t newsize = attr->ia_size; |
| 4517 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4518 | int ret; |
| 4519 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4520 | /* |
| 4521 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4522 | * special case where we need to update the times despite not having |
| 4523 | * these flags set. For all other operations the VFS set these flags |
| 4524 | * explicitly if it wants a timestamp update. |
| 4525 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4526 | if (newsize != oldsize) { |
| 4527 | inode_inc_iversion(inode); |
| 4528 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4529 | inode->i_ctime = inode->i_mtime = |
| 4530 | current_fs_time(inode->i_sb); |
| 4531 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4532 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4533 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4534 | truncate_pagecache(inode, newsize); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4535 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4536 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4537 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4538 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4539 | trans = btrfs_start_transaction(root, 1); |
| 4540 | if (IS_ERR(trans)) |
| 4541 | return PTR_ERR(trans); |
| 4542 | |
| 4543 | i_size_write(inode, newsize); |
| 4544 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4545 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4546 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4547 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4548 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4549 | /* |
| 4550 | * We're truncating a file that used to have good data down to |
| 4551 | * zero. Make sure it gets into the ordered flush list so that |
| 4552 | * any new writes get down to disk quickly. |
| 4553 | */ |
| 4554 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4555 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4556 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4557 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4558 | /* |
| 4559 | * 1 for the orphan item we're going to add |
| 4560 | * 1 for the orphan item deletion. |
| 4561 | */ |
| 4562 | trans = btrfs_start_transaction(root, 2); |
| 4563 | if (IS_ERR(trans)) |
| 4564 | return PTR_ERR(trans); |
| 4565 | |
| 4566 | /* |
| 4567 | * We need to do this in case we fail at _any_ point during the |
| 4568 | * actual truncate. Once we do the truncate_setsize we could |
| 4569 | * invalidate pages which forces any outstanding ordered io to |
| 4570 | * be instantly completed which will give us extents that need |
| 4571 | * to be truncated. If we fail to get an orphan inode down we |
| 4572 | * could have left over extents that were never meant to live, |
| 4573 | * so we need to garuntee from this point on that everything |
| 4574 | * will be consistent. |
| 4575 | */ |
| 4576 | ret = btrfs_orphan_add(trans, inode); |
| 4577 | btrfs_end_transaction(trans, root); |
| 4578 | if (ret) |
| 4579 | return ret; |
| 4580 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4581 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4582 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4583 | |
| 4584 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4585 | btrfs_inode_block_unlocked_dio(inode); |
| 4586 | inode_dio_wait(inode); |
| 4587 | btrfs_inode_resume_unlocked_dio(inode); |
| 4588 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4589 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4590 | if (ret && inode->i_nlink) { |
| 4591 | int err; |
| 4592 | |
| 4593 | /* |
| 4594 | * failed to truncate, disk_i_size is only adjusted down |
| 4595 | * as we remove extents, so it should represent the true |
| 4596 | * size of the inode, so reset the in memory size and |
| 4597 | * delete our orphan entry. |
| 4598 | */ |
| 4599 | trans = btrfs_join_transaction(root); |
| 4600 | if (IS_ERR(trans)) { |
| 4601 | btrfs_orphan_del(NULL, inode); |
| 4602 | return ret; |
| 4603 | } |
| 4604 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4605 | err = btrfs_orphan_del(trans, inode); |
| 4606 | if (err) |
| 4607 | btrfs_abort_transaction(trans, root, err); |
| 4608 | btrfs_end_transaction(trans, root); |
| 4609 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4612 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4615 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4616 | { |
| 4617 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4618 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4619 | int err; |
| 4620 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4621 | if (btrfs_root_readonly(root)) |
| 4622 | return -EROFS; |
| 4623 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4624 | err = inode_change_ok(inode, attr); |
| 4625 | if (err) |
| 4626 | return err; |
| 4627 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4628 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4629 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4630 | if (err) |
| 4631 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4632 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4633 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4634 | if (attr->ia_valid) { |
| 4635 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4636 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4637 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4638 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4639 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4640 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4641 | } |
| 4642 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4643 | return err; |
| 4644 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4645 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4646 | /* |
| 4647 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4648 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4649 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4650 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4651 | * extent_state structures over and over, wasting lots of time. |
| 4652 | * |
| 4653 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4654 | * those expensive operations on a per page basis and do only the ordered io |
| 4655 | * finishing, while we release here the extent_map and extent_state structures, |
| 4656 | * without the excessive merging and splitting. |
| 4657 | */ |
| 4658 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4659 | { |
| 4660 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4661 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4662 | struct rb_node *node; |
| 4663 | |
| 4664 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4665 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4666 | |
| 4667 | write_lock(&map_tree->lock); |
| 4668 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4669 | struct extent_map *em; |
| 4670 | |
| 4671 | node = rb_first(&map_tree->map); |
| 4672 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4673 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4674 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4675 | remove_extent_mapping(map_tree, em); |
| 4676 | free_extent_map(em); |
| 4677 | } |
| 4678 | write_unlock(&map_tree->lock); |
| 4679 | |
| 4680 | spin_lock(&io_tree->lock); |
| 4681 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4682 | struct extent_state *state; |
| 4683 | struct extent_state *cached_state = NULL; |
| 4684 | |
| 4685 | node = rb_first(&io_tree->state); |
| 4686 | state = rb_entry(node, struct extent_state, rb_node); |
| 4687 | atomic_inc(&state->refs); |
| 4688 | spin_unlock(&io_tree->lock); |
| 4689 | |
| 4690 | lock_extent_bits(io_tree, state->start, state->end, |
| 4691 | 0, &cached_state); |
| 4692 | clear_extent_bit(io_tree, state->start, state->end, |
| 4693 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4694 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4695 | EXTENT_DEFRAG, 1, 1, |
| 4696 | &cached_state, GFP_NOFS); |
| 4697 | free_extent_state(state); |
| 4698 | |
| 4699 | spin_lock(&io_tree->lock); |
| 4700 | } |
| 4701 | spin_unlock(&io_tree->lock); |
| 4702 | } |
| 4703 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4704 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4705 | { |
| 4706 | struct btrfs_trans_handle *trans; |
| 4707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4708 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4709 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4710 | int ret; |
| 4711 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4712 | trace_btrfs_inode_evict(inode); |
| 4713 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4714 | evict_inode_truncate_pages(inode); |
| 4715 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4716 | if (inode->i_nlink && |
| 4717 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4718 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4719 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4720 | goto no_delete; |
| 4721 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4722 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4723 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4724 | goto no_delete; |
| 4725 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4726 | /* 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] | 4727 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4728 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4729 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4730 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4731 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4732 | goto no_delete; |
| 4733 | } |
| 4734 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4735 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4736 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4737 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4738 | goto no_delete; |
| 4739 | } |
| 4740 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4741 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4742 | if (ret) { |
| 4743 | btrfs_orphan_del(NULL, inode); |
| 4744 | goto no_delete; |
| 4745 | } |
| 4746 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4747 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4748 | if (!rsv) { |
| 4749 | btrfs_orphan_del(NULL, inode); |
| 4750 | goto no_delete; |
| 4751 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4752 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4753 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4754 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4755 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4756 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4757 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4758 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4759 | * This is a bit simpler than btrfs_truncate since we've already |
| 4760 | * reserved our space for our orphan item in the unlink, so we just |
| 4761 | * need to reserve some slack space in case we add bytes and update |
| 4762 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4763 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4764 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4765 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4766 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4767 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4768 | /* |
| 4769 | * Try and steal from the global reserve since we will |
| 4770 | * likely not use this space anyway, we want to try as |
| 4771 | * hard as possible to get this to work. |
| 4772 | */ |
| 4773 | if (ret) |
| 4774 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4775 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4776 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4777 | btrfs_warn(root->fs_info, |
| 4778 | "Could not get space for a delete, will truncate on mount %d", |
| 4779 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4780 | btrfs_orphan_del(NULL, inode); |
| 4781 | btrfs_free_block_rsv(root, rsv); |
| 4782 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4783 | } |
| 4784 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4785 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4786 | if (IS_ERR(trans)) { |
| 4787 | btrfs_orphan_del(NULL, inode); |
| 4788 | btrfs_free_block_rsv(root, rsv); |
| 4789 | goto no_delete; |
| 4790 | } |
| 4791 | |
| 4792 | trans->block_rsv = rsv; |
| 4793 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4794 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4795 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4796 | break; |
| 4797 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4798 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4799 | btrfs_end_transaction(trans, root); |
| 4800 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4801 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4802 | } |
| 4803 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4804 | btrfs_free_block_rsv(root, rsv); |
| 4805 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4806 | /* |
| 4807 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4808 | * in the tree. They will be cleaned up on the next mount. |
| 4809 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4810 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4811 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4812 | btrfs_orphan_del(trans, inode); |
| 4813 | } else { |
| 4814 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4815 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4816 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4817 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4818 | if (!(root == root->fs_info->tree_root || |
| 4819 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4820 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4821 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4822 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4823 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4824 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4825 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4826 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4827 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | /* |
| 4831 | * this returns the key found in the dir entry in the location pointer. |
| 4832 | * If no dir entries were found, location->objectid is 0. |
| 4833 | */ |
| 4834 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4835 | struct btrfs_key *location) |
| 4836 | { |
| 4837 | const char *name = dentry->d_name.name; |
| 4838 | int namelen = dentry->d_name.len; |
| 4839 | struct btrfs_dir_item *di; |
| 4840 | struct btrfs_path *path; |
| 4841 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4842 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4843 | |
| 4844 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4845 | if (!path) |
| 4846 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4847 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4848 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4849 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4850 | if (IS_ERR(di)) |
| 4851 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4852 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4853 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4854 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4855 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4856 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4857 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4858 | btrfs_free_path(path); |
| 4859 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4860 | out_err: |
| 4861 | location->objectid = 0; |
| 4862 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /* |
| 4866 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4867 | * needs to be changed to reflect the root directory of the tree root. This |
| 4868 | * is kind of like crossing a mount point. |
| 4869 | */ |
| 4870 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4871 | struct inode *dir, |
| 4872 | struct dentry *dentry, |
| 4873 | struct btrfs_key *location, |
| 4874 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4875 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4876 | struct btrfs_path *path; |
| 4877 | struct btrfs_root *new_root; |
| 4878 | struct btrfs_root_ref *ref; |
| 4879 | struct extent_buffer *leaf; |
| 4880 | int ret; |
| 4881 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4882 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4883 | path = btrfs_alloc_path(); |
| 4884 | if (!path) { |
| 4885 | err = -ENOMEM; |
| 4886 | goto out; |
| 4887 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4888 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4889 | err = -ENOENT; |
Kelley Nielsen | 75ac2dd | 2013-11-04 19:35:58 -0800 | [diff] [blame] | 4890 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
| 4891 | BTRFS_I(dir)->root->root_key.objectid, |
| 4892 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4893 | if (ret) { |
| 4894 | if (ret < 0) |
| 4895 | err = ret; |
| 4896 | goto out; |
| 4897 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4898 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4899 | leaf = path->nodes[0]; |
| 4900 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4901 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4902 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4903 | goto out; |
| 4904 | |
| 4905 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4906 | (unsigned long)(ref + 1), |
| 4907 | dentry->d_name.len); |
| 4908 | if (ret) |
| 4909 | goto out; |
| 4910 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4911 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4912 | |
| 4913 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4914 | if (IS_ERR(new_root)) { |
| 4915 | err = PTR_ERR(new_root); |
| 4916 | goto out; |
| 4917 | } |
| 4918 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4919 | *sub_root = new_root; |
| 4920 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4921 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4922 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4923 | err = 0; |
| 4924 | out: |
| 4925 | btrfs_free_path(path); |
| 4926 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4927 | } |
| 4928 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4929 | static void inode_tree_add(struct inode *inode) |
| 4930 | { |
| 4931 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4932 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4933 | struct rb_node **p; |
| 4934 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4935 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4936 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4937 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4938 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4939 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4940 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4941 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4942 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4943 | while (*p) { |
| 4944 | parent = *p; |
| 4945 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4946 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4947 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4948 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4949 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4950 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4951 | else { |
| 4952 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4953 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4954 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4955 | RB_CLEAR_NODE(parent); |
| 4956 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4957 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4958 | } |
| 4959 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4960 | rb_link_node(new, parent, p); |
| 4961 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4962 | spin_unlock(&root->inode_lock); |
| 4963 | } |
| 4964 | |
| 4965 | static void inode_tree_del(struct inode *inode) |
| 4966 | { |
| 4967 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4968 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4969 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4970 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4971 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4972 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4973 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4974 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4975 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4976 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4977 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4978 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4979 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 4980 | spin_lock(&root->inode_lock); |
| 4981 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 4982 | spin_unlock(&root->inode_lock); |
| 4983 | if (empty) |
| 4984 | btrfs_add_dead_root(root); |
| 4985 | } |
| 4986 | } |
| 4987 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4988 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4989 | { |
| 4990 | struct rb_node *node; |
| 4991 | struct rb_node *prev; |
| 4992 | struct btrfs_inode *entry; |
| 4993 | struct inode *inode; |
| 4994 | u64 objectid = 0; |
| 4995 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 4996 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 4997 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4998 | |
| 4999 | spin_lock(&root->inode_lock); |
| 5000 | again: |
| 5001 | node = root->inode_tree.rb_node; |
| 5002 | prev = NULL; |
| 5003 | while (node) { |
| 5004 | prev = node; |
| 5005 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5006 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5007 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5008 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5009 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5010 | node = node->rb_right; |
| 5011 | else |
| 5012 | break; |
| 5013 | } |
| 5014 | if (!node) { |
| 5015 | while (prev) { |
| 5016 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5017 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5018 | node = prev; |
| 5019 | break; |
| 5020 | } |
| 5021 | prev = rb_next(prev); |
| 5022 | } |
| 5023 | } |
| 5024 | while (node) { |
| 5025 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5026 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5027 | inode = igrab(&entry->vfs_inode); |
| 5028 | if (inode) { |
| 5029 | spin_unlock(&root->inode_lock); |
| 5030 | if (atomic_read(&inode->i_count) > 1) |
| 5031 | d_prune_aliases(inode); |
| 5032 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5033 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5034 | * the inode cache when its usage count |
| 5035 | * hits zero. |
| 5036 | */ |
| 5037 | iput(inode); |
| 5038 | cond_resched(); |
| 5039 | spin_lock(&root->inode_lock); |
| 5040 | goto again; |
| 5041 | } |
| 5042 | |
| 5043 | if (cond_resched_lock(&root->inode_lock)) |
| 5044 | goto again; |
| 5045 | |
| 5046 | node = rb_next(node); |
| 5047 | } |
| 5048 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5049 | } |
| 5050 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5051 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5052 | { |
| 5053 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5054 | inode->i_ino = args->location->objectid; |
| 5055 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5056 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5057 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5058 | return 0; |
| 5059 | } |
| 5060 | |
| 5061 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5062 | { |
| 5063 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5064 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5065 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5066 | } |
| 5067 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5068 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5069 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5070 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5071 | { |
| 5072 | struct inode *inode; |
| 5073 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5074 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5075 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5076 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5077 | args.root = root; |
| 5078 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5079 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5080 | btrfs_init_locked_inode, |
| 5081 | (void *)&args); |
| 5082 | return inode; |
| 5083 | } |
| 5084 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5085 | /* Get an inode object given its location and corresponding root. |
| 5086 | * Returns in *is_new if the inode was read from disk |
| 5087 | */ |
| 5088 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5089 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5090 | { |
| 5091 | struct inode *inode; |
| 5092 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5093 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5094 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5095 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5096 | |
| 5097 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5098 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5099 | if (!is_bad_inode(inode)) { |
| 5100 | inode_tree_add(inode); |
| 5101 | unlock_new_inode(inode); |
| 5102 | if (new) |
| 5103 | *new = 1; |
| 5104 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5105 | unlock_new_inode(inode); |
| 5106 | iput(inode); |
| 5107 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5108 | } |
| 5109 | } |
| 5110 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5111 | return inode; |
| 5112 | } |
| 5113 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5114 | static struct inode *new_simple_dir(struct super_block *s, |
| 5115 | struct btrfs_key *key, |
| 5116 | struct btrfs_root *root) |
| 5117 | { |
| 5118 | struct inode *inode = new_inode(s); |
| 5119 | |
| 5120 | if (!inode) |
| 5121 | return ERR_PTR(-ENOMEM); |
| 5122 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5123 | BTRFS_I(inode)->root = root; |
| 5124 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5125 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5126 | |
| 5127 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5128 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5129 | inode->i_fop = &simple_dir_operations; |
| 5130 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5131 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5132 | |
| 5133 | return inode; |
| 5134 | } |
| 5135 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5136 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5137 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5138 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5139 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5140 | struct btrfs_root *sub_root = root; |
| 5141 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5142 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5143 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5144 | |
| 5145 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5146 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5147 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5148 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5149 | if (ret < 0) |
| 5150 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5151 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5152 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5153 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5154 | |
| 5155 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5156 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5157 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5158 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5159 | |
| 5160 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5161 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5162 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5163 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5164 | &location, &sub_root); |
| 5165 | if (ret < 0) { |
| 5166 | if (ret != -ENOENT) |
| 5167 | inode = ERR_PTR(ret); |
| 5168 | else |
| 5169 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5170 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5171 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5172 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5173 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5174 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5175 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5176 | down_read(&root->fs_info->cleanup_work_sem); |
| 5177 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5178 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5179 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5180 | if (ret) { |
| 5181 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5182 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5183 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5186 | return inode; |
| 5187 | } |
| 5188 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5189 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5190 | { |
| 5191 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5192 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5193 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5194 | if (!inode && !IS_ROOT(dentry)) |
| 5195 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5196 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5197 | if (inode) { |
| 5198 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5199 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5200 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5201 | |
| 5202 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5203 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5204 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5205 | return 0; |
| 5206 | } |
| 5207 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5208 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5209 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5210 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5211 | } |
| 5212 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5213 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5214 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5215 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5216 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5217 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5218 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5219 | if (IS_ERR(inode)) { |
| 5220 | if (PTR_ERR(inode) == -ENOENT) |
| 5221 | inode = NULL; |
| 5222 | else |
| 5223 | return ERR_CAST(inode); |
| 5224 | } |
| 5225 | |
Josef Bacik | 3a0dfa6 | 2014-02-14 13:43:48 -0500 | [diff] [blame] | 5226 | return d_materialise_unique(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5227 | } |
| 5228 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5229 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5230 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5231 | }; |
| 5232 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5233 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5234 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5235 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5236 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5237 | struct btrfs_item *item; |
| 5238 | struct btrfs_dir_item *di; |
| 5239 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5240 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5241 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5242 | struct list_head ins_list; |
| 5243 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5244 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5245 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5246 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5247 | unsigned char d_type; |
| 5248 | int over = 0; |
| 5249 | u32 di_cur; |
| 5250 | u32 di_total; |
| 5251 | u32 di_len; |
| 5252 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5253 | char tmp_name[32]; |
| 5254 | char *name_ptr; |
| 5255 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5256 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5257 | |
| 5258 | /* FIXME, use a real flag for deciding about the key type */ |
| 5259 | if (root->fs_info->tree_root == root) |
| 5260 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5261 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5262 | if (!dir_emit_dots(file, ctx)) |
| 5263 | return 0; |
| 5264 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5265 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5266 | if (!path) |
| 5267 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5268 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5269 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5270 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5271 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5272 | INIT_LIST_HEAD(&ins_list); |
| 5273 | INIT_LIST_HEAD(&del_list); |
| 5274 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5275 | } |
| 5276 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5277 | btrfs_set_key_type(&key, key_type); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5278 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5279 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5280 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5281 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5282 | if (ret < 0) |
| 5283 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5284 | |
| 5285 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5286 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5287 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5288 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5289 | ret = btrfs_next_leaf(root, path); |
| 5290 | if (ret < 0) |
| 5291 | goto err; |
| 5292 | else if (ret > 0) |
| 5293 | break; |
| 5294 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5295 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5296 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5297 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5298 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5299 | |
| 5300 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5301 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5302 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5303 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5304 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5305 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5306 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5307 | btrfs_should_delete_dir_index(&del_list, |
| 5308 | found_key.offset)) |
| 5309 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5310 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5311 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5312 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5313 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5314 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5315 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5316 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5317 | |
| 5318 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5319 | struct btrfs_key location; |
| 5320 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5321 | if (verify_dir_item(root, leaf, di)) |
| 5322 | break; |
| 5323 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5324 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5325 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5326 | name_ptr = tmp_name; |
| 5327 | } else { |
| 5328 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5329 | if (!name_ptr) { |
| 5330 | ret = -ENOMEM; |
| 5331 | goto err; |
| 5332 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5333 | } |
| 5334 | read_extent_buffer(leaf, name_ptr, |
| 5335 | (unsigned long)(di + 1), name_len); |
| 5336 | |
| 5337 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5338 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5339 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5340 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5341 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5342 | * skip it. |
| 5343 | * |
| 5344 | * In contrast to old kernels, we insert the snapshot's |
| 5345 | * dir item and dir index after it has been created, so |
| 5346 | * we won't find a reference to our own snapshot. We |
| 5347 | * still keep the following code for backward |
| 5348 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5349 | */ |
| 5350 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5351 | location.objectid == root->root_key.objectid) { |
| 5352 | over = 0; |
| 5353 | goto skip; |
| 5354 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5355 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5356 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5357 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5358 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5359 | if (name_ptr != tmp_name) |
| 5360 | kfree(name_ptr); |
| 5361 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5362 | if (over) |
| 5363 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5364 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5365 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5366 | di_cur += di_len; |
| 5367 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5368 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5369 | next: |
| 5370 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5371 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5372 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5373 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5374 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5375 | ctx->pos++; |
| 5376 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5377 | if (ret) |
| 5378 | goto nopos; |
| 5379 | } |
| 5380 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5381 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5382 | ctx->pos++; |
| 5383 | |
| 5384 | /* |
| 5385 | * Stop new entries from being returned after we return the last |
| 5386 | * entry. |
| 5387 | * |
| 5388 | * New directory entries are assigned a strictly increasing |
| 5389 | * offset. This means that new entries created during readdir |
| 5390 | * are *guaranteed* to be seen in the future by that readdir. |
| 5391 | * This has broken buggy programs which operate on names as |
| 5392 | * they're returned by readdir. Until we re-use freed offsets |
| 5393 | * we have this hack to stop new entries from being returned |
| 5394 | * under the assumption that they'll never reach this huge |
| 5395 | * offset. |
| 5396 | * |
| 5397 | * This is being careful not to overflow 32bit loff_t unless the |
| 5398 | * last entry requires it because doing so has broken 32bit apps |
| 5399 | * in the past. |
| 5400 | */ |
| 5401 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5402 | if (ctx->pos >= INT_MAX) |
| 5403 | ctx->pos = LLONG_MAX; |
| 5404 | else |
| 5405 | ctx->pos = INT_MAX; |
| 5406 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5407 | nopos: |
| 5408 | ret = 0; |
| 5409 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5410 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5411 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5412 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5413 | return ret; |
| 5414 | } |
| 5415 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5416 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5417 | { |
| 5418 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5419 | struct btrfs_trans_handle *trans; |
| 5420 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5421 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5422 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5423 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5424 | return 0; |
| 5425 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5426 | 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] | 5427 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5428 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5429 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5430 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5431 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5432 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5433 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5434 | if (IS_ERR(trans)) |
| 5435 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5436 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5437 | } |
| 5438 | return ret; |
| 5439 | } |
| 5440 | |
| 5441 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5442 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5443 | * inode changes. But, it is most likely to find the inode in cache. |
| 5444 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5445 | * to keep or drop this code. |
| 5446 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5447 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5448 | { |
| 5449 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5450 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5451 | int ret; |
| 5452 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5453 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5454 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5455 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5456 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5457 | if (IS_ERR(trans)) |
| 5458 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5459 | |
| 5460 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5461 | if (ret && ret == -ENOSPC) { |
| 5462 | /* whoops, lets try again with the full transaction */ |
| 5463 | btrfs_end_transaction(trans, root); |
| 5464 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5465 | if (IS_ERR(trans)) |
| 5466 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5467 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5468 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5469 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5470 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5471 | if (BTRFS_I(inode)->delayed_node) |
| 5472 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5473 | |
| 5474 | return ret; |
| 5475 | } |
| 5476 | |
| 5477 | /* |
| 5478 | * This is a copy of file_update_time. We need this so we can return error on |
| 5479 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5480 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5481 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5482 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5483 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5484 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5485 | |
| 5486 | if (btrfs_root_readonly(root)) |
| 5487 | return -EROFS; |
| 5488 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5489 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5490 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5491 | if (flags & S_CTIME) |
| 5492 | inode->i_ctime = *now; |
| 5493 | if (flags & S_MTIME) |
| 5494 | inode->i_mtime = *now; |
| 5495 | if (flags & S_ATIME) |
| 5496 | inode->i_atime = *now; |
| 5497 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5498 | } |
| 5499 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5500 | /* |
| 5501 | * find the highest existing sequence number in a directory |
| 5502 | * and then set the in-memory index_cnt variable to reflect |
| 5503 | * free sequence numbers |
| 5504 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5505 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5506 | { |
| 5507 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5508 | struct btrfs_key key, found_key; |
| 5509 | struct btrfs_path *path; |
| 5510 | struct extent_buffer *leaf; |
| 5511 | int ret; |
| 5512 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5513 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5514 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5515 | key.offset = (u64)-1; |
| 5516 | |
| 5517 | path = btrfs_alloc_path(); |
| 5518 | if (!path) |
| 5519 | return -ENOMEM; |
| 5520 | |
| 5521 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5522 | if (ret < 0) |
| 5523 | goto out; |
| 5524 | /* FIXME: we should be able to handle this */ |
| 5525 | if (ret == 0) |
| 5526 | goto out; |
| 5527 | ret = 0; |
| 5528 | |
| 5529 | /* |
| 5530 | * MAGIC NUMBER EXPLANATION: |
| 5531 | * since we search a directory based on f_pos we have to start at 2 |
| 5532 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5533 | * else has to start at 2 |
| 5534 | */ |
| 5535 | if (path->slots[0] == 0) { |
| 5536 | BTRFS_I(inode)->index_cnt = 2; |
| 5537 | goto out; |
| 5538 | } |
| 5539 | |
| 5540 | path->slots[0]--; |
| 5541 | |
| 5542 | leaf = path->nodes[0]; |
| 5543 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5544 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5545 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5546 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5547 | BTRFS_I(inode)->index_cnt = 2; |
| 5548 | goto out; |
| 5549 | } |
| 5550 | |
| 5551 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5552 | out: |
| 5553 | btrfs_free_path(path); |
| 5554 | return ret; |
| 5555 | } |
| 5556 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5557 | /* |
| 5558 | * helper to find a free sequence number in a given directory. This current |
| 5559 | * code is very simple, later versions will do smarter things in the btree |
| 5560 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5561 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5562 | { |
| 5563 | int ret = 0; |
| 5564 | |
| 5565 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5566 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5567 | if (ret) { |
| 5568 | ret = btrfs_set_inode_index_count(dir); |
| 5569 | if (ret) |
| 5570 | return ret; |
| 5571 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5572 | } |
| 5573 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5574 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5575 | BTRFS_I(dir)->index_cnt++; |
| 5576 | |
| 5577 | return ret; |
| 5578 | } |
| 5579 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5580 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5581 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5582 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5583 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5584 | u64 ref_objectid, u64 objectid, |
| 5585 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5586 | { |
| 5587 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5588 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5589 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5590 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5591 | struct btrfs_inode_ref *ref; |
| 5592 | struct btrfs_key key[2]; |
| 5593 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5594 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5595 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5596 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5597 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5598 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5599 | if (!path) |
| 5600 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5601 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5602 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5603 | if (!inode) { |
| 5604 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5605 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5606 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5607 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5608 | /* |
| 5609 | * we have to initialize this early, so we can reclaim the inode |
| 5610 | * number if we fail afterwards in this function. |
| 5611 | */ |
| 5612 | inode->i_ino = objectid; |
| 5613 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5614 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5615 | trace_btrfs_inode_request(dir); |
| 5616 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5617 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5618 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5619 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5620 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5621 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5622 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5623 | } else if (dir) { |
| 5624 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5625 | } |
| 5626 | /* |
| 5627 | * index_cnt is ignored for everything but a dir, |
| 5628 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5629 | * number |
| 5630 | */ |
| 5631 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5632 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5633 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5634 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5635 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5636 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5637 | /* |
| 5638 | * We could have gotten an inode number from somebody who was fsynced |
| 5639 | * and then removed in this same transaction, so let's just set full |
| 5640 | * sync since it will be a full sync anyway and this will blow away the |
| 5641 | * old info in the log. |
| 5642 | */ |
| 5643 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5644 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5645 | key[0].objectid = objectid; |
| 5646 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5647 | key[0].offset = 0; |
| 5648 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5649 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5650 | |
| 5651 | if (name) { |
| 5652 | /* |
| 5653 | * Start new inodes with an inode_ref. This is slightly more |
| 5654 | * efficient for small numbers of hard links since they will |
| 5655 | * be packed into one item. Extended refs will kick in if we |
| 5656 | * add more hard links than can fit in the ref item. |
| 5657 | */ |
| 5658 | key[1].objectid = objectid; |
| 5659 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5660 | key[1].offset = ref_objectid; |
| 5661 | |
| 5662 | sizes[1] = name_len + sizeof(*ref); |
| 5663 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5664 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5665 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5666 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5667 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5668 | goto fail; |
| 5669 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5670 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5671 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5672 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5673 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5674 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5675 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5676 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5677 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5678 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5679 | if (name) { |
| 5680 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5681 | struct btrfs_inode_ref); |
| 5682 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 5683 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 5684 | ptr = (unsigned long)(ref + 1); |
| 5685 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5686 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5687 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5688 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5689 | btrfs_free_path(path); |
| 5690 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5691 | location = &BTRFS_I(inode)->location; |
| 5692 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5693 | location->offset = 0; |
| 5694 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5695 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5696 | btrfs_inherit_iflags(inode, dir); |
| 5697 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5698 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5699 | if (btrfs_test_opt(root, NODATASUM)) |
| 5700 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5701 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5702 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5703 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5704 | } |
| 5705 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5706 | btrfs_insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5707 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5708 | |
| 5709 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5710 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5711 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5712 | btrfs_update_root_times(trans, root); |
| 5713 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5714 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5715 | if (ret) |
| 5716 | btrfs_err(root->fs_info, |
| 5717 | "error inheriting props for ino %llu (root %llu): %d", |
| 5718 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5719 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5720 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5721 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5722 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5723 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5724 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5725 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5726 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5730 | { |
| 5731 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5732 | } |
| 5733 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5734 | /* |
| 5735 | * utility function to add 'inode' into 'parent_inode' with |
| 5736 | * a give name and a given sequence number. |
| 5737 | * if 'add_backref' is true, also insert a backref from the |
| 5738 | * inode to the parent directory. |
| 5739 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5740 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5741 | struct inode *parent_inode, struct inode *inode, |
| 5742 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5743 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5744 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5745 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5746 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5747 | u64 ino = btrfs_ino(inode); |
| 5748 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5749 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5750 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5751 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5752 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5753 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5754 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5755 | key.offset = 0; |
| 5756 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5757 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5758 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5759 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5760 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5761 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5762 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5763 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5764 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5765 | } |
| 5766 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5767 | /* Nothing to clean up yet */ |
| 5768 | if (ret) |
| 5769 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5770 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5771 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5772 | parent_inode, &key, |
| 5773 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5774 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5775 | goto fail_dir_item; |
| 5776 | else if (ret) { |
| 5777 | btrfs_abort_transaction(trans, root, ret); |
| 5778 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5779 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5780 | |
| 5781 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5782 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5783 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5784 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5785 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5786 | if (ret) |
| 5787 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5788 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5789 | |
| 5790 | fail_dir_item: |
| 5791 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5792 | u64 local_index; |
| 5793 | int err; |
| 5794 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5795 | key.objectid, root->root_key.objectid, |
| 5796 | parent_ino, &local_index, name, name_len); |
| 5797 | |
| 5798 | } else if (add_backref) { |
| 5799 | u64 local_index; |
| 5800 | int err; |
| 5801 | |
| 5802 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5803 | ino, parent_ino, &local_index); |
| 5804 | } |
| 5805 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5809 | struct inode *dir, struct dentry *dentry, |
| 5810 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5811 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5812 | int err = btrfs_add_link(trans, dir, inode, |
| 5813 | dentry->d_name.name, dentry->d_name.len, |
| 5814 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5815 | if (err > 0) |
| 5816 | err = -EEXIST; |
| 5817 | return err; |
| 5818 | } |
| 5819 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5820 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5821 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5822 | { |
| 5823 | struct btrfs_trans_handle *trans; |
| 5824 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5825 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5826 | int err; |
| 5827 | int drop_inode = 0; |
| 5828 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5829 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5830 | |
| 5831 | if (!new_valid_dev(rdev)) |
| 5832 | return -EINVAL; |
| 5833 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5834 | /* |
| 5835 | * 2 for inode item and ref |
| 5836 | * 2 for dir items |
| 5837 | * 1 for xattr if selinux is on |
| 5838 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5839 | trans = btrfs_start_transaction(root, 5); |
| 5840 | if (IS_ERR(trans)) |
| 5841 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5842 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5843 | err = btrfs_find_free_ino(root, &objectid); |
| 5844 | if (err) |
| 5845 | goto out_unlock; |
| 5846 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5847 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5848 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5849 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5850 | if (IS_ERR(inode)) { |
| 5851 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5852 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5853 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5854 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5855 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5856 | if (err) { |
| 5857 | drop_inode = 1; |
| 5858 | goto out_unlock; |
| 5859 | } |
| 5860 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5861 | /* |
| 5862 | * If the active LSM wants to access the inode during |
| 5863 | * d_instantiate it needs these. Smack checks to see |
| 5864 | * if the filesystem supports xattrs by looking at the |
| 5865 | * ops vector. |
| 5866 | */ |
| 5867 | |
| 5868 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5869 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5870 | if (err) |
| 5871 | drop_inode = 1; |
| 5872 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5873 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5874 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5875 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5876 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5877 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5878 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 5879 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5880 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5881 | if (drop_inode) { |
| 5882 | inode_dec_link_count(inode); |
| 5883 | iput(inode); |
| 5884 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5885 | return err; |
| 5886 | } |
| 5887 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5888 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5889 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5890 | { |
| 5891 | struct btrfs_trans_handle *trans; |
| 5892 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5893 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5894 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5895 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5896 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5897 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5898 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5899 | /* |
| 5900 | * 2 for inode item and ref |
| 5901 | * 2 for dir items |
| 5902 | * 1 for xattr if selinux is on |
| 5903 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5904 | trans = btrfs_start_transaction(root, 5); |
| 5905 | if (IS_ERR(trans)) |
| 5906 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5907 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5908 | err = btrfs_find_free_ino(root, &objectid); |
| 5909 | if (err) |
| 5910 | goto out_unlock; |
| 5911 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5912 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5913 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5914 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5915 | if (IS_ERR(inode)) { |
| 5916 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5917 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5918 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5919 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5920 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5921 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5922 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5923 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5924 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5925 | err = btrfs_update_inode(trans, root, inode); |
| 5926 | if (err) |
| 5927 | goto out_unlock; |
| 5928 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5929 | /* |
| 5930 | * If the active LSM wants to access the inode during |
| 5931 | * d_instantiate it needs these. Smack checks to see |
| 5932 | * if the filesystem supports xattrs by looking at the |
| 5933 | * ops vector. |
| 5934 | */ |
| 5935 | inode->i_fop = &btrfs_file_operations; |
| 5936 | inode->i_op = &btrfs_file_inode_operations; |
| 5937 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5938 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5939 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5940 | goto out_unlock; |
| 5941 | |
| 5942 | inode->i_mapping->a_ops = &btrfs_aops; |
| 5943 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 5944 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 5945 | d_instantiate(dentry, inode); |
| 5946 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5947 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5948 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5949 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5950 | inode_dec_link_count(inode); |
| 5951 | iput(inode); |
| 5952 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 5953 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5954 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5955 | return err; |
| 5956 | } |
| 5957 | |
| 5958 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 5959 | struct dentry *dentry) |
| 5960 | { |
| 5961 | struct btrfs_trans_handle *trans; |
| 5962 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 5963 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5964 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5965 | int err; |
| 5966 | int drop_inode = 0; |
| 5967 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5968 | /* do not allow sys_link's with other subvols of the same device */ |
| 5969 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 5970 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 5971 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 5972 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 5973 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5974 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5975 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5976 | if (err) |
| 5977 | goto fail; |
| 5978 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5979 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5980 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5981 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5982 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5983 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 5984 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5985 | if (IS_ERR(trans)) { |
| 5986 | err = PTR_ERR(trans); |
| 5987 | goto fail; |
| 5988 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5989 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5990 | /* There are several dir indexes for this inode, clear the cache. */ |
| 5991 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 5992 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5993 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 5994 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 5995 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 5996 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5997 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5998 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5999 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6000 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6001 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6002 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6003 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6004 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6005 | if (err) |
| 6006 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6007 | if (inode->i_nlink == 1) { |
| 6008 | /* |
| 6009 | * If new hard link count is 1, it's a file created |
| 6010 | * with open(2) O_TMPFILE flag. |
| 6011 | */ |
| 6012 | err = btrfs_orphan_del(trans, inode); |
| 6013 | if (err) |
| 6014 | goto fail; |
| 6015 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6016 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6017 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6018 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6019 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6020 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6021 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6022 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6023 | if (drop_inode) { |
| 6024 | inode_dec_link_count(inode); |
| 6025 | iput(inode); |
| 6026 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6027 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6028 | return err; |
| 6029 | } |
| 6030 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6031 | 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] | 6032 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6033 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6034 | struct btrfs_trans_handle *trans; |
| 6035 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6036 | int err = 0; |
| 6037 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6038 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6039 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6040 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6041 | /* |
| 6042 | * 2 items for inode and ref |
| 6043 | * 2 items for dir items |
| 6044 | * 1 for xattr if selinux is on |
| 6045 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6046 | trans = btrfs_start_transaction(root, 5); |
| 6047 | if (IS_ERR(trans)) |
| 6048 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6049 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6050 | err = btrfs_find_free_ino(root, &objectid); |
| 6051 | if (err) |
| 6052 | goto out_fail; |
| 6053 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6054 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6055 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6056 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6057 | if (IS_ERR(inode)) { |
| 6058 | err = PTR_ERR(inode); |
| 6059 | goto out_fail; |
| 6060 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6061 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6062 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6063 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6064 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6065 | if (err) |
| 6066 | goto out_fail; |
| 6067 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6068 | inode->i_op = &btrfs_dir_inode_operations; |
| 6069 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6070 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6071 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6072 | err = btrfs_update_inode(trans, root, inode); |
| 6073 | if (err) |
| 6074 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6075 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6076 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6077 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6078 | if (err) |
| 6079 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6080 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6081 | d_instantiate(dentry, inode); |
| 6082 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6083 | |
| 6084 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6085 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6086 | if (drop_on_err) |
| 6087 | iput(inode); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6088 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6089 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6090 | return err; |
| 6091 | } |
| 6092 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6093 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 6094 | * and an extent that you want to insert, deal with overlap and insert |
| 6095 | * the new extent into the tree. |
| 6096 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6097 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6098 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6099 | struct extent_map *em, |
| 6100 | u64 map_start, u64 map_len) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6101 | { |
| 6102 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6103 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6104 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 6105 | start_diff = map_start - em->start; |
| 6106 | em->start = map_start; |
| 6107 | em->len = map_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6108 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6109 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6110 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6111 | em->block_len -= start_diff; |
| 6112 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6113 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6114 | } |
| 6115 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6116 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6117 | struct inode *inode, struct page *page, |
| 6118 | size_t pg_offset, u64 extent_offset, |
| 6119 | struct btrfs_file_extent_item *item) |
| 6120 | { |
| 6121 | int ret; |
| 6122 | struct extent_buffer *leaf = path->nodes[0]; |
| 6123 | char *tmp; |
| 6124 | size_t max_size; |
| 6125 | unsigned long inline_size; |
| 6126 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6127 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6128 | |
| 6129 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6130 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6131 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6132 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6133 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6134 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6135 | if (!tmp) |
| 6136 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6137 | ptr = btrfs_file_extent_inline_start(item); |
| 6138 | |
| 6139 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6140 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6141 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6142 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6143 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6144 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6145 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6146 | } |
| 6147 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6148 | /* |
| 6149 | * 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] | 6150 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6151 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6152 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6153 | * |
| 6154 | * This also copies inline extents directly into the page. |
| 6155 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6156 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6157 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6158 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6159 | int create) |
| 6160 | { |
| 6161 | int ret; |
| 6162 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6163 | u64 extent_start = 0; |
| 6164 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6165 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6166 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6167 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6168 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6169 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6170 | struct extent_buffer *leaf; |
| 6171 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6172 | struct extent_map *em = NULL; |
| 6173 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6174 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6175 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6176 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6177 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6178 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6179 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6180 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6181 | if (em) |
| 6182 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6183 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6184 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6185 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6186 | if (em->start > start || em->start + em->len <= start) |
| 6187 | free_extent_map(em); |
| 6188 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6189 | free_extent_map(em); |
| 6190 | else |
| 6191 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6192 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6193 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6194 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6195 | err = -ENOMEM; |
| 6196 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6197 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6198 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6199 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6200 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6201 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6202 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6203 | |
| 6204 | if (!path) { |
| 6205 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6206 | if (!path) { |
| 6207 | err = -ENOMEM; |
| 6208 | goto out; |
| 6209 | } |
| 6210 | /* |
| 6211 | * Chances are we'll be called again, so go ahead and do |
| 6212 | * readahead |
| 6213 | */ |
| 6214 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6215 | } |
| 6216 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6217 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6218 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6219 | if (ret < 0) { |
| 6220 | err = ret; |
| 6221 | goto out; |
| 6222 | } |
| 6223 | |
| 6224 | if (ret != 0) { |
| 6225 | if (path->slots[0] == 0) |
| 6226 | goto not_found; |
| 6227 | path->slots[0]--; |
| 6228 | } |
| 6229 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6230 | leaf = path->nodes[0]; |
| 6231 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6232 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6233 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6234 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6235 | found_type = btrfs_key_type(&found_key); |
| 6236 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6237 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6238 | /* |
| 6239 | * If we backup past the first extent we want to move forward |
| 6240 | * and see if there is an extent in front of us, otherwise we'll |
| 6241 | * say there is a hole for our whole search range which can |
| 6242 | * cause problems. |
| 6243 | */ |
| 6244 | extent_end = start; |
| 6245 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6246 | } |
| 6247 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6248 | found_type = btrfs_file_extent_type(leaf, item); |
| 6249 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6250 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6251 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6252 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6253 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6254 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6255 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6256 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6257 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6258 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6259 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6260 | if (start >= extent_end) { |
| 6261 | path->slots[0]++; |
| 6262 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6263 | ret = btrfs_next_leaf(root, path); |
| 6264 | if (ret < 0) { |
| 6265 | err = ret; |
| 6266 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6267 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6268 | if (ret > 0) |
| 6269 | goto not_found; |
| 6270 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6271 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6272 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6273 | if (found_key.objectid != objectid || |
| 6274 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6275 | goto not_found; |
| 6276 | if (start + len <= found_key.offset) |
| 6277 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame^] | 6278 | if (start > found_key.offset) |
| 6279 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6280 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6281 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6282 | em->len = found_key.offset - start; |
| 6283 | goto not_found_em; |
| 6284 | } |
| 6285 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6286 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6287 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6288 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6289 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6290 | goto insert; |
| 6291 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6292 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6293 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6294 | size_t size; |
| 6295 | size_t extent_offset; |
| 6296 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6297 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6298 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6299 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6300 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6301 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6302 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6303 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6304 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6305 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6306 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6307 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6308 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6309 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6310 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6311 | if (btrfs_file_extent_compression(leaf, item) != |
| 6312 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6313 | ret = uncompress_inline(path, inode, page, |
| 6314 | pg_offset, |
| 6315 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6316 | if (ret) { |
| 6317 | err = ret; |
| 6318 | goto out; |
| 6319 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6320 | } else { |
| 6321 | map = kmap(page); |
| 6322 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6323 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6324 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6325 | memset(map + pg_offset + copy_size, 0, |
| 6326 | PAGE_CACHE_SIZE - pg_offset - |
| 6327 | copy_size); |
| 6328 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6329 | kunmap(page); |
| 6330 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6331 | flush_dcache_page(page); |
| 6332 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6333 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6334 | if (!trans) { |
| 6335 | kunmap(page); |
| 6336 | free_extent_map(em); |
| 6337 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6338 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6339 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6340 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6341 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6342 | if (IS_ERR(trans)) |
| 6343 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6344 | goto again; |
| 6345 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6346 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6347 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6348 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6349 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6350 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6351 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6352 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6353 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6354 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6355 | } |
| 6356 | not_found: |
| 6357 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6358 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6359 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6360 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6361 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6362 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6363 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6364 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6365 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6366 | 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] | 6367 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6368 | err = -EIO; |
| 6369 | goto out; |
| 6370 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6371 | |
| 6372 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6373 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6374 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6375 | /* it is possible that someone inserted the extent into the tree |
| 6376 | * while we had the lock dropped. It is also possible that |
| 6377 | * an overlapping map exists in the tree |
| 6378 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6379 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6380 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6381 | |
| 6382 | ret = 0; |
| 6383 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6384 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6385 | if (existing && (existing->start > start || |
| 6386 | existing->start + existing->len <= start)) { |
| 6387 | free_extent_map(existing); |
| 6388 | existing = NULL; |
| 6389 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6390 | if (!existing) { |
| 6391 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6392 | em->len); |
| 6393 | if (existing) { |
| 6394 | err = merge_extent_mapping(em_tree, existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6395 | em, start, |
| 6396 | root->sectorsize); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6397 | free_extent_map(existing); |
| 6398 | if (err) { |
| 6399 | free_extent_map(em); |
| 6400 | em = NULL; |
| 6401 | } |
| 6402 | } else { |
| 6403 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6404 | free_extent_map(em); |
| 6405 | em = NULL; |
| 6406 | } |
| 6407 | } else { |
| 6408 | free_extent_map(em); |
| 6409 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6410 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6411 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6412 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6413 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6414 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6415 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6416 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6417 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6418 | if (path) |
| 6419 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6420 | if (trans) { |
| 6421 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6422 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6423 | err = ret; |
| 6424 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6425 | if (err) { |
| 6426 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6427 | return ERR_PTR(err); |
| 6428 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6429 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6430 | return em; |
| 6431 | } |
| 6432 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6433 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6434 | size_t pg_offset, u64 start, u64 len, |
| 6435 | int create) |
| 6436 | { |
| 6437 | struct extent_map *em; |
| 6438 | struct extent_map *hole_em = NULL; |
| 6439 | u64 range_start = start; |
| 6440 | u64 end; |
| 6441 | u64 found; |
| 6442 | u64 found_end; |
| 6443 | int err = 0; |
| 6444 | |
| 6445 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6446 | if (IS_ERR(em)) |
| 6447 | return em; |
| 6448 | if (em) { |
| 6449 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6450 | * if our em maps to |
| 6451 | * - a hole or |
| 6452 | * - a pre-alloc extent, |
| 6453 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6454 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6455 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6456 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6457 | return em; |
| 6458 | else |
| 6459 | hole_em = em; |
| 6460 | } |
| 6461 | |
| 6462 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6463 | end = start + len; |
| 6464 | if (end < start) |
| 6465 | end = (u64)-1; |
| 6466 | else |
| 6467 | end -= 1; |
| 6468 | |
| 6469 | em = NULL; |
| 6470 | |
| 6471 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6472 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6473 | end, len, EXTENT_DELALLOC, 1); |
| 6474 | found_end = range_start + found; |
| 6475 | if (found_end < range_start) |
| 6476 | found_end = (u64)-1; |
| 6477 | |
| 6478 | /* |
| 6479 | * we didn't find anything useful, return |
| 6480 | * the original results from get_extent() |
| 6481 | */ |
| 6482 | if (range_start > end || found_end <= start) { |
| 6483 | em = hole_em; |
| 6484 | hole_em = NULL; |
| 6485 | goto out; |
| 6486 | } |
| 6487 | |
| 6488 | /* adjust the range_start to make sure it doesn't |
| 6489 | * go backwards from the start they passed in |
| 6490 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6491 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6492 | found = found_end - range_start; |
| 6493 | |
| 6494 | if (found > 0) { |
| 6495 | u64 hole_start = start; |
| 6496 | u64 hole_len = len; |
| 6497 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6498 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6499 | if (!em) { |
| 6500 | err = -ENOMEM; |
| 6501 | goto out; |
| 6502 | } |
| 6503 | /* |
| 6504 | * when btrfs_get_extent can't find anything it |
| 6505 | * returns one huge hole |
| 6506 | * |
| 6507 | * make sure what it found really fits our range, and |
| 6508 | * adjust to make sure it is based on the start from |
| 6509 | * the caller |
| 6510 | */ |
| 6511 | if (hole_em) { |
| 6512 | u64 calc_end = extent_map_end(hole_em); |
| 6513 | |
| 6514 | if (calc_end <= start || (hole_em->start > end)) { |
| 6515 | free_extent_map(hole_em); |
| 6516 | hole_em = NULL; |
| 6517 | } else { |
| 6518 | hole_start = max(hole_em->start, start); |
| 6519 | hole_len = calc_end - hole_start; |
| 6520 | } |
| 6521 | } |
| 6522 | em->bdev = NULL; |
| 6523 | if (hole_em && range_start > hole_start) { |
| 6524 | /* our hole starts before our delalloc, so we |
| 6525 | * have to return just the parts of the hole |
| 6526 | * that go until the delalloc starts |
| 6527 | */ |
| 6528 | em->len = min(hole_len, |
| 6529 | range_start - hole_start); |
| 6530 | em->start = hole_start; |
| 6531 | em->orig_start = hole_start; |
| 6532 | /* |
| 6533 | * don't adjust block start at all, |
| 6534 | * it is fixed at EXTENT_MAP_HOLE |
| 6535 | */ |
| 6536 | em->block_start = hole_em->block_start; |
| 6537 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6538 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6539 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6540 | } else { |
| 6541 | em->start = range_start; |
| 6542 | em->len = found; |
| 6543 | em->orig_start = range_start; |
| 6544 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6545 | em->block_len = found; |
| 6546 | } |
| 6547 | } else if (hole_em) { |
| 6548 | return hole_em; |
| 6549 | } |
| 6550 | out: |
| 6551 | |
| 6552 | free_extent_map(hole_em); |
| 6553 | if (err) { |
| 6554 | free_extent_map(em); |
| 6555 | return ERR_PTR(err); |
| 6556 | } |
| 6557 | return em; |
| 6558 | } |
| 6559 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6560 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6561 | u64 start, u64 len) |
| 6562 | { |
| 6563 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6564 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6565 | struct btrfs_key ins; |
| 6566 | u64 alloc_hint; |
| 6567 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6568 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6569 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6570 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6571 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6572 | if (ret) |
| 6573 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6574 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6575 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6576 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6577 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6578 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6579 | return em; |
| 6580 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6581 | |
| 6582 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6583 | ins.offset, ins.offset, 0); |
| 6584 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6585 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6586 | free_extent_map(em); |
| 6587 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6588 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6589 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6590 | return em; |
| 6591 | } |
| 6592 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6593 | /* |
| 6594 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6595 | * block must be cow'd |
| 6596 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6597 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6598 | u64 *orig_start, u64 *orig_block_len, |
| 6599 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6600 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6601 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6602 | struct btrfs_path *path; |
| 6603 | int ret; |
| 6604 | struct extent_buffer *leaf; |
| 6605 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6606 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6607 | struct btrfs_file_extent_item *fi; |
| 6608 | struct btrfs_key key; |
| 6609 | u64 disk_bytenr; |
| 6610 | u64 backref_offset; |
| 6611 | u64 extent_end; |
| 6612 | u64 num_bytes; |
| 6613 | int slot; |
| 6614 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6615 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6616 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6617 | path = btrfs_alloc_path(); |
| 6618 | if (!path) |
| 6619 | return -ENOMEM; |
| 6620 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6621 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6622 | offset, 0); |
| 6623 | if (ret < 0) |
| 6624 | goto out; |
| 6625 | |
| 6626 | slot = path->slots[0]; |
| 6627 | if (ret == 1) { |
| 6628 | if (slot == 0) { |
| 6629 | /* can't find the item, must cow */ |
| 6630 | ret = 0; |
| 6631 | goto out; |
| 6632 | } |
| 6633 | slot--; |
| 6634 | } |
| 6635 | ret = 0; |
| 6636 | leaf = path->nodes[0]; |
| 6637 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6638 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6639 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6640 | /* not our file or wrong item type, must cow */ |
| 6641 | goto out; |
| 6642 | } |
| 6643 | |
| 6644 | if (key.offset > offset) { |
| 6645 | /* Wrong offset, must cow */ |
| 6646 | goto out; |
| 6647 | } |
| 6648 | |
| 6649 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6650 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6651 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6652 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6653 | /* not a regular extent, must cow */ |
| 6654 | goto out; |
| 6655 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6656 | |
| 6657 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6658 | goto out; |
| 6659 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6660 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6661 | if (extent_end <= offset) |
| 6662 | goto out; |
| 6663 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6664 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6665 | if (disk_bytenr == 0) |
| 6666 | goto out; |
| 6667 | |
| 6668 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6669 | btrfs_file_extent_encryption(leaf, fi) || |
| 6670 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6671 | goto out; |
| 6672 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6673 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6674 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6675 | if (orig_start) { |
| 6676 | *orig_start = key.offset - backref_offset; |
| 6677 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6678 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6679 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6680 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6681 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6682 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6683 | |
| 6684 | num_bytes = min(offset + *len, extent_end) - offset; |
| 6685 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 6686 | u64 range_end; |
| 6687 | |
| 6688 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 6689 | ret = test_range_bit(io_tree, offset, range_end, |
| 6690 | EXTENT_DELALLOC, 0, NULL); |
| 6691 | if (ret) { |
| 6692 | ret = -EAGAIN; |
| 6693 | goto out; |
| 6694 | } |
| 6695 | } |
| 6696 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6697 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6698 | |
| 6699 | /* |
| 6700 | * look for other files referencing this extent, if we |
| 6701 | * find any we must cow |
| 6702 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6703 | trans = btrfs_join_transaction(root); |
| 6704 | if (IS_ERR(trans)) { |
| 6705 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6706 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6707 | } |
| 6708 | |
| 6709 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6710 | key.offset - backref_offset, disk_bytenr); |
| 6711 | btrfs_end_transaction(trans, root); |
| 6712 | if (ret) { |
| 6713 | ret = 0; |
| 6714 | goto out; |
| 6715 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6716 | |
| 6717 | /* |
| 6718 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6719 | * in this extent we are about to write. If there |
| 6720 | * are any csums in that range we have to cow in order |
| 6721 | * to keep the csums correct |
| 6722 | */ |
| 6723 | disk_bytenr += backref_offset; |
| 6724 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6725 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6726 | goto out; |
| 6727 | /* |
| 6728 | * all of the above have passed, it is safe to overwrite this extent |
| 6729 | * without cow |
| 6730 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6731 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6732 | ret = 1; |
| 6733 | out: |
| 6734 | btrfs_free_path(path); |
| 6735 | return ret; |
| 6736 | } |
| 6737 | |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6738 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 6739 | { |
| 6740 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 6741 | int found = false; |
| 6742 | void **pagep = NULL; |
| 6743 | struct page *page = NULL; |
| 6744 | int start_idx; |
| 6745 | int end_idx; |
| 6746 | |
| 6747 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 6748 | |
| 6749 | /* |
| 6750 | * end is the last byte in the last page. end == start is legal |
| 6751 | */ |
| 6752 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 6753 | |
| 6754 | rcu_read_lock(); |
| 6755 | |
| 6756 | /* Most of the code in this while loop is lifted from |
| 6757 | * find_get_page. It's been modified to begin searching from a |
| 6758 | * page and return just the first page found in that range. If the |
| 6759 | * found idx is less than or equal to the end idx then we know that |
| 6760 | * a page exists. If no pages are found or if those pages are |
| 6761 | * outside of the range then we're fine (yay!) */ |
| 6762 | while (page == NULL && |
| 6763 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 6764 | page = radix_tree_deref_slot(pagep); |
| 6765 | if (unlikely(!page)) |
| 6766 | break; |
| 6767 | |
| 6768 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6769 | if (radix_tree_deref_retry(page)) { |
| 6770 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6771 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6772 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6773 | /* |
| 6774 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 6775 | * here as an exceptional entry: so return it without |
| 6776 | * attempting to raise page count. |
| 6777 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 6778 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6779 | break; /* TODO: Is this relevant for this use case? */ |
| 6780 | } |
| 6781 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6782 | if (!page_cache_get_speculative(page)) { |
| 6783 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6784 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6785 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6786 | |
| 6787 | /* |
| 6788 | * Has the page moved? |
| 6789 | * This is part of the lockless pagecache protocol. See |
| 6790 | * include/linux/pagemap.h for details. |
| 6791 | */ |
| 6792 | if (unlikely(page != *pagep)) { |
| 6793 | page_cache_release(page); |
| 6794 | page = NULL; |
| 6795 | } |
| 6796 | } |
| 6797 | |
| 6798 | if (page) { |
| 6799 | if (page->index <= end_idx) |
| 6800 | found = true; |
| 6801 | page_cache_release(page); |
| 6802 | } |
| 6803 | |
| 6804 | rcu_read_unlock(); |
| 6805 | return found; |
| 6806 | } |
| 6807 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6808 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6809 | struct extent_state **cached_state, int writing) |
| 6810 | { |
| 6811 | struct btrfs_ordered_extent *ordered; |
| 6812 | int ret = 0; |
| 6813 | |
| 6814 | while (1) { |
| 6815 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6816 | 0, cached_state); |
| 6817 | /* |
| 6818 | * We're concerned with the entire range that we're going to be |
| 6819 | * doing DIO to, so we need to make sure theres no ordered |
| 6820 | * extents in this range. |
| 6821 | */ |
| 6822 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6823 | lockend - lockstart + 1); |
| 6824 | |
| 6825 | /* |
| 6826 | * We need to make sure there are no buffered pages in this |
| 6827 | * range either, we could have raced between the invalidate in |
| 6828 | * generic_file_direct_write and locking the extent. The |
| 6829 | * invalidate needs to happen so that reads after a write do not |
| 6830 | * get stale data. |
| 6831 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6832 | if (!ordered && |
| 6833 | (!writing || |
| 6834 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6835 | break; |
| 6836 | |
| 6837 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6838 | cached_state, GFP_NOFS); |
| 6839 | |
| 6840 | if (ordered) { |
| 6841 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6842 | btrfs_put_ordered_extent(ordered); |
| 6843 | } else { |
| 6844 | /* Screw you mmap */ |
| 6845 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6846 | lockstart, |
| 6847 | lockend); |
| 6848 | if (ret) |
| 6849 | break; |
| 6850 | |
| 6851 | /* |
| 6852 | * If we found a page that couldn't be invalidated just |
| 6853 | * fall back to buffered. |
| 6854 | */ |
| 6855 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6856 | lockstart >> PAGE_CACHE_SHIFT, |
| 6857 | lockend >> PAGE_CACHE_SHIFT); |
| 6858 | if (ret) |
| 6859 | break; |
| 6860 | } |
| 6861 | |
| 6862 | cond_resched(); |
| 6863 | } |
| 6864 | |
| 6865 | return ret; |
| 6866 | } |
| 6867 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6868 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6869 | u64 len, u64 orig_start, |
| 6870 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6871 | u64 orig_block_len, u64 ram_bytes, |
| 6872 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6873 | { |
| 6874 | struct extent_map_tree *em_tree; |
| 6875 | struct extent_map *em; |
| 6876 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6877 | int ret; |
| 6878 | |
| 6879 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6880 | em = alloc_extent_map(); |
| 6881 | if (!em) |
| 6882 | return ERR_PTR(-ENOMEM); |
| 6883 | |
| 6884 | em->start = start; |
| 6885 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6886 | em->mod_start = start; |
| 6887 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6888 | em->len = len; |
| 6889 | em->block_len = block_len; |
| 6890 | em->block_start = block_start; |
| 6891 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6892 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6893 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6894 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6895 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6896 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6897 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6898 | |
| 6899 | do { |
| 6900 | btrfs_drop_extent_cache(inode, em->start, |
| 6901 | em->start + em->len - 1, 0); |
| 6902 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6903 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6904 | write_unlock(&em_tree->lock); |
| 6905 | } while (ret == -EEXIST); |
| 6906 | |
| 6907 | if (ret) { |
| 6908 | free_extent_map(em); |
| 6909 | return ERR_PTR(ret); |
| 6910 | } |
| 6911 | |
| 6912 | return em; |
| 6913 | } |
| 6914 | |
| 6915 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6916 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6917 | struct buffer_head *bh_result, int create) |
| 6918 | { |
| 6919 | struct extent_map *em; |
| 6920 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6921 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6922 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6923 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6924 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6925 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6926 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6927 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6928 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6929 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6930 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6931 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6932 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6933 | lockstart = start; |
| 6934 | lockend = start + len - 1; |
| 6935 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6936 | /* |
| 6937 | * If this errors out it's because we couldn't invalidate pagecache for |
| 6938 | * this range and we need to fallback to buffered. |
| 6939 | */ |
| 6940 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 6941 | return -ENOTBLK; |
| 6942 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6943 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6944 | if (IS_ERR(em)) { |
| 6945 | ret = PTR_ERR(em); |
| 6946 | goto unlock_err; |
| 6947 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6948 | |
| 6949 | /* |
| 6950 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 6951 | * io. INLINE is special, and we could probably kludge it in here, but |
| 6952 | * it's still buffered so for safety lets just fall back to the generic |
| 6953 | * buffered path. |
| 6954 | * |
| 6955 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 6956 | * decompress it, so there will be buffering required no matter what we |
| 6957 | * do, so go ahead and fallback to buffered. |
| 6958 | * |
| 6959 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 6960 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 6961 | * the generic code. |
| 6962 | */ |
| 6963 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 6964 | em->block_start == EXTENT_MAP_INLINE) { |
| 6965 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6966 | ret = -ENOTBLK; |
| 6967 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | /* Just a good old fashioned hole, return */ |
| 6971 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 6972 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 6973 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6974 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | /* |
| 6978 | * We don't allocate a new extent in the following cases |
| 6979 | * |
| 6980 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 6981 | * existing extent. |
| 6982 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 6983 | * just use the extent. |
| 6984 | * |
| 6985 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6986 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6987 | len = min(len, em->len - (start - em->start)); |
| 6988 | lockstart = start + len; |
| 6989 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6990 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6991 | |
| 6992 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 6993 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 6994 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6995 | int type; |
| 6996 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6997 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6998 | |
| 6999 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7000 | type = BTRFS_ORDERED_PREALLOC; |
| 7001 | else |
| 7002 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7003 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7004 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7005 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7006 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7007 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7008 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7009 | free_extent_map(em); |
| 7010 | em = create_pinned_em(inode, start, len, |
| 7011 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7012 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7013 | orig_block_len, |
| 7014 | ram_bytes, type); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7015 | if (IS_ERR(em)) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7016 | goto unlock_err; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7017 | } |
| 7018 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7019 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7020 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7021 | if (ret) { |
| 7022 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7023 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7024 | } |
| 7025 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7026 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7027 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7028 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7029 | /* |
| 7030 | * this will cow the extent, reset the len in case we changed |
| 7031 | * it above |
| 7032 | */ |
| 7033 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7034 | free_extent_map(em); |
| 7035 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7036 | if (IS_ERR(em)) { |
| 7037 | ret = PTR_ERR(em); |
| 7038 | goto unlock_err; |
| 7039 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7040 | len = min(len, em->len - (start - em->start)); |
| 7041 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7042 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7043 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7044 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7045 | bh_result->b_bdev = em->bdev; |
| 7046 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7047 | if (create) { |
| 7048 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7049 | set_buffer_new(bh_result); |
| 7050 | |
| 7051 | /* |
| 7052 | * Need to update the i_size under the extent lock so buffered |
| 7053 | * readers will get the updated i_size when we unlock. |
| 7054 | */ |
| 7055 | if (start + len > i_size_read(inode)) |
| 7056 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7057 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7058 | spin_lock(&BTRFS_I(inode)->lock); |
| 7059 | BTRFS_I(inode)->outstanding_extents++; |
| 7060 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7061 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7062 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7063 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 7064 | &cached_state, GFP_NOFS); |
| 7065 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7066 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7067 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7068 | /* |
| 7069 | * In the case of write we need to clear and unlock the entire range, |
| 7070 | * in the case of read we need to unlock only the end area that we |
| 7071 | * aren't using if there is any left over space. |
| 7072 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7073 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7074 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7075 | lockend, unlock_bits, 1, 0, |
| 7076 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7077 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7078 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7079 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7080 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7081 | free_extent_map(em); |
| 7082 | |
| 7083 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7084 | |
| 7085 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7086 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7087 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 7088 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7089 | } |
| 7090 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7091 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7092 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7093 | struct btrfs_dio_private *dip = bio->bi_private; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7094 | struct bio_vec *bvec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7095 | struct inode *inode = dip->inode; |
| 7096 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7097 | struct bio *dio_bio; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7098 | u32 *csums = (u32 *)dip->csum; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7099 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7100 | int i; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7101 | |
| 7102 | start = dip->logical_offset; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7103 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7104 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 7105 | struct page *page = bvec->bv_page; |
| 7106 | char *kaddr; |
| 7107 | u32 csum = ~(u32)0; |
| 7108 | unsigned long flags; |
| 7109 | |
| 7110 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 7111 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 7112 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7113 | csum, bvec->bv_len); |
| 7114 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 7115 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7116 | local_irq_restore(flags); |
| 7117 | |
| 7118 | flush_dcache_page(bvec->bv_page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7119 | if (csum != csums[i]) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7120 | 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] | 7121 | btrfs_ino(inode), start, csum, |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7122 | csums[i]); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7123 | err = -EIO; |
| 7124 | } |
| 7125 | } |
| 7126 | |
| 7127 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7128 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7129 | |
| 7130 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7131 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7132 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7133 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7134 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7135 | |
| 7136 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7137 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7138 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7139 | dio_end_io(dio_bio, err); |
| 7140 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7144 | { |
| 7145 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7146 | struct inode *inode = dip->inode; |
| 7147 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7148 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7149 | u64 ordered_offset = dip->logical_offset; |
| 7150 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7151 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7152 | int ret; |
| 7153 | |
| 7154 | if (err) |
| 7155 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7156 | again: |
| 7157 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7158 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7159 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7160 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7161 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7162 | |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7163 | btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL); |
| 7164 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7165 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7166 | out_test: |
| 7167 | /* |
| 7168 | * our bio might span multiple ordered extents. If we haven't |
| 7169 | * completed the accounting for the whole dio, go back and try again |
| 7170 | */ |
| 7171 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7172 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7173 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7174 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7175 | goto again; |
| 7176 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7177 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7178 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7179 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7180 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7181 | |
| 7182 | /* If we had an error make sure to clear the uptodate flag */ |
| 7183 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7184 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7185 | dio_end_io(dio_bio, err); |
| 7186 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7187 | } |
| 7188 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7189 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7190 | struct bio *bio, int mirror_num, |
| 7191 | unsigned long bio_flags, u64 offset) |
| 7192 | { |
| 7193 | int ret; |
| 7194 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7195 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7196 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7197 | return 0; |
| 7198 | } |
| 7199 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7200 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7201 | { |
| 7202 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7203 | |
| 7204 | if (err) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7205 | btrfs_err(BTRFS_I(dip->inode)->root->fs_info, |
| 7206 | "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] | 7207 | btrfs_ino(dip->inode), bio->bi_rw, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7208 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7209 | bio->bi_iter.bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7210 | dip->errors = 1; |
| 7211 | |
| 7212 | /* |
| 7213 | * before atomic variable goto zero, we must make sure |
| 7214 | * dip->errors is perceived to be set. |
| 7215 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7216 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | /* if there are more bios still pending for this dio, just exit */ |
| 7220 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7221 | goto out; |
| 7222 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7223 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7224 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7225 | } else { |
| 7226 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7227 | bio_endio(dip->orig_bio, 0); |
| 7228 | } |
| 7229 | out: |
| 7230 | bio_put(bio); |
| 7231 | } |
| 7232 | |
| 7233 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7234 | u64 first_sector, gfp_t gfp_flags) |
| 7235 | { |
| 7236 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7237 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7238 | } |
| 7239 | |
| 7240 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7241 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7242 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7243 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7244 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7245 | int write = rw & REQ_WRITE; |
| 7246 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7247 | int ret; |
| 7248 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7249 | if (async_submit) |
| 7250 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7251 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7252 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7253 | |
| 7254 | if (!write) { |
| 7255 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 7256 | if (ret) |
| 7257 | goto err; |
| 7258 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7259 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7260 | if (skip_sum) |
| 7261 | goto map; |
| 7262 | |
| 7263 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7264 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7265 | inode, rw, bio, 0, 0, |
| 7266 | file_offset, |
| 7267 | __btrfs_submit_bio_start_direct_io, |
| 7268 | __btrfs_submit_bio_done); |
| 7269 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7270 | } else if (write) { |
| 7271 | /* |
| 7272 | * If we aren't doing async submit, calculate the csum of the |
| 7273 | * bio now. |
| 7274 | */ |
| 7275 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7276 | if (ret) |
| 7277 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7278 | } else if (!skip_sum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7279 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
| 7280 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7281 | if (ret) |
| 7282 | goto err; |
| 7283 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7284 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7285 | map: |
| 7286 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7287 | err: |
| 7288 | bio_put(bio); |
| 7289 | return ret; |
| 7290 | } |
| 7291 | |
| 7292 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7293 | int skip_sum) |
| 7294 | { |
| 7295 | struct inode *inode = dip->inode; |
| 7296 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7297 | struct bio *bio; |
| 7298 | struct bio *orig_bio = dip->orig_bio; |
| 7299 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7300 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7301 | u64 file_offset = dip->logical_offset; |
| 7302 | u64 submit_len = 0; |
| 7303 | u64 map_length; |
| 7304 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7305 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7306 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7307 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7308 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7309 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7310 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7311 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7312 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7313 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7314 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7315 | bio = orig_bio; |
| 7316 | goto submit; |
| 7317 | } |
| 7318 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7319 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7320 | if (btrfs_get_alloc_profile(root, 1) & |
| 7321 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7322 | async_submit = 0; |
| 7323 | else |
| 7324 | async_submit = 1; |
| 7325 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7326 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7327 | if (!bio) |
| 7328 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7329 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7330 | bio->bi_private = dip; |
| 7331 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7332 | atomic_inc(&dip->pending_bios); |
| 7333 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7334 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7335 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7336 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7337 | bvec->bv_offset) < bvec->bv_len)) { |
| 7338 | /* |
| 7339 | * inc the count before we submit the bio so |
| 7340 | * we know the end IO handler won't happen before |
| 7341 | * we inc the count. Otherwise, the dip might get freed |
| 7342 | * before we're done setting it up |
| 7343 | */ |
| 7344 | atomic_inc(&dip->pending_bios); |
| 7345 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7346 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7347 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7348 | if (ret) { |
| 7349 | bio_put(bio); |
| 7350 | atomic_dec(&dip->pending_bios); |
| 7351 | goto out_err; |
| 7352 | } |
| 7353 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7354 | start_sector += submit_len >> 9; |
| 7355 | file_offset += submit_len; |
| 7356 | |
| 7357 | submit_len = 0; |
| 7358 | nr_pages = 0; |
| 7359 | |
| 7360 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7361 | start_sector, GFP_NOFS); |
| 7362 | if (!bio) |
| 7363 | goto out_err; |
| 7364 | bio->bi_private = dip; |
| 7365 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7366 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7367 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7368 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7369 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7370 | &map_length, NULL, 0); |
| 7371 | if (ret) { |
| 7372 | bio_put(bio); |
| 7373 | goto out_err; |
| 7374 | } |
| 7375 | } else { |
| 7376 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7377 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7378 | bvec++; |
| 7379 | } |
| 7380 | } |
| 7381 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7382 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7383 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7384 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7385 | if (!ret) |
| 7386 | return 0; |
| 7387 | |
| 7388 | bio_put(bio); |
| 7389 | out_err: |
| 7390 | dip->errors = 1; |
| 7391 | /* |
| 7392 | * before atomic variable goto zero, we must |
| 7393 | * make sure dip->errors is perceived to be set. |
| 7394 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7395 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7396 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7397 | bio_io_error(dip->orig_bio); |
| 7398 | |
| 7399 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7400 | return 0; |
| 7401 | } |
| 7402 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7403 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7404 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7405 | { |
| 7406 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7407 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7408 | struct bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7409 | int skip_sum; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7410 | int sum_len; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7411 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7412 | int ret = 0; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7413 | u16 csum_size; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7414 | |
| 7415 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7416 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7417 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7418 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7419 | ret = -ENOMEM; |
| 7420 | goto free_ordered; |
| 7421 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7422 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7423 | if (!skip_sum && !write) { |
| 7424 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7425 | sum_len = dio_bio->bi_iter.bi_size >> |
| 7426 | inode->i_sb->s_blocksize_bits; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7427 | sum_len *= csum_size; |
| 7428 | } else { |
| 7429 | sum_len = 0; |
| 7430 | } |
| 7431 | |
| 7432 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7433 | if (!dip) { |
| 7434 | ret = -ENOMEM; |
| 7435 | goto free_io_bio; |
| 7436 | } |
| 7437 | |
| 7438 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7439 | dip->inode = inode; |
| 7440 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7441 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7442 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7443 | io_bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7444 | dip->errors = 0; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7445 | dip->orig_bio = io_bio; |
| 7446 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7447 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7448 | |
| 7449 | if (write) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7450 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7451 | else |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7452 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7453 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7454 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7455 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7456 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7457 | |
| 7458 | free_io_bio: |
| 7459 | bio_put(io_bio); |
| 7460 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7461 | free_ordered: |
| 7462 | /* |
| 7463 | * If this is a write, we need to clean up the reserved space and kill |
| 7464 | * the ordered extent. |
| 7465 | */ |
| 7466 | if (write) { |
| 7467 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7468 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7469 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7470 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7471 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7472 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7473 | btrfs_put_ordered_extent(ordered); |
| 7474 | btrfs_put_ordered_extent(ordered); |
| 7475 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7476 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7477 | } |
| 7478 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7479 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7480 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7481 | { |
| 7482 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7483 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7484 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7485 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7486 | |
| 7487 | if (offset & blocksize_mask) |
| 7488 | goto out; |
| 7489 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7490 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 7491 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7492 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7493 | /* If this is a write we don't need to check anymore */ |
| 7494 | if (rw & WRITE) |
| 7495 | return 0; |
| 7496 | /* |
| 7497 | * Check to make sure we don't have duplicate iov_base's in this |
| 7498 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7499 | * when reading back. |
| 7500 | */ |
| 7501 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 7502 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 7503 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7504 | goto out; |
| 7505 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7506 | } |
| 7507 | retval = 0; |
| 7508 | out: |
| 7509 | return retval; |
| 7510 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7511 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7512 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 7513 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7514 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7515 | struct file *file = iocb->ki_filp; |
| 7516 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7517 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7518 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7519 | bool wakeup = true; |
| 7520 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7521 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7522 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7523 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7524 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7525 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7526 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7527 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7528 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7529 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 7530 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7531 | * isn't enough if we've written compressed pages to this area, so |
| 7532 | * we need to flush the dirty pages again to make absolutely sure |
| 7533 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7534 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 7535 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 7536 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7537 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 7538 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 7539 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7540 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7541 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7542 | /* |
| 7543 | * If the write DIO is beyond the EOF, we need update |
| 7544 | * the isize, but it is protected by i_mutex. So we can |
| 7545 | * not unlock the i_mutex at this case. |
| 7546 | */ |
| 7547 | if (offset + count <= inode->i_size) { |
| 7548 | mutex_unlock(&inode->i_mutex); |
| 7549 | relock = true; |
| 7550 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7551 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7552 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7553 | goto out; |
| 7554 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7555 | &BTRFS_I(inode)->runtime_flags))) { |
| 7556 | inode_dio_done(inode); |
| 7557 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7558 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7559 | } |
| 7560 | |
| 7561 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7562 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 7563 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7564 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7565 | if (rw & WRITE) { |
| 7566 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7567 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7568 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7569 | btrfs_delalloc_release_space(inode, |
| 7570 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7571 | else |
| 7572 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7573 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7574 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7575 | if (wakeup) |
| 7576 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7577 | if (relock) |
| 7578 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7579 | |
| 7580 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7581 | } |
| 7582 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7583 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7584 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7585 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7586 | __u64 start, __u64 len) |
| 7587 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7588 | int ret; |
| 7589 | |
| 7590 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7591 | if (ret) |
| 7592 | return ret; |
| 7593 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7594 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7595 | } |
| 7596 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7597 | int btrfs_readpage(struct file *file, struct page *page) |
| 7598 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7599 | struct extent_io_tree *tree; |
| 7600 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7601 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7602 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7603 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7604 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7605 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7606 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7607 | |
| 7608 | |
| 7609 | if (current->flags & PF_MEMALLOC) { |
| 7610 | redirty_page_for_writepage(wbc, page); |
| 7611 | unlock_page(page); |
| 7612 | return 0; |
| 7613 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7614 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7615 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7616 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7617 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7618 | static int btrfs_writepages(struct address_space *mapping, |
| 7619 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7620 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7621 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7622 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7623 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7624 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7625 | } |
| 7626 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7627 | static int |
| 7628 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7629 | struct list_head *pages, unsigned nr_pages) |
| 7630 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7631 | struct extent_io_tree *tree; |
| 7632 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7633 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7634 | btrfs_get_extent); |
| 7635 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7636 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7637 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7638 | struct extent_io_tree *tree; |
| 7639 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7640 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7641 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7642 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7643 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7644 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7645 | if (ret == 1) { |
| 7646 | ClearPagePrivate(page); |
| 7647 | set_page_private(page, 0); |
| 7648 | page_cache_release(page); |
| 7649 | } |
| 7650 | return ret; |
| 7651 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7652 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7653 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7654 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7655 | if (PageWriteback(page) || PageDirty(page)) |
| 7656 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7657 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7658 | } |
| 7659 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 7660 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 7661 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7662 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7663 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7664 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7665 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7666 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7667 | u64 page_start = page_offset(page); |
| 7668 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7669 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7670 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7671 | /* |
| 7672 | * we have the page locked, so new writeback can't start, |
| 7673 | * and the dirty bit won't be cleared while we are here. |
| 7674 | * |
| 7675 | * Wait for IO on this page so that we can safely clear |
| 7676 | * the PagePrivate2 bit and do ordered accounting |
| 7677 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7678 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7679 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7680 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7681 | if (offset) { |
| 7682 | btrfs_releasepage(page, GFP_NOFS); |
| 7683 | return; |
| 7684 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7685 | |
| 7686 | if (!inode_evicting) |
| 7687 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 7688 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7689 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7690 | /* |
| 7691 | * IO on this page will never be started, so we need |
| 7692 | * to account for any ordered extents now |
| 7693 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7694 | if (!inode_evicting) |
| 7695 | clear_extent_bit(tree, page_start, page_end, |
| 7696 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7697 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7698 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 7699 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7700 | /* |
| 7701 | * whoever cleared the private bit is responsible |
| 7702 | * for the finish_ordered_io |
| 7703 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 7704 | if (TestClearPagePrivate2(page)) { |
| 7705 | struct btrfs_ordered_inode_tree *tree; |
| 7706 | u64 new_len; |
| 7707 | |
| 7708 | tree = &BTRFS_I(inode)->ordered_tree; |
| 7709 | |
| 7710 | spin_lock_irq(&tree->lock); |
| 7711 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 7712 | new_len = page_start - ordered->file_offset; |
| 7713 | if (new_len < ordered->truncated_len) |
| 7714 | ordered->truncated_len = new_len; |
| 7715 | spin_unlock_irq(&tree->lock); |
| 7716 | |
| 7717 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 7718 | page_start, |
| 7719 | PAGE_CACHE_SIZE, 1)) |
| 7720 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7721 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7722 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7723 | if (!inode_evicting) { |
| 7724 | cached_state = NULL; |
| 7725 | lock_extent_bits(tree, page_start, page_end, 0, |
| 7726 | &cached_state); |
| 7727 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7728 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7729 | |
| 7730 | if (!inode_evicting) { |
| 7731 | clear_extent_bit(tree, page_start, page_end, |
| 7732 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 7733 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 7734 | EXTENT_DEFRAG, 1, 1, |
| 7735 | &cached_state, GFP_NOFS); |
| 7736 | |
| 7737 | __btrfs_releasepage(page, GFP_NOFS); |
| 7738 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7739 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7740 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7741 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7742 | ClearPagePrivate(page); |
| 7743 | set_page_private(page, 0); |
| 7744 | page_cache_release(page); |
| 7745 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7746 | } |
| 7747 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7748 | /* |
| 7749 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7750 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7751 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7752 | * for a written page which means we get ENOSPC checking when writing into |
| 7753 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7754 | * support these features. |
| 7755 | * |
| 7756 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7757 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7758 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7759 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7760 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7761 | * unlock the page. |
| 7762 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7763 | 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] | 7764 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7765 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7766 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7767 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7768 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7769 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7770 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7771 | char *kaddr; |
| 7772 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7773 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7774 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7775 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7776 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7777 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7778 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7779 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7780 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7781 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7782 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7783 | reserved = 1; |
| 7784 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7785 | if (ret) { |
| 7786 | if (ret == -ENOMEM) |
| 7787 | ret = VM_FAULT_OOM; |
| 7788 | else /* -ENOSPC, -EIO, etc */ |
| 7789 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7790 | if (reserved) |
| 7791 | goto out; |
| 7792 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7793 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7794 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7795 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7796 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7797 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7798 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7799 | page_start = page_offset(page); |
| 7800 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7801 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7802 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7803 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7804 | /* page got truncated out from underneath us */ |
| 7805 | goto out_unlock; |
| 7806 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7807 | wait_on_page_writeback(page); |
| 7808 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7809 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7810 | set_page_extent_mapped(page); |
| 7811 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7812 | /* |
| 7813 | * we can't set the delalloc bits if there are pending ordered |
| 7814 | * extents. Drop our locks and wait for them to finish |
| 7815 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7816 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7817 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7818 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7819 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7820 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7821 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7822 | btrfs_put_ordered_extent(ordered); |
| 7823 | goto again; |
| 7824 | } |
| 7825 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7826 | /* |
| 7827 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7828 | * if it was already dirty, so for space accounting reasons we need to |
| 7829 | * clear any delalloc bits for the range we are fixing to save. There |
| 7830 | * is probably a better way to do this, but for now keep consistent with |
| 7831 | * prepare_pages in the normal write path. |
| 7832 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7833 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7834 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7835 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7836 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7837 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7838 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7839 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7840 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7841 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7842 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7843 | ret = VM_FAULT_SIGBUS; |
| 7844 | goto out_unlock; |
| 7845 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7846 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7847 | |
| 7848 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7849 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7850 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7851 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7852 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7853 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7854 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7855 | kaddr = kmap(page); |
| 7856 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7857 | flush_dcache_page(page); |
| 7858 | kunmap(page); |
| 7859 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7860 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7861 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7862 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7863 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7864 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7865 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7866 | 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] | 7867 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7868 | 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] | 7869 | |
| 7870 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7871 | if (!ret) { |
| 7872 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7873 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7874 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7875 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7876 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7877 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7878 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7879 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7880 | return ret; |
| 7881 | } |
| 7882 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7883 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7884 | { |
| 7885 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7886 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 7887 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7888 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7889 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7890 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7891 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7892 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 7893 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 7894 | (u64)-1); |
| 7895 | if (ret) |
| 7896 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7897 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7898 | /* |
| 7899 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7900 | * 3 things going on here |
| 7901 | * |
| 7902 | * 1) We need to reserve space for our orphan item and the space to |
| 7903 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7904 | * orphan item because we didn't reserve space to remove it. |
| 7905 | * |
| 7906 | * 2) We need to reserve space to update our inode. |
| 7907 | * |
| 7908 | * 3) We need to have something to cache all the space that is going to |
| 7909 | * be free'd up by the truncate operation, but also have some slack |
| 7910 | * space reserved in case it uses space during the truncate (thank you |
| 7911 | * very much snapshotting). |
| 7912 | * |
| 7913 | * And we need these to all be seperate. The fact is we can use alot of |
| 7914 | * space doing the truncate, and we have no earthly idea how much space |
| 7915 | * we will use, so we need the truncate reservation to be seperate so it |
| 7916 | * doesn't end up using space reserved for updating the inode or |
| 7917 | * removing the orphan item. We also need to be able to stop the |
| 7918 | * transaction and start a new one, which means we need to be able to |
| 7919 | * update the inode several times, and we have no idea of knowing how |
| 7920 | * many times that will be, so we can't just reserve 1 item for the |
| 7921 | * entirety of the opration, so that has to be done seperately as well. |
| 7922 | * Then there is the orphan item, which does indeed need to be held on |
| 7923 | * to for the whole operation, and we need nobody to touch this reserved |
| 7924 | * space except the orphan code. |
| 7925 | * |
| 7926 | * So that leaves us with |
| 7927 | * |
| 7928 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7929 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7930 | * transaction reservation. |
| 7931 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7932 | * updating the inode. |
| 7933 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7934 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7935 | if (!rsv) |
| 7936 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 7937 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7938 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7939 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7940 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7941 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7942 | * 1 for updating the inode. |
| 7943 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 7944 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7945 | if (IS_ERR(trans)) { |
| 7946 | err = PTR_ERR(trans); |
| 7947 | goto out; |
| 7948 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7949 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 7950 | /* Migrate the slack space for the truncate to our reserve */ |
| 7951 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 7952 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7953 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 7954 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7955 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7956 | * So if we truncate and then write and fsync we normally would just |
| 7957 | * write the extents that changed, which is a problem if we need to |
| 7958 | * first truncate that entire inode. So set this flag so we write out |
| 7959 | * all of the extents in the inode to the sync log so we're completely |
| 7960 | * safe. |
| 7961 | */ |
| 7962 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7963 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 7964 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7965 | while (1) { |
| 7966 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 7967 | inode->i_size, |
| 7968 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7969 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7970 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7971 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7972 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7973 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7974 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7975 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7976 | if (ret) { |
| 7977 | err = ret; |
| 7978 | break; |
| 7979 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7980 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7981 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 7982 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 7983 | |
| 7984 | trans = btrfs_start_transaction(root, 2); |
| 7985 | if (IS_ERR(trans)) { |
| 7986 | ret = err = PTR_ERR(trans); |
| 7987 | trans = NULL; |
| 7988 | break; |
| 7989 | } |
| 7990 | |
| 7991 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 7992 | rsv, min_size); |
| 7993 | BUG_ON(ret); /* shouldn't happen */ |
| 7994 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
| 7997 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7998 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 7999 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8000 | if (ret) |
| 8001 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8004 | if (trans) { |
| 8005 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8006 | ret = btrfs_update_inode(trans, root, inode); |
| 8007 | if (ret && !err) |
| 8008 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8009 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8010 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8011 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8012 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8013 | |
| 8014 | out: |
| 8015 | btrfs_free_block_rsv(root, rsv); |
| 8016 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8017 | if (ret && !err) |
| 8018 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8019 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8020 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8021 | } |
| 8022 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8023 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8024 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8025 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8026 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8027 | struct btrfs_root *new_root, |
| 8028 | struct btrfs_root *parent_root, |
| 8029 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8030 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8031 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8032 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8033 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8034 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8035 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8036 | new_dirid, new_dirid, |
| 8037 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8038 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8039 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8040 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8041 | inode->i_op = &btrfs_dir_inode_operations; |
| 8042 | inode->i_fop = &btrfs_dir_file_operations; |
| 8043 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8044 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8045 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8046 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8047 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8048 | if (err) |
| 8049 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8050 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8051 | new_root->root_key.objectid, err); |
| 8052 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8053 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8054 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8055 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8056 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8057 | } |
| 8058 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8059 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8060 | { |
| 8061 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8062 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8063 | |
| 8064 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8065 | if (!ei) |
| 8066 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8067 | |
| 8068 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8069 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8070 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8071 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8072 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8073 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8074 | ei->disk_i_size = 0; |
| 8075 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8076 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8077 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8078 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8079 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8080 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8081 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8082 | spin_lock_init(&ei->lock); |
| 8083 | ei->outstanding_extents = 0; |
| 8084 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8085 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8086 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8087 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8088 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8089 | ei->delayed_node = NULL; |
| 8090 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8091 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8092 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8093 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8094 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8095 | ei->io_tree.track_uptodate = 1; |
| 8096 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8097 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8098 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8099 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8100 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8101 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8102 | RB_CLEAR_NODE(&ei->rb_node); |
| 8103 | |
| 8104 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8105 | } |
| 8106 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8107 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8108 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8109 | { |
| 8110 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8111 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8112 | } |
| 8113 | #endif |
| 8114 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8115 | static void btrfs_i_callback(struct rcu_head *head) |
| 8116 | { |
| 8117 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8118 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8119 | } |
| 8120 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8121 | void btrfs_destroy_inode(struct inode *inode) |
| 8122 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8123 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8124 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8125 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8126 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8127 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8128 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8129 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8130 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8131 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8132 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8133 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8134 | * This can happen where we create an inode, but somebody else also |
| 8135 | * created the same inode and we need to destroy the one we already |
| 8136 | * created. |
| 8137 | */ |
| 8138 | if (!root) |
| 8139 | goto free; |
| 8140 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8141 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8142 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8143 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8144 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8145 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8146 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8147 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8148 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8149 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8150 | if (!ordered) |
| 8151 | break; |
| 8152 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8153 | 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] | 8154 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8155 | btrfs_remove_ordered_extent(inode, ordered); |
| 8156 | btrfs_put_ordered_extent(ordered); |
| 8157 | btrfs_put_ordered_extent(ordered); |
| 8158 | } |
| 8159 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8160 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8161 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8162 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8163 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8164 | } |
| 8165 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8166 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8167 | { |
| 8168 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8169 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8170 | if (root == NULL) |
| 8171 | return 1; |
| 8172 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8173 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8174 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8175 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8176 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8177 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8178 | } |
| 8179 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8180 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8181 | { |
| 8182 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8183 | |
| 8184 | inode_init_once(&ei->vfs_inode); |
| 8185 | } |
| 8186 | |
| 8187 | void btrfs_destroy_cachep(void) |
| 8188 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8189 | /* |
| 8190 | * Make sure all delayed rcu free inodes are flushed before we |
| 8191 | * destroy cache. |
| 8192 | */ |
| 8193 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8194 | if (btrfs_inode_cachep) |
| 8195 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8196 | if (btrfs_trans_handle_cachep) |
| 8197 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8198 | if (btrfs_transaction_cachep) |
| 8199 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8200 | if (btrfs_path_cachep) |
| 8201 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8202 | if (btrfs_free_space_cachep) |
| 8203 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8204 | if (btrfs_delalloc_work_cachep) |
| 8205 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8206 | } |
| 8207 | |
| 8208 | int btrfs_init_cachep(void) |
| 8209 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8210 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8211 | sizeof(struct btrfs_inode), 0, |
| 8212 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8213 | if (!btrfs_inode_cachep) |
| 8214 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8215 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8216 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8217 | sizeof(struct btrfs_trans_handle), 0, |
| 8218 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8219 | if (!btrfs_trans_handle_cachep) |
| 8220 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8221 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8222 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8223 | sizeof(struct btrfs_transaction), 0, |
| 8224 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8225 | if (!btrfs_transaction_cachep) |
| 8226 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8227 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8228 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8229 | sizeof(struct btrfs_path), 0, |
| 8230 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8231 | if (!btrfs_path_cachep) |
| 8232 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8233 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8234 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8235 | sizeof(struct btrfs_free_space), 0, |
| 8236 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8237 | if (!btrfs_free_space_cachep) |
| 8238 | goto fail; |
| 8239 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8240 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8241 | sizeof(struct btrfs_delalloc_work), 0, |
| 8242 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8243 | NULL); |
| 8244 | if (!btrfs_delalloc_work_cachep) |
| 8245 | goto fail; |
| 8246 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8247 | return 0; |
| 8248 | fail: |
| 8249 | btrfs_destroy_cachep(); |
| 8250 | return -ENOMEM; |
| 8251 | } |
| 8252 | |
| 8253 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8254 | struct dentry *dentry, struct kstat *stat) |
| 8255 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8256 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8257 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8258 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8259 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8260 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8261 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8262 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8263 | |
| 8264 | spin_lock(&BTRFS_I(inode)->lock); |
| 8265 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8266 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8267 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8268 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8269 | return 0; |
| 8270 | } |
| 8271 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8272 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8273 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8274 | { |
| 8275 | struct btrfs_trans_handle *trans; |
| 8276 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8277 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8278 | struct inode *new_inode = new_dentry->d_inode; |
| 8279 | struct inode *old_inode = old_dentry->d_inode; |
| 8280 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8281 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8282 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8283 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8284 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8285 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8286 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8287 | return -EPERM; |
| 8288 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8289 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8290 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8291 | return -EXDEV; |
| 8292 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8293 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8294 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8295 | return -ENOTEMPTY; |
| 8296 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8297 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8298 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8299 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8300 | |
| 8301 | |
| 8302 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8303 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8304 | new_dentry->d_name.name, |
| 8305 | new_dentry->d_name.len); |
| 8306 | |
| 8307 | if (ret) { |
| 8308 | if (ret == -EEXIST) { |
| 8309 | /* we shouldn't get |
| 8310 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8311 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8312 | return ret; |
| 8313 | } |
| 8314 | } else { |
| 8315 | /* maybe -EOVERFLOW */ |
| 8316 | return ret; |
| 8317 | } |
| 8318 | } |
| 8319 | ret = 0; |
| 8320 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8321 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8322 | * we're using rename to replace one file with another. Start IO on it |
| 8323 | * now so we don't add too much work to the end of the transaction |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8324 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8325 | 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] | 8326 | filemap_flush(old_inode->i_mapping); |
| 8327 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8328 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8329 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8330 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8331 | /* |
| 8332 | * We want to reserve the absolute worst case amount of items. So if |
| 8333 | * both inodes are subvols and we need to unlink them then that would |
| 8334 | * require 4 item modifications, but if they are both normal inodes it |
| 8335 | * would require 5 item modifications, so we'll assume their normal |
| 8336 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8337 | * should cover the worst case number of items we'll modify. |
| 8338 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8339 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8340 | if (IS_ERR(trans)) { |
| 8341 | ret = PTR_ERR(trans); |
| 8342 | goto out_notrans; |
| 8343 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8344 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8345 | if (dest != root) |
| 8346 | btrfs_record_root_in_trans(trans, dest); |
| 8347 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8348 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8349 | if (ret) |
| 8350 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8351 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8352 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8353 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8354 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8355 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8356 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8357 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8358 | new_dentry->d_name.name, |
| 8359 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8360 | old_ino, |
| 8361 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8362 | if (ret) |
| 8363 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8364 | /* |
| 8365 | * this is an ugly little race, but the rename is required |
| 8366 | * to make sure that if we crash, the inode is either at the |
| 8367 | * old name or the new one. pinning the log transaction lets |
| 8368 | * us make sure we don't allow a log commit to come in after |
| 8369 | * we unlink the name but before we add the new name back in. |
| 8370 | */ |
| 8371 | btrfs_pin_log_trans(root); |
| 8372 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8373 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8374 | inode_inc_iversion(old_dir); |
| 8375 | inode_inc_iversion(new_dir); |
| 8376 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8377 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8378 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8379 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8380 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8381 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8382 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8383 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8384 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8385 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8386 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8387 | old_dentry->d_name.name, |
| 8388 | old_dentry->d_name.len); |
| 8389 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8390 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8391 | old_dentry->d_inode, |
| 8392 | old_dentry->d_name.name, |
| 8393 | old_dentry->d_name.len); |
| 8394 | if (!ret) |
| 8395 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8396 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8397 | if (ret) { |
| 8398 | btrfs_abort_transaction(trans, root, ret); |
| 8399 | goto out_fail; |
| 8400 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8401 | |
| 8402 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8403 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8404 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8405 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8406 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8407 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8408 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8409 | root_objectid, |
| 8410 | new_dentry->d_name.name, |
| 8411 | new_dentry->d_name.len); |
| 8412 | BUG_ON(new_inode->i_nlink == 0); |
| 8413 | } else { |
| 8414 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8415 | new_dentry->d_inode, |
| 8416 | new_dentry->d_name.name, |
| 8417 | new_dentry->d_name.len); |
| 8418 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8419 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8420 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8421 | if (ret) { |
| 8422 | btrfs_abort_transaction(trans, root, ret); |
| 8423 | goto out_fail; |
| 8424 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8425 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8426 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8427 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8428 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8429 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8430 | if (ret) { |
| 8431 | btrfs_abort_transaction(trans, root, ret); |
| 8432 | goto out_fail; |
| 8433 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8434 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8435 | if (old_inode->i_nlink == 1) |
| 8436 | BTRFS_I(old_inode)->dir_index = index; |
| 8437 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8438 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8439 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8440 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8441 | btrfs_end_log_trans(root); |
| 8442 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8443 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8444 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8445 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8446 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8447 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8448 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8449 | return ret; |
| 8450 | } |
| 8451 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8452 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8453 | { |
| 8454 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8455 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8456 | |
| 8457 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8458 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8459 | inode = delalloc_work->inode; |
| 8460 | if (delalloc_work->wait) { |
| 8461 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 8462 | } else { |
| 8463 | filemap_flush(inode->i_mapping); |
| 8464 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8465 | &BTRFS_I(inode)->runtime_flags)) |
| 8466 | filemap_flush(inode->i_mapping); |
| 8467 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8468 | |
| 8469 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8470 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8471 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8472 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8473 | complete(&delalloc_work->completion); |
| 8474 | } |
| 8475 | |
| 8476 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8477 | int wait, int delay_iput) |
| 8478 | { |
| 8479 | struct btrfs_delalloc_work *work; |
| 8480 | |
| 8481 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8482 | if (!work) |
| 8483 | return NULL; |
| 8484 | |
| 8485 | init_completion(&work->completion); |
| 8486 | INIT_LIST_HEAD(&work->list); |
| 8487 | work->inode = inode; |
| 8488 | work->wait = wait; |
| 8489 | work->delay_iput = delay_iput; |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 8490 | btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8491 | |
| 8492 | return work; |
| 8493 | } |
| 8494 | |
| 8495 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8496 | { |
| 8497 | wait_for_completion(&work->completion); |
| 8498 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8499 | } |
| 8500 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8501 | /* |
| 8502 | * some fairly slow code that needs optimization. This walks the list |
| 8503 | * of all the inodes with pending delalloc and forces them to disk. |
| 8504 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8505 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 8506 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8507 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8508 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8509 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8510 | struct btrfs_delalloc_work *work, *next; |
| 8511 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8512 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8513 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8514 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8515 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8516 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8517 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8518 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8519 | spin_lock(&root->delalloc_lock); |
| 8520 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8521 | while (!list_empty(&splice)) { |
| 8522 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8523 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8524 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8525 | list_move_tail(&binode->delalloc_inodes, |
| 8526 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8527 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8528 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8529 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8530 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8531 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8532 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8533 | |
| 8534 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8535 | if (unlikely(!work)) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 8536 | if (delay_iput) |
| 8537 | btrfs_add_delayed_iput(inode); |
| 8538 | else |
| 8539 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8540 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 8541 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8542 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8543 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 8544 | btrfs_queue_work(root->fs_info->flush_workers, |
| 8545 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8546 | ret++; |
| 8547 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 8548 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8549 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8550 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8551 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8552 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8553 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 8554 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8555 | list_for_each_entry_safe(work, next, &works, list) { |
| 8556 | list_del_init(&work->list); |
| 8557 | btrfs_wait_and_free_delalloc_work(work); |
| 8558 | } |
| 8559 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8560 | if (!list_empty_careful(&splice)) { |
| 8561 | spin_lock(&root->delalloc_lock); |
| 8562 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 8563 | spin_unlock(&root->delalloc_lock); |
| 8564 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8565 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8566 | return ret; |
| 8567 | } |
| 8568 | |
| 8569 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 8570 | { |
| 8571 | int ret; |
| 8572 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 8573 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8574 | return -EROFS; |
| 8575 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8576 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 8577 | if (ret > 0) |
| 8578 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8579 | /* |
| 8580 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8581 | * we have to make sure the IO is actually started and that |
| 8582 | * ordered extents get created before we return |
| 8583 | */ |
| 8584 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8585 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8586 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8587 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8588 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8589 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8590 | } |
| 8591 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8592 | return ret; |
| 8593 | } |
| 8594 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8595 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 8596 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8597 | { |
| 8598 | struct btrfs_root *root; |
| 8599 | struct list_head splice; |
| 8600 | int ret; |
| 8601 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 8602 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8603 | return -EROFS; |
| 8604 | |
| 8605 | INIT_LIST_HEAD(&splice); |
| 8606 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8607 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8608 | spin_lock(&fs_info->delalloc_root_lock); |
| 8609 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8610 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8611 | root = list_first_entry(&splice, struct btrfs_root, |
| 8612 | delalloc_root); |
| 8613 | root = btrfs_grab_fs_root(root); |
| 8614 | BUG_ON(!root); |
| 8615 | list_move_tail(&root->delalloc_root, |
| 8616 | &fs_info->delalloc_roots); |
| 8617 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8618 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8619 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8620 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8621 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8622 | goto out; |
| 8623 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8624 | if (nr != -1) { |
| 8625 | nr -= ret; |
| 8626 | WARN_ON(nr < 0); |
| 8627 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8628 | spin_lock(&fs_info->delalloc_root_lock); |
| 8629 | } |
| 8630 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8631 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8632 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8633 | atomic_inc(&fs_info->async_submit_draining); |
| 8634 | while (atomic_read(&fs_info->nr_async_submits) || |
| 8635 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 8636 | wait_event(fs_info->async_submit_wait, |
| 8637 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 8638 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 8639 | } |
| 8640 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8641 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8642 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8643 | spin_lock(&fs_info->delalloc_root_lock); |
| 8644 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 8645 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8646 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8647 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8648 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8649 | } |
| 8650 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8651 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8652 | const char *symname) |
| 8653 | { |
| 8654 | struct btrfs_trans_handle *trans; |
| 8655 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8656 | struct btrfs_path *path; |
| 8657 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8658 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8659 | int err; |
| 8660 | int drop_inode = 0; |
| 8661 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8662 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8663 | int name_len; |
| 8664 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8665 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8666 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8667 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8668 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8669 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8670 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8671 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8672 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8673 | /* |
| 8674 | * 2 items for inode item and ref |
| 8675 | * 2 items for dir items |
| 8676 | * 1 item for xattr if selinux is on |
| 8677 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8678 | trans = btrfs_start_transaction(root, 5); |
| 8679 | if (IS_ERR(trans)) |
| 8680 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8681 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8682 | err = btrfs_find_free_ino(root, &objectid); |
| 8683 | if (err) |
| 8684 | goto out_unlock; |
| 8685 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8686 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8687 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8688 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8689 | if (IS_ERR(inode)) { |
| 8690 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8691 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8692 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8693 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8694 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8695 | if (err) { |
| 8696 | drop_inode = 1; |
| 8697 | goto out_unlock; |
| 8698 | } |
| 8699 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8700 | /* |
| 8701 | * If the active LSM wants to access the inode during |
| 8702 | * d_instantiate it needs these. Smack checks to see |
| 8703 | * if the filesystem supports xattrs by looking at the |
| 8704 | * ops vector. |
| 8705 | */ |
| 8706 | inode->i_fop = &btrfs_file_operations; |
| 8707 | inode->i_op = &btrfs_file_inode_operations; |
| 8708 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8709 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8710 | if (err) |
| 8711 | drop_inode = 1; |
| 8712 | else { |
| 8713 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8714 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8715 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8716 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8717 | if (drop_inode) |
| 8718 | goto out_unlock; |
| 8719 | |
| 8720 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8721 | if (!path) { |
| 8722 | err = -ENOMEM; |
| 8723 | drop_inode = 1; |
| 8724 | goto out_unlock; |
| 8725 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8726 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8727 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8728 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8729 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8730 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8731 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8732 | if (err) { |
| 8733 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8734 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8735 | goto out_unlock; |
| 8736 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8737 | leaf = path->nodes[0]; |
| 8738 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8739 | struct btrfs_file_extent_item); |
| 8740 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8741 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8742 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8743 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8744 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8745 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8746 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8747 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8748 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8749 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8750 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8751 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8752 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8753 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8754 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8755 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8756 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8757 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8758 | err = btrfs_update_inode(trans, root, inode); |
| 8759 | if (err) |
| 8760 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8761 | |
| 8762 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8763 | if (!err) |
| 8764 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8765 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8766 | if (drop_inode) { |
| 8767 | inode_dec_link_count(inode); |
| 8768 | iput(inode); |
| 8769 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8770 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8771 | return err; |
| 8772 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8773 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8774 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8775 | u64 start, u64 num_bytes, u64 min_size, |
| 8776 | loff_t actual_len, u64 *alloc_hint, |
| 8777 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8778 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8779 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8780 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8781 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8782 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8783 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8784 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8785 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8786 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8787 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8788 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8789 | if (trans) |
| 8790 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8791 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8792 | if (own_trans) { |
| 8793 | trans = btrfs_start_transaction(root, 3); |
| 8794 | if (IS_ERR(trans)) { |
| 8795 | ret = PTR_ERR(trans); |
| 8796 | break; |
| 8797 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8798 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8799 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8800 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8801 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 8802 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8803 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8804 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8805 | if (own_trans) |
| 8806 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8807 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8808 | } |
| 8809 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8810 | ret = insert_reserved_file_extent(trans, inode, |
| 8811 | cur_offset, ins.objectid, |
| 8812 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8813 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8814 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8815 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8816 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8817 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8818 | btrfs_abort_transaction(trans, root, ret); |
| 8819 | if (own_trans) |
| 8820 | btrfs_end_transaction(trans, root); |
| 8821 | break; |
| 8822 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8823 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8824 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8825 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8826 | em = alloc_extent_map(); |
| 8827 | if (!em) { |
| 8828 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8829 | &BTRFS_I(inode)->runtime_flags); |
| 8830 | goto next; |
| 8831 | } |
| 8832 | |
| 8833 | em->start = cur_offset; |
| 8834 | em->orig_start = cur_offset; |
| 8835 | em->len = ins.offset; |
| 8836 | em->block_start = ins.objectid; |
| 8837 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8838 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8839 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8840 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8841 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8842 | em->generation = trans->transid; |
| 8843 | |
| 8844 | while (1) { |
| 8845 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8846 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8847 | write_unlock(&em_tree->lock); |
| 8848 | if (ret != -EEXIST) |
| 8849 | break; |
| 8850 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8851 | cur_offset + ins.offset - 1, |
| 8852 | 0); |
| 8853 | } |
| 8854 | free_extent_map(em); |
| 8855 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8856 | num_bytes -= ins.offset; |
| 8857 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8858 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8859 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8860 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8861 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8862 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8863 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8864 | (actual_len > inode->i_size) && |
| 8865 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8866 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8867 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8868 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8869 | i_size = cur_offset; |
| 8870 | i_size_write(inode, i_size); |
| 8871 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8872 | } |
| 8873 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8874 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8875 | |
| 8876 | if (ret) { |
| 8877 | btrfs_abort_transaction(trans, root, ret); |
| 8878 | if (own_trans) |
| 8879 | btrfs_end_transaction(trans, root); |
| 8880 | break; |
| 8881 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8882 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8883 | if (own_trans) |
| 8884 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8885 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8886 | return ret; |
| 8887 | } |
| 8888 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8889 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8890 | u64 start, u64 num_bytes, u64 min_size, |
| 8891 | loff_t actual_len, u64 *alloc_hint) |
| 8892 | { |
| 8893 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8894 | min_size, actual_len, alloc_hint, |
| 8895 | NULL); |
| 8896 | } |
| 8897 | |
| 8898 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8899 | struct btrfs_trans_handle *trans, int mode, |
| 8900 | u64 start, u64 num_bytes, u64 min_size, |
| 8901 | loff_t actual_len, u64 *alloc_hint) |
| 8902 | { |
| 8903 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8904 | min_size, actual_len, alloc_hint, trans); |
| 8905 | } |
| 8906 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8907 | static int btrfs_set_page_dirty(struct page *page) |
| 8908 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8909 | return __set_page_dirty_nobuffers(page); |
| 8910 | } |
| 8911 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8912 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8913 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8914 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8915 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8916 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8917 | if (mask & MAY_WRITE && |
| 8918 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8919 | if (btrfs_root_readonly(root)) |
| 8920 | return -EROFS; |
| 8921 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8922 | return -EACCES; |
| 8923 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8924 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8925 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8926 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 8927 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 8928 | { |
| 8929 | struct btrfs_trans_handle *trans; |
| 8930 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8931 | struct inode *inode = NULL; |
| 8932 | u64 objectid; |
| 8933 | u64 index; |
| 8934 | int ret = 0; |
| 8935 | |
| 8936 | /* |
| 8937 | * 5 units required for adding orphan entry |
| 8938 | */ |
| 8939 | trans = btrfs_start_transaction(root, 5); |
| 8940 | if (IS_ERR(trans)) |
| 8941 | return PTR_ERR(trans); |
| 8942 | |
| 8943 | ret = btrfs_find_free_ino(root, &objectid); |
| 8944 | if (ret) |
| 8945 | goto out; |
| 8946 | |
| 8947 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 8948 | btrfs_ino(dir), objectid, mode, &index); |
| 8949 | if (IS_ERR(inode)) { |
| 8950 | ret = PTR_ERR(inode); |
| 8951 | inode = NULL; |
| 8952 | goto out; |
| 8953 | } |
| 8954 | |
| 8955 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 8956 | if (ret) |
| 8957 | goto out; |
| 8958 | |
| 8959 | ret = btrfs_update_inode(trans, root, inode); |
| 8960 | if (ret) |
| 8961 | goto out; |
| 8962 | |
| 8963 | inode->i_fop = &btrfs_file_operations; |
| 8964 | inode->i_op = &btrfs_file_inode_operations; |
| 8965 | |
| 8966 | inode->i_mapping->a_ops = &btrfs_aops; |
| 8967 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 8968 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 8969 | |
| 8970 | ret = btrfs_orphan_add(trans, inode); |
| 8971 | if (ret) |
| 8972 | goto out; |
| 8973 | |
| 8974 | d_tmpfile(dentry, inode); |
| 8975 | mark_inode_dirty(inode); |
| 8976 | |
| 8977 | out: |
| 8978 | btrfs_end_transaction(trans, root); |
| 8979 | if (ret) |
| 8980 | iput(inode); |
| 8981 | btrfs_balance_delayed_items(root); |
| 8982 | btrfs_btree_balance_dirty(root); |
| 8983 | |
| 8984 | return ret; |
| 8985 | } |
| 8986 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 8987 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8988 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8989 | .lookup = btrfs_lookup, |
| 8990 | .create = btrfs_create, |
| 8991 | .unlink = btrfs_unlink, |
| 8992 | .link = btrfs_link, |
| 8993 | .mkdir = btrfs_mkdir, |
| 8994 | .rmdir = btrfs_rmdir, |
| 8995 | .rename = btrfs_rename, |
| 8996 | .symlink = btrfs_symlink, |
| 8997 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 8998 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 8999 | .setxattr = btrfs_setxattr, |
| 9000 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9001 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9002 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9003 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9004 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9005 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9006 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9007 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9008 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9009 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9010 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9011 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9012 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9013 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9014 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9015 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9016 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9017 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9018 | .llseek = generic_file_llseek, |
| 9019 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9020 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9021 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9022 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9023 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9024 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9025 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9026 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9027 | }; |
| 9028 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9029 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9030 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9031 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9032 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9033 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9034 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9035 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9036 | .set_bit_hook = btrfs_set_bit_hook, |
| 9037 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9038 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9039 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9040 | }; |
| 9041 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9042 | /* |
| 9043 | * btrfs doesn't support the bmap operation because swapfiles |
| 9044 | * use bmap to make a mapping of extents in the file. They assume |
| 9045 | * these extents won't change over the life of the file and they |
| 9046 | * use the bmap result to do IO directly to the drive. |
| 9047 | * |
| 9048 | * the btrfs bmap call would return logical addresses that aren't |
| 9049 | * suitable for IO and they also will change frequently as COW |
| 9050 | * operations happen. So, swapfile + btrfs == corruption. |
| 9051 | * |
| 9052 | * For now we're avoiding this by dropping bmap. |
| 9053 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9054 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9055 | .readpage = btrfs_readpage, |
| 9056 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9057 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9058 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9059 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9060 | .invalidatepage = btrfs_invalidatepage, |
| 9061 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9062 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9063 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9064 | }; |
| 9065 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9066 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9067 | .readpage = btrfs_readpage, |
| 9068 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9069 | .invalidatepage = btrfs_invalidatepage, |
| 9070 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9071 | }; |
| 9072 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9073 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9074 | .getattr = btrfs_getattr, |
| 9075 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9076 | .setxattr = btrfs_setxattr, |
| 9077 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9078 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9079 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9080 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9081 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9082 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9083 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9084 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9085 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9086 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9087 | .getattr = btrfs_getattr, |
| 9088 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9089 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9090 | .setxattr = btrfs_setxattr, |
| 9091 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9092 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9093 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9094 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9095 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9096 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9097 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9098 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9099 | .readlink = generic_readlink, |
| 9100 | .follow_link = page_follow_link_light, |
| 9101 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9102 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9103 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9104 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9105 | .setxattr = btrfs_setxattr, |
| 9106 | .getxattr = btrfs_getxattr, |
| 9107 | .listxattr = btrfs_listxattr, |
| 9108 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9109 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9110 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9111 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9112 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9113 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9114 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9115 | }; |