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); |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 781 | if (ret) { |
| 782 | btrfs_drop_extent_cache(inode, async_extent->start, |
| 783 | async_extent->start + |
| 784 | async_extent->ram_size - 1, 0); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 785 | goto out_free_reserve; |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 786 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 787 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 788 | /* |
| 789 | * clear dirty, set writeback and unlock the pages. |
| 790 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 791 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 792 | async_extent->start + |
| 793 | async_extent->ram_size - 1, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 794 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
| 795 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 796 | PAGE_SET_WRITEBACK); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 797 | ret = btrfs_submit_compressed_write(inode, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 798 | async_extent->start, |
| 799 | async_extent->ram_size, |
| 800 | ins.objectid, |
| 801 | ins.offset, async_extent->pages, |
| 802 | async_extent->nr_pages); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 803 | alloc_hint = ins.objectid + ins.offset; |
| 804 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 805 | if (ret) |
| 806 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 807 | cond_resched(); |
| 808 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 809 | ret = 0; |
| 810 | out: |
| 811 | return ret; |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 812 | out_free_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 813 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 814 | out_free: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 815 | extent_clear_unlock_delalloc(inode, async_extent->start, |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 816 | async_extent->start + |
| 817 | async_extent->ram_size - 1, |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 818 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 819 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
| 820 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 821 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 822 | kfree(async_extent); |
Josef Bacik | 3e04e7f | 2013-02-06 16:49:15 -0500 | [diff] [blame] | 823 | goto again; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 824 | } |
| 825 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 826 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
| 827 | u64 num_bytes) |
| 828 | { |
| 829 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 830 | struct extent_map *em; |
| 831 | u64 alloc_hint = 0; |
| 832 | |
| 833 | read_lock(&em_tree->lock); |
| 834 | em = search_extent_mapping(em_tree, start, num_bytes); |
| 835 | if (em) { |
| 836 | /* |
| 837 | * if block start isn't an actual block number then find the |
| 838 | * first block in this inode and use that as a hint. If that |
| 839 | * block is also bogus then just don't worry about it. |
| 840 | */ |
| 841 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 842 | free_extent_map(em); |
| 843 | em = search_extent_mapping(em_tree, 0, 0); |
| 844 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) |
| 845 | alloc_hint = em->block_start; |
| 846 | if (em) |
| 847 | free_extent_map(em); |
| 848 | } else { |
| 849 | alloc_hint = em->block_start; |
| 850 | free_extent_map(em); |
| 851 | } |
| 852 | } |
| 853 | read_unlock(&em_tree->lock); |
| 854 | |
| 855 | return alloc_hint; |
| 856 | } |
| 857 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 858 | /* |
| 859 | * when extent_io.c finds a delayed allocation range in the file, |
| 860 | * the call backs end up in this code. The basic idea is to |
| 861 | * allocate extents on disk for the range, and create ordered data structs |
| 862 | * in ram to track those extents. |
| 863 | * |
| 864 | * locked_page is the page that writepage had locked already. We use |
| 865 | * it to make sure we don't do extra locks or unlocks. |
| 866 | * |
| 867 | * *page_started is set to one if we unlock locked_page and do everything |
| 868 | * required to start IO on it. It may be clean and already done with |
| 869 | * IO when we return. |
| 870 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 871 | static noinline int cow_file_range(struct inode *inode, |
| 872 | struct page *locked_page, |
| 873 | u64 start, u64 end, int *page_started, |
| 874 | unsigned long *nr_written, |
| 875 | int unlock) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 876 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 877 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 878 | u64 alloc_hint = 0; |
| 879 | u64 num_bytes; |
| 880 | unsigned long ram_size; |
| 881 | u64 disk_num_bytes; |
| 882 | u64 cur_alloc_size; |
| 883 | u64 blocksize = root->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 884 | struct btrfs_key ins; |
| 885 | struct extent_map *em; |
| 886 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 887 | int ret = 0; |
| 888 | |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 889 | if (btrfs_is_free_space_inode(inode)) { |
| 890 | WARN_ON_ONCE(1); |
Josef Bacik | 29bce2f | 2014-02-07 12:21:23 -0500 | [diff] [blame] | 891 | ret = -EINVAL; |
| 892 | goto out_unlock; |
Josef Bacik | 02ecd2c | 2013-10-25 16:19:08 -0400 | [diff] [blame] | 893 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 894 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 895 | num_bytes = ALIGN(end - start + 1, blocksize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 896 | num_bytes = max(blocksize, num_bytes); |
| 897 | disk_num_bytes = num_bytes; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 898 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 899 | /* if this is a small write inside eof, kick off defrag */ |
Liu Bo | 4cb13e5 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 900 | if (num_bytes < 64 * 1024 && |
| 901 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 902 | btrfs_add_inode_defrag(NULL, inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 903 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 904 | if (start == 0) { |
| 905 | /* lets try to make an inline extent */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 906 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
| 907 | NULL); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 908 | if (ret == 0) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 909 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
| 910 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 911 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 912 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
| 913 | PAGE_END_WRITEBACK); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 914 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 915 | *nr_written = *nr_written + |
| 916 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; |
| 917 | *page_started = 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 918 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 919 | } else if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 920 | goto out_unlock; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 921 | } |
| 922 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 923 | |
| 924 | BUG_ON(disk_num_bytes > |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 925 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 926 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 927 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 928 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 929 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 930 | while (disk_num_bytes > 0) { |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 931 | unsigned long op; |
| 932 | |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 933 | cur_alloc_size = disk_num_bytes; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 934 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 935 | root->sectorsize, 0, alloc_hint, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 936 | &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 937 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 938 | goto out_unlock; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 939 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 940 | em = alloc_extent_map(); |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 941 | if (!em) { |
| 942 | ret = -ENOMEM; |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 943 | goto out_reserve; |
Liu Bo | b9aa55b | 2013-05-14 02:12:15 +0000 | [diff] [blame] | 944 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 945 | em->start = start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 946 | em->orig_start = em->start; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 947 | ram_size = ins.offset; |
| 948 | em->len = ins.offset; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 949 | em->mod_start = em->start; |
| 950 | em->mod_len = em->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 951 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 952 | em->block_start = ins.objectid; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 953 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 954 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 955 | em->ram_bytes = ram_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 956 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 957 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 958 | em->generation = -1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 959 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 960 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 961 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 962 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 963 | write_unlock(&em_tree->lock); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 964 | if (ret != -EEXIST) { |
| 965 | free_extent_map(em); |
| 966 | break; |
| 967 | } |
| 968 | btrfs_drop_extent_cache(inode, start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 969 | start + ram_size - 1, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 970 | } |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 971 | if (ret) |
| 972 | goto out_reserve; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 973 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 974 | cur_alloc_size = ins.offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 975 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 976 | ram_size, cur_alloc_size, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 977 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 978 | goto out_drop_extent_cache; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 979 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 980 | if (root->root_key.objectid == |
| 981 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 982 | ret = btrfs_reloc_clone_csums(inode, start, |
| 983 | cur_alloc_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 984 | if (ret) |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 985 | goto out_drop_extent_cache; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 986 | } |
| 987 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 988 | if (disk_num_bytes < cur_alloc_size) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 989 | break; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 990 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 991 | /* we're not doing compressed IO, don't unlock the first |
| 992 | * page (which the caller expects to stay locked), don't |
| 993 | * clear any dirty bits and don't set any writeback bits |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 994 | * |
| 995 | * Do set the Private2 bit so we know this page was properly |
| 996 | * setup for writepage |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 997 | */ |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 998 | op = unlock ? PAGE_UNLOCK : 0; |
| 999 | op |= PAGE_SET_PRIVATE2; |
Chris Mason | a791e35 | 2009-10-08 11:27:10 -0400 | [diff] [blame] | 1000 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1001 | extent_clear_unlock_delalloc(inode, start, |
| 1002 | start + ram_size - 1, locked_page, |
| 1003 | EXTENT_LOCKED | EXTENT_DELALLOC, |
| 1004 | op); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1005 | disk_num_bytes -= cur_alloc_size; |
Chris Mason | c59f895 | 2007-12-17 20:14:04 -0500 | [diff] [blame] | 1006 | num_bytes -= cur_alloc_size; |
| 1007 | alloc_hint = ins.objectid + ins.offset; |
| 1008 | start += cur_alloc_size; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1009 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1010 | out: |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1011 | return ret; |
Miao Xie | b7d5b0a | 2012-11-01 07:32:18 +0000 | [diff] [blame] | 1012 | |
Filipe Manana | d9f8596 | 2014-08-25 10:43:00 +0100 | [diff] [blame] | 1013 | out_drop_extent_cache: |
| 1014 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); |
Liu Bo | ace68ba | 2013-04-22 10:53:47 +0000 | [diff] [blame] | 1015 | out_reserve: |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 1016 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1017 | out_unlock: |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1018 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1019 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 1020 | EXTENT_DELALLOC | EXTENT_DEFRAG, |
| 1021 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | |
| 1022 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1023 | goto out; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1024 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1025 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1026 | /* |
| 1027 | * work queue call back to started compression on a file and pages |
| 1028 | */ |
| 1029 | static noinline void async_cow_start(struct btrfs_work *work) |
| 1030 | { |
| 1031 | struct async_cow *async_cow; |
| 1032 | int num_added = 0; |
| 1033 | async_cow = container_of(work, struct async_cow, work); |
| 1034 | |
| 1035 | compress_file_range(async_cow->inode, async_cow->locked_page, |
| 1036 | async_cow->start, async_cow->end, async_cow, |
| 1037 | &num_added); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1038 | if (num_added == 0) { |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1039 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1040 | async_cow->inode = NULL; |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1041 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * work queue call back to submit previously compressed pages |
| 1046 | */ |
| 1047 | static noinline void async_cow_submit(struct btrfs_work *work) |
| 1048 | { |
| 1049 | struct async_cow *async_cow; |
| 1050 | struct btrfs_root *root; |
| 1051 | unsigned long nr_pages; |
| 1052 | |
| 1053 | async_cow = container_of(work, struct async_cow, work); |
| 1054 | |
| 1055 | root = async_cow->root; |
| 1056 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
| 1057 | PAGE_CACHE_SHIFT; |
| 1058 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1059 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1060 | 5 * 1024 * 1024 && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1061 | waitqueue_active(&root->fs_info->async_submit_wait)) |
| 1062 | wake_up(&root->fs_info->async_submit_wait); |
| 1063 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1064 | if (async_cow->inode) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1065 | submit_compressed_extents(async_cow->inode, async_cow); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1066 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1067 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1068 | static noinline void async_cow_free(struct btrfs_work *work) |
| 1069 | { |
| 1070 | struct async_cow *async_cow; |
| 1071 | async_cow = container_of(work, struct async_cow, work); |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1072 | if (async_cow->inode) |
Josef Bacik | cb77fcd | 2012-06-15 12:19:48 -0600 | [diff] [blame] | 1073 | btrfs_add_delayed_iput(async_cow->inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1074 | kfree(async_cow); |
| 1075 | } |
| 1076 | |
| 1077 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, |
| 1078 | u64 start, u64 end, int *page_started, |
| 1079 | unsigned long *nr_written) |
| 1080 | { |
| 1081 | struct async_cow *async_cow; |
| 1082 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1083 | unsigned long nr_pages; |
| 1084 | u64 cur_end; |
Liu Bo | 287082b | 2012-06-28 04:02:24 -0600 | [diff] [blame] | 1085 | int limit = 10 * 1024 * 1024; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1086 | |
Chris Mason | a3429ab | 2009-10-08 12:30:20 -0400 | [diff] [blame] | 1087 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
| 1088 | 1, 0, NULL, GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1089 | while (start < end) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1090 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1091 | BUG_ON(!async_cow); /* -ENOMEM */ |
Josef Bacik | 8180ef8 | 2012-06-08 15:16:12 -0400 | [diff] [blame] | 1092 | async_cow->inode = igrab(inode); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1093 | async_cow->root = root; |
| 1094 | async_cow->locked_page = locked_page; |
| 1095 | async_cow->start = start; |
| 1096 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1097 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1098 | cur_end = end; |
| 1099 | else |
| 1100 | cur_end = min(end, start + 512 * 1024 - 1); |
| 1101 | |
| 1102 | async_cow->end = cur_end; |
| 1103 | INIT_LIST_HEAD(&async_cow->extents); |
| 1104 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1105 | btrfs_init_work(&async_cow->work, |
| 1106 | btrfs_delalloc_helper, |
| 1107 | async_cow_start, async_cow_submit, |
| 1108 | async_cow_free); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1109 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1110 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
| 1111 | PAGE_CACHE_SHIFT; |
| 1112 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
| 1113 | |
Qu Wenruo | afe3d24 | 2014-02-28 10:46:07 +0800 | [diff] [blame] | 1114 | btrfs_queue_work(root->fs_info->delalloc_workers, |
| 1115 | &async_cow->work); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1116 | |
| 1117 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { |
| 1118 | wait_event(root->fs_info->async_submit_wait, |
| 1119 | (atomic_read(&root->fs_info->async_delalloc_pages) < |
| 1120 | limit)); |
| 1121 | } |
| 1122 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1123 | while (atomic_read(&root->fs_info->async_submit_draining) && |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1124 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1125 | wait_event(root->fs_info->async_submit_wait, |
| 1126 | (atomic_read(&root->fs_info->async_delalloc_pages) == |
| 1127 | 0)); |
| 1128 | } |
| 1129 | |
| 1130 | *nr_written += nr_pages; |
| 1131 | start = cur_end + 1; |
| 1132 | } |
| 1133 | *page_started = 1; |
| 1134 | return 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1135 | } |
| 1136 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1137 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1138 | u64 bytenr, u64 num_bytes) |
| 1139 | { |
| 1140 | int ret; |
| 1141 | struct btrfs_ordered_sum *sums; |
| 1142 | LIST_HEAD(list); |
| 1143 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 1144 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1145 | bytenr + num_bytes - 1, &list, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1146 | if (ret == 0 && list_empty(&list)) |
| 1147 | return 0; |
| 1148 | |
| 1149 | while (!list_empty(&list)) { |
| 1150 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 1151 | list_del(&sums->list); |
| 1152 | kfree(sums); |
| 1153 | } |
| 1154 | return 1; |
| 1155 | } |
| 1156 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1157 | /* |
| 1158 | * when nowcow writeback call back. This checks for snapshots or COW copies |
| 1159 | * of the extents that exist in the file, and COWs the file as required. |
| 1160 | * |
| 1161 | * If no cow copies or snapshots exist, we write directly to the existing |
| 1162 | * blocks on disk |
| 1163 | */ |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1164 | static noinline int run_delalloc_nocow(struct inode *inode, |
| 1165 | struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1166 | u64 start, u64 end, int *page_started, int force, |
| 1167 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1168 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1169 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1170 | struct btrfs_trans_handle *trans; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1171 | struct extent_buffer *leaf; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1172 | struct btrfs_path *path; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1173 | struct btrfs_file_extent_item *fi; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1174 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1175 | u64 cow_start; |
| 1176 | u64 cur_offset; |
| 1177 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1178 | u64 extent_offset; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1179 | u64 disk_bytenr; |
| 1180 | u64 num_bytes; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1181 | u64 disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1182 | u64 ram_bytes; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1183 | int extent_type; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1184 | int ret, err; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1185 | int type; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1186 | int nocow; |
| 1187 | int check_prev = 1; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1188 | bool nolock; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1189 | u64 ino = btrfs_ino(inode); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1190 | |
| 1191 | path = btrfs_alloc_path(); |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1192 | if (!path) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1193 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1194 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1195 | EXTENT_DO_ACCOUNTING | |
| 1196 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1197 | PAGE_CLEAR_DIRTY | |
| 1198 | PAGE_SET_WRITEBACK | |
| 1199 | PAGE_END_WRITEBACK); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1200 | return -ENOMEM; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1201 | } |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1202 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1203 | nolock = btrfs_is_free_space_inode(inode); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1204 | |
| 1205 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1206 | trans = btrfs_join_transaction_nolock(root); |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 1207 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 1208 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 1209 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1210 | if (IS_ERR(trans)) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1211 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
| 1212 | EXTENT_LOCKED | EXTENT_DELALLOC | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1213 | EXTENT_DO_ACCOUNTING | |
| 1214 | EXTENT_DEFRAG, PAGE_UNLOCK | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1215 | PAGE_CLEAR_DIRTY | |
| 1216 | PAGE_SET_WRITEBACK | |
| 1217 | PAGE_END_WRITEBACK); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1218 | btrfs_free_path(path); |
| 1219 | return PTR_ERR(trans); |
| 1220 | } |
| 1221 | |
Josef Bacik | 74b2107 | 2011-04-13 12:02:53 -0400 | [diff] [blame] | 1222 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1223 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1224 | cow_start = (u64)-1; |
| 1225 | cur_offset = start; |
| 1226 | while (1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1227 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1228 | cur_offset, 0); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1229 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1230 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1231 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
| 1232 | leaf = path->nodes[0]; |
| 1233 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1234 | path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1235 | if (found_key.objectid == ino && |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1236 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
| 1237 | path->slots[0]--; |
| 1238 | } |
| 1239 | check_prev = 0; |
| 1240 | next_slot: |
| 1241 | leaf = path->nodes[0]; |
| 1242 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1243 | ret = btrfs_next_leaf(root, path); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1244 | if (ret < 0) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1245 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1246 | if (ret > 0) |
| 1247 | break; |
| 1248 | leaf = path->nodes[0]; |
| 1249 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1250 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1251 | nocow = 0; |
| 1252 | disk_bytenr = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1253 | num_bytes = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1254 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1255 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1256 | if (found_key.objectid > ino || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1257 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
| 1258 | found_key.offset > end) |
| 1259 | break; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1260 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1261 | if (found_key.offset > cur_offset) { |
| 1262 | extent_end = found_key.offset; |
Chris Mason | e9061e2 | 2009-10-09 09:57:45 -0400 | [diff] [blame] | 1263 | extent_type = 0; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1264 | goto out_check; |
| 1265 | } |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1266 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1267 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1268 | struct btrfs_file_extent_item); |
| 1269 | extent_type = btrfs_file_extent_type(leaf, fi); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1270 | |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1271 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1272 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 1273 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1274 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1275 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1276 | extent_end = found_key.offset + |
| 1277 | btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1278 | disk_num_bytes = |
| 1279 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1280 | if (extent_end <= start) { |
| 1281 | path->slots[0]++; |
| 1282 | goto next_slot; |
| 1283 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1284 | if (disk_bytenr == 0) |
| 1285 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1286 | if (btrfs_file_extent_compression(leaf, fi) || |
| 1287 | btrfs_file_extent_encryption(leaf, fi) || |
| 1288 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1289 | goto out_check; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1290 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
| 1291 | goto out_check; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1292 | if (btrfs_extent_readonly(root, disk_bytenr)) |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1293 | goto out_check; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1294 | if (btrfs_cross_ref_exist(trans, root, ino, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1295 | found_key.offset - |
| 1296 | extent_offset, disk_bytenr)) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1297 | goto out_check; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1298 | disk_bytenr += extent_offset; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1299 | disk_bytenr += cur_offset - found_key.offset; |
| 1300 | num_bytes = min(end + 1, extent_end) - cur_offset; |
| 1301 | /* |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1302 | * if there are pending snapshots for this root, |
| 1303 | * we fall into common COW way. |
| 1304 | */ |
| 1305 | if (!nolock) { |
| 1306 | err = btrfs_start_nocow_write(root); |
| 1307 | if (!err) |
| 1308 | goto out_check; |
| 1309 | } |
| 1310 | /* |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1311 | * force cow if csum exists in the range. |
| 1312 | * this ensure that csum for a given extent are |
| 1313 | * either valid or do not exist. |
| 1314 | */ |
| 1315 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 1316 | goto out_check; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1317 | nocow = 1; |
| 1318 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1319 | extent_end = found_key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1320 | btrfs_file_extent_inline_len(leaf, |
| 1321 | path->slots[0], fi); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1322 | extent_end = ALIGN(extent_end, root->sectorsize); |
| 1323 | } else { |
| 1324 | BUG_ON(1); |
| 1325 | } |
| 1326 | out_check: |
| 1327 | if (extent_end <= start) { |
| 1328 | path->slots[0]++; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1329 | if (!nolock && nocow) |
| 1330 | btrfs_end_nocow_write(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1331 | goto next_slot; |
| 1332 | } |
| 1333 | if (!nocow) { |
| 1334 | if (cow_start == (u64)-1) |
| 1335 | cow_start = cur_offset; |
| 1336 | cur_offset = extent_end; |
| 1337 | if (cur_offset > end) |
| 1338 | break; |
| 1339 | path->slots[0]++; |
| 1340 | goto next_slot; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1341 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1342 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1343 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1344 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1345 | ret = cow_file_range(inode, locked_page, |
| 1346 | cow_start, found_key.offset - 1, |
| 1347 | page_started, nr_written, 1); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1348 | if (ret) { |
| 1349 | if (!nolock && nocow) |
| 1350 | btrfs_end_nocow_write(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1351 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1352 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1353 | cow_start = (u64)-1; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1354 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1355 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1356 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 1357 | struct extent_map *em; |
| 1358 | struct extent_map_tree *em_tree; |
| 1359 | em_tree = &BTRFS_I(inode)->extent_tree; |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 1360 | em = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1361 | BUG_ON(!em); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1362 | em->start = cur_offset; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1363 | em->orig_start = found_key.offset - extent_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1364 | em->len = num_bytes; |
| 1365 | em->block_len = num_bytes; |
| 1366 | em->block_start = disk_bytenr; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 1367 | em->orig_block_len = disk_num_bytes; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 1368 | em->ram_bytes = ram_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1369 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 1370 | em->mod_start = em->start; |
| 1371 | em->mod_len = em->len; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1372 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 1373 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 1374 | em->generation = -1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1375 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1376 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 1377 | ret = add_extent_mapping(em_tree, em, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 1378 | write_unlock(&em_tree->lock); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1379 | if (ret != -EEXIST) { |
| 1380 | free_extent_map(em); |
| 1381 | break; |
| 1382 | } |
| 1383 | btrfs_drop_extent_cache(inode, em->start, |
| 1384 | em->start + em->len - 1, 0); |
| 1385 | } |
| 1386 | type = BTRFS_ORDERED_PREALLOC; |
| 1387 | } else { |
| 1388 | type = BTRFS_ORDERED_NOCOW; |
| 1389 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1390 | |
| 1391 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1392 | num_bytes, num_bytes, type); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1393 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1394 | |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1395 | if (root->root_key.objectid == |
| 1396 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 1397 | ret = btrfs_reloc_clone_csums(inode, cur_offset, |
| 1398 | num_bytes); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1399 | if (ret) { |
| 1400 | if (!nolock && nocow) |
| 1401 | btrfs_end_nocow_write(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1402 | goto error; |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1403 | } |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1406 | extent_clear_unlock_delalloc(inode, cur_offset, |
| 1407 | cur_offset + num_bytes - 1, |
| 1408 | locked_page, EXTENT_LOCKED | |
| 1409 | EXTENT_DELALLOC, PAGE_UNLOCK | |
| 1410 | PAGE_SET_PRIVATE2); |
Wang Shilong | e9894fd | 2014-03-27 11:12:25 +0800 | [diff] [blame] | 1411 | if (!nolock && nocow) |
| 1412 | btrfs_end_nocow_write(root); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1413 | cur_offset = extent_end; |
| 1414 | if (cur_offset > end) |
| 1415 | break; |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1416 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1417 | btrfs_release_path(path); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1418 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1419 | if (cur_offset <= end && cow_start == (u64)-1) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1420 | cow_start = cur_offset; |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1421 | cur_offset = end; |
| 1422 | } |
| 1423 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1424 | if (cow_start != (u64)-1) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 1425 | ret = cow_file_range(inode, locked_page, cow_start, end, |
| 1426 | page_started, nr_written, 1); |
Josef Bacik | d788a34 | 2013-10-25 16:55:08 -0400 | [diff] [blame] | 1427 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1428 | goto error; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1429 | } |
| 1430 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1431 | error: |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 1432 | err = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1433 | if (!ret) |
| 1434 | ret = err; |
| 1435 | |
Josef Bacik | 17ca04a | 2012-05-31 15:58:55 -0400 | [diff] [blame] | 1436 | if (ret && cur_offset < end) |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1437 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
| 1438 | locked_page, EXTENT_LOCKED | |
Josef Bacik | 151a41b | 2013-07-29 13:22:24 -0400 | [diff] [blame] | 1439 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
| 1440 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | |
| 1441 | PAGE_CLEAR_DIRTY | |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1442 | PAGE_SET_WRITEBACK | |
| 1443 | PAGE_END_WRITEBACK); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 1444 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1445 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1446 | } |
| 1447 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1448 | /* |
| 1449 | * extent_io.c call back to do delayed allocation processing |
| 1450 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1451 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1452 | u64 start, u64 end, int *page_started, |
| 1453 | unsigned long *nr_written) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1454 | { |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1455 | int ret; |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1456 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1457 | |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1458 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1459 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1460 | page_started, 1, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1461 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1462 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1463 | page_started, 0, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1464 | } else if (!btrfs_test_opt(root, COMPRESS) && |
| 1465 | !(BTRFS_I(inode)->force_compress) && |
| 1466 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { |
Chris Mason | 7f366cf | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 1467 | ret = cow_file_range(inode, locked_page, start, end, |
| 1468 | page_started, nr_written, 1); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1469 | } else { |
| 1470 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1471 | &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1472 | ret = cow_file_range_async(inode, locked_page, start, end, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1473 | page_started, nr_written); |
Josef Bacik | 7ddf5a4 | 2012-06-08 15:26:47 -0400 | [diff] [blame] | 1474 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1475 | return ret; |
| 1476 | } |
| 1477 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1478 | static void btrfs_split_extent_hook(struct inode *inode, |
| 1479 | struct extent_state *orig, u64 split) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1480 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1481 | /* not delalloc, ignore it */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1482 | if (!(orig->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1483 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1484 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1485 | spin_lock(&BTRFS_I(inode)->lock); |
| 1486 | BTRFS_I(inode)->outstanding_extents++; |
| 1487 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * extent_io.c merge_extent_hook, used to track merged delayed allocation |
| 1492 | * extents so we can keep track of new extents that are just merged onto old |
| 1493 | * extents, such as when we are doing sequential writes, so we can properly |
| 1494 | * account for the metadata space we'll need. |
| 1495 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1496 | static void btrfs_merge_extent_hook(struct inode *inode, |
| 1497 | struct extent_state *new, |
| 1498 | struct extent_state *other) |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1499 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1500 | /* not delalloc, ignore it */ |
| 1501 | if (!(other->state & EXTENT_DELALLOC)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1502 | return; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1503 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1504 | spin_lock(&BTRFS_I(inode)->lock); |
| 1505 | BTRFS_I(inode)->outstanding_extents--; |
| 1506 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1507 | } |
| 1508 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1509 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
| 1510 | struct inode *inode) |
| 1511 | { |
| 1512 | spin_lock(&root->delalloc_lock); |
| 1513 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1514 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, |
| 1515 | &root->delalloc_inodes); |
| 1516 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1517 | &BTRFS_I(inode)->runtime_flags); |
| 1518 | root->nr_delalloc_inodes++; |
| 1519 | if (root->nr_delalloc_inodes == 1) { |
| 1520 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1521 | BUG_ON(!list_empty(&root->delalloc_root)); |
| 1522 | list_add_tail(&root->delalloc_root, |
| 1523 | &root->fs_info->delalloc_roots); |
| 1524 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1525 | } |
| 1526 | } |
| 1527 | spin_unlock(&root->delalloc_lock); |
| 1528 | } |
| 1529 | |
| 1530 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, |
| 1531 | struct inode *inode) |
| 1532 | { |
| 1533 | spin_lock(&root->delalloc_lock); |
| 1534 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
| 1535 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); |
| 1536 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
| 1537 | &BTRFS_I(inode)->runtime_flags); |
| 1538 | root->nr_delalloc_inodes--; |
| 1539 | if (!root->nr_delalloc_inodes) { |
| 1540 | spin_lock(&root->fs_info->delalloc_root_lock); |
| 1541 | BUG_ON(list_empty(&root->delalloc_root)); |
| 1542 | list_del_init(&root->delalloc_root); |
| 1543 | spin_unlock(&root->fs_info->delalloc_root_lock); |
| 1544 | } |
| 1545 | } |
| 1546 | spin_unlock(&root->delalloc_lock); |
| 1547 | } |
| 1548 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1549 | /* |
| 1550 | * extent_io.c set_bit_hook, used to track delayed allocation |
| 1551 | * bytes in this file, and to maintain the list of inodes that |
| 1552 | * have pending delalloc work to be done. |
| 1553 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1554 | static void btrfs_set_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1555 | struct extent_state *state, unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1556 | { |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1557 | |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1558 | /* |
| 1559 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1560 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1561 | * bit, which is only set or cleared with irqs on |
| 1562 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1563 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1565 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1566 | bool do_list = !btrfs_is_free_space_inode(inode); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1567 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1568 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1569 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1570 | } else { |
| 1571 | spin_lock(&BTRFS_I(inode)->lock); |
| 1572 | BTRFS_I(inode)->outstanding_extents++; |
| 1573 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1574 | } |
Josef Bacik | 287a0ab | 2010-03-19 18:07:23 +0000 | [diff] [blame] | 1575 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1576 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
| 1577 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1578 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1579 | BTRFS_I(inode)->delalloc_bytes += len; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1580 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1581 | &BTRFS_I(inode)->runtime_flags)) |
| 1582 | btrfs_add_delalloc_inodes(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1583 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1584 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1585 | } |
| 1586 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1587 | /* |
| 1588 | * extent_io.c clear_bit_hook, see set_bit_hook for why |
| 1589 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 1590 | static void btrfs_clear_bit_hook(struct inode *inode, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1591 | struct extent_state *state, |
| 1592 | unsigned long *bits) |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1593 | { |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1594 | /* |
| 1595 | * set_bit and clear bit hooks normally require _irqsave/restore |
Sergei Trofimovich | 27160b6 | 2011-05-20 20:20:32 +0000 | [diff] [blame] | 1596 | * but in this case, we are only testing for the DELALLOC |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1597 | * bit, which is only set or cleared with irqs on |
| 1598 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1599 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1600 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1601 | u64 len = state->end + 1 - state->start; |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1602 | bool do_list = !btrfs_is_free_space_inode(inode); |
Chris Mason | bcbfce8 | 2008-04-22 13:26:47 -0400 | [diff] [blame] | 1603 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1604 | if (*bits & EXTENT_FIRST_DELALLOC) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1605 | *bits &= ~EXTENT_FIRST_DELALLOC; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1606 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
| 1607 | spin_lock(&BTRFS_I(inode)->lock); |
| 1608 | BTRFS_I(inode)->outstanding_extents--; |
| 1609 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1610 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1611 | |
Josef Bacik | b6d08f0 | 2013-09-27 14:57:43 -0400 | [diff] [blame] | 1612 | /* |
| 1613 | * We don't reserve metadata space for space cache inodes so we |
| 1614 | * don't need to call dellalloc_release_metadata if there is an |
| 1615 | * error. |
| 1616 | */ |
| 1617 | if (*bits & EXTENT_DO_ACCOUNTING && |
| 1618 | root != root->fs_info->tree_root) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1619 | btrfs_delalloc_release_metadata(inode, len); |
| 1620 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1621 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1622 | && do_list && !(state->state & EXTENT_NORESERVE)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1623 | btrfs_free_reserved_data_space(inode, len); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 1624 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 1625 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
| 1626 | root->fs_info->delalloc_batch); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1627 | spin_lock(&BTRFS_I(inode)->lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1628 | BTRFS_I(inode)->delalloc_bytes -= len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 1629 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1630 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 1631 | &BTRFS_I(inode)->runtime_flags)) |
| 1632 | btrfs_del_delalloc_inode(root, inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 1633 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1634 | } |
Chris Mason | 291d673 | 2008-01-29 15:55:23 -0500 | [diff] [blame] | 1635 | } |
| 1636 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1637 | /* |
| 1638 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure |
| 1639 | * we don't create bios that span stripes or chunks |
| 1640 | */ |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1641 | 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] | 1642 | size_t size, struct bio *bio, |
| 1643 | unsigned long bio_flags) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1644 | { |
| 1645 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1646 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1647 | u64 length = 0; |
| 1648 | u64 map_length; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1649 | int ret; |
| 1650 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1651 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
| 1652 | return 0; |
| 1653 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1654 | length = bio->bi_iter.bi_size; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1655 | map_length = length; |
David Woodhouse | 64a1670 | 2009-07-15 23:29:37 +0100 | [diff] [blame] | 1656 | ret = btrfs_map_block(root->fs_info, rw, logical, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 1657 | &map_length, NULL, 0); |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1658 | /* Will always return 0 with map_multi == NULL */ |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1659 | BUG_ON(ret < 0); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1660 | if (map_length < length + size) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1661 | return 1; |
Jeff Mahoney | 3444a97 | 2011-10-03 23:23:13 -0400 | [diff] [blame] | 1662 | return 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1663 | } |
| 1664 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1665 | /* |
| 1666 | * in order to insert checksums into the metadata in large chunks, |
| 1667 | * we wait until bio submission time. All the pages in the bio are |
| 1668 | * checksummed and sums are attached onto the ordered extent record. |
| 1669 | * |
| 1670 | * At IO completion time the cums attached on the ordered extent record |
| 1671 | * are inserted into the btree |
| 1672 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1673 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
| 1674 | struct bio *bio, int mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1675 | unsigned long bio_flags, |
| 1676 | u64 bio_offset) |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1677 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1678 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1679 | int ret = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1680 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1681 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1682 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1683 | return 0; |
| 1684 | } |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 1685 | |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1686 | /* |
| 1687 | * in order to insert checksums into the metadata in large chunks, |
| 1688 | * we wait until bio submission time. All the pages in the bio are |
| 1689 | * checksummed and sums are attached onto the ordered extent record. |
| 1690 | * |
| 1691 | * At IO completion time the cums attached on the ordered extent record |
| 1692 | * are inserted into the btree |
| 1693 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1694 | 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] | 1695 | int mirror_num, unsigned long bio_flags, |
| 1696 | u64 bio_offset) |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1697 | { |
| 1698 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1699 | int ret; |
| 1700 | |
| 1701 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); |
| 1702 | if (ret) |
| 1703 | bio_endio(bio, ret); |
| 1704 | return ret; |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1705 | } |
| 1706 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1707 | /* |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1708 | * extent_io.c submission hook. This does the right thing for csum calculation |
| 1709 | * on write, or reading the csums from the tree before a read |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1710 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1711 | 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] | 1712 | int mirror_num, unsigned long bio_flags, |
| 1713 | u64 bio_offset) |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1714 | { |
| 1715 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1716 | int ret = 0; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1717 | int skip_sum; |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1718 | int metadata = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1719 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1720 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1721 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1722 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 1723 | if (btrfs_is_free_space_inode(inode)) |
Jeff Mahoney | 0417341 | 2011-10-03 23:23:12 -0400 | [diff] [blame] | 1724 | metadata = 2; |
| 1725 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1726 | if (!(rw & REQ_WRITE)) { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1727 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
| 1728 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1729 | goto out; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 1730 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1731 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1732 | ret = btrfs_submit_compressed_read(inode, bio, |
| 1733 | mirror_num, |
| 1734 | bio_flags); |
| 1735 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1736 | } else if (!skip_sum) { |
| 1737 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); |
| 1738 | if (ret) |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1739 | goto out; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 1740 | } |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 1741 | goto mapit; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1742 | } else if (async && !skip_sum) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1743 | /* csum items have already been cloned */ |
| 1744 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 1745 | goto mapit; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1746 | /* we're doing a write, do the async checksumming */ |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1747 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
Chris Mason | 44b8bd7 | 2008-04-16 11:14:51 -0400 | [diff] [blame] | 1748 | inode, rw, bio, mirror_num, |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 1749 | bio_flags, bio_offset, |
| 1750 | __btrfs_submit_bio_start, |
Chris Mason | 4a69a41 | 2008-11-06 22:03:00 -0500 | [diff] [blame] | 1751 | __btrfs_submit_bio_done); |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1752 | goto out; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1753 | } else if (!skip_sum) { |
| 1754 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
| 1755 | if (ret) |
| 1756 | goto out; |
Chris Mason | 19b9bdb | 2008-10-30 14:23:13 -0400 | [diff] [blame] | 1757 | } |
| 1758 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1759 | mapit: |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1760 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
| 1761 | |
| 1762 | out: |
| 1763 | if (ret < 0) |
| 1764 | bio_endio(bio, ret); |
| 1765 | return ret; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 1766 | } |
Chris Mason | 6885f30 | 2008-02-20 16:11:05 -0500 | [diff] [blame] | 1767 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1768 | /* |
| 1769 | * given a list of ordered sums record them in the inode. This happens |
| 1770 | * at IO completion time based on sums calculated at bio submission time. |
| 1771 | */ |
Chris Mason | ba1da2f | 2008-07-17 12:54:15 -0400 | [diff] [blame] | 1772 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1773 | struct inode *inode, u64 file_offset, |
| 1774 | struct list_head *list) |
| 1775 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1776 | struct btrfs_ordered_sum *sum; |
| 1777 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1778 | list_for_each_entry(sum, list, list) { |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1779 | trans->adding_csums = 1; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1780 | btrfs_csum_file_blocks(trans, |
| 1781 | BTRFS_I(inode)->root->fs_info->csum_root, sum); |
Miao Xie | 39847c4 | 2013-03-28 08:08:20 +0000 | [diff] [blame] | 1782 | trans->adding_csums = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1783 | } |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1787 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
| 1788 | struct extent_state **cached_state) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1789 | { |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 1790 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1791 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1792 | cached_state, GFP_NOFS); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 1793 | } |
| 1794 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1795 | /* see btrfs_writepage_start_hook for details on why this is required */ |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1796 | struct btrfs_writepage_fixup { |
| 1797 | struct page *page; |
| 1798 | struct btrfs_work work; |
| 1799 | }; |
| 1800 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1801 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1802 | { |
| 1803 | struct btrfs_writepage_fixup *fixup; |
| 1804 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1805 | struct extent_state *cached_state = NULL; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1806 | struct page *page; |
| 1807 | struct inode *inode; |
| 1808 | u64 page_start; |
| 1809 | u64 page_end; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1810 | int ret; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1811 | |
| 1812 | fixup = container_of(work, struct btrfs_writepage_fixup, work); |
| 1813 | page = fixup->page; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1814 | again: |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1815 | lock_page(page); |
| 1816 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { |
| 1817 | ClearPageChecked(page); |
| 1818 | goto out_page; |
| 1819 | } |
| 1820 | |
| 1821 | inode = page->mapping->host; |
| 1822 | page_start = page_offset(page); |
| 1823 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; |
| 1824 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1825 | 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] | 1826 | &cached_state); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1827 | |
| 1828 | /* already ordered? We're done */ |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1829 | if (PagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1830 | goto out; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1831 | |
| 1832 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 1833 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1834 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
| 1835 | page_end, &cached_state, GFP_NOFS); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1836 | unlock_page(page); |
| 1837 | btrfs_start_ordered_extent(inode, ordered, 1); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1838 | btrfs_put_ordered_extent(ordered); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1839 | goto again; |
| 1840 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1841 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1842 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 1843 | if (ret) { |
| 1844 | mapping_set_error(page->mapping, ret); |
| 1845 | end_extent_writepage(page, ret, page_start, page_end); |
| 1846 | ClearPageChecked(page); |
| 1847 | goto out; |
| 1848 | } |
| 1849 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1850 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1851 | ClearPageChecked(page); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1852 | set_page_dirty(page); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1853 | out: |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1854 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
| 1855 | &cached_state, GFP_NOFS); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1856 | out_page: |
| 1857 | unlock_page(page); |
| 1858 | page_cache_release(page); |
Miao Xie | b897abe | 2011-01-26 16:19:22 +0800 | [diff] [blame] | 1859 | kfree(fixup); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | * There are a few paths in the higher layers of the kernel that directly |
| 1864 | * set the page dirty bit without asking the filesystem if it is a |
| 1865 | * good idea. This causes problems because we want to make sure COW |
| 1866 | * properly happens and the data=ordered rules are followed. |
| 1867 | * |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1868 | * 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] | 1869 | * hasn't been properly setup for IO. We kick off an async process |
| 1870 | * to fix it up. The async helper will wait for ordered extents, set |
| 1871 | * the delalloc bit and make it safe to write the page. |
| 1872 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1873 | 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] | 1874 | { |
| 1875 | struct inode *inode = page->mapping->host; |
| 1876 | struct btrfs_writepage_fixup *fixup; |
| 1877 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1878 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1879 | /* this page is properly in the ordered list */ |
| 1880 | if (TestClearPagePrivate2(page)) |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1881 | return 0; |
| 1882 | |
| 1883 | if (PageChecked(page)) |
| 1884 | return -EAGAIN; |
| 1885 | |
| 1886 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); |
| 1887 | if (!fixup) |
| 1888 | return -EAGAIN; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 1889 | |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1890 | SetPageChecked(page); |
| 1891 | page_cache_get(page); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1892 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
| 1893 | btrfs_writepage_fixup_worker, NULL, NULL); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1894 | fixup->page = page; |
Qu Wenruo | dc6e320 | 2014-02-28 10:46:14 +0800 | [diff] [blame] | 1895 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 1896 | return -EBUSY; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 1897 | } |
| 1898 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1899 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 1900 | struct inode *inode, u64 file_pos, |
| 1901 | u64 disk_bytenr, u64 disk_num_bytes, |
| 1902 | u64 num_bytes, u64 ram_bytes, |
| 1903 | u8 compression, u8 encryption, |
| 1904 | u16 other_encoding, int extent_type) |
| 1905 | { |
| 1906 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1907 | struct btrfs_file_extent_item *fi; |
| 1908 | struct btrfs_path *path; |
| 1909 | struct extent_buffer *leaf; |
| 1910 | struct btrfs_key ins; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1911 | int extent_inserted = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1912 | int ret; |
| 1913 | |
| 1914 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1915 | if (!path) |
| 1916 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1917 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 1918 | /* |
| 1919 | * we may be replacing one extent in the tree with another. |
| 1920 | * The new extent is pinned in the extent map, and we don't want |
| 1921 | * to drop it from the cache until it is completely in the btree. |
| 1922 | * |
| 1923 | * So, tell btrfs_drop_extents to leave this extent in the cache. |
| 1924 | * the caller is expected to unpin it and allow it to be merged |
| 1925 | * with the others. |
| 1926 | */ |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1927 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
| 1928 | file_pos + num_bytes, NULL, 0, |
| 1929 | 1, sizeof(*fi), &extent_inserted); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1930 | if (ret) |
| 1931 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1932 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1933 | if (!extent_inserted) { |
| 1934 | ins.objectid = btrfs_ino(inode); |
| 1935 | ins.offset = file_pos; |
| 1936 | ins.type = BTRFS_EXTENT_DATA_KEY; |
| 1937 | |
| 1938 | path->leave_spinning = 1; |
| 1939 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 1940 | sizeof(*fi)); |
| 1941 | if (ret) |
| 1942 | goto out; |
| 1943 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1944 | leaf = path->nodes[0]; |
| 1945 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1946 | struct btrfs_file_extent_item); |
| 1947 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 1948 | btrfs_set_file_extent_type(leaf, fi, extent_type); |
| 1949 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); |
| 1950 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); |
| 1951 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1952 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1953 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); |
| 1954 | btrfs_set_file_extent_compression(leaf, fi, compression); |
| 1955 | btrfs_set_file_extent_encryption(leaf, fi, encryption); |
| 1956 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1957 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1958 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | ce19533 | 2012-09-25 15:26:16 -0400 | [diff] [blame] | 1959 | btrfs_release_path(path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1960 | |
| 1961 | inode_add_bytes(inode, num_bytes); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1962 | |
| 1963 | ins.objectid = disk_bytenr; |
| 1964 | ins.offset = disk_num_bytes; |
| 1965 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1966 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
| 1967 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1968 | btrfs_ino(inode), file_pos, &ins); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1969 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1970 | btrfs_free_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1971 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1972 | return ret; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1973 | } |
| 1974 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 1975 | /* snapshot-aware defrag */ |
| 1976 | struct sa_defrag_extent_backref { |
| 1977 | struct rb_node node; |
| 1978 | struct old_sa_defrag_extent *old; |
| 1979 | u64 root_id; |
| 1980 | u64 inum; |
| 1981 | u64 file_pos; |
| 1982 | u64 extent_offset; |
| 1983 | u64 num_bytes; |
| 1984 | u64 generation; |
| 1985 | }; |
| 1986 | |
| 1987 | struct old_sa_defrag_extent { |
| 1988 | struct list_head list; |
| 1989 | struct new_sa_defrag_extent *new; |
| 1990 | |
| 1991 | u64 extent_offset; |
| 1992 | u64 bytenr; |
| 1993 | u64 offset; |
| 1994 | u64 len; |
| 1995 | int count; |
| 1996 | }; |
| 1997 | |
| 1998 | struct new_sa_defrag_extent { |
| 1999 | struct rb_root root; |
| 2000 | struct list_head head; |
| 2001 | struct btrfs_path *path; |
| 2002 | struct inode *inode; |
| 2003 | u64 file_pos; |
| 2004 | u64 len; |
| 2005 | u64 bytenr; |
| 2006 | u64 disk_len; |
| 2007 | u8 compress_type; |
| 2008 | }; |
| 2009 | |
| 2010 | static int backref_comp(struct sa_defrag_extent_backref *b1, |
| 2011 | struct sa_defrag_extent_backref *b2) |
| 2012 | { |
| 2013 | if (b1->root_id < b2->root_id) |
| 2014 | return -1; |
| 2015 | else if (b1->root_id > b2->root_id) |
| 2016 | return 1; |
| 2017 | |
| 2018 | if (b1->inum < b2->inum) |
| 2019 | return -1; |
| 2020 | else if (b1->inum > b2->inum) |
| 2021 | return 1; |
| 2022 | |
| 2023 | if (b1->file_pos < b2->file_pos) |
| 2024 | return -1; |
| 2025 | else if (b1->file_pos > b2->file_pos) |
| 2026 | return 1; |
| 2027 | |
| 2028 | /* |
| 2029 | * [------------------------------] ===> (a range of space) |
| 2030 | * |<--->| |<---->| =============> (fs/file tree A) |
| 2031 | * |<---------------------------->| ===> (fs/file tree B) |
| 2032 | * |
| 2033 | * A range of space can refer to two file extents in one tree while |
| 2034 | * refer to only one file extent in another tree. |
| 2035 | * |
| 2036 | * So we may process a disk offset more than one time(two extents in A) |
| 2037 | * and locate at the same extent(one extent in B), then insert two same |
| 2038 | * backrefs(both refer to the extent in B). |
| 2039 | */ |
| 2040 | return 0; |
| 2041 | } |
| 2042 | |
| 2043 | static void backref_insert(struct rb_root *root, |
| 2044 | struct sa_defrag_extent_backref *backref) |
| 2045 | { |
| 2046 | struct rb_node **p = &root->rb_node; |
| 2047 | struct rb_node *parent = NULL; |
| 2048 | struct sa_defrag_extent_backref *entry; |
| 2049 | int ret; |
| 2050 | |
| 2051 | while (*p) { |
| 2052 | parent = *p; |
| 2053 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); |
| 2054 | |
| 2055 | ret = backref_comp(backref, entry); |
| 2056 | if (ret < 0) |
| 2057 | p = &(*p)->rb_left; |
| 2058 | else |
| 2059 | p = &(*p)->rb_right; |
| 2060 | } |
| 2061 | |
| 2062 | rb_link_node(&backref->node, parent, p); |
| 2063 | rb_insert_color(&backref->node, root); |
| 2064 | } |
| 2065 | |
| 2066 | /* |
| 2067 | * Note the backref might has changed, and in this case we just return 0. |
| 2068 | */ |
| 2069 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, |
| 2070 | void *ctx) |
| 2071 | { |
| 2072 | struct btrfs_file_extent_item *extent; |
| 2073 | struct btrfs_fs_info *fs_info; |
| 2074 | struct old_sa_defrag_extent *old = ctx; |
| 2075 | struct new_sa_defrag_extent *new = old->new; |
| 2076 | struct btrfs_path *path = new->path; |
| 2077 | struct btrfs_key key; |
| 2078 | struct btrfs_root *root; |
| 2079 | struct sa_defrag_extent_backref *backref; |
| 2080 | struct extent_buffer *leaf; |
| 2081 | struct inode *inode = new->inode; |
| 2082 | int slot; |
| 2083 | int ret; |
| 2084 | u64 extent_offset; |
| 2085 | u64 num_bytes; |
| 2086 | |
| 2087 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && |
| 2088 | inum == btrfs_ino(inode)) |
| 2089 | return 0; |
| 2090 | |
| 2091 | key.objectid = root_id; |
| 2092 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2093 | key.offset = (u64)-1; |
| 2094 | |
| 2095 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2096 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2097 | if (IS_ERR(root)) { |
| 2098 | if (PTR_ERR(root) == -ENOENT) |
| 2099 | return 0; |
| 2100 | WARN_ON(1); |
| 2101 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", |
| 2102 | inum, offset, root_id); |
| 2103 | return PTR_ERR(root); |
| 2104 | } |
| 2105 | |
| 2106 | key.objectid = inum; |
| 2107 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2108 | if (offset > (u64)-1 << 32) |
| 2109 | key.offset = 0; |
| 2110 | else |
| 2111 | key.offset = offset; |
| 2112 | |
| 2113 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2114 | if (WARN_ON(ret < 0)) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2115 | return ret; |
Josef Bacik | 50f1319 | 2013-07-22 12:50:37 -0400 | [diff] [blame] | 2116 | ret = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2117 | |
| 2118 | while (1) { |
| 2119 | cond_resched(); |
| 2120 | |
| 2121 | leaf = path->nodes[0]; |
| 2122 | slot = path->slots[0]; |
| 2123 | |
| 2124 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2125 | ret = btrfs_next_leaf(root, path); |
| 2126 | if (ret < 0) { |
| 2127 | goto out; |
| 2128 | } else if (ret > 0) { |
| 2129 | ret = 0; |
| 2130 | goto out; |
| 2131 | } |
| 2132 | continue; |
| 2133 | } |
| 2134 | |
| 2135 | path->slots[0]++; |
| 2136 | |
| 2137 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2138 | |
| 2139 | if (key.objectid > inum) |
| 2140 | goto out; |
| 2141 | |
| 2142 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) |
| 2143 | continue; |
| 2144 | |
| 2145 | extent = btrfs_item_ptr(leaf, slot, |
| 2146 | struct btrfs_file_extent_item); |
| 2147 | |
| 2148 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) |
| 2149 | continue; |
| 2150 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2151 | /* |
| 2152 | * 'offset' refers to the exact key.offset, |
| 2153 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. |
| 2154 | * (key.offset - extent_offset). |
| 2155 | */ |
| 2156 | if (key.offset != offset) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2157 | continue; |
| 2158 | |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2159 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2160 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2161 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2162 | if (extent_offset >= old->extent_offset + old->offset + |
| 2163 | old->len || extent_offset + num_bytes <= |
| 2164 | old->extent_offset + old->offset) |
| 2165 | continue; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2166 | break; |
| 2167 | } |
| 2168 | |
| 2169 | backref = kmalloc(sizeof(*backref), GFP_NOFS); |
| 2170 | if (!backref) { |
| 2171 | ret = -ENOENT; |
| 2172 | goto out; |
| 2173 | } |
| 2174 | |
| 2175 | backref->root_id = root_id; |
| 2176 | backref->inum = inum; |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2177 | backref->file_pos = offset; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2178 | backref->num_bytes = num_bytes; |
| 2179 | backref->extent_offset = extent_offset; |
| 2180 | backref->generation = btrfs_file_extent_generation(leaf, extent); |
| 2181 | backref->old = old; |
| 2182 | backref_insert(&new->root, backref); |
| 2183 | old->count++; |
| 2184 | out: |
| 2185 | btrfs_release_path(path); |
| 2186 | WARN_ON(ret); |
| 2187 | return ret; |
| 2188 | } |
| 2189 | |
| 2190 | static noinline bool record_extent_backrefs(struct btrfs_path *path, |
| 2191 | struct new_sa_defrag_extent *new) |
| 2192 | { |
| 2193 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; |
| 2194 | struct old_sa_defrag_extent *old, *tmp; |
| 2195 | int ret; |
| 2196 | |
| 2197 | new->path = path; |
| 2198 | |
| 2199 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
Liu Bo | e68afa4 | 2013-07-01 22:13:26 +0800 | [diff] [blame] | 2200 | ret = iterate_inodes_from_logical(old->bytenr + |
| 2201 | old->extent_offset, fs_info, |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2202 | path, record_one_backref, |
| 2203 | old); |
Josef Bacik | 4724b10 | 2013-11-05 11:11:40 -0500 | [diff] [blame] | 2204 | if (ret < 0 && ret != -ENOENT) |
| 2205 | return false; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2206 | |
| 2207 | /* no backref to be processed for this extent */ |
| 2208 | if (!old->count) { |
| 2209 | list_del(&old->list); |
| 2210 | kfree(old); |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | if (list_empty(&new->head)) |
| 2215 | return false; |
| 2216 | |
| 2217 | return true; |
| 2218 | } |
| 2219 | |
| 2220 | static int relink_is_mergable(struct extent_buffer *leaf, |
| 2221 | struct btrfs_file_extent_item *fi, |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2222 | struct new_sa_defrag_extent *new) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2223 | { |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2224 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2225 | return 0; |
| 2226 | |
| 2227 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2228 | return 0; |
| 2229 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2230 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
| 2231 | return 0; |
| 2232 | |
| 2233 | if (btrfs_file_extent_encryption(leaf, fi) || |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2234 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 2235 | return 0; |
| 2236 | |
| 2237 | return 1; |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * Note the backref might has changed, and in this case we just return 0. |
| 2242 | */ |
| 2243 | static noinline int relink_extent_backref(struct btrfs_path *path, |
| 2244 | struct sa_defrag_extent_backref *prev, |
| 2245 | struct sa_defrag_extent_backref *backref) |
| 2246 | { |
| 2247 | struct btrfs_file_extent_item *extent; |
| 2248 | struct btrfs_file_extent_item *item; |
| 2249 | struct btrfs_ordered_extent *ordered; |
| 2250 | struct btrfs_trans_handle *trans; |
| 2251 | struct btrfs_fs_info *fs_info; |
| 2252 | struct btrfs_root *root; |
| 2253 | struct btrfs_key key; |
| 2254 | struct extent_buffer *leaf; |
| 2255 | struct old_sa_defrag_extent *old = backref->old; |
| 2256 | struct new_sa_defrag_extent *new = old->new; |
| 2257 | struct inode *src_inode = new->inode; |
| 2258 | struct inode *inode; |
| 2259 | struct extent_state *cached = NULL; |
| 2260 | int ret = 0; |
| 2261 | u64 start; |
| 2262 | u64 len; |
| 2263 | u64 lock_start; |
| 2264 | u64 lock_end; |
| 2265 | bool merge = false; |
| 2266 | int index; |
| 2267 | |
| 2268 | if (prev && prev->root_id == backref->root_id && |
| 2269 | prev->inum == backref->inum && |
| 2270 | prev->file_pos + prev->num_bytes == backref->file_pos) |
| 2271 | merge = true; |
| 2272 | |
| 2273 | /* step 1: get root */ |
| 2274 | key.objectid = backref->root_id; |
| 2275 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2276 | key.offset = (u64)-1; |
| 2277 | |
| 2278 | fs_info = BTRFS_I(src_inode)->root->fs_info; |
| 2279 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 2280 | |
| 2281 | root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 2282 | if (IS_ERR(root)) { |
| 2283 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2284 | if (PTR_ERR(root) == -ENOENT) |
| 2285 | return 0; |
| 2286 | return PTR_ERR(root); |
| 2287 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2288 | |
Wang Shilong | bcbba5e | 2014-02-08 23:46:35 +0800 | [diff] [blame] | 2289 | if (btrfs_root_readonly(root)) { |
| 2290 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2291 | return 0; |
| 2292 | } |
| 2293 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2294 | /* step 2: get inode */ |
| 2295 | key.objectid = backref->inum; |
| 2296 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2297 | key.offset = 0; |
| 2298 | |
| 2299 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 2300 | if (IS_ERR(inode)) { |
| 2301 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
| 2305 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 2306 | |
| 2307 | /* step 3: relink backref */ |
| 2308 | lock_start = backref->file_pos; |
| 2309 | lock_end = backref->file_pos + backref->num_bytes - 1; |
| 2310 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2311 | 0, &cached); |
| 2312 | |
| 2313 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); |
| 2314 | if (ordered) { |
| 2315 | btrfs_put_ordered_extent(ordered); |
| 2316 | goto out_unlock; |
| 2317 | } |
| 2318 | |
| 2319 | trans = btrfs_join_transaction(root); |
| 2320 | if (IS_ERR(trans)) { |
| 2321 | ret = PTR_ERR(trans); |
| 2322 | goto out_unlock; |
| 2323 | } |
| 2324 | |
| 2325 | key.objectid = backref->inum; |
| 2326 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2327 | key.offset = backref->file_pos; |
| 2328 | |
| 2329 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2330 | if (ret < 0) { |
| 2331 | goto out_free_path; |
| 2332 | } else if (ret > 0) { |
| 2333 | ret = 0; |
| 2334 | goto out_free_path; |
| 2335 | } |
| 2336 | |
| 2337 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2338 | struct btrfs_file_extent_item); |
| 2339 | |
| 2340 | if (btrfs_file_extent_generation(path->nodes[0], extent) != |
| 2341 | backref->generation) |
| 2342 | goto out_free_path; |
| 2343 | |
| 2344 | btrfs_release_path(path); |
| 2345 | |
| 2346 | start = backref->file_pos; |
| 2347 | if (backref->extent_offset < old->extent_offset + old->offset) |
| 2348 | start += old->extent_offset + old->offset - |
| 2349 | backref->extent_offset; |
| 2350 | |
| 2351 | len = min(backref->extent_offset + backref->num_bytes, |
| 2352 | old->extent_offset + old->offset + old->len); |
| 2353 | len -= max(backref->extent_offset, old->extent_offset + old->offset); |
| 2354 | |
| 2355 | ret = btrfs_drop_extents(trans, root, inode, start, |
| 2356 | start + len, 1); |
| 2357 | if (ret) |
| 2358 | goto out_free_path; |
| 2359 | again: |
| 2360 | key.objectid = btrfs_ino(inode); |
| 2361 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2362 | key.offset = start; |
| 2363 | |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2364 | path->leave_spinning = 1; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2365 | if (merge) { |
| 2366 | struct btrfs_file_extent_item *fi; |
| 2367 | u64 extent_len; |
| 2368 | struct btrfs_key found_key; |
| 2369 | |
Gui Hecheng | 3c9665d | 2014-01-23 13:41:09 +0800 | [diff] [blame] | 2370 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2371 | if (ret < 0) |
| 2372 | goto out_free_path; |
| 2373 | |
| 2374 | path->slots[0]--; |
| 2375 | leaf = path->nodes[0]; |
| 2376 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2377 | |
| 2378 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2379 | struct btrfs_file_extent_item); |
| 2380 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 2381 | |
Liu Bo | 116e002 | 2013-08-02 16:30:40 +0800 | [diff] [blame] | 2382 | if (extent_len + found_key.offset == start && |
| 2383 | relink_is_mergable(leaf, fi, new)) { |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2384 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 2385 | extent_len + len); |
| 2386 | btrfs_mark_buffer_dirty(leaf); |
| 2387 | inode_add_bytes(inode, len); |
| 2388 | |
| 2389 | ret = 1; |
| 2390 | goto out_free_path; |
| 2391 | } else { |
| 2392 | merge = false; |
| 2393 | btrfs_release_path(path); |
| 2394 | goto again; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2399 | sizeof(*extent)); |
| 2400 | if (ret) { |
| 2401 | btrfs_abort_transaction(trans, root, ret); |
| 2402 | goto out_free_path; |
| 2403 | } |
| 2404 | |
| 2405 | leaf = path->nodes[0]; |
| 2406 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2407 | struct btrfs_file_extent_item); |
| 2408 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); |
| 2409 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); |
| 2410 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); |
| 2411 | btrfs_set_file_extent_num_bytes(leaf, item, len); |
| 2412 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); |
| 2413 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 2414 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
| 2415 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); |
| 2416 | btrfs_set_file_extent_encryption(leaf, item, 0); |
| 2417 | btrfs_set_file_extent_other_encoding(leaf, item, 0); |
| 2418 | |
| 2419 | btrfs_mark_buffer_dirty(leaf); |
| 2420 | inode_add_bytes(inode, len); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2421 | btrfs_release_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2422 | |
| 2423 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, |
| 2424 | new->disk_len, 0, |
| 2425 | backref->root_id, backref->inum, |
| 2426 | new->file_pos, 0); /* start - extent_offset */ |
| 2427 | if (ret) { |
| 2428 | btrfs_abort_transaction(trans, root, ret); |
| 2429 | goto out_free_path; |
| 2430 | } |
| 2431 | |
| 2432 | ret = 1; |
| 2433 | out_free_path: |
| 2434 | btrfs_release_path(path); |
Liu Bo | a09a0a7 | 2013-03-11 09:20:58 +0000 | [diff] [blame] | 2435 | path->leave_spinning = 0; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2436 | btrfs_end_transaction(trans, root); |
| 2437 | out_unlock: |
| 2438 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, |
| 2439 | &cached, GFP_NOFS); |
| 2440 | iput(inode); |
| 2441 | return ret; |
| 2442 | } |
| 2443 | |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2444 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
| 2445 | { |
| 2446 | struct old_sa_defrag_extent *old, *tmp; |
| 2447 | |
| 2448 | if (!new) |
| 2449 | return; |
| 2450 | |
| 2451 | list_for_each_entry_safe(old, tmp, &new->head, list) { |
| 2452 | list_del(&old->list); |
| 2453 | kfree(old); |
| 2454 | } |
| 2455 | kfree(new); |
| 2456 | } |
| 2457 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2458 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
| 2459 | { |
| 2460 | struct btrfs_path *path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2461 | struct sa_defrag_extent_backref *backref; |
| 2462 | struct sa_defrag_extent_backref *prev = NULL; |
| 2463 | struct inode *inode; |
| 2464 | struct btrfs_root *root; |
| 2465 | struct rb_node *node; |
| 2466 | int ret; |
| 2467 | |
| 2468 | inode = new->inode; |
| 2469 | root = BTRFS_I(inode)->root; |
| 2470 | |
| 2471 | path = btrfs_alloc_path(); |
| 2472 | if (!path) |
| 2473 | return; |
| 2474 | |
| 2475 | if (!record_extent_backrefs(path, new)) { |
| 2476 | btrfs_free_path(path); |
| 2477 | goto out; |
| 2478 | } |
| 2479 | btrfs_release_path(path); |
| 2480 | |
| 2481 | while (1) { |
| 2482 | node = rb_first(&new->root); |
| 2483 | if (!node) |
| 2484 | break; |
| 2485 | rb_erase(node, &new->root); |
| 2486 | |
| 2487 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); |
| 2488 | |
| 2489 | ret = relink_extent_backref(path, prev, backref); |
| 2490 | WARN_ON(ret < 0); |
| 2491 | |
| 2492 | kfree(prev); |
| 2493 | |
| 2494 | if (ret == 1) |
| 2495 | prev = backref; |
| 2496 | else |
| 2497 | prev = NULL; |
| 2498 | cond_resched(); |
| 2499 | } |
| 2500 | kfree(prev); |
| 2501 | |
| 2502 | btrfs_free_path(path); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2503 | out: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2504 | free_sa_defrag_extent(new); |
| 2505 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2506 | atomic_dec(&root->fs_info->defrag_running); |
| 2507 | wake_up(&root->fs_info->transaction_wait); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | static struct new_sa_defrag_extent * |
| 2511 | record_old_file_extents(struct inode *inode, |
| 2512 | struct btrfs_ordered_extent *ordered) |
| 2513 | { |
| 2514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2515 | struct btrfs_path *path; |
| 2516 | struct btrfs_key key; |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2517 | struct old_sa_defrag_extent *old; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2518 | struct new_sa_defrag_extent *new; |
| 2519 | int ret; |
| 2520 | |
| 2521 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 2522 | if (!new) |
| 2523 | return NULL; |
| 2524 | |
| 2525 | new->inode = inode; |
| 2526 | new->file_pos = ordered->file_offset; |
| 2527 | new->len = ordered->len; |
| 2528 | new->bytenr = ordered->start; |
| 2529 | new->disk_len = ordered->disk_len; |
| 2530 | new->compress_type = ordered->compress_type; |
| 2531 | new->root = RB_ROOT; |
| 2532 | INIT_LIST_HEAD(&new->head); |
| 2533 | |
| 2534 | path = btrfs_alloc_path(); |
| 2535 | if (!path) |
| 2536 | goto out_kfree; |
| 2537 | |
| 2538 | key.objectid = btrfs_ino(inode); |
| 2539 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2540 | key.offset = new->file_pos; |
| 2541 | |
| 2542 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2543 | if (ret < 0) |
| 2544 | goto out_free_path; |
| 2545 | if (ret > 0 && path->slots[0] > 0) |
| 2546 | path->slots[0]--; |
| 2547 | |
| 2548 | /* find out all the old extents for the file range */ |
| 2549 | while (1) { |
| 2550 | struct btrfs_file_extent_item *extent; |
| 2551 | struct extent_buffer *l; |
| 2552 | int slot; |
| 2553 | u64 num_bytes; |
| 2554 | u64 offset; |
| 2555 | u64 end; |
| 2556 | u64 disk_bytenr; |
| 2557 | u64 extent_offset; |
| 2558 | |
| 2559 | l = path->nodes[0]; |
| 2560 | slot = path->slots[0]; |
| 2561 | |
| 2562 | if (slot >= btrfs_header_nritems(l)) { |
| 2563 | ret = btrfs_next_leaf(root, path); |
| 2564 | if (ret < 0) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2565 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2566 | else if (ret > 0) |
| 2567 | break; |
| 2568 | continue; |
| 2569 | } |
| 2570 | |
| 2571 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2572 | |
| 2573 | if (key.objectid != btrfs_ino(inode)) |
| 2574 | break; |
| 2575 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2576 | break; |
| 2577 | if (key.offset >= new->file_pos + new->len) |
| 2578 | break; |
| 2579 | |
| 2580 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); |
| 2581 | |
| 2582 | num_bytes = btrfs_file_extent_num_bytes(l, extent); |
| 2583 | if (key.offset + num_bytes < new->file_pos) |
| 2584 | goto next; |
| 2585 | |
| 2586 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); |
| 2587 | if (!disk_bytenr) |
| 2588 | goto next; |
| 2589 | |
| 2590 | extent_offset = btrfs_file_extent_offset(l, extent); |
| 2591 | |
| 2592 | old = kmalloc(sizeof(*old), GFP_NOFS); |
| 2593 | if (!old) |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2594 | goto out_free_path; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2595 | |
| 2596 | offset = max(new->file_pos, key.offset); |
| 2597 | end = min(new->file_pos + new->len, key.offset + num_bytes); |
| 2598 | |
| 2599 | old->bytenr = disk_bytenr; |
| 2600 | old->extent_offset = extent_offset; |
| 2601 | old->offset = offset - key.offset; |
| 2602 | old->len = end - offset; |
| 2603 | old->new = new; |
| 2604 | old->count = 0; |
| 2605 | list_add_tail(&old->list, &new->head); |
| 2606 | next: |
| 2607 | path->slots[0]++; |
| 2608 | cond_resched(); |
| 2609 | } |
| 2610 | |
| 2611 | btrfs_free_path(path); |
| 2612 | atomic_inc(&root->fs_info->defrag_running); |
| 2613 | |
| 2614 | return new; |
| 2615 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2616 | out_free_path: |
| 2617 | btrfs_free_path(path); |
| 2618 | out_kfree: |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2619 | free_sa_defrag_extent(new); |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2620 | return NULL; |
| 2621 | } |
| 2622 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2623 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
| 2624 | u64 start, u64 len) |
| 2625 | { |
| 2626 | struct btrfs_block_group_cache *cache; |
| 2627 | |
| 2628 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2629 | ASSERT(cache); |
| 2630 | |
| 2631 | spin_lock(&cache->lock); |
| 2632 | cache->delalloc_bytes -= len; |
| 2633 | spin_unlock(&cache->lock); |
| 2634 | |
| 2635 | btrfs_put_block_group(cache); |
| 2636 | } |
| 2637 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2638 | /* as ordered data IO finishes, this gets called so we can finish |
| 2639 | * an ordered extent if the range of bytes in the file it covers are |
| 2640 | * fully written. |
| 2641 | */ |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2642 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2643 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2644 | struct inode *inode = ordered_extent->inode; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2645 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2646 | struct btrfs_trans_handle *trans = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2647 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2648 | struct extent_state *cached_state = NULL; |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2649 | struct new_sa_defrag_extent *new = NULL; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2650 | int compress_type = 0; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2651 | int ret = 0; |
| 2652 | u64 logical_len = ordered_extent->len; |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 2653 | bool nolock; |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2654 | bool truncated = false; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2655 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 2656 | nolock = btrfs_is_free_space_inode(inode); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2657 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2658 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
| 2659 | ret = -EIO; |
| 2660 | goto out; |
| 2661 | } |
| 2662 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2663 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
| 2664 | truncated = true; |
| 2665 | logical_len = ordered_extent->truncated_len; |
| 2666 | /* Truncated the entire extent, don't bother adding */ |
| 2667 | if (!logical_len) |
| 2668 | goto out; |
| 2669 | } |
| 2670 | |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2671 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2672 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2673 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2674 | if (nolock) |
| 2675 | trans = btrfs_join_transaction_nolock(root); |
| 2676 | else |
| 2677 | trans = btrfs_join_transaction(root); |
| 2678 | if (IS_ERR(trans)) { |
| 2679 | ret = PTR_ERR(trans); |
| 2680 | trans = NULL; |
| 2681 | goto out; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2682 | } |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2683 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
| 2684 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2685 | if (ret) /* -ENOMEM or corruption */ |
| 2686 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2687 | goto out; |
| 2688 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2689 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2690 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
| 2691 | ordered_extent->file_offset + ordered_extent->len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2692 | 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2693 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2694 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
| 2695 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2696 | EXTENT_DEFRAG, 1, cached_state); |
| 2697 | if (ret) { |
| 2698 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Josef Bacik | 8101c8d | 2014-01-29 16:05:30 -0500 | [diff] [blame] | 2699 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2700 | /* the inode is shared */ |
| 2701 | new = record_old_file_extents(inode, ordered_extent); |
| 2702 | |
| 2703 | clear_extent_bit(io_tree, ordered_extent->file_offset, |
| 2704 | ordered_extent->file_offset + ordered_extent->len - 1, |
| 2705 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); |
| 2706 | } |
| 2707 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2708 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2709 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2710 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 2711 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2712 | if (IS_ERR(trans)) { |
| 2713 | ret = PTR_ERR(trans); |
| 2714 | trans = NULL; |
| 2715 | goto out_unlock; |
| 2716 | } |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2717 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2718 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2719 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2720 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2721 | compress_type = ordered_extent->compress_type; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2722 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2723 | BUG_ON(compress_type); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 2724 | ret = btrfs_mark_extent_written(trans, inode, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2725 | ordered_extent->file_offset, |
| 2726 | ordered_extent->file_offset + |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2727 | logical_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2728 | } else { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2729 | BUG_ON(root == root->fs_info->tree_root); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2730 | ret = insert_reserved_file_extent(trans, inode, |
| 2731 | ordered_extent->file_offset, |
| 2732 | ordered_extent->start, |
| 2733 | ordered_extent->disk_len, |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2734 | logical_len, logical_len, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 2735 | compress_type, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2736 | BTRFS_FILE_EXTENT_REG); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2737 | if (!ret) |
| 2738 | btrfs_release_delalloc_bytes(root, |
| 2739 | ordered_extent->start, |
| 2740 | ordered_extent->disk_len); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2741 | } |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2742 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
| 2743 | ordered_extent->file_offset, ordered_extent->len, |
| 2744 | trans->transid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2745 | if (ret < 0) { |
| 2746 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2747 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2748 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 2749 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2750 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
| 2751 | &ordered_extent->list); |
| 2752 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 2753 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
| 2754 | ret = btrfs_update_inode_fallback(trans, root, inode); |
| 2755 | if (ret) { /* -ENOMEM or corruption */ |
| 2756 | btrfs_abort_transaction(trans, root, ret); |
| 2757 | goto out_unlock; |
Josef Bacik | 1ef30be | 2011-04-05 19:25:36 -0400 | [diff] [blame] | 2758 | } |
| 2759 | ret = 0; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2760 | out_unlock: |
| 2761 | unlock_extent_cached(io_tree, ordered_extent->file_offset, |
| 2762 | ordered_extent->file_offset + |
| 2763 | ordered_extent->len - 1, &cached_state, GFP_NOFS); |
Yan, Zheng | c216775 | 2009-11-12 09:34:21 +0000 | [diff] [blame] | 2764 | out: |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2765 | if (root != root->fs_info->tree_root) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2766 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 2767 | if (trans) |
| 2768 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2769 | |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2770 | if (ret || truncated) { |
| 2771 | u64 start, end; |
| 2772 | |
| 2773 | if (truncated) |
| 2774 | start = ordered_extent->file_offset + logical_len; |
| 2775 | else |
| 2776 | start = ordered_extent->file_offset; |
| 2777 | end = ordered_extent->file_offset + ordered_extent->len - 1; |
| 2778 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); |
| 2779 | |
| 2780 | /* Drop the cache for the part of the extent we didn't write. */ |
| 2781 | btrfs_drop_extent_cache(inode, start, end, 0); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2782 | |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2783 | /* |
| 2784 | * If the ordered extent had an IOERR or something else went |
| 2785 | * wrong we need to return the space for this ordered extent |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2786 | * back to the allocator. We only free the extent in the |
| 2787 | * truncated case if we didn't write out the extent at all. |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2788 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 2789 | if ((ret || !logical_len) && |
| 2790 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2791 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
| 2792 | btrfs_free_reserved_extent(root, ordered_extent->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 2793 | ordered_extent->disk_len, 1); |
Josef Bacik | 0bec9ef | 2013-01-31 14:58:00 -0500 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2797 | /* |
Liu Bo | 8bad3c0 | 2012-06-18 12:14:23 +0800 | [diff] [blame] | 2798 | * This needs to be done to make sure anybody waiting knows we are done |
| 2799 | * updating everything for this ordered extent. |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2800 | */ |
| 2801 | btrfs_remove_ordered_extent(inode, ordered_extent); |
| 2802 | |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2803 | /* for snapshot-aware defrag */ |
Liu Bo | 6f51956 | 2013-10-29 10:45:05 +0800 | [diff] [blame] | 2804 | if (new) { |
| 2805 | if (ret) { |
| 2806 | free_sa_defrag_extent(new); |
| 2807 | atomic_dec(&root->fs_info->defrag_running); |
| 2808 | } else { |
| 2809 | relink_file_extents(new); |
| 2810 | } |
| 2811 | } |
Liu Bo | 38c227d | 2013-01-29 03:18:40 +0000 | [diff] [blame] | 2812 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2813 | /* once for us */ |
| 2814 | btrfs_put_ordered_extent(ordered_extent); |
| 2815 | /* once for the tree */ |
| 2816 | btrfs_put_ordered_extent(ordered_extent); |
| 2817 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2818 | return ret; |
| 2819 | } |
| 2820 | |
| 2821 | static void finish_ordered_fn(struct btrfs_work *work) |
| 2822 | { |
| 2823 | struct btrfs_ordered_extent *ordered_extent; |
| 2824 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); |
| 2825 | btrfs_finish_ordered_io(ordered_extent); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2826 | } |
| 2827 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2828 | 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] | 2829 | struct extent_state *state, int uptodate) |
| 2830 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2831 | struct inode *inode = page->mapping->host; |
| 2832 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2833 | struct btrfs_ordered_extent *ordered_extent = NULL; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2834 | struct btrfs_workqueue *wq; |
| 2835 | btrfs_work_func_t func; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2836 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2837 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
| 2838 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 2839 | ClearPagePrivate2(page); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2840 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
| 2841 | end - start + 1, uptodate)) |
| 2842 | return 0; |
| 2843 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2844 | if (btrfs_is_free_space_inode(inode)) { |
| 2845 | wq = root->fs_info->endio_freespace_worker; |
| 2846 | func = btrfs_freespace_write_helper; |
| 2847 | } else { |
| 2848 | wq = root->fs_info->endio_write_workers; |
| 2849 | func = btrfs_endio_write_helper; |
| 2850 | } |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2851 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2852 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
| 2853 | NULL); |
| 2854 | btrfs_queue_work(wq, &ordered_extent->work); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 2855 | |
| 2856 | return 0; |
Chris Mason | 211f90e | 2008-07-18 11:56:15 -0400 | [diff] [blame] | 2857 | } |
| 2858 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2859 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2860 | * 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] | 2861 | * if there's a match, we allow the bio to finish. If not, the code in |
| 2862 | * extent_io.c will try to find good copies for us. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2863 | */ |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2864 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
| 2865 | u64 phy_offset, struct page *page, |
| 2866 | u64 start, u64 end, int mirror) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2867 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2868 | size_t offset = start - page_offset(page); |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2869 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2870 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2871 | char *kaddr; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2872 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2873 | u32 csum_expected; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2874 | u32 csum = ~(u32)0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2875 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 2876 | DEFAULT_RATELIMIT_BURST); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2877 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2878 | if (PageChecked(page)) { |
| 2879 | ClearPageChecked(page); |
| 2880 | goto good; |
| 2881 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2882 | |
| 2883 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) |
Jan Schmidt | 08d2f34 | 2011-05-04 16:18:50 +0200 | [diff] [blame] | 2884 | goto good; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2885 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2886 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2887 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 2888 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
| 2889 | GFP_NOFS); |
| 2890 | return 0; |
| 2891 | } |
| 2892 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2893 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2894 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2895 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2896 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2897 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 2898 | btrfs_csum_final(csum, (char *)&csum); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2899 | if (csum != csum_expected) |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2900 | goto zeroit; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2901 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2902 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2903 | good: |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2904 | return 0; |
| 2905 | |
| 2906 | zeroit: |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2907 | if (__ratelimit(&_rs)) |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2908 | 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] | 2909 | btrfs_ino(page->mapping->host), start, csum, csum_expected); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2910 | memset(kaddr + offset, 1, end - start + 1); |
| 2911 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 2912 | kunmap_atomic(kaddr); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2913 | if (csum_expected == 0) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2914 | return 0; |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2915 | return -EIO; |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 2916 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2917 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2918 | struct delayed_iput { |
| 2919 | struct list_head list; |
| 2920 | struct inode *inode; |
| 2921 | }; |
| 2922 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2923 | /* JDM: If this is fs-wide, why can't we add a pointer to |
| 2924 | * btrfs_inode instead and avoid the allocation? */ |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2925 | void btrfs_add_delayed_iput(struct inode *inode) |
| 2926 | { |
| 2927 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2928 | struct delayed_iput *delayed; |
| 2929 | |
| 2930 | if (atomic_add_unless(&inode->i_count, -1, 1)) |
| 2931 | return; |
| 2932 | |
| 2933 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); |
| 2934 | delayed->inode = inode; |
| 2935 | |
| 2936 | spin_lock(&fs_info->delayed_iput_lock); |
| 2937 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); |
| 2938 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2939 | } |
| 2940 | |
| 2941 | void btrfs_run_delayed_iputs(struct btrfs_root *root) |
| 2942 | { |
| 2943 | LIST_HEAD(list); |
| 2944 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2945 | struct delayed_iput *delayed; |
| 2946 | int empty; |
| 2947 | |
| 2948 | spin_lock(&fs_info->delayed_iput_lock); |
| 2949 | empty = list_empty(&fs_info->delayed_iputs); |
| 2950 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2951 | if (empty) |
| 2952 | return; |
| 2953 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2954 | spin_lock(&fs_info->delayed_iput_lock); |
| 2955 | list_splice_init(&fs_info->delayed_iputs, &list); |
| 2956 | spin_unlock(&fs_info->delayed_iput_lock); |
| 2957 | |
| 2958 | while (!list_empty(&list)) { |
| 2959 | delayed = list_entry(list.next, struct delayed_iput, list); |
| 2960 | list_del(&delayed->list); |
| 2961 | iput(delayed->inode); |
| 2962 | kfree(delayed); |
| 2963 | } |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2966 | /* |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 2967 | * This is called in transaction commit time. If there are no orphan |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2968 | * files in the subvolume, it removes orphan item and frees block_rsv |
| 2969 | * structure. |
| 2970 | */ |
| 2971 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, |
| 2972 | struct btrfs_root *root) |
| 2973 | { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2974 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2975 | int ret; |
| 2976 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2977 | if (atomic_read(&root->orphan_inodes) || |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2978 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
| 2979 | return; |
| 2980 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2981 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 2982 | if (atomic_read(&root->orphan_inodes)) { |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 2983 | spin_unlock(&root->orphan_lock); |
| 2984 | return; |
| 2985 | } |
| 2986 | |
| 2987 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { |
| 2988 | spin_unlock(&root->orphan_lock); |
| 2989 | return; |
| 2990 | } |
| 2991 | |
| 2992 | block_rsv = root->orphan_block_rsv; |
| 2993 | root->orphan_block_rsv = NULL; |
| 2994 | spin_unlock(&root->orphan_lock); |
| 2995 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2996 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2997 | btrfs_root_refs(&root->root_item) > 0) { |
| 2998 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, |
| 2999 | root->root_key.objectid); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3000 | if (ret) |
| 3001 | btrfs_abort_transaction(trans, root, ret); |
| 3002 | else |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3003 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
| 3004 | &root->state); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3005 | } |
| 3006 | |
Josef Bacik | 90290e1 | 2011-12-02 15:44:12 -0500 | [diff] [blame] | 3007 | if (block_rsv) { |
| 3008 | WARN_ON(block_rsv->size > 0); |
| 3009 | btrfs_free_block_rsv(root, block_rsv); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | /* |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3014 | * This creates an orphan entry for the given inode in case something goes |
| 3015 | * wrong in the middle of an unlink/truncate. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3016 | * |
| 3017 | * NOTE: caller of this function should reserve 5 units of metadata for |
| 3018 | * this function. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3019 | */ |
| 3020 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) |
| 3021 | { |
| 3022 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3023 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3024 | int reserve = 0; |
| 3025 | int insert = 0; |
| 3026 | int ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3027 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3028 | if (!root->orphan_block_rsv) { |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 3029 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3030 | if (!block_rsv) |
| 3031 | return -ENOMEM; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3032 | } |
| 3033 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3034 | spin_lock(&root->orphan_lock); |
| 3035 | if (!root->orphan_block_rsv) { |
| 3036 | root->orphan_block_rsv = block_rsv; |
| 3037 | } else if (block_rsv) { |
| 3038 | btrfs_free_block_rsv(root, block_rsv); |
| 3039 | block_rsv = NULL; |
| 3040 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3041 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3042 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3043 | &BTRFS_I(inode)->runtime_flags)) { |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3044 | #if 0 |
| 3045 | /* |
| 3046 | * For proper ENOSPC handling, we should do orphan |
| 3047 | * cleanup when mounting. But this introduces backward |
| 3048 | * compatibility issue. |
| 3049 | */ |
| 3050 | if (!xchg(&root->orphan_item_inserted, 1)) |
| 3051 | insert = 2; |
| 3052 | else |
| 3053 | insert = 1; |
| 3054 | #endif |
| 3055 | insert = 1; |
Miao Xie | 321f0e7 | 2012-08-28 22:13:02 -0600 | [diff] [blame] | 3056 | atomic_inc(&root->orphan_inodes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3057 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3058 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3059 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3060 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3061 | reserve = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3062 | spin_unlock(&root->orphan_lock); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3063 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3064 | /* grab metadata reservation from transaction handle */ |
| 3065 | if (reserve) { |
| 3066 | ret = btrfs_orphan_reserve_metadata(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3067 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | /* insert an orphan item to track this unlinked/truncated file */ |
| 3071 | if (insert >= 1) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3072 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3073 | if (ret) { |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3074 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3075 | if (reserve) { |
| 3076 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3077 | &BTRFS_I(inode)->runtime_flags); |
| 3078 | btrfs_orphan_release_metadata(inode); |
| 3079 | } |
| 3080 | if (ret != -EEXIST) { |
Josef Bacik | e8e7cff | 2013-08-21 15:54:00 -0400 | [diff] [blame] | 3081 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3082 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3083 | btrfs_abort_transaction(trans, root, ret); |
| 3084 | return ret; |
| 3085 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3086 | } |
| 3087 | ret = 0; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | /* insert an orphan item to track subvolume contains orphan files */ |
| 3091 | if (insert >= 2) { |
| 3092 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, |
| 3093 | root->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3094 | if (ret && ret != -EEXIST) { |
| 3095 | btrfs_abort_transaction(trans, root, ret); |
| 3096 | return ret; |
| 3097 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3098 | } |
| 3099 | return 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * We have done the truncate/delete so we can go ahead and remove the orphan |
| 3104 | * item for this particular inode. |
| 3105 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3106 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
| 3107 | struct inode *inode) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3108 | { |
| 3109 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3110 | int delete_item = 0; |
| 3111 | int release_rsv = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3112 | int ret = 0; |
| 3113 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3114 | spin_lock(&root->orphan_lock); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3115 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3116 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3117 | delete_item = 1; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3118 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 3119 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
| 3120 | &BTRFS_I(inode)->runtime_flags)) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3121 | release_rsv = 1; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3122 | spin_unlock(&root->orphan_lock); |
| 3123 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3124 | if (delete_item) { |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3125 | atomic_dec(&root->orphan_inodes); |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3126 | if (trans) |
| 3127 | ret = btrfs_del_orphan_item(trans, root, |
| 3128 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3129 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3130 | |
Filipe David Borba Manana | 703c88e | 2013-09-22 21:54:55 +0100 | [diff] [blame] | 3131 | if (release_rsv) |
| 3132 | btrfs_orphan_release_metadata(inode); |
| 3133 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3134 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | /* |
| 3138 | * this cleans up any orphans that may be left on the list from the last use |
| 3139 | * of this root. |
| 3140 | */ |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3141 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3142 | { |
| 3143 | struct btrfs_path *path; |
| 3144 | struct extent_buffer *leaf; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3145 | struct btrfs_key key, found_key; |
| 3146 | struct btrfs_trans_handle *trans; |
| 3147 | struct inode *inode; |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3148 | u64 last_objectid = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3149 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
| 3150 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3151 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3152 | return 0; |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 3153 | |
| 3154 | path = btrfs_alloc_path(); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3155 | if (!path) { |
| 3156 | ret = -ENOMEM; |
| 3157 | goto out; |
| 3158 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3159 | path->reada = -1; |
| 3160 | |
| 3161 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 3162 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); |
| 3163 | key.offset = (u64)-1; |
| 3164 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3165 | while (1) { |
| 3166 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3167 | if (ret < 0) |
| 3168 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3169 | |
| 3170 | /* |
| 3171 | * 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] | 3172 | * 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] | 3173 | * find the key and see if we have stuff that matches |
| 3174 | */ |
| 3175 | if (ret > 0) { |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3176 | ret = 0; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3177 | if (path->slots[0] == 0) |
| 3178 | break; |
| 3179 | path->slots[0]--; |
| 3180 | } |
| 3181 | |
| 3182 | /* pull out the item */ |
| 3183 | leaf = path->nodes[0]; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3184 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3185 | |
| 3186 | /* make sure the item matches what we want */ |
| 3187 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) |
| 3188 | break; |
| 3189 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) |
| 3190 | break; |
| 3191 | |
| 3192 | /* release the path since we're done with it */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3193 | btrfs_release_path(path); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3194 | |
| 3195 | /* |
| 3196 | * this is where we are basically btrfs_lookup, without the |
| 3197 | * crossing root thing. we store the inode number in the |
| 3198 | * offset of the orphan item. |
| 3199 | */ |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3200 | |
| 3201 | if (found_key.offset == last_objectid) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3202 | btrfs_err(root->fs_info, |
| 3203 | "Error removing orphan entry, stopping orphan cleanup"); |
Josef Bacik | 8f6d7f4 | 2011-09-26 15:55:20 -0400 | [diff] [blame] | 3204 | ret = -EINVAL; |
| 3205 | goto out; |
| 3206 | } |
| 3207 | |
| 3208 | last_objectid = found_key.offset; |
| 3209 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3210 | found_key.objectid = found_key.offset; |
| 3211 | found_key.type = BTRFS_INODE_ITEM_KEY; |
| 3212 | found_key.offset = 0; |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 3213 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 3214 | ret = PTR_ERR_OR_ZERO(inode); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3215 | if (ret && ret != -ESTALE) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3216 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3217 | |
Arne Jansen | f8e9e0b | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 3218 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
| 3219 | struct btrfs_root *dead_root; |
| 3220 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3221 | int is_dead_root = 0; |
| 3222 | |
| 3223 | /* |
| 3224 | * this is an orphan in the tree root. Currently these |
| 3225 | * could come from 2 sources: |
| 3226 | * a) a snapshot deletion in progress |
| 3227 | * b) a free space cache inode |
| 3228 | * We need to distinguish those two, as the snapshot |
| 3229 | * orphan must not get deleted. |
| 3230 | * find_dead_roots already ran before us, so if this |
| 3231 | * is a snapshot deletion, we should find the root |
| 3232 | * in the dead_roots list |
| 3233 | */ |
| 3234 | spin_lock(&fs_info->trans_lock); |
| 3235 | list_for_each_entry(dead_root, &fs_info->dead_roots, |
| 3236 | root_list) { |
| 3237 | if (dead_root->root_key.objectid == |
| 3238 | found_key.objectid) { |
| 3239 | is_dead_root = 1; |
| 3240 | break; |
| 3241 | } |
| 3242 | } |
| 3243 | spin_unlock(&fs_info->trans_lock); |
| 3244 | if (is_dead_root) { |
| 3245 | /* prevent this orphan from being found again */ |
| 3246 | key.offset = found_key.objectid - 1; |
| 3247 | continue; |
| 3248 | } |
| 3249 | } |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3250 | /* |
| 3251 | * Inode is already gone but the orphan item is still there, |
| 3252 | * kill the orphan item. |
| 3253 | */ |
| 3254 | if (ret == -ESTALE) { |
| 3255 | trans = btrfs_start_transaction(root, 1); |
| 3256 | if (IS_ERR(trans)) { |
| 3257 | ret = PTR_ERR(trans); |
| 3258 | goto out; |
| 3259 | } |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3260 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
| 3261 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3262 | ret = btrfs_del_orphan_item(trans, root, |
| 3263 | found_key.objectid); |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3264 | btrfs_end_transaction(trans, root); |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 3265 | if (ret) |
| 3266 | goto out; |
Josef Bacik | a8c9e57 | 2011-09-21 16:55:59 -0400 | [diff] [blame] | 3267 | continue; |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3268 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3269 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3270 | /* |
| 3271 | * add this inode to the orphan list so btrfs_orphan_del does |
| 3272 | * the proper thing when we hit it |
| 3273 | */ |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 3274 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 3275 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 925396e | 2013-02-01 15:57:47 -0500 | [diff] [blame] | 3276 | atomic_inc(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3277 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3278 | /* if we have links, this was a truncate, lets do that */ |
| 3279 | if (inode->i_nlink) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3280 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 3281 | iput(inode); |
| 3282 | continue; |
| 3283 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3284 | nr_truncate++; |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3285 | |
| 3286 | /* 1 for the orphan item deletion. */ |
| 3287 | trans = btrfs_start_transaction(root, 1); |
| 3288 | if (IS_ERR(trans)) { |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3289 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3290 | ret = PTR_ERR(trans); |
| 3291 | goto out; |
| 3292 | } |
| 3293 | ret = btrfs_orphan_add(trans, inode); |
| 3294 | btrfs_end_transaction(trans, root); |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3295 | if (ret) { |
| 3296 | iput(inode); |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3297 | goto out; |
Josef Bacik | c69b26b | 2013-06-03 16:51:23 -0400 | [diff] [blame] | 3298 | } |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 3299 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3300 | ret = btrfs_truncate(inode); |
Josef Bacik | 4a7d0f6 | 2013-02-07 16:27:28 -0500 | [diff] [blame] | 3301 | if (ret) |
| 3302 | btrfs_orphan_del(NULL, inode); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3303 | } else { |
| 3304 | nr_unlink++; |
| 3305 | } |
| 3306 | |
| 3307 | /* this will do delete_inode and everything for us */ |
| 3308 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3309 | if (ret) |
| 3310 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3311 | } |
Miao Xie | 3254c87 | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 3312 | /* release the path since we're done with it */ |
| 3313 | btrfs_release_path(path); |
| 3314 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3315 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
| 3316 | |
| 3317 | if (root->orphan_block_rsv) |
| 3318 | btrfs_block_rsv_release(root, root->orphan_block_rsv, |
| 3319 | (u64)-1); |
| 3320 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3321 | if (root->orphan_block_rsv || |
| 3322 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3323 | trans = btrfs_join_transaction(root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3324 | if (!IS_ERR(trans)) |
| 3325 | btrfs_end_transaction(trans, root); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3326 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3327 | |
| 3328 | if (nr_unlink) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3329 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3330 | if (nr_truncate) |
David Sterba | 4884b47 | 2013-03-20 13:31:27 +0000 | [diff] [blame] | 3331 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3332 | |
| 3333 | out: |
| 3334 | if (ret) |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3335 | btrfs_crit(root->fs_info, |
| 3336 | "could not do orphan cleanup %d", ret); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 3337 | btrfs_free_path(path); |
| 3338 | return ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3339 | } |
| 3340 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3341 | /* |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3342 | * very simple check to peek ahead in the leaf looking for xattrs. If we |
| 3343 | * don't find any xattrs, we know there can't be any acls. |
| 3344 | * |
| 3345 | * slot is the slot the inode is in, objectid is the objectid of the inode |
| 3346 | */ |
| 3347 | 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] | 3348 | int slot, u64 objectid, |
| 3349 | int *first_xattr_slot) |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3350 | { |
| 3351 | u32 nritems = btrfs_header_nritems(leaf); |
| 3352 | struct btrfs_key found_key; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3353 | static u64 xattr_access = 0; |
| 3354 | static u64 xattr_default = 0; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3355 | int scanned = 0; |
| 3356 | |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3357 | if (!xattr_access) { |
| 3358 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, |
| 3359 | strlen(POSIX_ACL_XATTR_ACCESS)); |
| 3360 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, |
| 3361 | strlen(POSIX_ACL_XATTR_DEFAULT)); |
| 3362 | } |
| 3363 | |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3364 | slot++; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3365 | *first_xattr_slot = -1; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3366 | while (slot < nritems) { |
| 3367 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3368 | |
| 3369 | /* we found a different objectid, there must not be acls */ |
| 3370 | if (found_key.objectid != objectid) |
| 3371 | return 0; |
| 3372 | |
| 3373 | /* we found an xattr, assume we've got an acl */ |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3374 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3375 | if (*first_xattr_slot == -1) |
| 3376 | *first_xattr_slot = slot; |
Josef Bacik | f23b5a5 | 2013-06-19 10:16:26 -0400 | [diff] [blame] | 3377 | if (found_key.offset == xattr_access || |
| 3378 | found_key.offset == xattr_default) |
| 3379 | return 1; |
| 3380 | } |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3381 | |
| 3382 | /* |
| 3383 | * we found a key greater than an xattr key, there can't |
| 3384 | * be any acls later on |
| 3385 | */ |
| 3386 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) |
| 3387 | return 0; |
| 3388 | |
| 3389 | slot++; |
| 3390 | scanned++; |
| 3391 | |
| 3392 | /* |
| 3393 | * it goes inode, inode backrefs, xattrs, extents, |
| 3394 | * so if there are a ton of hard links to an inode there can |
| 3395 | * be a lot of backrefs. Don't waste time searching too hard, |
| 3396 | * this is just an optimization |
| 3397 | */ |
| 3398 | if (scanned >= 8) |
| 3399 | break; |
| 3400 | } |
| 3401 | /* we hit the end of the leaf before we found an xattr or |
| 3402 | * something larger than an xattr. We have to assume the inode |
| 3403 | * has acls |
| 3404 | */ |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3405 | if (*first_xattr_slot == -1) |
| 3406 | *first_xattr_slot = slot; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3407 | return 1; |
| 3408 | } |
| 3409 | |
| 3410 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3411 | * read an inode from the btree into the in-memory inode |
| 3412 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3413 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3414 | { |
| 3415 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3416 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3417 | struct btrfs_inode_item *inode_item; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3418 | struct btrfs_timespec *tspec; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3419 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3420 | struct btrfs_key location; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3421 | unsigned long ptr; |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3422 | int maybe_acls; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3423 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3424 | int ret; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3425 | bool filled = false; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3426 | int first_xattr_slot; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3427 | |
| 3428 | ret = btrfs_fill_inode(inode, &rdev); |
| 3429 | if (!ret) |
| 3430 | filled = true; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3431 | |
| 3432 | path = btrfs_alloc_path(); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 3433 | if (!path) |
| 3434 | goto make_bad; |
| 3435 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3436 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 3437 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3438 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3439 | if (ret) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3440 | goto make_bad; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3441 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3442 | leaf = path->nodes[0]; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3443 | |
| 3444 | if (filled) |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3445 | goto cache_index; |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3446 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3447 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3448 | struct btrfs_inode_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3449 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3450 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 3451 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
| 3452 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3453 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3454 | |
| 3455 | tspec = btrfs_inode_atime(inode_item); |
| 3456 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3457 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3458 | |
| 3459 | tspec = btrfs_inode_mtime(inode_item); |
| 3460 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3461 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3462 | |
| 3463 | tspec = btrfs_inode_ctime(inode_item); |
| 3464 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); |
| 3465 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); |
| 3466 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3467 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3468 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3469 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
| 3470 | |
| 3471 | /* |
| 3472 | * If we were modified in the current generation and evicted from memory |
| 3473 | * and then re-read we need to do a full sync since we don't have any |
| 3474 | * idea about which extents were modified before we were evicted from |
| 3475 | * cache. |
| 3476 | */ |
| 3477 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) |
| 3478 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 3479 | &BTRFS_I(inode)->runtime_flags); |
| 3480 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3481 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3482 | inode->i_generation = BTRFS_I(inode)->generation; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3483 | inode->i_rdev = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3484 | rdev = btrfs_inode_rdev(leaf, inode_item); |
| 3485 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3486 | BTRFS_I(inode)->index_cnt = (u64)-1; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3487 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3488 | |
| 3489 | cache_index: |
| 3490 | path->slots[0]++; |
| 3491 | if (inode->i_nlink != 1 || |
| 3492 | path->slots[0] >= btrfs_header_nritems(leaf)) |
| 3493 | goto cache_acl; |
| 3494 | |
| 3495 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); |
| 3496 | if (location.objectid != btrfs_ino(inode)) |
| 3497 | goto cache_acl; |
| 3498 | |
| 3499 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3500 | if (location.type == BTRFS_INODE_REF_KEY) { |
| 3501 | struct btrfs_inode_ref *ref; |
| 3502 | |
| 3503 | ref = (struct btrfs_inode_ref *)ptr; |
| 3504 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); |
| 3505 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { |
| 3506 | struct btrfs_inode_extref *extref; |
| 3507 | |
| 3508 | extref = (struct btrfs_inode_extref *)ptr; |
| 3509 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, |
| 3510 | extref); |
| 3511 | } |
Miao Xie | 2f7e33d | 2011-06-23 07:27:13 +0000 | [diff] [blame] | 3512 | cache_acl: |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3513 | /* |
| 3514 | * try to precache a NULL acl entry for files that don't have |
| 3515 | * any xattrs or acls |
| 3516 | */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3517 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3518 | btrfs_ino(inode), &first_xattr_slot); |
| 3519 | if (first_xattr_slot != -1) { |
| 3520 | path->slots[0] = first_xattr_slot; |
| 3521 | ret = btrfs_load_inode_props(inode, path); |
| 3522 | if (ret) |
| 3523 | btrfs_err(root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 3524 | "error loading props for ino %llu (root %llu): %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 3525 | btrfs_ino(inode), |
| 3526 | root->root_key.objectid, ret); |
| 3527 | } |
| 3528 | btrfs_free_path(path); |
| 3529 | |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 3530 | if (!maybe_acls) |
| 3531 | cache_no_acl(inode); |
Chris Mason | 46a53cc | 2009-04-27 11:47:50 -0400 | [diff] [blame] | 3532 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3533 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3534 | case S_IFREG: |
| 3535 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3536 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3537 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3538 | inode->i_fop = &btrfs_file_operations; |
| 3539 | inode->i_op = &btrfs_file_inode_operations; |
| 3540 | break; |
| 3541 | case S_IFDIR: |
| 3542 | inode->i_fop = &btrfs_dir_file_operations; |
| 3543 | if (root == root->fs_info->tree_root) |
| 3544 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 3545 | else |
| 3546 | inode->i_op = &btrfs_dir_inode_operations; |
| 3547 | break; |
| 3548 | case S_IFLNK: |
| 3549 | inode->i_op = &btrfs_symlink_inode_operations; |
| 3550 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 3551 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3552 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3553 | default: |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 3554 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 3555 | init_special_inode(inode, inode->i_mode, rdev); |
| 3556 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3557 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 3558 | |
| 3559 | btrfs_update_iflags(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3560 | return; |
| 3561 | |
| 3562 | make_bad: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3563 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3564 | make_bad_inode(inode); |
| 3565 | } |
| 3566 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3567 | /* |
| 3568 | * given a leaf and an inode, copy the inode fields into the leaf |
| 3569 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3570 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
| 3571 | struct extent_buffer *leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3572 | struct btrfs_inode_item *item, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3573 | struct inode *inode) |
| 3574 | { |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3575 | struct btrfs_map_token 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_init_map_token(&token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3578 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3579 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
| 3580 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); |
| 3581 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, |
| 3582 | &token); |
| 3583 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); |
| 3584 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3585 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3586 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
| 3587 | inode->i_atime.tv_sec, &token); |
| 3588 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), |
| 3589 | inode->i_atime.tv_nsec, &token); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3590 | |
Liu Bo | 51fab69 | 2012-12-27 09:01:21 +0000 | [diff] [blame] | 3591 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
| 3592 | inode->i_mtime.tv_sec, &token); |
| 3593 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), |
| 3594 | inode->i_mtime.tv_nsec, &token); |
| 3595 | |
| 3596 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
| 3597 | inode->i_ctime.tv_sec, &token); |
| 3598 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), |
| 3599 | inode->i_ctime.tv_nsec, &token); |
| 3600 | |
| 3601 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
| 3602 | &token); |
| 3603 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, |
| 3604 | &token); |
| 3605 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); |
| 3606 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); |
| 3607 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); |
| 3608 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); |
| 3609 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3610 | } |
| 3611 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3612 | /* |
| 3613 | * copy everything in the in-memory inode into the btree. |
| 3614 | */ |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3615 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3616 | struct btrfs_root *root, struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3617 | { |
| 3618 | struct btrfs_inode_item *inode_item; |
| 3619 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3620 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3621 | int ret; |
| 3622 | |
| 3623 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3624 | if (!path) |
| 3625 | return -ENOMEM; |
| 3626 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3627 | path->leave_spinning = 1; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3628 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
| 3629 | 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3630 | if (ret) { |
| 3631 | if (ret > 0) |
| 3632 | ret = -ENOENT; |
| 3633 | goto failed; |
| 3634 | } |
| 3635 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3636 | leaf = path->nodes[0]; |
| 3637 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3638 | struct btrfs_inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3639 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3640 | fill_inode_item(trans, leaf, inode_item, inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3641 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 3642 | btrfs_set_inode_last_trans(trans, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3643 | ret = 0; |
| 3644 | failed: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3645 | btrfs_free_path(path); |
| 3646 | return ret; |
| 3647 | } |
| 3648 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3649 | /* |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3650 | * copy everything in the in-memory inode into the btree. |
| 3651 | */ |
| 3652 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 3653 | struct btrfs_root *root, struct inode *inode) |
| 3654 | { |
| 3655 | int ret; |
| 3656 | |
| 3657 | /* |
| 3658 | * If the inode is a free space inode, we can deadlock during commit |
| 3659 | * if we put it into the delayed code. |
| 3660 | * |
| 3661 | * The data relocation inode should also be directly updated |
| 3662 | * without delay |
| 3663 | */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 3664 | if (!btrfs_is_free_space_inode(inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3665 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 3666 | btrfs_update_root_times(trans, root); |
| 3667 | |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3668 | ret = btrfs_delayed_update_inode(trans, root, inode); |
| 3669 | if (!ret) |
| 3670 | btrfs_set_inode_last_trans(trans, inode); |
| 3671 | return ret; |
| 3672 | } |
| 3673 | |
| 3674 | return btrfs_update_inode_item(trans, root, inode); |
| 3675 | } |
| 3676 | |
Josef Bacik | be6aef6 | 2012-10-22 15:43:12 -0400 | [diff] [blame] | 3677 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
| 3678 | struct btrfs_root *root, |
| 3679 | struct inode *inode) |
Chris Mason | 2115133 | 2011-11-10 20:39:08 -0500 | [diff] [blame] | 3680 | { |
| 3681 | int ret; |
| 3682 | |
| 3683 | ret = btrfs_update_inode(trans, root, inode); |
| 3684 | if (ret == -ENOSPC) |
| 3685 | return btrfs_update_inode_item(trans, root, inode); |
| 3686 | return ret; |
| 3687 | } |
| 3688 | |
| 3689 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3690 | * unlink helper that gets used here in inode.c and in the tree logging |
| 3691 | * recovery code. It remove a link in a directory with a given name, and |
| 3692 | * also drops the back refs in the inode to the directory |
| 3693 | */ |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3694 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3695 | struct btrfs_root *root, |
| 3696 | struct inode *dir, struct inode *inode, |
| 3697 | const char *name, int name_len) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3698 | { |
| 3699 | struct btrfs_path *path; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3700 | int ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3701 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3702 | struct btrfs_dir_item *di; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3703 | struct btrfs_key key; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3704 | u64 index; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3705 | u64 ino = btrfs_ino(inode); |
| 3706 | u64 dir_ino = btrfs_ino(dir); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3707 | |
| 3708 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3709 | if (!path) { |
| 3710 | ret = -ENOMEM; |
Tsutomu Itoh | 554233a | 2011-02-03 03:16:25 +0000 | [diff] [blame] | 3711 | goto out; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3712 | } |
| 3713 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3714 | path->leave_spinning = 1; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3715 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3716 | name, name_len, -1); |
| 3717 | if (IS_ERR(di)) { |
| 3718 | ret = PTR_ERR(di); |
| 3719 | goto err; |
| 3720 | } |
| 3721 | if (!di) { |
| 3722 | ret = -ENOENT; |
| 3723 | goto err; |
| 3724 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3725 | leaf = path->nodes[0]; |
| 3726 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3727 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3728 | if (ret) |
| 3729 | goto err; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3730 | btrfs_release_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3731 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3732 | /* |
| 3733 | * If we don't have dir index, we have to get it by looking up |
| 3734 | * the inode ref, since we get the inode ref, remove it directly, |
| 3735 | * it is unnecessary to do delayed deletion. |
| 3736 | * |
| 3737 | * But if we have dir index, needn't search inode ref to get it. |
| 3738 | * Since the inode ref is close to the inode item, it is better |
| 3739 | * that we delay to delete it, and just do this deletion when |
| 3740 | * we update the inode item. |
| 3741 | */ |
| 3742 | if (BTRFS_I(inode)->dir_index) { |
| 3743 | ret = btrfs_delayed_delete_inode_ref(inode); |
| 3744 | if (!ret) { |
| 3745 | index = BTRFS_I(inode)->dir_index; |
| 3746 | goto skip_backref; |
| 3747 | } |
| 3748 | } |
| 3749 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3750 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
| 3751 | dir_ino, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3752 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3753 | btrfs_info(root->fs_info, |
| 3754 | "failed to delete reference to %.*s, inode %llu parent %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3755 | name_len, name, ino, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3756 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 3757 | goto err; |
| 3758 | } |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 3759 | skip_backref: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3760 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3761 | if (ret) { |
| 3762 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3763 | goto err; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3764 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3765 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3766 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3767 | inode, dir_ino); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3768 | if (ret != 0 && ret != -ENOENT) { |
| 3769 | btrfs_abort_transaction(trans, root, ret); |
| 3770 | goto err; |
| 3771 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3772 | |
| 3773 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, |
| 3774 | dir, index); |
Chris Mason | 6418c96 | 2010-10-30 07:34:24 -0400 | [diff] [blame] | 3775 | if (ret == -ENOENT) |
| 3776 | ret = 0; |
Zach Brown | d4e3991 | 2013-04-02 21:02:16 +0000 | [diff] [blame] | 3777 | else if (ret) |
| 3778 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3779 | err: |
| 3780 | btrfs_free_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3781 | if (ret) |
| 3782 | goto out; |
| 3783 | |
| 3784 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3785 | inode_inc_iversion(inode); |
| 3786 | inode_inc_iversion(dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3787 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Tsutomu Itoh | b995929 | 2012-06-25 21:25:22 -0600 | [diff] [blame] | 3788 | ret = btrfs_update_inode(trans, root, dir); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3789 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3790 | return ret; |
| 3791 | } |
| 3792 | |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3793 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
| 3794 | struct btrfs_root *root, |
| 3795 | struct inode *dir, struct inode *inode, |
| 3796 | const char *name, int name_len) |
| 3797 | { |
| 3798 | int ret; |
| 3799 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); |
| 3800 | if (!ret) { |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 3801 | drop_nlink(inode); |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 3802 | ret = btrfs_update_inode(trans, root, inode); |
| 3803 | } |
| 3804 | return ret; |
| 3805 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3806 | |
| 3807 | /* |
| 3808 | * helper to start transaction for unlink and rmdir. |
| 3809 | * |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3810 | * unlink and rmdir are special in btrfs, they do not always free space, so |
| 3811 | * if we cannot make our reservations the normal way try and see if there is |
| 3812 | * plenty of slack room in the global reserve to migrate, otherwise we cannot |
| 3813 | * allow the unlink to occur. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3814 | */ |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3815 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3816 | { |
| 3817 | struct btrfs_trans_handle *trans; |
| 3818 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3819 | int ret; |
| 3820 | |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3821 | /* |
| 3822 | * 1 for the possible orphan item |
| 3823 | * 1 for the dir item |
| 3824 | * 1 for the dir index |
| 3825 | * 1 for the inode ref |
Josef Bacik | e70bea5 | 2011-10-11 14:18:24 -0400 | [diff] [blame] | 3826 | * 1 for the inode |
| 3827 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 3828 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3829 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
| 3830 | return trans; |
| 3831 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3832 | if (PTR_ERR(trans) == -ENOSPC) { |
| 3833 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3834 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3835 | trans = btrfs_start_transaction(root, 0); |
| 3836 | if (IS_ERR(trans)) |
| 3837 | return trans; |
| 3838 | ret = btrfs_cond_migrate_bytes(root->fs_info, |
| 3839 | &root->fs_info->trans_block_rsv, |
| 3840 | num_bytes, 5); |
| 3841 | if (ret) { |
| 3842 | btrfs_end_transaction(trans, root); |
| 3843 | return ERR_PTR(ret); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3844 | } |
Josef Bacik | 5a77d76 | 2011-11-01 14:32:23 -0400 | [diff] [blame] | 3845 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3846 | trans->bytes_reserved = num_bytes; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3847 | } |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3848 | return trans; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3849 | } |
| 3850 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3851 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3852 | { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3853 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3854 | struct btrfs_trans_handle *trans; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3855 | struct inode *inode = dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3856 | int ret; |
| 3857 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3858 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3859 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3860 | return PTR_ERR(trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3861 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 3862 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
| 3863 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3864 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3865 | dentry->d_name.name, dentry->d_name.len); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3866 | if (ret) |
| 3867 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3868 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3869 | if (inode->i_nlink == 0) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3870 | ret = btrfs_orphan_add(trans, inode); |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3871 | if (ret) |
| 3872 | goto out; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3873 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3874 | |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 3875 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3876 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3877 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3878 | return ret; |
| 3879 | } |
| 3880 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3881 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
| 3882 | struct btrfs_root *root, |
| 3883 | struct inode *dir, u64 objectid, |
| 3884 | const char *name, int name_len) |
| 3885 | { |
| 3886 | struct btrfs_path *path; |
| 3887 | struct extent_buffer *leaf; |
| 3888 | struct btrfs_dir_item *di; |
| 3889 | struct btrfs_key key; |
| 3890 | u64 index; |
| 3891 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3892 | u64 dir_ino = btrfs_ino(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3893 | |
| 3894 | path = btrfs_alloc_path(); |
| 3895 | if (!path) |
| 3896 | return -ENOMEM; |
| 3897 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3898 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3899 | name, name_len, -1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3900 | if (IS_ERR_OR_NULL(di)) { |
| 3901 | if (!di) |
| 3902 | ret = -ENOENT; |
| 3903 | else |
| 3904 | ret = PTR_ERR(di); |
| 3905 | goto out; |
| 3906 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3907 | |
| 3908 | leaf = path->nodes[0]; |
| 3909 | btrfs_dir_item_key_to_cpu(leaf, di, &key); |
| 3910 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); |
| 3911 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3912 | if (ret) { |
| 3913 | btrfs_abort_transaction(trans, root, ret); |
| 3914 | goto out; |
| 3915 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3916 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3917 | |
| 3918 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 3919 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3920 | dir_ino, &index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3921 | if (ret < 0) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3922 | if (ret != -ENOENT) { |
| 3923 | btrfs_abort_transaction(trans, root, ret); |
| 3924 | goto out; |
| 3925 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3926 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3927 | name, name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3928 | if (IS_ERR_OR_NULL(di)) { |
| 3929 | if (!di) |
| 3930 | ret = -ENOENT; |
| 3931 | else |
| 3932 | ret = PTR_ERR(di); |
| 3933 | btrfs_abort_transaction(trans, root, ret); |
| 3934 | goto out; |
| 3935 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3936 | |
| 3937 | leaf = path->nodes[0]; |
| 3938 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3939 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3940 | index = key.offset; |
| 3941 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3942 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3943 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3944 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3945 | if (ret) { |
| 3946 | btrfs_abort_transaction(trans, root, ret); |
| 3947 | goto out; |
| 3948 | } |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3949 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3950 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3951 | inode_inc_iversion(dir); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3952 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Josef Bacik | 5a24e84 | 2012-08-08 10:12:59 -0600 | [diff] [blame] | 3953 | ret = btrfs_update_inode_fallback(trans, root, dir); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3954 | if (ret) |
| 3955 | btrfs_abort_transaction(trans, root, ret); |
| 3956 | out: |
Josef Bacik | 71d7aed | 2011-06-14 14:24:32 -0400 | [diff] [blame] | 3957 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3958 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3959 | } |
| 3960 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3961 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3962 | { |
| 3963 | struct inode *inode = dentry->d_inode; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 3964 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3965 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3966 | struct btrfs_trans_handle *trans; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3967 | |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3968 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3969 | return -ENOTEMPTY; |
David Sterba | b3ae244 | 2012-09-13 16:04:34 -0600 | [diff] [blame] | 3970 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
| 3971 | return -EPERM; |
Yan | 134d451 | 2007-10-25 15:49:25 -0400 | [diff] [blame] | 3972 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3973 | trans = __unlink_start_trans(dir); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3974 | if (IS_ERR(trans)) |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3975 | return PTR_ERR(trans); |
Josef Bacik | 5df6a9f | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3976 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3977 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3978 | err = btrfs_unlink_subvol(trans, root, dir, |
| 3979 | BTRFS_I(inode)->location.objectid, |
| 3980 | dentry->d_name.name, |
| 3981 | dentry->d_name.len); |
| 3982 | goto out; |
| 3983 | } |
| 3984 | |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3985 | err = btrfs_orphan_add(trans, inode); |
| 3986 | if (err) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3987 | goto out; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3988 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3989 | /* now the directory is empty */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3990 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
| 3991 | dentry->d_name.name, dentry->d_name.len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3992 | if (!err) |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 3993 | btrfs_i_size_write(inode, 0); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 3994 | out: |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 3995 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 3996 | btrfs_btree_balance_dirty(root); |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 3997 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3998 | return err; |
| 3999 | } |
| 4000 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4001 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4002 | * this can truncate away extent items, csum items and directory items. |
| 4003 | * 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] | 4004 | * any higher than new_size |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4005 | * |
| 4006 | * csum items that cross the new i_size are truncated to the new size |
| 4007 | * as well. |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4008 | * |
| 4009 | * min_type is the minimum key type to truncate down to. If set to 0, this |
| 4010 | * 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] | 4011 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4012 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
| 4013 | struct btrfs_root *root, |
| 4014 | struct inode *inode, |
| 4015 | u64 new_size, u32 min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4016 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4017 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4018 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4019 | struct btrfs_file_extent_item *fi; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4020 | struct btrfs_key key; |
| 4021 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4022 | u64 extent_start = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4023 | u64 extent_num_bytes = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4024 | u64 extent_offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4025 | u64 item_end = 0; |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4026 | u64 last_size = (u64)-1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4027 | u32 found_type = (u8)-1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4028 | int found_extent; |
| 4029 | int del_item; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4030 | int pending_del_nr = 0; |
| 4031 | int pending_del_slot = 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4032 | int extent_type = -1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4033 | int ret; |
| 4034 | int err = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4035 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4036 | |
| 4037 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4038 | |
Mark Fasheh | 0eb0e19 | 2011-07-12 16:44:10 -0700 | [diff] [blame] | 4039 | path = btrfs_alloc_path(); |
| 4040 | if (!path) |
| 4041 | return -ENOMEM; |
| 4042 | path->reada = -1; |
| 4043 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4044 | /* |
| 4045 | * We want to drop from the next block forward in case this new size is |
| 4046 | * not block aligned since we will be keeping the last block of the |
| 4047 | * extent just the way it is. |
| 4048 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4049 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4050 | root == root->fs_info->tree_root) |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4051 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
| 4052 | root->sectorsize), (u64)-1, 0); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4053 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4054 | /* |
| 4055 | * This function is also used to drop the items in the log tree before |
| 4056 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means |
| 4057 | * it is used to drop the loged items. So we shouldn't kill the delayed |
| 4058 | * items. |
| 4059 | */ |
| 4060 | if (min_type == 0 && root == BTRFS_I(inode)->root) |
| 4061 | btrfs_kill_delayed_inode_items(inode); |
| 4062 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4063 | key.objectid = ino; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4064 | key.offset = (u64)-1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4065 | key.type = (u8)-1; |
| 4066 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4067 | search_again: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4068 | path->leave_spinning = 1; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4069 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4070 | if (ret < 0) { |
| 4071 | err = ret; |
| 4072 | goto out; |
| 4073 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4074 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4075 | if (ret > 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4076 | /* there are no items in the tree for us to truncate, we're |
| 4077 | * done |
| 4078 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4079 | if (path->slots[0] == 0) |
| 4080 | goto out; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4081 | path->slots[0]--; |
| 4082 | } |
| 4083 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4084 | while (1) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4085 | fi = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4086 | leaf = path->nodes[0]; |
| 4087 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4088 | found_type = btrfs_key_type(&found_key); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4089 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4090 | if (found_key.objectid != ino) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4091 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4092 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4093 | if (found_type < min_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4094 | break; |
| 4095 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4096 | item_end = found_key.offset; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4097 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4098 | fi = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4099 | struct btrfs_file_extent_item); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4100 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4101 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4102 | item_end += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4103 | btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4104 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4105 | item_end += btrfs_file_extent_inline_len(leaf, |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4106 | path->slots[0], fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4107 | } |
Yan | 008630c | 2007-11-07 13:31:09 -0500 | [diff] [blame] | 4108 | item_end--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4109 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4110 | if (found_type > min_type) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4111 | del_item = 1; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4112 | } else { |
| 4113 | if (item_end < new_size) |
| 4114 | break; |
| 4115 | if (found_key.offset >= new_size) |
| 4116 | del_item = 1; |
| 4117 | else |
| 4118 | del_item = 0; |
| 4119 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4120 | found_extent = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4121 | /* FIXME, shrink the extent if the ref count is only 1 */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4122 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
| 4123 | goto delete; |
| 4124 | |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4125 | if (del_item) |
| 4126 | last_size = found_key.offset; |
| 4127 | else |
| 4128 | last_size = new_size; |
| 4129 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4130 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4131 | u64 num_dec; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4132 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
Miao Xie | f70a9a6b | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4133 | if (!del_item) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4134 | u64 orig_num_bytes = |
| 4135 | btrfs_file_extent_num_bytes(leaf, fi); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4136 | extent_num_bytes = ALIGN(new_size - |
| 4137 | found_key.offset, |
| 4138 | root->sectorsize); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4139 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4140 | extent_num_bytes); |
| 4141 | num_dec = (orig_num_bytes - |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4142 | extent_num_bytes); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4143 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4144 | &root->state) && |
| 4145 | extent_start != 0) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4146 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4147 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4148 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4149 | extent_num_bytes = |
| 4150 | btrfs_file_extent_disk_num_bytes(leaf, |
| 4151 | fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4152 | extent_offset = found_key.offset - |
| 4153 | btrfs_file_extent_offset(leaf, fi); |
| 4154 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4155 | /* FIXME blocksize != 4096 */ |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4156 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4157 | if (extent_start != 0) { |
| 4158 | found_extent = 1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4159 | if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4160 | &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4161 | inode_sub_bytes(inode, num_dec); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4162 | } |
| 4163 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4164 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4165 | /* |
| 4166 | * we can't truncate inline items that have had |
| 4167 | * special encodings |
| 4168 | */ |
| 4169 | if (!del_item && |
| 4170 | btrfs_file_extent_compression(leaf, fi) == 0 && |
| 4171 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
| 4172 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4173 | u32 size = new_size - found_key.offset; |
| 4174 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4175 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4176 | inode_sub_bytes(inode, item_end + 1 - |
| 4177 | new_size); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4178 | |
| 4179 | /* |
| 4180 | * update the ram bytes to properly reflect |
| 4181 | * the new size of our item |
| 4182 | */ |
| 4183 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4184 | size = |
| 4185 | btrfs_file_extent_calc_inline_size(size); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4186 | btrfs_truncate_item(root, path, size, 1); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4187 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
| 4188 | &root->state)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4189 | inode_sub_bytes(inode, item_end + 1 - |
| 4190 | found_key.offset); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 4191 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4192 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4193 | delete: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4194 | if (del_item) { |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4195 | if (!pending_del_nr) { |
| 4196 | /* no pending yet, add ourselves */ |
| 4197 | pending_del_slot = path->slots[0]; |
| 4198 | pending_del_nr = 1; |
| 4199 | } else if (pending_del_nr && |
| 4200 | path->slots[0] + 1 == pending_del_slot) { |
| 4201 | /* hop on the pending chunk */ |
| 4202 | pending_del_nr++; |
| 4203 | pending_del_slot = path->slots[0]; |
| 4204 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4205 | BUG(); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4206 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4207 | } else { |
| 4208 | break; |
| 4209 | } |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4210 | if (found_extent && |
| 4211 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 4212 | root == root->fs_info->tree_root)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4213 | btrfs_set_path_blocking(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4214 | ret = btrfs_free_extent(trans, root, extent_start, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4215 | extent_num_bytes, 0, |
| 4216 | btrfs_header_owner(leaf), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 4217 | ino, extent_offset, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4218 | BUG_ON(ret); |
| 4219 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4220 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4221 | if (found_type == BTRFS_INODE_ITEM_KEY) |
| 4222 | break; |
| 4223 | |
| 4224 | if (path->slots[0] == 0 || |
| 4225 | path->slots[0] != pending_del_slot) { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4226 | if (pending_del_nr) { |
| 4227 | ret = btrfs_del_items(trans, root, path, |
| 4228 | pending_del_slot, |
| 4229 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4230 | if (ret) { |
| 4231 | btrfs_abort_transaction(trans, |
| 4232 | root, ret); |
| 4233 | goto error; |
| 4234 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4235 | pending_del_nr = 0; |
| 4236 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4237 | btrfs_release_path(path); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4238 | goto search_again; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4239 | } else { |
| 4240 | path->slots[0]--; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4241 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4242 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4243 | out: |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4244 | if (pending_del_nr) { |
| 4245 | ret = btrfs_del_items(trans, root, path, pending_del_slot, |
| 4246 | pending_del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4247 | if (ret) |
| 4248 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4249 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4250 | error: |
Filipe Manana | dac5705 | 2014-08-29 20:54:26 +0100 | [diff] [blame^] | 4251 | if (last_size != (u64)-1 && |
| 4252 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4253 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4254 | btrfs_free_path(path); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4255 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4256 | } |
| 4257 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4258 | /* |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4259 | * btrfs_truncate_page - read, zero a chunk and write a page |
| 4260 | * @inode - inode that we're zeroing |
| 4261 | * @from - the offset to start zeroing |
| 4262 | * @len - the length to zero, 0 to zero the entire range respective to the |
| 4263 | * offset |
| 4264 | * @front - zero up to the offset instead of from the offset on |
| 4265 | * |
| 4266 | * This will find the page for the "from" offset and cow the page and zero the |
| 4267 | * 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] | 4268 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4269 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
| 4270 | int front) |
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 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4274 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4275 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4276 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4277 | char *kaddr; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4278 | u32 blocksize = root->sectorsize; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4279 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 4280 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 4281 | struct page *page; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4282 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4283 | int ret = 0; |
| 4284 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4285 | u64 page_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4286 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4287 | if ((offset & (blocksize - 1)) == 0 && |
| 4288 | (!len || ((len & (blocksize - 1)) == 0))) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4289 | goto out; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4290 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4291 | if (ret) |
| 4292 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4293 | |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4294 | again: |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 4295 | page = find_or_create_page(mapping, index, mask); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4296 | if (!page) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4297 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Miao Xie | ac6a2b3 | 2012-12-05 10:56:13 +0000 | [diff] [blame] | 4298 | ret = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4299 | goto out; |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4300 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4301 | |
| 4302 | page_start = page_offset(page); |
| 4303 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 4304 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4305 | if (!PageUptodate(page)) { |
| 4306 | ret = btrfs_readpage(NULL, page); |
| 4307 | lock_page(page); |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4308 | if (page->mapping != mapping) { |
| 4309 | unlock_page(page); |
| 4310 | page_cache_release(page); |
| 4311 | goto again; |
| 4312 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4313 | if (!PageUptodate(page)) { |
| 4314 | ret = -EIO; |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4315 | goto out_unlock; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4316 | } |
| 4317 | } |
Chris Mason | 211c17f | 2008-05-15 09:13:45 -0400 | [diff] [blame] | 4318 | wait_on_page_writeback(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4319 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4320 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4321 | set_page_extent_mapped(page); |
| 4322 | |
| 4323 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4324 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4325 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4326 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4327 | unlock_page(page); |
| 4328 | page_cache_release(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 4329 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4330 | btrfs_put_ordered_extent(ordered); |
| 4331 | goto again; |
| 4332 | } |
| 4333 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4334 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 4335 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 4336 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4337 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4338 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4339 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 4340 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4341 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4342 | unlock_extent_cached(io_tree, page_start, page_end, |
| 4343 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4344 | goto out_unlock; |
| 4345 | } |
| 4346 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4347 | if (offset != PAGE_CACHE_SIZE) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4348 | if (!len) |
| 4349 | len = PAGE_CACHE_SIZE - offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4350 | kaddr = kmap(page); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4351 | if (front) |
| 4352 | memset(kaddr, 0, offset); |
| 4353 | else |
| 4354 | memset(kaddr + offset, 0, len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4355 | flush_dcache_page(page); |
| 4356 | kunmap(page); |
| 4357 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 4358 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4359 | set_page_dirty(page); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4360 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
| 4361 | GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4362 | |
Chris Mason | 8964222 | 2008-07-24 09:41:53 -0400 | [diff] [blame] | 4363 | out_unlock: |
Josef Bacik | 5d5e103 | 2009-10-13 16:46:49 -0400 | [diff] [blame] | 4364 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4365 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4366 | unlock_page(page); |
| 4367 | page_cache_release(page); |
| 4368 | out: |
| 4369 | return ret; |
| 4370 | } |
| 4371 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4372 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
| 4373 | u64 offset, u64 len) |
| 4374 | { |
| 4375 | struct btrfs_trans_handle *trans; |
| 4376 | int ret; |
| 4377 | |
| 4378 | /* |
| 4379 | * Still need to make sure the inode looks like it's been updated so |
| 4380 | * that any holes get logged if we fsync. |
| 4381 | */ |
| 4382 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { |
| 4383 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 4384 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
| 4385 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; |
| 4386 | return 0; |
| 4387 | } |
| 4388 | |
| 4389 | /* |
| 4390 | * 1 - for the one we're dropping |
| 4391 | * 1 - for the one we're adding |
| 4392 | * 1 - for updating the inode. |
| 4393 | */ |
| 4394 | trans = btrfs_start_transaction(root, 3); |
| 4395 | if (IS_ERR(trans)) |
| 4396 | return PTR_ERR(trans); |
| 4397 | |
| 4398 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); |
| 4399 | if (ret) { |
| 4400 | btrfs_abort_transaction(trans, root, ret); |
| 4401 | btrfs_end_transaction(trans, root); |
| 4402 | return ret; |
| 4403 | } |
| 4404 | |
| 4405 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 4406 | 0, 0, len, 0, len, 0, 0, 0); |
| 4407 | if (ret) |
| 4408 | btrfs_abort_transaction(trans, root, ret); |
| 4409 | else |
| 4410 | btrfs_update_inode(trans, root, inode); |
| 4411 | btrfs_end_transaction(trans, root); |
| 4412 | return ret; |
| 4413 | } |
| 4414 | |
Josef Bacik | 695a0d0 | 2011-03-04 15:46:53 -0500 | [diff] [blame] | 4415 | /* |
| 4416 | * This function puts in dummy file extents for the area we're creating a hole |
| 4417 | * for. So if we are truncating this file to a larger size we need to insert |
| 4418 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for |
| 4419 | * the range between oldsize and size |
| 4420 | */ |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4421 | 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] | 4422 | { |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4423 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4424 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4425 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4426 | struct extent_state *cached_state = NULL; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4427 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4428 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
| 4429 | u64 block_end = ALIGN(size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4430 | u64 last_byte; |
| 4431 | u64 cur_offset; |
| 4432 | u64 hole_size; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4433 | int err = 0; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4434 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 4435 | /* |
| 4436 | * If our size started in the middle of a page we need to zero out the |
| 4437 | * rest of the page before we expand the i_size, otherwise we could |
| 4438 | * expose stale data. |
| 4439 | */ |
| 4440 | err = btrfs_truncate_page(inode, oldsize, 0, 0); |
| 4441 | if (err) |
| 4442 | return err; |
| 4443 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4444 | if (size <= hole_start) |
| 4445 | return 0; |
| 4446 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4447 | while (1) { |
| 4448 | struct btrfs_ordered_extent *ordered; |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4449 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4450 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4451 | &cached_state); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4452 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
| 4453 | block_end - hole_start); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4454 | if (!ordered) |
| 4455 | break; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4456 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
| 4457 | &cached_state, GFP_NOFS); |
Miao Xie | fa7c149 | 2013-09-26 13:15:27 +0800 | [diff] [blame] | 4458 | btrfs_start_ordered_extent(inode, ordered, 1); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4459 | btrfs_put_ordered_extent(ordered); |
| 4460 | } |
| 4461 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4462 | cur_offset = hole_start; |
| 4463 | while (1) { |
| 4464 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 4465 | block_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4466 | if (IS_ERR(em)) { |
| 4467 | err = PTR_ERR(em); |
Zach Brown | f276795 | 2013-01-08 19:37:58 +0000 | [diff] [blame] | 4468 | em = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4469 | break; |
| 4470 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4471 | last_byte = min(extent_map_end(em), block_end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4472 | last_byte = ALIGN(last_byte , root->sectorsize); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4473 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4474 | struct extent_map *hole_em; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4475 | hole_size = last_byte - cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4476 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4477 | err = maybe_insert_hole(root, inode, cur_offset, |
| 4478 | hole_size); |
| 4479 | if (err) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4480 | break; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4481 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4482 | cur_offset + hole_size - 1, 0); |
| 4483 | hole_em = alloc_extent_map(); |
| 4484 | if (!hole_em) { |
| 4485 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4486 | &BTRFS_I(inode)->runtime_flags); |
| 4487 | goto next; |
| 4488 | } |
| 4489 | hole_em->start = cur_offset; |
| 4490 | hole_em->len = hole_size; |
| 4491 | hole_em->orig_start = cur_offset; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4492 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4493 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 4494 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 4495 | hole_em->orig_block_len = 0; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 4496 | hole_em->ram_bytes = hole_size; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4497 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4498 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4499 | hole_em->generation = root->fs_info->generation; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4500 | |
| 4501 | while (1) { |
| 4502 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4503 | err = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 4504 | write_unlock(&em_tree->lock); |
| 4505 | if (err != -EEXIST) |
| 4506 | break; |
| 4507 | btrfs_drop_extent_cache(inode, cur_offset, |
| 4508 | cur_offset + |
| 4509 | hole_size - 1, 0); |
| 4510 | } |
| 4511 | free_extent_map(hole_em); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4512 | } |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4513 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4514 | free_extent_map(em); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4515 | em = NULL; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4516 | cur_offset = last_byte; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4517 | if (cur_offset >= block_end) |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4518 | break; |
| 4519 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4520 | free_extent_map(em); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4521 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
| 4522 | GFP_NOFS); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4523 | return err; |
| 4524 | } |
| 4525 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4526 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4527 | { |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4528 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4529 | struct btrfs_trans_handle *trans; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4530 | loff_t oldsize = i_size_read(inode); |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4531 | loff_t newsize = attr->ia_size; |
| 4532 | int mask = attr->ia_valid; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4533 | int ret; |
| 4534 | |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4535 | /* |
| 4536 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a |
| 4537 | * special case where we need to update the times despite not having |
| 4538 | * these flags set. For all other operations the VFS set these flags |
| 4539 | * explicitly if it wants a timestamp update. |
| 4540 | */ |
Christoph Hellwig | dff6efc | 2013-11-19 07:17:07 -0800 | [diff] [blame] | 4541 | if (newsize != oldsize) { |
| 4542 | inode_inc_iversion(inode); |
| 4543 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) |
| 4544 | inode->i_ctime = inode->i_mtime = |
| 4545 | current_fs_time(inode->i_sb); |
| 4546 | } |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4547 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4548 | if (newsize > oldsize) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 4549 | truncate_pagecache(inode, newsize); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4550 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4551 | if (ret) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4552 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4553 | |
Miao Xie | f4a2f4c | 2011-12-14 20:12:01 -0500 | [diff] [blame] | 4554 | trans = btrfs_start_transaction(root, 1); |
| 4555 | if (IS_ERR(trans)) |
| 4556 | return PTR_ERR(trans); |
| 4557 | |
| 4558 | i_size_write(inode, newsize); |
| 4559 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); |
| 4560 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 4561 | btrfs_end_transaction(trans, root); |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4562 | } else { |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4563 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4564 | /* |
| 4565 | * We're truncating a file that used to have good data down to |
| 4566 | * zero. Make sure it gets into the ordered flush list so that |
| 4567 | * any new writes get down to disk quickly. |
| 4568 | */ |
| 4569 | if (newsize == 0) |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4570 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 4571 | &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4572 | |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 4573 | /* |
| 4574 | * 1 for the orphan item we're going to add |
| 4575 | * 1 for the orphan item deletion. |
| 4576 | */ |
| 4577 | trans = btrfs_start_transaction(root, 2); |
| 4578 | if (IS_ERR(trans)) |
| 4579 | return PTR_ERR(trans); |
| 4580 | |
| 4581 | /* |
| 4582 | * We need to do this in case we fail at _any_ point during the |
| 4583 | * actual truncate. Once we do the truncate_setsize we could |
| 4584 | * invalidate pages which forces any outstanding ordered io to |
| 4585 | * be instantly completed which will give us extents that need |
| 4586 | * to be truncated. If we fail to get an orphan inode down we |
| 4587 | * could have left over extents that were never meant to live, |
| 4588 | * so we need to garuntee from this point on that everything |
| 4589 | * will be consistent. |
| 4590 | */ |
| 4591 | ret = btrfs_orphan_add(trans, inode); |
| 4592 | btrfs_end_transaction(trans, root); |
| 4593 | if (ret) |
| 4594 | return ret; |
| 4595 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4596 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
| 4597 | truncate_setsize(inode, newsize); |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 4598 | |
| 4599 | /* Disable nonlocked read DIO to avoid the end less truncate */ |
| 4600 | btrfs_inode_block_unlocked_dio(inode); |
| 4601 | inode_dio_wait(inode); |
| 4602 | btrfs_inode_resume_unlocked_dio(inode); |
| 4603 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4604 | ret = btrfs_truncate(inode); |
Josef Bacik | 7f4f6e0 | 2013-08-29 16:43:28 -0400 | [diff] [blame] | 4605 | if (ret && inode->i_nlink) { |
| 4606 | int err; |
| 4607 | |
| 4608 | /* |
| 4609 | * failed to truncate, disk_i_size is only adjusted down |
| 4610 | * as we remove extents, so it should represent the true |
| 4611 | * size of the inode, so reset the in memory size and |
| 4612 | * delete our orphan entry. |
| 4613 | */ |
| 4614 | trans = btrfs_join_transaction(root); |
| 4615 | if (IS_ERR(trans)) { |
| 4616 | btrfs_orphan_del(NULL, inode); |
| 4617 | return ret; |
| 4618 | } |
| 4619 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); |
| 4620 | err = btrfs_orphan_del(trans, inode); |
| 4621 | if (err) |
| 4622 | btrfs_abort_transaction(trans, root, err); |
| 4623 | btrfs_end_transaction(trans, root); |
| 4624 | } |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 4627 | return ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4630 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 4631 | { |
| 4632 | struct inode *inode = dentry->d_inode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4633 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4634 | int err; |
| 4635 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 4636 | if (btrfs_root_readonly(root)) |
| 4637 | return -EROFS; |
| 4638 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4639 | err = inode_change_ok(inode, attr); |
| 4640 | if (err) |
| 4641 | return err; |
| 4642 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 4643 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
Eric Sandeen | 3972f26 | 2013-01-12 02:57:22 +0000 | [diff] [blame] | 4644 | err = btrfs_setsize(inode, attr); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4645 | if (err) |
| 4646 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4647 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 4648 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4649 | if (attr->ia_valid) { |
| 4650 | setattr_copy(inode, attr); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 4651 | inode_inc_iversion(inode); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4652 | err = btrfs_dirty_inode(inode); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 4653 | |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 4654 | if (!err && attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 4655 | err = posix_acl_chmod(inode, inode->i_mode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 4656 | } |
| 4657 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4658 | return err; |
| 4659 | } |
Chris Mason | 61295eb | 2008-01-14 16:24:38 -0500 | [diff] [blame] | 4660 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4661 | /* |
| 4662 | * While truncating the inode pages during eviction, we get the VFS calling |
| 4663 | * btrfs_invalidatepage() against each page of the inode. This is slow because |
| 4664 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to |
| 4665 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting |
| 4666 | * extent_state structures over and over, wasting lots of time. |
| 4667 | * |
| 4668 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all |
| 4669 | * those expensive operations on a per page basis and do only the ordered io |
| 4670 | * finishing, while we release here the extent_map and extent_state structures, |
| 4671 | * without the excessive merging and splitting. |
| 4672 | */ |
| 4673 | static void evict_inode_truncate_pages(struct inode *inode) |
| 4674 | { |
| 4675 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 4676 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; |
| 4677 | struct rb_node *node; |
| 4678 | |
| 4679 | ASSERT(inode->i_state & I_FREEING); |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 4680 | truncate_inode_pages_final(&inode->i_data); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4681 | |
| 4682 | write_lock(&map_tree->lock); |
| 4683 | while (!RB_EMPTY_ROOT(&map_tree->map)) { |
| 4684 | struct extent_map *em; |
| 4685 | |
| 4686 | node = rb_first(&map_tree->map); |
| 4687 | em = rb_entry(node, struct extent_map, rb_node); |
Wang Shilong | 180589e | 2013-12-14 15:27:31 +0800 | [diff] [blame] | 4688 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4689 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4690 | remove_extent_mapping(map_tree, em); |
| 4691 | free_extent_map(em); |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4692 | if (need_resched()) { |
| 4693 | write_unlock(&map_tree->lock); |
| 4694 | cond_resched(); |
| 4695 | write_lock(&map_tree->lock); |
| 4696 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4697 | } |
| 4698 | write_unlock(&map_tree->lock); |
| 4699 | |
| 4700 | spin_lock(&io_tree->lock); |
| 4701 | while (!RB_EMPTY_ROOT(&io_tree->state)) { |
| 4702 | struct extent_state *state; |
| 4703 | struct extent_state *cached_state = NULL; |
| 4704 | |
| 4705 | node = rb_first(&io_tree->state); |
| 4706 | state = rb_entry(node, struct extent_state, rb_node); |
| 4707 | atomic_inc(&state->refs); |
| 4708 | spin_unlock(&io_tree->lock); |
| 4709 | |
| 4710 | lock_extent_bits(io_tree, state->start, state->end, |
| 4711 | 0, &cached_state); |
| 4712 | clear_extent_bit(io_tree, state->start, state->end, |
| 4713 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 4714 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 4715 | EXTENT_DEFRAG, 1, 1, |
| 4716 | &cached_state, GFP_NOFS); |
| 4717 | free_extent_state(state); |
| 4718 | |
Filipe Manana | 7064dd5 | 2014-08-08 02:47:05 +0100 | [diff] [blame] | 4719 | cond_resched(); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4720 | spin_lock(&io_tree->lock); |
| 4721 | } |
| 4722 | spin_unlock(&io_tree->lock); |
| 4723 | } |
| 4724 | |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4725 | void btrfs_evict_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4726 | { |
| 4727 | struct btrfs_trans_handle *trans; |
| 4728 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4729 | struct btrfs_block_rsv *rsv, *global_rsv; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 4730 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4731 | int ret; |
| 4732 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4733 | trace_btrfs_inode_evict(inode); |
| 4734 | |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 4735 | evict_inode_truncate_pages(inode); |
| 4736 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4737 | if (inode->i_nlink && |
| 4738 | ((btrfs_root_refs(&root->root_item) != 0 && |
| 4739 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || |
| 4740 | btrfs_is_free_space_inode(inode))) |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4741 | goto no_delete; |
| 4742 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4743 | if (is_bad_inode(inode)) { |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4744 | btrfs_orphan_del(NULL, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4745 | goto no_delete; |
| 4746 | } |
Al Viro | bd55597 | 2010-06-07 11:35:40 -0400 | [diff] [blame] | 4747 | /* 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] | 4748 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4749 | |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4750 | if (root->fs_info->log_root_recovering) { |
Liu Bo | 6bf0231 | 2012-06-25 21:59:09 -0600 | [diff] [blame] | 4751 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 4752 | &BTRFS_I(inode)->runtime_flags)); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 4753 | goto no_delete; |
| 4754 | } |
| 4755 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4756 | if (inode->i_nlink > 0) { |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4757 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
| 4758 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4759 | goto no_delete; |
| 4760 | } |
| 4761 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4762 | ret = btrfs_commit_inode_delayed_inode(inode); |
| 4763 | if (ret) { |
| 4764 | btrfs_orphan_del(NULL, inode); |
| 4765 | goto no_delete; |
| 4766 | } |
| 4767 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4768 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4769 | if (!rsv) { |
| 4770 | btrfs_orphan_del(NULL, inode); |
| 4771 | goto no_delete; |
| 4772 | } |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 4773 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4774 | rsv->failfast = 1; |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4775 | global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4776 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 4777 | btrfs_i_size_write(inode, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4778 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4779 | /* |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4780 | * This is a bit simpler than btrfs_truncate since we've already |
| 4781 | * reserved our space for our orphan item in the unlink, so we just |
| 4782 | * need to reserve some slack space in case we add bytes and update |
| 4783 | * inode item when doing the truncate. |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4784 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4785 | while (1) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4786 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
| 4787 | BTRFS_RESERVE_FLUSH_LIMIT); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4788 | |
Josef Bacik | 726c35f | 2011-09-26 15:46:06 -0400 | [diff] [blame] | 4789 | /* |
| 4790 | * Try and steal from the global reserve since we will |
| 4791 | * likely not use this space anyway, we want to try as |
| 4792 | * hard as possible to get this to work. |
| 4793 | */ |
| 4794 | if (ret) |
| 4795 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); |
| 4796 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4797 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4798 | btrfs_warn(root->fs_info, |
| 4799 | "Could not get space for a delete, will truncate on mount %d", |
| 4800 | ret); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4801 | btrfs_orphan_del(NULL, inode); |
| 4802 | btrfs_free_block_rsv(root, rsv); |
| 4803 | goto no_delete; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4804 | } |
| 4805 | |
Miao Xie | 0e8c36a | 2012-12-19 06:59:51 +0000 | [diff] [blame] | 4806 | trans = btrfs_join_transaction(root); |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4807 | if (IS_ERR(trans)) { |
| 4808 | btrfs_orphan_del(NULL, inode); |
| 4809 | btrfs_free_block_rsv(root, rsv); |
| 4810 | goto no_delete; |
| 4811 | } |
| 4812 | |
| 4813 | trans->block_rsv = rsv; |
| 4814 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4815 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 4816 | if (ret != -ENOSPC) |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4817 | break; |
| 4818 | |
Miao Xie | 8407aa4 | 2012-09-07 01:43:32 -0600 | [diff] [blame] | 4819 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4820 | btrfs_end_transaction(trans, root); |
| 4821 | trans = NULL; |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4822 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 4823 | } |
| 4824 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4825 | btrfs_free_block_rsv(root, rsv); |
| 4826 | |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4827 | /* |
| 4828 | * Errors here aren't a big deal, it just means we leave orphan items |
| 4829 | * in the tree. They will be cleaned up on the next mount. |
| 4830 | */ |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4831 | if (ret == 0) { |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4832 | trans->block_rsv = root->orphan_block_rsv; |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 4833 | btrfs_orphan_del(trans, inode); |
| 4834 | } else { |
| 4835 | btrfs_orphan_del(NULL, inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4836 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4837 | |
Josef Bacik | 4289a66 | 2011-08-05 13:22:24 -0400 | [diff] [blame] | 4838 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4839 | if (!(root == root->fs_info->tree_root || |
| 4840 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4841 | btrfs_return_ino(root, btrfs_ino(inode)); |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 4842 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4843 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 4844 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4845 | no_delete: |
Miao Xie | 89042e5 | 2013-05-15 07:48:15 +0000 | [diff] [blame] | 4846 | btrfs_remove_delayed_node(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 4847 | clear_inode(inode); |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 4848 | return; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | /* |
| 4852 | * this returns the key found in the dir entry in the location pointer. |
| 4853 | * If no dir entries were found, location->objectid is 0. |
| 4854 | */ |
| 4855 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 4856 | struct btrfs_key *location) |
| 4857 | { |
| 4858 | const char *name = dentry->d_name.name; |
| 4859 | int namelen = dentry->d_name.len; |
| 4860 | struct btrfs_dir_item *di; |
| 4861 | struct btrfs_path *path; |
| 4862 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4863 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4864 | |
| 4865 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4866 | if (!path) |
| 4867 | return -ENOMEM; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4868 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4869 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4870 | namelen, 0); |
Yan | 0d9f7f3 | 2007-10-25 15:48:28 -0400 | [diff] [blame] | 4871 | if (IS_ERR(di)) |
| 4872 | ret = PTR_ERR(di); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4873 | |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4874 | if (IS_ERR_OR_NULL(di)) |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4875 | goto out_err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4876 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4877 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4878 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4879 | btrfs_free_path(path); |
| 4880 | return ret; |
Chris Mason | 3954401 | 2007-12-12 14:38:19 -0500 | [diff] [blame] | 4881 | out_err: |
| 4882 | location->objectid = 0; |
| 4883 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /* |
| 4887 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 4888 | * needs to be changed to reflect the root directory of the tree root. This |
| 4889 | * is kind of like crossing a mount point. |
| 4890 | */ |
| 4891 | static int fixup_tree_root_location(struct btrfs_root *root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4892 | struct inode *dir, |
| 4893 | struct dentry *dentry, |
| 4894 | struct btrfs_key *location, |
| 4895 | struct btrfs_root **sub_root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4896 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4897 | struct btrfs_path *path; |
| 4898 | struct btrfs_root *new_root; |
| 4899 | struct btrfs_root_ref *ref; |
| 4900 | struct extent_buffer *leaf; |
| 4901 | int ret; |
| 4902 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4903 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4904 | path = btrfs_alloc_path(); |
| 4905 | if (!path) { |
| 4906 | err = -ENOMEM; |
| 4907 | goto out; |
| 4908 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4909 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4910 | err = -ENOENT; |
Kelley Nielsen | 75ac2dd | 2013-11-04 19:35:58 -0800 | [diff] [blame] | 4911 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
| 4912 | BTRFS_I(dir)->root->root_key.objectid, |
| 4913 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4914 | if (ret) { |
| 4915 | if (ret < 0) |
| 4916 | err = ret; |
| 4917 | goto out; |
| 4918 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4919 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4920 | leaf = path->nodes[0]; |
| 4921 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4922 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4923 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
| 4924 | goto out; |
| 4925 | |
| 4926 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
| 4927 | (unsigned long)(ref + 1), |
| 4928 | dentry->d_name.len); |
| 4929 | if (ret) |
| 4930 | goto out; |
| 4931 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4932 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4933 | |
| 4934 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); |
| 4935 | if (IS_ERR(new_root)) { |
| 4936 | err = PTR_ERR(new_root); |
| 4937 | goto out; |
| 4938 | } |
| 4939 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4940 | *sub_root = new_root; |
| 4941 | location->objectid = btrfs_root_dirid(&new_root->root_item); |
| 4942 | location->type = BTRFS_INODE_ITEM_KEY; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4943 | location->offset = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 4944 | err = 0; |
| 4945 | out: |
| 4946 | btrfs_free_path(path); |
| 4947 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 4948 | } |
| 4949 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4950 | static void inode_tree_add(struct inode *inode) |
| 4951 | { |
| 4952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4953 | struct btrfs_inode *entry; |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4954 | struct rb_node **p; |
| 4955 | struct rb_node *parent; |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4956 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4957 | u64 ino = btrfs_ino(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4958 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 4959 | if (inode_unhashed(inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4960 | return; |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4961 | parent = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4962 | spin_lock(&root->inode_lock); |
Miao Xie | e1409ce | 2013-05-15 07:48:16 +0000 | [diff] [blame] | 4963 | p = &root->inode_tree.rb_node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4964 | while (*p) { |
| 4965 | parent = *p; |
| 4966 | entry = rb_entry(parent, struct btrfs_inode, rb_node); |
| 4967 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4968 | if (ino < btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4969 | p = &parent->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 4970 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4971 | p = &parent->rb_right; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4972 | else { |
| 4973 | WARN_ON(!(entry->vfs_inode.i_state & |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 4974 | (I_WILL_FREE | I_FREEING))); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4975 | rb_replace_node(parent, new, &root->inode_tree); |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4976 | RB_CLEAR_NODE(parent); |
| 4977 | spin_unlock(&root->inode_lock); |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4978 | return; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4979 | } |
| 4980 | } |
Filipe David Borba Manana | cef2193 | 2013-09-02 12:19:13 +0100 | [diff] [blame] | 4981 | rb_link_node(new, parent, p); |
| 4982 | rb_insert_color(new, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4983 | spin_unlock(&root->inode_lock); |
| 4984 | } |
| 4985 | |
| 4986 | static void inode_tree_del(struct inode *inode) |
| 4987 | { |
| 4988 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4989 | int empty = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4990 | |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4991 | spin_lock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4992 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4993 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4994 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4995 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4996 | } |
From: Nick Piggin | 03e860b | 2009-08-21 10:09:44 +0200 | [diff] [blame] | 4997 | spin_unlock(&root->inode_lock); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4998 | |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 4999 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5000 | synchronize_srcu(&root->fs_info->subvol_srcu); |
| 5001 | spin_lock(&root->inode_lock); |
| 5002 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
| 5003 | spin_unlock(&root->inode_lock); |
| 5004 | if (empty) |
| 5005 | btrfs_add_dead_root(root); |
| 5006 | } |
| 5007 | } |
| 5008 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5009 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5010 | { |
| 5011 | struct rb_node *node; |
| 5012 | struct rb_node *prev; |
| 5013 | struct btrfs_inode *entry; |
| 5014 | struct inode *inode; |
| 5015 | u64 objectid = 0; |
| 5016 | |
Liu Bo | 7813b3d | 2014-02-10 17:37:25 +0800 | [diff] [blame] | 5017 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
| 5018 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5019 | |
| 5020 | spin_lock(&root->inode_lock); |
| 5021 | again: |
| 5022 | node = root->inode_tree.rb_node; |
| 5023 | prev = NULL; |
| 5024 | while (node) { |
| 5025 | prev = node; |
| 5026 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
| 5027 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5028 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5029 | node = node->rb_left; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5030 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5031 | node = node->rb_right; |
| 5032 | else |
| 5033 | break; |
| 5034 | } |
| 5035 | if (!node) { |
| 5036 | while (prev) { |
| 5037 | entry = rb_entry(prev, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5038 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5039 | node = prev; |
| 5040 | break; |
| 5041 | } |
| 5042 | prev = rb_next(prev); |
| 5043 | } |
| 5044 | } |
| 5045 | while (node) { |
| 5046 | entry = rb_entry(node, struct btrfs_inode, rb_node); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5047 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5048 | inode = igrab(&entry->vfs_inode); |
| 5049 | if (inode) { |
| 5050 | spin_unlock(&root->inode_lock); |
| 5051 | if (atomic_read(&inode->i_count) > 1) |
| 5052 | d_prune_aliases(inode); |
| 5053 | /* |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 5054 | * btrfs_drop_inode will have it removed from |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5055 | * the inode cache when its usage count |
| 5056 | * hits zero. |
| 5057 | */ |
| 5058 | iput(inode); |
| 5059 | cond_resched(); |
| 5060 | spin_lock(&root->inode_lock); |
| 5061 | goto again; |
| 5062 | } |
| 5063 | |
| 5064 | if (cond_resched_lock(&root->inode_lock)) |
| 5065 | goto again; |
| 5066 | |
| 5067 | node = rb_next(node); |
| 5068 | } |
| 5069 | spin_unlock(&root->inode_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5070 | } |
| 5071 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5072 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 5073 | { |
| 5074 | struct btrfs_iget_args *args = p; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5075 | inode->i_ino = args->location->objectid; |
| 5076 | memcpy(&BTRFS_I(inode)->location, args->location, |
| 5077 | sizeof(*args->location)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5078 | BTRFS_I(inode)->root = args->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5079 | return 0; |
| 5080 | } |
| 5081 | |
| 5082 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 5083 | { |
| 5084 | struct btrfs_iget_args *args = opaque; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5085 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5086 | args->root == BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5087 | } |
| 5088 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5089 | static struct inode *btrfs_iget_locked(struct super_block *s, |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5090 | struct btrfs_key *location, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5091 | struct btrfs_root *root) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5092 | { |
| 5093 | struct inode *inode; |
| 5094 | struct btrfs_iget_args args; |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5095 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5096 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5097 | args.location = location; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5098 | args.root = root; |
| 5099 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5100 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5101 | btrfs_init_locked_inode, |
| 5102 | (void *)&args); |
| 5103 | return inode; |
| 5104 | } |
| 5105 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5106 | /* Get an inode object given its location and corresponding root. |
| 5107 | * Returns in *is_new if the inode was read from disk |
| 5108 | */ |
| 5109 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5110 | struct btrfs_root *root, int *new) |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5111 | { |
| 5112 | struct inode *inode; |
| 5113 | |
Chris Mason | 90d3e59 | 2014-01-09 17:28:00 -0800 | [diff] [blame] | 5114 | inode = btrfs_iget_locked(s, location, root); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5115 | if (!inode) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5116 | return ERR_PTR(-ENOMEM); |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5117 | |
| 5118 | if (inode->i_state & I_NEW) { |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5119 | btrfs_read_locked_inode(inode); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5120 | if (!is_bad_inode(inode)) { |
| 5121 | inode_tree_add(inode); |
| 5122 | unlock_new_inode(inode); |
| 5123 | if (new) |
| 5124 | *new = 1; |
| 5125 | } else { |
Sergei Trofimovich | e0b6d65 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 5126 | unlock_new_inode(inode); |
| 5127 | iput(inode); |
| 5128 | inode = ERR_PTR(-ESTALE); |
Mark Fasheh | 1748f84 | 2011-07-12 11:25:31 -0700 | [diff] [blame] | 5129 | } |
| 5130 | } |
| 5131 | |
Balaji Rao | 1a54ef8 | 2008-07-21 02:01:04 +0530 | [diff] [blame] | 5132 | return inode; |
| 5133 | } |
| 5134 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5135 | static struct inode *new_simple_dir(struct super_block *s, |
| 5136 | struct btrfs_key *key, |
| 5137 | struct btrfs_root *root) |
| 5138 | { |
| 5139 | struct inode *inode = new_inode(s); |
| 5140 | |
| 5141 | if (!inode) |
| 5142 | return ERR_PTR(-ENOMEM); |
| 5143 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5144 | BTRFS_I(inode)->root = root; |
| 5145 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5146 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5147 | |
| 5148 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5149 | inode->i_op = &btrfs_dir_ro_inode_operations; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5150 | inode->i_fop = &simple_dir_operations; |
| 5151 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; |
| 5152 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 5153 | |
| 5154 | return inode; |
| 5155 | } |
| 5156 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5157 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5158 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5159 | struct inode *inode; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5160 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5161 | struct btrfs_root *sub_root = root; |
| 5162 | struct btrfs_key location; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5163 | int index; |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5164 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5165 | |
| 5166 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 5167 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5168 | |
Jeff Layton | 39e3c95 | 2012-11-28 11:30:53 -0500 | [diff] [blame] | 5169 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5170 | if (ret < 0) |
| 5171 | return ERR_PTR(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5172 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5173 | if (location.objectid == 0) |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5174 | return ERR_PTR(-ENOENT); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5175 | |
| 5176 | if (location.type == BTRFS_INODE_ITEM_KEY) { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5177 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5178 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5179 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5180 | |
| 5181 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); |
| 5182 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5183 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5184 | ret = fixup_tree_root_location(root, dir, dentry, |
| 5185 | &location, &sub_root); |
| 5186 | if (ret < 0) { |
| 5187 | if (ret != -ENOENT) |
| 5188 | inode = ERR_PTR(ret); |
| 5189 | else |
| 5190 | inode = new_simple_dir(dir->i_sb, &location, sub_root); |
| 5191 | } else { |
Josef Bacik | 73f7341 | 2009-12-04 17:38:27 +0000 | [diff] [blame] | 5192 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5193 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5194 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
| 5195 | |
Julia Lawall | 34d19ba | 2011-01-24 19:55:19 +0000 | [diff] [blame] | 5196 | if (!IS_ERR(inode) && root != sub_root) { |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5197 | down_read(&root->fs_info->cleanup_work_sem); |
| 5198 | if (!(inode->i_sb->s_flags & MS_RDONLY)) |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5199 | ret = btrfs_orphan_cleanup(sub_root); |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5200 | up_read(&root->fs_info->cleanup_work_sem); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5201 | if (ret) { |
| 5202 | iput(inode); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 5203 | inode = ERR_PTR(ret); |
Josef Bacik | 01cd336 | 2013-06-03 21:39:49 -0400 | [diff] [blame] | 5204 | } |
Filipe Manana | 9c3b306 | 2014-07-31 14:41:07 +0100 | [diff] [blame] | 5205 | /* |
| 5206 | * If orphan cleanup did remove any orphans, it means the tree |
| 5207 | * was modified and therefore the commit root is not the same as |
| 5208 | * the current root anymore. This is a problem, because send |
| 5209 | * uses the commit root and therefore can see inode items that |
| 5210 | * don't exist in the current root anymore, and for example make |
| 5211 | * calls to btrfs_iget, which will do tree lookups based on the |
| 5212 | * current root and not on the commit root. Those lookups will |
| 5213 | * fail, returning a -ESTALE error, and making send fail with |
| 5214 | * that error. So make sure a send does not see any orphans we |
| 5215 | * have just removed, and that it will see the same inodes |
| 5216 | * regardless of whether a transaction commit happened before |
| 5217 | * it started (meaning that the commit root will be the same as |
| 5218 | * the current root) or not. |
| 5219 | */ |
| 5220 | if (sub_root->node != sub_root->commit_root) { |
| 5221 | u64 sub_flags = btrfs_root_flags(&sub_root->root_item); |
| 5222 | |
| 5223 | if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) { |
| 5224 | struct extent_buffer *eb; |
| 5225 | |
| 5226 | /* |
| 5227 | * Assert we can't have races between dentry |
| 5228 | * lookup called through the snapshot creation |
| 5229 | * ioctl and the VFS. |
| 5230 | */ |
| 5231 | ASSERT(mutex_is_locked(&dir->i_mutex)); |
| 5232 | |
| 5233 | down_write(&root->fs_info->commit_root_sem); |
| 5234 | eb = sub_root->commit_root; |
| 5235 | sub_root->commit_root = |
| 5236 | btrfs_root_node(sub_root); |
| 5237 | up_write(&root->fs_info->commit_root_sem); |
| 5238 | free_extent_buffer(eb); |
| 5239 | } |
| 5240 | } |
Yan, Zheng | c71bf09 | 2009-11-12 09:34:40 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5243 | return inode; |
| 5244 | } |
| 5245 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 5246 | static int btrfs_dentry_delete(const struct dentry *dentry) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5247 | { |
| 5248 | struct btrfs_root *root; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5249 | struct inode *inode = dentry->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5250 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5251 | if (!inode && !IS_ROOT(dentry)) |
| 5252 | inode = dentry->d_parent->d_inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5253 | |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5254 | if (inode) { |
| 5255 | root = BTRFS_I(inode)->root; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5256 | if (btrfs_root_refs(&root->root_item) == 0) |
| 5257 | return 1; |
Li Zefan | 848cce0 | 2012-02-21 17:04:28 +0800 | [diff] [blame] | 5258 | |
| 5259 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
| 5260 | return 1; |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5261 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5262 | return 0; |
| 5263 | } |
| 5264 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5265 | static void btrfs_dentry_release(struct dentry *dentry) |
| 5266 | { |
Daeseok Youn | 944a451 | 2014-04-14 15:37:02 +0900 | [diff] [blame] | 5267 | kfree(dentry->d_fsdata); |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5268 | } |
| 5269 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5270 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 5271 | unsigned int flags) |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5272 | { |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5273 | struct inode *inode; |
Josef Bacik | a66e7cc | 2011-09-18 10:34:03 -0400 | [diff] [blame] | 5274 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 5275 | inode = btrfs_lookup_dentry(dir, dentry); |
| 5276 | if (IS_ERR(inode)) { |
| 5277 | if (PTR_ERR(inode) == -ENOENT) |
| 5278 | inode = NULL; |
| 5279 | else |
| 5280 | return ERR_CAST(inode); |
| 5281 | } |
| 5282 | |
Josef Bacik | 3a0dfa6 | 2014-02-14 13:43:48 -0500 | [diff] [blame] | 5283 | return d_materialise_unique(dentry, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5284 | } |
| 5285 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5286 | unsigned char btrfs_filetype_table[] = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5287 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 5288 | }; |
| 5289 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5290 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5291 | { |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5292 | struct inode *inode = file_inode(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5293 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5294 | struct btrfs_item *item; |
| 5295 | struct btrfs_dir_item *di; |
| 5296 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5297 | struct btrfs_key found_key; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5298 | struct btrfs_path *path; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5299 | struct list_head ins_list; |
| 5300 | struct list_head del_list; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5301 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5302 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5303 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5304 | unsigned char d_type; |
| 5305 | int over = 0; |
| 5306 | u32 di_cur; |
| 5307 | u32 di_total; |
| 5308 | u32 di_len; |
| 5309 | int key_type = BTRFS_DIR_INDEX_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5310 | char tmp_name[32]; |
| 5311 | char *name_ptr; |
| 5312 | int name_len; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5313 | int is_curr = 0; /* ctx->pos points to the current index? */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5314 | |
| 5315 | /* FIXME, use a real flag for deciding about the key type */ |
| 5316 | if (root->fs_info->tree_root == root) |
| 5317 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5318 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5319 | if (!dir_emit_dots(file, ctx)) |
| 5320 | return 0; |
| 5321 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5322 | path = btrfs_alloc_path(); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5323 | if (!path) |
| 5324 | return -ENOMEM; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5325 | |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 5326 | path->reada = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5327 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5328 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5329 | INIT_LIST_HEAD(&ins_list); |
| 5330 | INIT_LIST_HEAD(&del_list); |
| 5331 | btrfs_get_delayed_items(inode, &ins_list, &del_list); |
| 5332 | } |
| 5333 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5334 | btrfs_set_key_type(&key, key_type); |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5335 | key.offset = ctx->pos; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5336 | key.objectid = btrfs_ino(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5337 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5338 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5339 | if (ret < 0) |
| 5340 | goto err; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5341 | |
| 5342 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5343 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5344 | slot = path->slots[0]; |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5345 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5346 | ret = btrfs_next_leaf(root, path); |
| 5347 | if (ret < 0) |
| 5348 | goto err; |
| 5349 | else if (ret > 0) |
| 5350 | break; |
| 5351 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5352 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5353 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5354 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5355 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5356 | |
| 5357 | if (found_key.objectid != key.objectid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5358 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5359 | if (btrfs_key_type(&found_key) != key_type) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5360 | break; |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5361 | if (found_key.offset < ctx->pos) |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5362 | goto next; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5363 | if (key_type == BTRFS_DIR_INDEX_KEY && |
| 5364 | btrfs_should_delete_dir_index(&del_list, |
| 5365 | found_key.offset)) |
| 5366 | goto next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5367 | |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5368 | ctx->pos = found_key.offset; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5369 | is_curr = 1; |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5370 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5371 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 5372 | di_cur = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5373 | di_total = btrfs_item_size(leaf, item); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5374 | |
| 5375 | while (di_cur < di_total) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5376 | struct btrfs_key location; |
| 5377 | |
Josef Bacik | 22a94d4 | 2011-03-16 16:47:17 -0400 | [diff] [blame] | 5378 | if (verify_dir_item(root, leaf, di)) |
| 5379 | break; |
| 5380 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5381 | name_len = btrfs_dir_name_len(leaf, di); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5382 | if (name_len <= sizeof(tmp_name)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5383 | name_ptr = tmp_name; |
| 5384 | } else { |
| 5385 | name_ptr = kmalloc(name_len, GFP_NOFS); |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5386 | if (!name_ptr) { |
| 5387 | ret = -ENOMEM; |
| 5388 | goto err; |
| 5389 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5390 | } |
| 5391 | read_extent_buffer(leaf, name_ptr, |
| 5392 | (unsigned long)(di + 1), name_len); |
| 5393 | |
| 5394 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; |
| 5395 | btrfs_dir_item_key_to_cpu(leaf, di, &location); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5396 | |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 5397 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5398 | /* is this a reference to our own snapshot? If so |
Arne Jansen | 8c9c2bf | 2012-02-25 09:09:30 +0100 | [diff] [blame] | 5399 | * skip it. |
| 5400 | * |
| 5401 | * In contrast to old kernels, we insert the snapshot's |
| 5402 | * dir item and dir index after it has been created, so |
| 5403 | * we won't find a reference to our own snapshot. We |
| 5404 | * still keep the following code for backward |
| 5405 | * compatibility. |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5406 | */ |
| 5407 | if (location.type == BTRFS_ROOT_ITEM_KEY && |
| 5408 | location.objectid == root->root_key.objectid) { |
| 5409 | over = 0; |
| 5410 | goto skip; |
| 5411 | } |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5412 | over = !dir_emit(ctx, name_ptr, name_len, |
| 5413 | location.objectid, d_type); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5414 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5415 | skip: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5416 | if (name_ptr != tmp_name) |
| 5417 | kfree(name_ptr); |
| 5418 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5419 | if (over) |
| 5420 | goto nopos; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 5421 | di_len = btrfs_dir_name_len(leaf, di) + |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5422 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5423 | di_cur += di_len; |
| 5424 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 5425 | } |
Li Zefan | b9e03af | 2011-03-23 10:43:58 +0800 | [diff] [blame] | 5426 | next: |
| 5427 | path->slots[0]++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5428 | } |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5429 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5430 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5431 | if (is_curr) |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 5432 | ctx->pos++; |
| 5433 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5434 | if (ret) |
| 5435 | goto nopos; |
| 5436 | } |
| 5437 | |
David Woodhouse | 49593bf | 2008-08-17 17:08:36 +0100 | [diff] [blame] | 5438 | /* Reached end of directory/root. Bump pos past the last item. */ |
Zach Brown | db62efb | 2013-07-11 16:19:42 -0700 | [diff] [blame] | 5439 | ctx->pos++; |
| 5440 | |
| 5441 | /* |
| 5442 | * Stop new entries from being returned after we return the last |
| 5443 | * entry. |
| 5444 | * |
| 5445 | * New directory entries are assigned a strictly increasing |
| 5446 | * offset. This means that new entries created during readdir |
| 5447 | * are *guaranteed* to be seen in the future by that readdir. |
| 5448 | * This has broken buggy programs which operate on names as |
| 5449 | * they're returned by readdir. Until we re-use freed offsets |
| 5450 | * we have this hack to stop new entries from being returned |
| 5451 | * under the assumption that they'll never reach this huge |
| 5452 | * offset. |
| 5453 | * |
| 5454 | * This is being careful not to overflow 32bit loff_t unless the |
| 5455 | * last entry requires it because doing so has broken 32bit apps |
| 5456 | * in the past. |
| 5457 | */ |
| 5458 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5459 | if (ctx->pos >= INT_MAX) |
| 5460 | ctx->pos = LLONG_MAX; |
| 5461 | else |
| 5462 | ctx->pos = INT_MAX; |
| 5463 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5464 | nopos: |
| 5465 | ret = 0; |
| 5466 | err: |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5467 | if (key_type == BTRFS_DIR_INDEX_KEY) |
| 5468 | btrfs_put_delayed_items(&ins_list, &del_list); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5469 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5470 | return ret; |
| 5471 | } |
| 5472 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5473 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5474 | { |
| 5475 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5476 | struct btrfs_trans_handle *trans; |
| 5477 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5478 | bool nolock = false; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5479 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5480 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4ca8b41 | 2008-08-05 13:30:48 -0400 | [diff] [blame] | 5481 | return 0; |
| 5482 | |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 5483 | 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] | 5484 | nolock = true; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5485 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 5486 | if (wbc->sync_mode == WB_SYNC_ALL) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5487 | if (nolock) |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5488 | trans = btrfs_join_transaction_nolock(root); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5489 | else |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5490 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 5491 | if (IS_ERR(trans)) |
| 5492 | return PTR_ERR(trans); |
Miao Xie | a698d075 | 2012-09-20 01:51:59 -0600 | [diff] [blame] | 5493 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5494 | } |
| 5495 | return ret; |
| 5496 | } |
| 5497 | |
| 5498 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5499 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5500 | * inode changes. But, it is most likely to find the inode in cache. |
| 5501 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 5502 | * to keep or drop this code. |
| 5503 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5504 | static int btrfs_dirty_inode(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5505 | { |
| 5506 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5507 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5508 | int ret; |
| 5509 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5510 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5511 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5512 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 5513 | trans = btrfs_join_transaction(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5514 | if (IS_ERR(trans)) |
| 5515 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5516 | |
| 5517 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5518 | if (ret && ret == -ENOSPC) { |
| 5519 | /* whoops, lets try again with the full transaction */ |
| 5520 | btrfs_end_transaction(trans, root); |
| 5521 | trans = btrfs_start_transaction(root, 1); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5522 | if (IS_ERR(trans)) |
| 5523 | return PTR_ERR(trans); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5524 | |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5525 | ret = btrfs_update_inode(trans, root, inode); |
Chris Mason | 94b6044 | 2010-05-26 11:02:00 -0400 | [diff] [blame] | 5526 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5527 | btrfs_end_transaction(trans, root); |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5528 | if (BTRFS_I(inode)->delayed_node) |
| 5529 | btrfs_balance_delayed_items(root); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5530 | |
| 5531 | return ret; |
| 5532 | } |
| 5533 | |
| 5534 | /* |
| 5535 | * This is a copy of file_update_time. We need this so we can return error on |
| 5536 | * ENOSPC for updating the inode in the case of file write and mmap writes. |
| 5537 | */ |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5538 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
| 5539 | int flags) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5540 | { |
Alexander Block | 2bc556528 | 2012-06-15 09:49:33 +0200 | [diff] [blame] | 5541 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5542 | |
| 5543 | if (btrfs_root_readonly(root)) |
| 5544 | return -EROFS; |
| 5545 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5546 | if (flags & S_VERSION) |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 5547 | inode_inc_iversion(inode); |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 5548 | if (flags & S_CTIME) |
| 5549 | inode->i_ctime = *now; |
| 5550 | if (flags & S_MTIME) |
| 5551 | inode->i_mtime = *now; |
| 5552 | if (flags & S_ATIME) |
| 5553 | inode->i_atime = *now; |
| 5554 | return btrfs_dirty_inode(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5555 | } |
| 5556 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5557 | /* |
| 5558 | * find the highest existing sequence number in a directory |
| 5559 | * and then set the in-memory index_cnt variable to reflect |
| 5560 | * free sequence numbers |
| 5561 | */ |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5562 | static int btrfs_set_inode_index_count(struct inode *inode) |
| 5563 | { |
| 5564 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5565 | struct btrfs_key key, found_key; |
| 5566 | struct btrfs_path *path; |
| 5567 | struct extent_buffer *leaf; |
| 5568 | int ret; |
| 5569 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5570 | key.objectid = btrfs_ino(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5571 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 5572 | key.offset = (u64)-1; |
| 5573 | |
| 5574 | path = btrfs_alloc_path(); |
| 5575 | if (!path) |
| 5576 | return -ENOMEM; |
| 5577 | |
| 5578 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5579 | if (ret < 0) |
| 5580 | goto out; |
| 5581 | /* FIXME: we should be able to handle this */ |
| 5582 | if (ret == 0) |
| 5583 | goto out; |
| 5584 | ret = 0; |
| 5585 | |
| 5586 | /* |
| 5587 | * MAGIC NUMBER EXPLANATION: |
| 5588 | * since we search a directory based on f_pos we have to start at 2 |
| 5589 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody |
| 5590 | * else has to start at 2 |
| 5591 | */ |
| 5592 | if (path->slots[0] == 0) { |
| 5593 | BTRFS_I(inode)->index_cnt = 2; |
| 5594 | goto out; |
| 5595 | } |
| 5596 | |
| 5597 | path->slots[0]--; |
| 5598 | |
| 5599 | leaf = path->nodes[0]; |
| 5600 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5601 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5602 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5603 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
| 5604 | BTRFS_I(inode)->index_cnt = 2; |
| 5605 | goto out; |
| 5606 | } |
| 5607 | |
| 5608 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; |
| 5609 | out: |
| 5610 | btrfs_free_path(path); |
| 5611 | return ret; |
| 5612 | } |
| 5613 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5614 | /* |
| 5615 | * helper to find a free sequence number in a given directory. This current |
| 5616 | * code is very simple, later versions will do smarter things in the btree |
| 5617 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5618 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5619 | { |
| 5620 | int ret = 0; |
| 5621 | |
| 5622 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 5623 | ret = btrfs_inode_delayed_dir_index_count(dir); |
| 5624 | if (ret) { |
| 5625 | ret = btrfs_set_inode_index_count(dir); |
| 5626 | if (ret) |
| 5627 | return ret; |
| 5628 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5629 | } |
| 5630 | |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5631 | *index = BTRFS_I(dir)->index_cnt; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5632 | BTRFS_I(dir)->index_cnt++; |
| 5633 | |
| 5634 | return ret; |
| 5635 | } |
| 5636 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5637 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 5638 | struct btrfs_root *root, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5639 | struct inode *dir, |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5640 | const char *name, int name_len, |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 5641 | u64 ref_objectid, u64 objectid, |
| 5642 | umode_t mode, u64 *index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5643 | { |
| 5644 | struct inode *inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5645 | struct btrfs_inode_item *inode_item; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5646 | struct btrfs_key *location; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5647 | struct btrfs_path *path; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5648 | struct btrfs_inode_ref *ref; |
| 5649 | struct btrfs_key key[2]; |
| 5650 | u32 sizes[2]; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5651 | int nitems = name ? 2 : 1; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5652 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5653 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5654 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5655 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5656 | if (!path) |
| 5657 | return ERR_PTR(-ENOMEM); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5658 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5659 | inode = new_inode(root->fs_info->sb); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5660 | if (!inode) { |
| 5661 | btrfs_free_path(path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5662 | return ERR_PTR(-ENOMEM); |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5663 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5664 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5665 | /* |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 5666 | * O_TMPFILE, set link count to 0, so that after this point, |
| 5667 | * we fill in an inode item with the correct link count. |
| 5668 | */ |
| 5669 | if (!name) |
| 5670 | set_nlink(inode, 0); |
| 5671 | |
| 5672 | /* |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5673 | * we have to initialize this early, so we can reclaim the inode |
| 5674 | * number if we fail afterwards in this function. |
| 5675 | */ |
| 5676 | inode->i_ino = objectid; |
| 5677 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5678 | if (dir && name) { |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5679 | trace_btrfs_inode_request(dir); |
| 5680 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5681 | ret = btrfs_set_inode_index(dir, index); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5682 | if (ret) { |
Yoshinori Sano | 8fb2764 | 2011-04-09 02:30:07 +0000 | [diff] [blame] | 5683 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5684 | iput(inode); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5685 | return ERR_PTR(ret); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5686 | } |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5687 | } else if (dir) { |
| 5688 | *index = 0; |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5689 | } |
| 5690 | /* |
| 5691 | * index_cnt is ignored for everything but a dir, |
| 5692 | * btrfs_get_inode_index_count has an explanation for the magic |
| 5693 | * number |
| 5694 | */ |
| 5695 | BTRFS_I(inode)->index_cnt = 2; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 5696 | BTRFS_I(inode)->dir_index = *index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5697 | BTRFS_I(inode)->root = root; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5698 | BTRFS_I(inode)->generation = trans->transid; |
Josef Bacik | 7619585 | 2010-11-19 02:18:02 +0000 | [diff] [blame] | 5699 | inode->i_generation = BTRFS_I(inode)->generation; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5700 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 5701 | /* |
| 5702 | * We could have gotten an inode number from somebody who was fsynced |
| 5703 | * and then removed in this same transaction, so let's just set full |
| 5704 | * sync since it will be a full sync anyway and this will blow away the |
| 5705 | * old info in the log. |
| 5706 | */ |
| 5707 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
| 5708 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5709 | key[0].objectid = objectid; |
| 5710 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); |
| 5711 | key[0].offset = 0; |
| 5712 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5713 | sizes[0] = sizeof(struct btrfs_inode_item); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5714 | |
| 5715 | if (name) { |
| 5716 | /* |
| 5717 | * Start new inodes with an inode_ref. This is slightly more |
| 5718 | * efficient for small numbers of hard links since they will |
| 5719 | * be packed into one item. Extended refs will kick in if we |
| 5720 | * add more hard links than can fit in the ref item. |
| 5721 | */ |
| 5722 | key[1].objectid = objectid; |
| 5723 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); |
| 5724 | key[1].offset = ref_objectid; |
| 5725 | |
| 5726 | sizes[1] = name_len + sizeof(*ref); |
| 5727 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5728 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5729 | path->leave_spinning = 1; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5730 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5731 | if (ret != 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5732 | goto fail; |
| 5733 | |
Dmitry Monakhov | ecc11fab | 2010-03-04 17:31:47 +0300 | [diff] [blame] | 5734 | inode_init_owner(inode, dir, mode); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5735 | inode_set_bytes(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5736 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5737 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 5738 | struct btrfs_inode_item); |
Li Zefan | 293f7e0 | 2012-07-10 00:58:58 -0600 | [diff] [blame] | 5739 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
| 5740 | sizeof(*inode_item)); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5741 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5742 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5743 | if (name) { |
| 5744 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 5745 | struct btrfs_inode_ref); |
| 5746 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); |
| 5747 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
| 5748 | ptr = (unsigned long)(ref + 1); |
| 5749 | write_extent_buffer(path->nodes[0], name, ptr, name_len); |
| 5750 | } |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 5751 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5752 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 5753 | btrfs_free_path(path); |
| 5754 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5755 | location = &BTRFS_I(inode)->location; |
| 5756 | location->objectid = objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5757 | location->offset = 0; |
| 5758 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 5759 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5760 | btrfs_inherit_iflags(inode, dir); |
| 5761 | |
Al Viro | 569254b | 2011-07-24 17:08:40 -0400 | [diff] [blame] | 5762 | if (S_ISREG(mode)) { |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5763 | if (btrfs_test_opt(root, NODATASUM)) |
| 5764 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 5765 | if (btrfs_test_opt(root, NODATACOW)) |
Josef Bacik | f2bdf9a | 2013-02-21 15:28:28 -0500 | [diff] [blame] | 5766 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
| 5767 | BTRFS_INODE_NODATASUM; |
Chris Mason | 9427216 | 2009-07-02 12:26:06 -0400 | [diff] [blame] | 5768 | } |
| 5769 | |
Filipe David Borba Manana | 778ba82 | 2013-10-06 22:22:33 +0100 | [diff] [blame] | 5770 | btrfs_insert_inode_hash(inode); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5771 | inode_tree_add(inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5772 | |
| 5773 | trace_btrfs_inode_new(inode); |
Chris Mason | 1973f0f | 2011-06-24 13:13:29 -0400 | [diff] [blame] | 5774 | btrfs_set_inode_last_trans(trans, inode); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5775 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5776 | btrfs_update_root_times(trans, root); |
| 5777 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 5778 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
| 5779 | if (ret) |
| 5780 | btrfs_err(root->fs_info, |
| 5781 | "error inheriting props for ino %llu (root %llu): %d", |
| 5782 | btrfs_ino(inode), root->root_key.objectid, ret); |
| 5783 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5784 | return inode; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5785 | fail: |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 5786 | if (dir && name) |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5787 | BTRFS_I(dir)->index_cnt--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5788 | btrfs_free_path(path); |
Shen Feng | 0977143 | 2009-04-02 16:46:06 -0400 | [diff] [blame] | 5789 | iput(inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5790 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5791 | } |
| 5792 | |
| 5793 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 5794 | { |
| 5795 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 5796 | } |
| 5797 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5798 | /* |
| 5799 | * utility function to add 'inode' into 'parent_inode' with |
| 5800 | * a give name and a given sequence number. |
| 5801 | * if 'add_backref' is true, also insert a backref from the |
| 5802 | * inode to the parent directory. |
| 5803 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5804 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 5805 | struct inode *parent_inode, struct inode *inode, |
| 5806 | const char *name, int name_len, int add_backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5807 | { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5808 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5809 | struct btrfs_key key; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5810 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5811 | u64 ino = btrfs_ino(inode); |
| 5812 | u64 parent_ino = btrfs_ino(parent_inode); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5813 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5814 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5815 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
| 5816 | } else { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5817 | key.objectid = ino; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5818 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 5819 | key.offset = 0; |
| 5820 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5821 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5822 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5823 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 5824 | key.objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5825 | parent_ino, index, name, name_len); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5826 | } else if (add_backref) { |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5827 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
| 5828 | parent_ino, index); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5829 | } |
| 5830 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5831 | /* Nothing to clean up yet */ |
| 5832 | if (ret) |
| 5833 | return ret; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 5834 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5835 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
| 5836 | parent_inode, &key, |
| 5837 | btrfs_inode_type(inode), index); |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 5838 | if (ret == -EEXIST || ret == -EOVERFLOW) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5839 | goto fail_dir_item; |
| 5840 | else if (ret) { |
| 5841 | btrfs_abort_transaction(trans, root, ret); |
| 5842 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5843 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5844 | |
| 5845 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
| 5846 | name_len * 2); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 5847 | inode_inc_iversion(parent_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5848 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
| 5849 | ret = btrfs_update_inode(trans, root, parent_inode); |
| 5850 | if (ret) |
| 5851 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5852 | return ret; |
Chris Mason | fe66a05 | 2012-02-20 08:40:56 -0500 | [diff] [blame] | 5853 | |
| 5854 | fail_dir_item: |
| 5855 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
| 5856 | u64 local_index; |
| 5857 | int err; |
| 5858 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, |
| 5859 | key.objectid, root->root_key.objectid, |
| 5860 | parent_ino, &local_index, name, name_len); |
| 5861 | |
| 5862 | } else if (add_backref) { |
| 5863 | u64 local_index; |
| 5864 | int err; |
| 5865 | |
| 5866 | err = btrfs_del_inode_ref(trans, root, name, name_len, |
| 5867 | ino, parent_ino, &local_index); |
| 5868 | } |
| 5869 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5873 | struct inode *dir, struct dentry *dentry, |
| 5874 | struct inode *inode, int backref, u64 index) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5875 | { |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5876 | int err = btrfs_add_link(trans, dir, inode, |
| 5877 | dentry->d_name.name, dentry->d_name.len, |
| 5878 | backref, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5879 | if (err > 0) |
| 5880 | err = -EEXIST; |
| 5881 | return err; |
| 5882 | } |
| 5883 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5884 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 5885 | umode_t mode, dev_t rdev) |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5886 | { |
| 5887 | struct btrfs_trans_handle *trans; |
| 5888 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5889 | struct inode *inode = NULL; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5890 | int err; |
| 5891 | int drop_inode = 0; |
| 5892 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5893 | u64 index = 0; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5894 | |
| 5895 | if (!new_valid_dev(rdev)) |
| 5896 | return -EINVAL; |
| 5897 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5898 | /* |
| 5899 | * 2 for inode item and ref |
| 5900 | * 2 for dir items |
| 5901 | * 1 for xattr if selinux is on |
| 5902 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5903 | trans = btrfs_start_transaction(root, 5); |
| 5904 | if (IS_ERR(trans)) |
| 5905 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5906 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5907 | err = btrfs_find_free_ino(root, &objectid); |
| 5908 | if (err) |
| 5909 | goto out_unlock; |
| 5910 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5911 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5912 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5913 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5914 | if (IS_ERR(inode)) { |
| 5915 | err = PTR_ERR(inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5916 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5917 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5918 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5919 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5920 | if (err) { |
| 5921 | drop_inode = 1; |
| 5922 | goto out_unlock; |
| 5923 | } |
| 5924 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5925 | /* |
| 5926 | * If the active LSM wants to access the inode during |
| 5927 | * d_instantiate it needs these. Smack checks to see |
| 5928 | * if the filesystem supports xattrs by looking at the |
| 5929 | * ops vector. |
| 5930 | */ |
| 5931 | |
| 5932 | inode->i_op = &btrfs_special_inode_operations; |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 5933 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5934 | if (err) |
| 5935 | drop_inode = 1; |
| 5936 | else { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5937 | init_special_inode(inode, inode->i_mode, rdev); |
Yan | 1b4ab1b | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 5938 | btrfs_update_inode(trans, root, inode); |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 5939 | d_instantiate(dentry, inode); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5940 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5941 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 5942 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 5943 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 5944 | btrfs_btree_balance_dirty(root); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5945 | if (drop_inode) { |
| 5946 | inode_dec_link_count(inode); |
| 5947 | iput(inode); |
| 5948 | } |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 5949 | return err; |
| 5950 | } |
| 5951 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5952 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 5953 | umode_t mode, bool excl) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5954 | { |
| 5955 | struct btrfs_trans_handle *trans; |
| 5956 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 5957 | struct inode *inode = NULL; |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5958 | int drop_inode_on_err = 0; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5959 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5960 | u64 objectid; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 5961 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5962 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5963 | /* |
| 5964 | * 2 for inode item and ref |
| 5965 | * 2 for dir items |
| 5966 | * 1 for xattr if selinux is on |
| 5967 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5968 | trans = btrfs_start_transaction(root, 5); |
| 5969 | if (IS_ERR(trans)) |
| 5970 | return PTR_ERR(trans); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5971 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 5972 | err = btrfs_find_free_ino(root, &objectid); |
| 5973 | if (err) |
| 5974 | goto out_unlock; |
| 5975 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 5976 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 5977 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5978 | mode, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5979 | if (IS_ERR(inode)) { |
| 5980 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5981 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 5982 | } |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5983 | drop_inode_on_err = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 5984 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 5985 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 5986 | if (err) |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5987 | goto out_unlock; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 5988 | |
Filipe Brandenburger | 9185aa5 | 2012-11-30 03:40:08 +0000 | [diff] [blame] | 5989 | err = btrfs_update_inode(trans, root, inode); |
| 5990 | if (err) |
| 5991 | goto out_unlock; |
| 5992 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 5993 | /* |
| 5994 | * If the active LSM wants to access the inode during |
| 5995 | * d_instantiate it needs these. Smack checks to see |
| 5996 | * if the filesystem supports xattrs by looking at the |
| 5997 | * ops vector. |
| 5998 | */ |
| 5999 | inode->i_fop = &btrfs_file_operations; |
| 6000 | inode->i_op = &btrfs_file_inode_operations; |
| 6001 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6002 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6003 | if (err) |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6004 | goto out_unlock; |
| 6005 | |
| 6006 | inode->i_mapping->a_ops = &btrfs_aops; |
| 6007 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 6008 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 6009 | d_instantiate(dentry, inode); |
| 6010 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6011 | out_unlock: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6012 | btrfs_end_transaction(trans, root); |
Filipe Brandenburger | 43baa57 | 2012-11-30 03:40:09 +0000 | [diff] [blame] | 6013 | if (err && drop_inode_on_err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6014 | inode_dec_link_count(inode); |
| 6015 | iput(inode); |
| 6016 | } |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6017 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6018 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6019 | return err; |
| 6020 | } |
| 6021 | |
| 6022 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 6023 | struct dentry *dentry) |
| 6024 | { |
| 6025 | struct btrfs_trans_handle *trans; |
| 6026 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6027 | struct inode *inode = old_dentry->d_inode; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6028 | u64 index; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6029 | int err; |
| 6030 | int drop_inode = 0; |
| 6031 | |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6032 | /* do not allow sys_link's with other subvols of the same device */ |
| 6033 | if (root->objectid != BTRFS_I(inode)->root->objectid) |
Mark Fasheh | 3ab3564 | 2011-03-22 17:20:26 +0000 | [diff] [blame] | 6034 | return -EXDEV; |
TARUISI Hiroaki | 4a8be42 | 2009-11-12 07:14:26 +0000 | [diff] [blame] | 6035 | |
Mark Fasheh | f186373 | 2012-08-08 11:32:27 -0700 | [diff] [blame] | 6036 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
Al Viro | c055e99 | 2011-03-04 17:15:18 +0000 | [diff] [blame] | 6037 | return -EMLINK; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6038 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 6039 | err = btrfs_set_inode_index(dir, &index); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6040 | if (err) |
| 6041 | goto fail; |
| 6042 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6043 | /* |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6044 | * 2 items for inode and inode ref |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6045 | * 2 items for dir items |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6046 | * 1 item for parent inode |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6047 | */ |
Miao Xie | 7e6b646 | 2011-02-18 09:21:17 +0000 | [diff] [blame] | 6048 | trans = btrfs_start_transaction(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6049 | if (IS_ERR(trans)) { |
| 6050 | err = PTR_ERR(trans); |
| 6051 | goto fail; |
| 6052 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6053 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 6054 | /* There are several dir indexes for this inode, clear the cache. */ |
| 6055 | BTRFS_I(inode)->dir_index = 0ULL; |
Zach Brown | 8b558c5 | 2013-10-16 12:10:34 -0700 | [diff] [blame] | 6056 | inc_nlink(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 6057 | inode_inc_iversion(inode); |
Miao Xie | 3153495 | 2011-04-13 13:19:21 +0800 | [diff] [blame] | 6058 | inode->i_ctime = CURRENT_TIME; |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 6059 | ihold(inode); |
Josef Bacik | e997615 | 2012-10-11 15:53:56 -0400 | [diff] [blame] | 6060 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6061 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6062 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6063 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6064 | if (err) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6065 | drop_inode = 1; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6066 | } else { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 6067 | struct dentry *parent = dentry->d_parent; |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6068 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6069 | if (err) |
| 6070 | goto fail; |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 6071 | if (inode->i_nlink == 1) { |
| 6072 | /* |
| 6073 | * If new hard link count is 1, it's a file created |
| 6074 | * with open(2) O_TMPFILE flag. |
| 6075 | */ |
| 6076 | err = btrfs_orphan_del(trans, inode); |
| 6077 | if (err) |
| 6078 | goto fail; |
| 6079 | } |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 6080 | d_instantiate(dentry, inode); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 6081 | btrfs_log_new_name(trans, inode, NULL, parent); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 6082 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6083 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6084 | btrfs_end_transaction(trans, root); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6085 | btrfs_balance_delayed_items(root); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 6086 | fail: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6087 | if (drop_inode) { |
| 6088 | inode_dec_link_count(inode); |
| 6089 | iput(inode); |
| 6090 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6091 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6092 | return err; |
| 6093 | } |
| 6094 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 6095 | 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] | 6096 | { |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6097 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6098 | struct btrfs_trans_handle *trans; |
| 6099 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 6100 | int err = 0; |
| 6101 | int drop_on_err = 0; |
Chris Mason | b9d8666 | 2008-05-02 16:13:49 -0400 | [diff] [blame] | 6102 | u64 objectid = 0; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 6103 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6104 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6105 | /* |
| 6106 | * 2 items for inode and ref |
| 6107 | * 2 items for dir items |
| 6108 | * 1 for xattr if selinux is on |
| 6109 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6110 | trans = btrfs_start_transaction(root, 5); |
| 6111 | if (IS_ERR(trans)) |
| 6112 | return PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6113 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 6114 | err = btrfs_find_free_ino(root, &objectid); |
| 6115 | if (err) |
| 6116 | goto out_fail; |
| 6117 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 6118 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6119 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6120 | S_IFDIR | mode, &index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6121 | if (IS_ERR(inode)) { |
| 6122 | err = PTR_ERR(inode); |
| 6123 | goto out_fail; |
| 6124 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6125 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6126 | drop_on_err = 1; |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6127 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 6128 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 6129 | if (err) |
| 6130 | goto out_fail; |
| 6131 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6132 | inode->i_op = &btrfs_dir_inode_operations; |
| 6133 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6134 | |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 6135 | btrfs_i_size_write(inode, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6136 | err = btrfs_update_inode(trans, root, inode); |
| 6137 | if (err) |
| 6138 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6139 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 6140 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
| 6141 | dentry->d_name.len, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6142 | if (err) |
| 6143 | goto out_fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6144 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6145 | d_instantiate(dentry, inode); |
| 6146 | drop_on_err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6147 | |
| 6148 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 6149 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6150 | if (drop_on_err) |
| 6151 | iput(inode); |
Josef Bacik | c581afc | 2014-02-06 16:06:06 -0500 | [diff] [blame] | 6152 | btrfs_balance_delayed_items(root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 6153 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 6154 | return err; |
| 6155 | } |
| 6156 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6157 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
| 6158 | * and an extent that you want to insert, deal with overlap and insert |
| 6159 | * the new extent into the tree. |
| 6160 | */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6161 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 6162 | struct extent_map *existing, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6163 | struct extent_map *em, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6164 | u64 map_start) |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6165 | { |
| 6166 | u64 start_diff; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6167 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6168 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 6169 | start_diff = map_start - em->start; |
| 6170 | em->start = map_start; |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6171 | em->len = existing->start - em->start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6172 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 6173 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6174 | em->block_start += start_diff; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6175 | em->block_len -= start_diff; |
| 6176 | } |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6177 | return add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6178 | } |
| 6179 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6180 | static noinline int uncompress_inline(struct btrfs_path *path, |
| 6181 | struct inode *inode, struct page *page, |
| 6182 | size_t pg_offset, u64 extent_offset, |
| 6183 | struct btrfs_file_extent_item *item) |
| 6184 | { |
| 6185 | int ret; |
| 6186 | struct extent_buffer *leaf = path->nodes[0]; |
| 6187 | char *tmp; |
| 6188 | size_t max_size; |
| 6189 | unsigned long inline_size; |
| 6190 | unsigned long ptr; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6191 | int compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6192 | |
| 6193 | WARN_ON(pg_offset != 0); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6194 | compress_type = btrfs_file_extent_compression(leaf, item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6195 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
| 6196 | inline_size = btrfs_file_extent_inline_item_len(leaf, |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 6197 | btrfs_item_nr(path->slots[0])); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6198 | tmp = kmalloc(inline_size, GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 6199 | if (!tmp) |
| 6200 | return -ENOMEM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6201 | ptr = btrfs_file_extent_inline_start(item); |
| 6202 | |
| 6203 | read_extent_buffer(leaf, tmp, ptr, inline_size); |
| 6204 | |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 6205 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6206 | ret = btrfs_decompress(compress_type, tmp, page, |
| 6207 | extent_offset, inline_size, max_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6208 | kfree(tmp); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6209 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6210 | } |
| 6211 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6212 | /* |
| 6213 | * 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] | 6214 | * the ugly parts come from merging extents from the disk with the in-ram |
| 6215 | * representation. This gets more complex because of the data=ordered code, |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 6216 | * where the in-ram extents might be locked pending data=ordered completion. |
| 6217 | * |
| 6218 | * This also copies inline extents directly into the page. |
| 6219 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6220 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6221 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6222 | size_t pg_offset, u64 start, u64 len, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6223 | int create) |
| 6224 | { |
| 6225 | int ret; |
| 6226 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6227 | u64 extent_start = 0; |
| 6228 | u64 extent_end = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6229 | u64 objectid = btrfs_ino(inode); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6230 | u32 found_type; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6231 | struct btrfs_path *path = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6232 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6233 | struct btrfs_file_extent_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6234 | struct extent_buffer *leaf; |
| 6235 | struct btrfs_key found_key; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6236 | struct extent_map *em = NULL; |
| 6237 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6238 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6239 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6240 | const bool new_inline = !page || create; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6241 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6242 | again: |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6243 | read_lock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6244 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6245 | if (em) |
| 6246 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6247 | read_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6248 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6249 | if (em) { |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6250 | if (em->start > start || em->start + em->len <= start) |
| 6251 | free_extent_map(em); |
| 6252 | else if (em->block_start == EXTENT_MAP_INLINE && page) |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6253 | free_extent_map(em); |
| 6254 | else |
| 6255 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6256 | } |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6257 | em = alloc_extent_map(); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6258 | if (!em) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6259 | err = -ENOMEM; |
| 6260 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6261 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6262 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6263 | em->start = EXTENT_MAP_HOLE; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 6264 | em->orig_start = EXTENT_MAP_HOLE; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6265 | em->len = (u64)-1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6266 | em->block_len = (u64)-1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6267 | |
| 6268 | if (!path) { |
| 6269 | path = btrfs_alloc_path(); |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6270 | if (!path) { |
| 6271 | err = -ENOMEM; |
| 6272 | goto out; |
| 6273 | } |
| 6274 | /* |
| 6275 | * Chances are we'll be called again, so go ahead and do |
| 6276 | * readahead |
| 6277 | */ |
| 6278 | path->reada = 1; |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6279 | } |
| 6280 | |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6281 | ret = btrfs_lookup_file_extent(trans, root, path, |
| 6282 | objectid, start, trans != NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6283 | if (ret < 0) { |
| 6284 | err = ret; |
| 6285 | goto out; |
| 6286 | } |
| 6287 | |
| 6288 | if (ret != 0) { |
| 6289 | if (path->slots[0] == 0) |
| 6290 | goto not_found; |
| 6291 | path->slots[0]--; |
| 6292 | } |
| 6293 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6294 | leaf = path->nodes[0]; |
| 6295 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6296 | struct btrfs_file_extent_item); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6297 | /* are we inside the extent that was found? */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6298 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6299 | found_type = btrfs_key_type(&found_key); |
| 6300 | if (found_key.objectid != objectid || |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6301 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6302 | /* |
| 6303 | * If we backup past the first extent we want to move forward |
| 6304 | * and see if there is an extent in front of us, otherwise we'll |
| 6305 | * say there is a hole for our whole search range which can |
| 6306 | * cause problems. |
| 6307 | */ |
| 6308 | extent_end = start; |
| 6309 | goto next; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6310 | } |
| 6311 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6312 | found_type = btrfs_file_extent_type(leaf, item); |
| 6313 | extent_start = found_key.offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6314 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6315 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6316 | extent_end = extent_start + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6317 | btrfs_file_extent_num_bytes(leaf, item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6318 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 6319 | size_t size; |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6320 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6321 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6322 | } |
Josef Bacik | 25a5034 | 2013-10-14 12:08:38 -0400 | [diff] [blame] | 6323 | next: |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6324 | if (start >= extent_end) { |
| 6325 | path->slots[0]++; |
| 6326 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 6327 | ret = btrfs_next_leaf(root, path); |
| 6328 | if (ret < 0) { |
| 6329 | err = ret; |
| 6330 | goto out; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6331 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6332 | if (ret > 0) |
| 6333 | goto not_found; |
| 6334 | leaf = path->nodes[0]; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6335 | } |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6336 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6337 | if (found_key.objectid != objectid || |
| 6338 | found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 6339 | goto not_found; |
| 6340 | if (start + len <= found_key.offset) |
| 6341 | goto not_found; |
Wang Shilong | e2eca69 | 2014-07-17 11:44:14 +0800 | [diff] [blame] | 6342 | if (start > found_key.offset) |
| 6343 | goto next; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6344 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6345 | em->orig_start = start; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6346 | em->len = found_key.offset - start; |
| 6347 | goto not_found_em; |
| 6348 | } |
| 6349 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6350 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
| 6351 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6352 | if (found_type == BTRFS_FILE_EXTENT_REG || |
| 6353 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6354 | goto insert; |
| 6355 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6356 | unsigned long ptr; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6357 | char *map; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6358 | size_t size; |
| 6359 | size_t extent_offset; |
| 6360 | size_t copy_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6361 | |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 6362 | if (new_inline) |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6363 | goto out; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6364 | |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 6365 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6366 | extent_offset = page_offset(page) + pg_offset - extent_start; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6367 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6368 | size - extent_offset); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 6369 | em->start = extent_start + extent_offset; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6370 | em->len = ALIGN(copy_size, root->sectorsize); |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6371 | em->orig_block_len = em->len; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6372 | em->orig_start = em->start; |
Yan | 689f934 | 2007-10-29 11:41:07 -0400 | [diff] [blame] | 6373 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6374 | if (create == 0 && !PageUptodate(page)) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6375 | if (btrfs_file_extent_compression(leaf, item) != |
| 6376 | BTRFS_COMPRESS_NONE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6377 | ret = uncompress_inline(path, inode, page, |
| 6378 | pg_offset, |
| 6379 | extent_offset, item); |
Zach Brown | 166ae5a | 2014-05-09 17:15:10 -0400 | [diff] [blame] | 6380 | if (ret) { |
| 6381 | err = ret; |
| 6382 | goto out; |
| 6383 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6384 | } else { |
| 6385 | map = kmap(page); |
| 6386 | read_extent_buffer(leaf, map + pg_offset, ptr, |
| 6387 | copy_size); |
Chris Mason | 93c82d5 | 2009-09-11 12:36:29 -0400 | [diff] [blame] | 6388 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
| 6389 | memset(map + pg_offset + copy_size, 0, |
| 6390 | PAGE_CACHE_SIZE - pg_offset - |
| 6391 | copy_size); |
| 6392 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6393 | kunmap(page); |
| 6394 | } |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6395 | flush_dcache_page(page); |
| 6396 | } else if (create && PageUptodate(page)) { |
Jan Schmidt | 6bf7e08 | 2011-12-01 14:35:19 +0100 | [diff] [blame] | 6397 | BUG(); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6398 | if (!trans) { |
| 6399 | kunmap(page); |
| 6400 | free_extent_map(em); |
| 6401 | em = NULL; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6402 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6403 | btrfs_release_path(path); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6404 | trans = btrfs_join_transaction(root); |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 6405 | |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 6406 | if (IS_ERR(trans)) |
| 6407 | return ERR_CAST(trans); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6408 | goto again; |
| 6409 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6410 | map = kmap(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 6411 | write_extent_buffer(leaf, map + pg_offset, ptr, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6412 | copy_size); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6413 | kunmap(page); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 6414 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6415 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6416 | set_extent_uptodate(io_tree, em->start, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 6417 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6418 | goto insert; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6419 | } |
| 6420 | not_found: |
| 6421 | em->start = start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6422 | em->orig_start = start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6423 | em->len = len; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6424 | not_found_em: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6425 | em->block_start = EXTENT_MAP_HOLE; |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 6426 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6427 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6428 | btrfs_release_path(path); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6429 | if (em->start > start || extent_map_end(em) <= start) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6430 | 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] | 6431 | em->start, em->len, start, len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6432 | err = -EIO; |
| 6433 | goto out; |
| 6434 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6435 | |
| 6436 | err = 0; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6437 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6438 | ret = add_extent_mapping(em_tree, em, 0); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6439 | /* it is possible that someone inserted the extent into the tree |
| 6440 | * while we had the lock dropped. It is also possible that |
| 6441 | * an overlapping map exists in the tree |
| 6442 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6443 | if (ret == -EEXIST) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6444 | struct extent_map *existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6445 | |
| 6446 | ret = 0; |
| 6447 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6448 | existing = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | e1c4b74 | 2008-04-22 13:26:46 -0400 | [diff] [blame] | 6449 | if (existing && (existing->start > start || |
| 6450 | existing->start + existing->len <= start)) { |
| 6451 | free_extent_map(existing); |
| 6452 | existing = NULL; |
| 6453 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6454 | if (!existing) { |
| 6455 | existing = lookup_extent_mapping(em_tree, em->start, |
| 6456 | em->len); |
| 6457 | if (existing) { |
| 6458 | err = merge_extent_mapping(em_tree, existing, |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 6459 | em, start); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6460 | free_extent_map(existing); |
| 6461 | if (err) { |
| 6462 | free_extent_map(em); |
| 6463 | em = NULL; |
| 6464 | } |
| 6465 | } else { |
| 6466 | err = -EIO; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6467 | free_extent_map(em); |
| 6468 | em = NULL; |
| 6469 | } |
| 6470 | } else { |
| 6471 | free_extent_map(em); |
| 6472 | em = existing; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6473 | err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6474 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6475 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6476 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6477 | out: |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6478 | |
Steven Rostedt | 4cd8587 | 2013-11-14 22:57:29 -0500 | [diff] [blame] | 6479 | trace_btrfs_get_extent(root, em); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6480 | |
Chris Mason | f421950 | 2008-07-22 11:18:09 -0400 | [diff] [blame] | 6481 | if (path) |
| 6482 | btrfs_free_path(path); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6483 | if (trans) { |
| 6484 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6485 | if (!err) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6486 | err = ret; |
| 6487 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6488 | if (err) { |
| 6489 | free_extent_map(em); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6490 | return ERR_PTR(err); |
| 6491 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6492 | BUG_ON(!em); /* Error is always set */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6493 | return em; |
| 6494 | } |
| 6495 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6496 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
| 6497 | size_t pg_offset, u64 start, u64 len, |
| 6498 | int create) |
| 6499 | { |
| 6500 | struct extent_map *em; |
| 6501 | struct extent_map *hole_em = NULL; |
| 6502 | u64 range_start = start; |
| 6503 | u64 end; |
| 6504 | u64 found; |
| 6505 | u64 found_end; |
| 6506 | int err = 0; |
| 6507 | |
| 6508 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); |
| 6509 | if (IS_ERR(em)) |
| 6510 | return em; |
| 6511 | if (em) { |
| 6512 | /* |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6513 | * if our em maps to |
| 6514 | * - a hole or |
| 6515 | * - a pre-alloc extent, |
| 6516 | * there might actually be delalloc bytes behind it. |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6517 | */ |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6518 | if (em->block_start != EXTENT_MAP_HOLE && |
| 6519 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6520 | return em; |
| 6521 | else |
| 6522 | hole_em = em; |
| 6523 | } |
| 6524 | |
| 6525 | /* check to see if we've wrapped (len == -1 or similar) */ |
| 6526 | end = start + len; |
| 6527 | if (end < start) |
| 6528 | end = (u64)-1; |
| 6529 | else |
| 6530 | end -= 1; |
| 6531 | |
| 6532 | em = NULL; |
| 6533 | |
| 6534 | /* ok, we didn't find anything, lets look for delalloc */ |
| 6535 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, |
| 6536 | end, len, EXTENT_DELALLOC, 1); |
| 6537 | found_end = range_start + found; |
| 6538 | if (found_end < range_start) |
| 6539 | found_end = (u64)-1; |
| 6540 | |
| 6541 | /* |
| 6542 | * we didn't find anything useful, return |
| 6543 | * the original results from get_extent() |
| 6544 | */ |
| 6545 | if (range_start > end || found_end <= start) { |
| 6546 | em = hole_em; |
| 6547 | hole_em = NULL; |
| 6548 | goto out; |
| 6549 | } |
| 6550 | |
| 6551 | /* adjust the range_start to make sure it doesn't |
| 6552 | * go backwards from the start they passed in |
| 6553 | */ |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 6554 | range_start = max(start, range_start); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6555 | found = found_end - range_start; |
| 6556 | |
| 6557 | if (found > 0) { |
| 6558 | u64 hole_start = start; |
| 6559 | u64 hole_len = len; |
| 6560 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6561 | em = alloc_extent_map(); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6562 | if (!em) { |
| 6563 | err = -ENOMEM; |
| 6564 | goto out; |
| 6565 | } |
| 6566 | /* |
| 6567 | * when btrfs_get_extent can't find anything it |
| 6568 | * returns one huge hole |
| 6569 | * |
| 6570 | * make sure what it found really fits our range, and |
| 6571 | * adjust to make sure it is based on the start from |
| 6572 | * the caller |
| 6573 | */ |
| 6574 | if (hole_em) { |
| 6575 | u64 calc_end = extent_map_end(hole_em); |
| 6576 | |
| 6577 | if (calc_end <= start || (hole_em->start > end)) { |
| 6578 | free_extent_map(hole_em); |
| 6579 | hole_em = NULL; |
| 6580 | } else { |
| 6581 | hole_start = max(hole_em->start, start); |
| 6582 | hole_len = calc_end - hole_start; |
| 6583 | } |
| 6584 | } |
| 6585 | em->bdev = NULL; |
| 6586 | if (hole_em && range_start > hole_start) { |
| 6587 | /* our hole starts before our delalloc, so we |
| 6588 | * have to return just the parts of the hole |
| 6589 | * that go until the delalloc starts |
| 6590 | */ |
| 6591 | em->len = min(hole_len, |
| 6592 | range_start - hole_start); |
| 6593 | em->start = hole_start; |
| 6594 | em->orig_start = hole_start; |
| 6595 | /* |
| 6596 | * don't adjust block start at all, |
| 6597 | * it is fixed at EXTENT_MAP_HOLE |
| 6598 | */ |
| 6599 | em->block_start = hole_em->block_start; |
| 6600 | em->block_len = hole_len; |
Liu Bo | f9e4fb5 | 2013-01-07 10:10:12 +0000 | [diff] [blame] | 6601 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
| 6602 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 6603 | } else { |
| 6604 | em->start = range_start; |
| 6605 | em->len = found; |
| 6606 | em->orig_start = range_start; |
| 6607 | em->block_start = EXTENT_MAP_DELALLOC; |
| 6608 | em->block_len = found; |
| 6609 | } |
| 6610 | } else if (hole_em) { |
| 6611 | return hole_em; |
| 6612 | } |
| 6613 | out: |
| 6614 | |
| 6615 | free_extent_map(hole_em); |
| 6616 | if (err) { |
| 6617 | free_extent_map(em); |
| 6618 | return ERR_PTR(err); |
| 6619 | } |
| 6620 | return em; |
| 6621 | } |
| 6622 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6623 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
| 6624 | u64 start, u64 len) |
| 6625 | { |
| 6626 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6627 | struct extent_map *em; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6628 | struct btrfs_key ins; |
| 6629 | u64 alloc_hint; |
| 6630 | int ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6631 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6632 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6633 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6634 | alloc_hint, &ins, 1, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6635 | if (ret) |
| 6636 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6637 | |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6638 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6639 | ins.offset, ins.offset, ins.offset, 0); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6640 | if (IS_ERR(em)) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6641 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6642 | return em; |
| 6643 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6644 | |
| 6645 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, |
| 6646 | ins.offset, ins.offset, 0); |
| 6647 | if (ret) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6648 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6649 | free_extent_map(em); |
| 6650 | return ERR_PTR(ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6651 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6652 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6653 | return em; |
| 6654 | } |
| 6655 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6656 | /* |
| 6657 | * returns 1 when the nocow is safe, < 1 on error, 0 if the |
| 6658 | * block must be cow'd |
| 6659 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6660 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6661 | u64 *orig_start, u64 *orig_block_len, |
| 6662 | u64 *ram_bytes) |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6663 | { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6664 | struct btrfs_trans_handle *trans; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6665 | struct btrfs_path *path; |
| 6666 | int ret; |
| 6667 | struct extent_buffer *leaf; |
| 6668 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6669 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6670 | struct btrfs_file_extent_item *fi; |
| 6671 | struct btrfs_key key; |
| 6672 | u64 disk_bytenr; |
| 6673 | u64 backref_offset; |
| 6674 | u64 extent_end; |
| 6675 | u64 num_bytes; |
| 6676 | int slot; |
| 6677 | int found_type; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6678 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6679 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6680 | path = btrfs_alloc_path(); |
| 6681 | if (!path) |
| 6682 | return -ENOMEM; |
| 6683 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6684 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6685 | offset, 0); |
| 6686 | if (ret < 0) |
| 6687 | goto out; |
| 6688 | |
| 6689 | slot = path->slots[0]; |
| 6690 | if (ret == 1) { |
| 6691 | if (slot == 0) { |
| 6692 | /* can't find the item, must cow */ |
| 6693 | ret = 0; |
| 6694 | goto out; |
| 6695 | } |
| 6696 | slot--; |
| 6697 | } |
| 6698 | ret = 0; |
| 6699 | leaf = path->nodes[0]; |
| 6700 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 6701 | if (key.objectid != btrfs_ino(inode) || |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6702 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6703 | /* not our file or wrong item type, must cow */ |
| 6704 | goto out; |
| 6705 | } |
| 6706 | |
| 6707 | if (key.offset > offset) { |
| 6708 | /* Wrong offset, must cow */ |
| 6709 | goto out; |
| 6710 | } |
| 6711 | |
| 6712 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 6713 | found_type = btrfs_file_extent_type(leaf, fi); |
| 6714 | if (found_type != BTRFS_FILE_EXTENT_REG && |
| 6715 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { |
| 6716 | /* not a regular extent, must cow */ |
| 6717 | goto out; |
| 6718 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6719 | |
| 6720 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) |
| 6721 | goto out; |
| 6722 | |
Miao Xie | e77751a | 2013-12-27 21:11:50 +0800 | [diff] [blame] | 6723 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 6724 | if (extent_end <= offset) |
| 6725 | goto out; |
| 6726 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6727 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6728 | if (disk_bytenr == 0) |
| 6729 | goto out; |
| 6730 | |
| 6731 | if (btrfs_file_extent_compression(leaf, fi) || |
| 6732 | btrfs_file_extent_encryption(leaf, fi) || |
| 6733 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 6734 | goto out; |
| 6735 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6736 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
| 6737 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 6738 | if (orig_start) { |
| 6739 | *orig_start = key.offset - backref_offset; |
| 6740 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6741 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6742 | } |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6743 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6744 | if (btrfs_extent_readonly(root, disk_bytenr)) |
| 6745 | goto out; |
Miao Xie | 7b2b708 | 2014-02-27 13:58:05 +0800 | [diff] [blame] | 6746 | |
| 6747 | num_bytes = min(offset + *len, extent_end) - offset; |
| 6748 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 6749 | u64 range_end; |
| 6750 | |
| 6751 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; |
| 6752 | ret = test_range_bit(io_tree, offset, range_end, |
| 6753 | EXTENT_DELALLOC, 0, NULL); |
| 6754 | if (ret) { |
| 6755 | ret = -EAGAIN; |
| 6756 | goto out; |
| 6757 | } |
| 6758 | } |
| 6759 | |
Josef Bacik | 1bda19e | 2013-10-18 12:10:36 -0400 | [diff] [blame] | 6760 | btrfs_release_path(path); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6761 | |
| 6762 | /* |
| 6763 | * look for other files referencing this extent, if we |
| 6764 | * find any we must cow |
| 6765 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6766 | trans = btrfs_join_transaction(root); |
| 6767 | if (IS_ERR(trans)) { |
| 6768 | ret = 0; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6769 | goto out; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), |
| 6773 | key.offset - backref_offset, disk_bytenr); |
| 6774 | btrfs_end_transaction(trans, root); |
| 6775 | if (ret) { |
| 6776 | ret = 0; |
| 6777 | goto out; |
| 6778 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6779 | |
| 6780 | /* |
| 6781 | * adjust disk_bytenr and num_bytes to cover just the bytes |
| 6782 | * in this extent we are about to write. If there |
| 6783 | * are any csums in that range we have to cow in order |
| 6784 | * to keep the csums correct |
| 6785 | */ |
| 6786 | disk_bytenr += backref_offset; |
| 6787 | disk_bytenr += offset - key.offset; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6788 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
| 6789 | goto out; |
| 6790 | /* |
| 6791 | * all of the above have passed, it is safe to overwrite this extent |
| 6792 | * without cow |
| 6793 | */ |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 6794 | *len = num_bytes; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 6795 | ret = 1; |
| 6796 | out: |
| 6797 | btrfs_free_path(path); |
| 6798 | return ret; |
| 6799 | } |
| 6800 | |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6801 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
| 6802 | { |
| 6803 | struct radix_tree_root *root = &inode->i_mapping->page_tree; |
| 6804 | int found = false; |
| 6805 | void **pagep = NULL; |
| 6806 | struct page *page = NULL; |
| 6807 | int start_idx; |
| 6808 | int end_idx; |
| 6809 | |
| 6810 | start_idx = start >> PAGE_CACHE_SHIFT; |
| 6811 | |
| 6812 | /* |
| 6813 | * end is the last byte in the last page. end == start is legal |
| 6814 | */ |
| 6815 | end_idx = end >> PAGE_CACHE_SHIFT; |
| 6816 | |
| 6817 | rcu_read_lock(); |
| 6818 | |
| 6819 | /* Most of the code in this while loop is lifted from |
| 6820 | * find_get_page. It's been modified to begin searching from a |
| 6821 | * page and return just the first page found in that range. If the |
| 6822 | * found idx is less than or equal to the end idx then we know that |
| 6823 | * a page exists. If no pages are found or if those pages are |
| 6824 | * outside of the range then we're fine (yay!) */ |
| 6825 | while (page == NULL && |
| 6826 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { |
| 6827 | page = radix_tree_deref_slot(pagep); |
| 6828 | if (unlikely(!page)) |
| 6829 | break; |
| 6830 | |
| 6831 | if (radix_tree_exception(page)) { |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6832 | if (radix_tree_deref_retry(page)) { |
| 6833 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6834 | continue; |
Filipe Manana | 809f901 | 2014-06-05 13:22:25 +0100 | [diff] [blame] | 6835 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6836 | /* |
| 6837 | * Otherwise, shmem/tmpfs must be storing a swap entry |
| 6838 | * here as an exceptional entry: so return it without |
| 6839 | * attempting to raise page count. |
| 6840 | */ |
Filipe Manana | 6fdef6d | 2014-06-05 13:22:26 +0100 | [diff] [blame] | 6841 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6842 | break; /* TODO: Is this relevant for this use case? */ |
| 6843 | } |
| 6844 | |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6845 | if (!page_cache_get_speculative(page)) { |
| 6846 | page = NULL; |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6847 | continue; |
Filipe Manana | 9140515 | 2014-06-05 13:22:24 +0100 | [diff] [blame] | 6848 | } |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6849 | |
| 6850 | /* |
| 6851 | * Has the page moved? |
| 6852 | * This is part of the lockless pagecache protocol. See |
| 6853 | * include/linux/pagemap.h for details. |
| 6854 | */ |
| 6855 | if (unlikely(page != *pagep)) { |
| 6856 | page_cache_release(page); |
| 6857 | page = NULL; |
| 6858 | } |
| 6859 | } |
| 6860 | |
| 6861 | if (page) { |
| 6862 | if (page->index <= end_idx) |
| 6863 | found = true; |
| 6864 | page_cache_release(page); |
| 6865 | } |
| 6866 | |
| 6867 | rcu_read_unlock(); |
| 6868 | return found; |
| 6869 | } |
| 6870 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6871 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
| 6872 | struct extent_state **cached_state, int writing) |
| 6873 | { |
| 6874 | struct btrfs_ordered_extent *ordered; |
| 6875 | int ret = 0; |
| 6876 | |
| 6877 | while (1) { |
| 6878 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6879 | 0, cached_state); |
| 6880 | /* |
| 6881 | * We're concerned with the entire range that we're going to be |
| 6882 | * doing DIO to, so we need to make sure theres no ordered |
| 6883 | * extents in this range. |
| 6884 | */ |
| 6885 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 6886 | lockend - lockstart + 1); |
| 6887 | |
| 6888 | /* |
| 6889 | * We need to make sure there are no buffered pages in this |
| 6890 | * range either, we could have raced between the invalidate in |
| 6891 | * generic_file_direct_write and locking the extent. The |
| 6892 | * invalidate needs to happen so that reads after a write do not |
| 6893 | * get stale data. |
| 6894 | */ |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 6895 | if (!ordered && |
| 6896 | (!writing || |
| 6897 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6898 | break; |
| 6899 | |
| 6900 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 6901 | cached_state, GFP_NOFS); |
| 6902 | |
| 6903 | if (ordered) { |
| 6904 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6905 | btrfs_put_ordered_extent(ordered); |
| 6906 | } else { |
| 6907 | /* Screw you mmap */ |
| 6908 | ret = filemap_write_and_wait_range(inode->i_mapping, |
| 6909 | lockstart, |
| 6910 | lockend); |
| 6911 | if (ret) |
| 6912 | break; |
| 6913 | |
| 6914 | /* |
| 6915 | * If we found a page that couldn't be invalidated just |
| 6916 | * fall back to buffered. |
| 6917 | */ |
| 6918 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6919 | lockstart >> PAGE_CACHE_SHIFT, |
| 6920 | lockend >> PAGE_CACHE_SHIFT); |
| 6921 | if (ret) |
| 6922 | break; |
| 6923 | } |
| 6924 | |
| 6925 | cond_resched(); |
| 6926 | } |
| 6927 | |
| 6928 | return ret; |
| 6929 | } |
| 6930 | |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6931 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
| 6932 | u64 len, u64 orig_start, |
| 6933 | u64 block_start, u64 block_len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6934 | u64 orig_block_len, u64 ram_bytes, |
| 6935 | int type) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6936 | { |
| 6937 | struct extent_map_tree *em_tree; |
| 6938 | struct extent_map *em; |
| 6939 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6940 | int ret; |
| 6941 | |
| 6942 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 6943 | em = alloc_extent_map(); |
| 6944 | if (!em) |
| 6945 | return ERR_PTR(-ENOMEM); |
| 6946 | |
| 6947 | em->start = start; |
| 6948 | em->orig_start = orig_start; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 6949 | em->mod_start = start; |
| 6950 | em->mod_len = len; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6951 | em->len = len; |
| 6952 | em->block_len = block_len; |
| 6953 | em->block_start = block_start; |
| 6954 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 6955 | em->orig_block_len = orig_block_len; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 6956 | em->ram_bytes = ram_bytes; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6957 | em->generation = -1; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6958 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6959 | if (type == BTRFS_ORDERED_PREALLOC) |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 6960 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6961 | |
| 6962 | do { |
| 6963 | btrfs_drop_extent_cache(inode, em->start, |
| 6964 | em->start + em->len - 1, 0); |
| 6965 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6966 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 6967 | write_unlock(&em_tree->lock); |
| 6968 | } while (ret == -EEXIST); |
| 6969 | |
| 6970 | if (ret) { |
| 6971 | free_extent_map(em); |
| 6972 | return ERR_PTR(ret); |
| 6973 | } |
| 6974 | |
| 6975 | return em; |
| 6976 | } |
| 6977 | |
| 6978 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6979 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
| 6980 | struct buffer_head *bh_result, int create) |
| 6981 | { |
| 6982 | struct extent_map *em; |
| 6983 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6984 | struct extent_state *cached_state = NULL; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6985 | u64 start = iblock << inode->i_blkbits; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6986 | u64 lockstart, lockend; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 6987 | u64 len = bh_result->b_size; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6988 | int unlock_bits = EXTENT_LOCKED; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6989 | int ret = 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6990 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6991 | if (create) |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6992 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 6993 | else |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6994 | len = min_t(u64, len, root->sectorsize); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6995 | |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 6996 | lockstart = start; |
| 6997 | lockend = start + len - 1; |
| 6998 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 6999 | /* |
| 7000 | * If this errors out it's because we couldn't invalidate pagecache for |
| 7001 | * this range and we need to fallback to buffered. |
| 7002 | */ |
| 7003 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) |
| 7004 | return -ENOTBLK; |
| 7005 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7006 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7007 | if (IS_ERR(em)) { |
| 7008 | ret = PTR_ERR(em); |
| 7009 | goto unlock_err; |
| 7010 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7011 | |
| 7012 | /* |
| 7013 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
| 7014 | * io. INLINE is special, and we could probably kludge it in here, but |
| 7015 | * it's still buffered so for safety lets just fall back to the generic |
| 7016 | * buffered path. |
| 7017 | * |
| 7018 | * For COMPRESSED we _have_ to read the entire extent in so we can |
| 7019 | * decompress it, so there will be buffering required no matter what we |
| 7020 | * do, so go ahead and fallback to buffered. |
| 7021 | * |
| 7022 | * We return -ENOTBLK because thats what makes DIO go ahead and go back |
| 7023 | * to buffered IO. Don't blame me, this is the price we pay for using |
| 7024 | * the generic code. |
| 7025 | */ |
| 7026 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
| 7027 | em->block_start == EXTENT_MAP_INLINE) { |
| 7028 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7029 | ret = -ENOTBLK; |
| 7030 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7031 | } |
| 7032 | |
| 7033 | /* Just a good old fashioned hole, return */ |
| 7034 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
| 7035 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 7036 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7037 | goto unlock_err; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7038 | } |
| 7039 | |
| 7040 | /* |
| 7041 | * We don't allocate a new extent in the following cases |
| 7042 | * |
| 7043 | * 1) The inode is marked as NODATACOW. In this case we'll just use the |
| 7044 | * existing extent. |
| 7045 | * 2) The extent is marked as PREALLOC. We're good to go here and can |
| 7046 | * just use the extent. |
| 7047 | * |
| 7048 | */ |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7049 | if (!create) { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7050 | len = min(len, em->len - (start - em->start)); |
| 7051 | lockstart = start + len; |
| 7052 | goto unlock; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7053 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7054 | |
| 7055 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
| 7056 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && |
| 7057 | em->block_start != EXTENT_MAP_HOLE)) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7058 | int type; |
| 7059 | int ret; |
Josef Bacik | eb384b5 | 2013-04-24 16:32:55 -0400 | [diff] [blame] | 7060 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7061 | |
| 7062 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7063 | type = BTRFS_ORDERED_PREALLOC; |
| 7064 | else |
| 7065 | type = BTRFS_ORDERED_NOCOW; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7066 | len = min(len, em->len - (start - em->start)); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7067 | block_start = em->block_start + (start - em->start); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7068 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7069 | if (can_nocow_extent(inode, start, &len, &orig_start, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 7070 | &orig_block_len, &ram_bytes) == 1) { |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7071 | if (type == BTRFS_ORDERED_PREALLOC) { |
| 7072 | free_extent_map(em); |
| 7073 | em = create_pinned_em(inode, start, len, |
| 7074 | orig_start, |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 7075 | block_start, len, |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 7076 | orig_block_len, |
| 7077 | ram_bytes, type); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7078 | if (IS_ERR(em)) |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7079 | goto unlock_err; |
Josef Bacik | 69ffb54 | 2012-09-11 15:40:07 -0400 | [diff] [blame] | 7080 | } |
| 7081 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7082 | ret = btrfs_add_ordered_extent_dio(inode, start, |
| 7083 | block_start, len, len, type); |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7084 | if (ret) { |
| 7085 | free_extent_map(em); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7086 | goto unlock_err; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7087 | } |
| 7088 | goto unlock; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7089 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7090 | } |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7091 | |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7092 | /* |
| 7093 | * this will cow the extent, reset the len in case we changed |
| 7094 | * it above |
| 7095 | */ |
| 7096 | len = bh_result->b_size; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 7097 | free_extent_map(em); |
| 7098 | em = btrfs_new_extent_direct(inode, start, len); |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7099 | if (IS_ERR(em)) { |
| 7100 | ret = PTR_ERR(em); |
| 7101 | goto unlock_err; |
| 7102 | } |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7103 | len = min(len, em->len - (start - em->start)); |
| 7104 | unlock: |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7105 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
| 7106 | inode->i_blkbits; |
Chris Mason | 46bfbb5 | 2010-05-26 11:04:10 -0400 | [diff] [blame] | 7107 | bh_result->b_size = len; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7108 | bh_result->b_bdev = em->bdev; |
| 7109 | set_buffer_mapped(bh_result); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7110 | if (create) { |
| 7111 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 7112 | set_buffer_new(bh_result); |
| 7113 | |
| 7114 | /* |
| 7115 | * Need to update the i_size under the extent lock so buffered |
| 7116 | * readers will get the updated i_size when we unlock. |
| 7117 | */ |
| 7118 | if (start + len > i_size_read(inode)) |
| 7119 | i_size_write(inode, start + len); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7120 | |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7121 | spin_lock(&BTRFS_I(inode)->lock); |
| 7122 | BTRFS_I(inode)->outstanding_extents++; |
| 7123 | spin_unlock(&BTRFS_I(inode)->lock); |
| 7124 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7125 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7126 | lockstart + len - 1, EXTENT_DELALLOC, NULL, |
| 7127 | &cached_state, GFP_NOFS); |
| 7128 | BUG_ON(ret); |
Josef Bacik | c3473e8 | 2012-06-19 10:59:00 -0400 | [diff] [blame] | 7129 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7130 | |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7131 | /* |
| 7132 | * In the case of write we need to clear and unlock the entire range, |
| 7133 | * in the case of read we need to unlock only the end area that we |
| 7134 | * aren't using if there is any left over space. |
| 7135 | */ |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7136 | if (lockstart < lockend) { |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7137 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 7138 | lockend, unlock_bits, 1, 0, |
| 7139 | &cached_state, GFP_NOFS); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7140 | } else { |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7141 | free_extent_state(cached_state); |
Liu Bo | 24c03fa | 2012-08-22 20:10:38 -0600 | [diff] [blame] | 7142 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7143 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7144 | free_extent_map(em); |
| 7145 | |
| 7146 | return 0; |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7147 | |
| 7148 | unlock_err: |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7149 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 7150 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); |
| 7151 | return ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7152 | } |
| 7153 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7154 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
| 7155 | { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7156 | struct btrfs_dio_private *dip = bio->bi_private; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7157 | struct bio_vec *bvec; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7158 | struct inode *inode = dip->inode; |
| 7159 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7160 | struct bio *dio_bio; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7161 | u32 *csums = (u32 *)dip->csum; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7162 | u64 start; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7163 | int i; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7164 | |
| 7165 | start = dip->logical_offset; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7166 | bio_for_each_segment_all(bvec, bio, i) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7167 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
| 7168 | struct page *page = bvec->bv_page; |
| 7169 | char *kaddr; |
| 7170 | u32 csum = ~(u32)0; |
| 7171 | unsigned long flags; |
| 7172 | |
| 7173 | local_irq_save(flags); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 7174 | kaddr = kmap_atomic(page); |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 7175 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7176 | csum, bvec->bv_len); |
| 7177 | btrfs_csum_final(csum, (char *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 7178 | kunmap_atomic(kaddr); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7179 | local_irq_restore(flags); |
| 7180 | |
| 7181 | flush_dcache_page(bvec->bv_page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7182 | if (csum != csums[i]) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7183 | 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] | 7184 | btrfs_ino(inode), start, csum, |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7185 | csums[i]); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7186 | err = -EIO; |
| 7187 | } |
| 7188 | } |
| 7189 | |
| 7190 | start += bvec->bv_len; |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 7191 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7192 | |
| 7193 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7194 | dip->logical_offset + dip->bytes - 1); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7195 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7196 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7197 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7198 | |
| 7199 | /* If we had a csum failure make sure to clear the uptodate flag */ |
| 7200 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7201 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7202 | dio_end_io(dio_bio, err); |
| 7203 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | static void btrfs_endio_direct_write(struct bio *bio, int err) |
| 7207 | { |
| 7208 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7209 | struct inode *inode = dip->inode; |
| 7210 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7211 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7212 | u64 ordered_offset = dip->logical_offset; |
| 7213 | u64 ordered_bytes = dip->bytes; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7214 | struct bio *dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7215 | int ret; |
| 7216 | |
| 7217 | if (err) |
| 7218 | goto out_done; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7219 | again: |
| 7220 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, |
| 7221 | &ordered_offset, |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7222 | ordered_bytes, !err); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7223 | if (!ret) |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7224 | goto out_test; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7225 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 7226 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
| 7227 | finish_ordered_fn, NULL, NULL); |
Qu Wenruo | fccb5d8 | 2014-02-28 10:46:10 +0800 | [diff] [blame] | 7228 | btrfs_queue_work(root->fs_info->endio_write_workers, |
| 7229 | &ordered->work); |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7230 | out_test: |
| 7231 | /* |
| 7232 | * our bio might span multiple ordered extents. If we haven't |
| 7233 | * completed the accounting for the whole dio, go back and try again |
| 7234 | */ |
| 7235 | if (ordered_offset < dip->logical_offset + dip->bytes) { |
| 7236 | ordered_bytes = dip->logical_offset + dip->bytes - |
| 7237 | ordered_offset; |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7238 | ordered = NULL; |
Chris Mason | 163cf09 | 2010-11-28 19:56:33 -0500 | [diff] [blame] | 7239 | goto again; |
| 7240 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7241 | out_done: |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7242 | dio_bio = dip->dio_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7243 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7244 | kfree(dip); |
Josef Bacik | c0da7aa | 2011-03-22 11:05:07 -0400 | [diff] [blame] | 7245 | |
| 7246 | /* If we had an error make sure to clear the uptodate flag */ |
| 7247 | if (err) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7248 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
| 7249 | dio_end_io(dio_bio, err); |
| 7250 | bio_put(bio); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7251 | } |
| 7252 | |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7253 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
| 7254 | struct bio *bio, int mirror_num, |
| 7255 | unsigned long bio_flags, u64 offset) |
| 7256 | { |
| 7257 | int ret; |
| 7258 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7259 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7260 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7261 | return 0; |
| 7262 | } |
| 7263 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7264 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
| 7265 | { |
| 7266 | struct btrfs_dio_private *dip = bio->bi_private; |
| 7267 | |
| 7268 | if (err) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7269 | btrfs_err(BTRFS_I(dip->inode)->root->fs_info, |
| 7270 | "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] | 7271 | btrfs_ino(dip->inode), bio->bi_rw, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7272 | (unsigned long long)bio->bi_iter.bi_sector, |
| 7273 | bio->bi_iter.bi_size, err); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7274 | dip->errors = 1; |
| 7275 | |
| 7276 | /* |
| 7277 | * before atomic variable goto zero, we must make sure |
| 7278 | * dip->errors is perceived to be set. |
| 7279 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7280 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7281 | } |
| 7282 | |
| 7283 | /* if there are more bios still pending for this dio, just exit */ |
| 7284 | if (!atomic_dec_and_test(&dip->pending_bios)) |
| 7285 | goto out; |
| 7286 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7287 | if (dip->errors) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7288 | bio_io_error(dip->orig_bio); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7289 | } else { |
| 7290 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7291 | bio_endio(dip->orig_bio, 0); |
| 7292 | } |
| 7293 | out: |
| 7294 | bio_put(bio); |
| 7295 | } |
| 7296 | |
| 7297 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, |
| 7298 | u64 first_sector, gfp_t gfp_flags) |
| 7299 | { |
| 7300 | int nr_vecs = bio_get_nr_vecs(bdev); |
| 7301 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); |
| 7302 | } |
| 7303 | |
| 7304 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
| 7305 | int rw, u64 file_offset, int skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7306 | int async_submit) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7307 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7308 | struct btrfs_dio_private *dip = bio->bi_private; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7309 | int write = rw & REQ_WRITE; |
| 7310 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7311 | int ret; |
| 7312 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 7313 | if (async_submit) |
| 7314 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); |
| 7315 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7316 | bio_get(bio); |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7317 | |
| 7318 | if (!write) { |
| 7319 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
| 7320 | if (ret) |
| 7321 | goto err; |
| 7322 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7323 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7324 | if (skip_sum) |
| 7325 | goto map; |
| 7326 | |
| 7327 | if (write && async_submit) { |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7328 | ret = btrfs_wq_submit_bio(root->fs_info, |
| 7329 | inode, rw, bio, 0, 0, |
| 7330 | file_offset, |
| 7331 | __btrfs_submit_bio_start_direct_io, |
| 7332 | __btrfs_submit_bio_done); |
| 7333 | goto err; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7334 | } else if (write) { |
| 7335 | /* |
| 7336 | * If we aren't doing async submit, calculate the csum of the |
| 7337 | * bio now. |
| 7338 | */ |
| 7339 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); |
| 7340 | if (ret) |
| 7341 | goto err; |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7342 | } else if (!skip_sum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7343 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
| 7344 | file_offset); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 7345 | if (ret) |
| 7346 | goto err; |
| 7347 | } |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7348 | |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7349 | map: |
| 7350 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7351 | err: |
| 7352 | bio_put(bio); |
| 7353 | return ret; |
| 7354 | } |
| 7355 | |
| 7356 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, |
| 7357 | int skip_sum) |
| 7358 | { |
| 7359 | struct inode *inode = dip->inode; |
| 7360 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7361 | struct bio *bio; |
| 7362 | struct bio *orig_bio = dip->orig_bio; |
| 7363 | struct bio_vec *bvec = orig_bio->bi_io_vec; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7364 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7365 | u64 file_offset = dip->logical_offset; |
| 7366 | u64 submit_len = 0; |
| 7367 | u64 map_length; |
| 7368 | int nr_pages = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7369 | int ret = 0; |
Josef Bacik | 1ae3993 | 2011-04-06 14:41:34 -0400 | [diff] [blame] | 7370 | int async_submit = 0; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7371 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7372 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7373 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7374 | &map_length, NULL, 0); |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7375 | if (ret) |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7376 | return -EIO; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7377 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7378 | if (map_length >= orig_bio->bi_iter.bi_size) { |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7379 | bio = orig_bio; |
| 7380 | goto submit; |
| 7381 | } |
| 7382 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7383 | /* async crcs make it difficult to collect full stripe writes. */ |
| 7384 | if (btrfs_get_alloc_profile(root, 1) & |
| 7385 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) |
| 7386 | async_submit = 0; |
| 7387 | else |
| 7388 | async_submit = 1; |
| 7389 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7390 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
| 7391 | if (!bio) |
| 7392 | return -ENOMEM; |
Miao Xie | 7a5c3c9 | 2014-06-17 18:58:59 +0800 | [diff] [blame] | 7393 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7394 | bio->bi_private = dip; |
| 7395 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7396 | atomic_inc(&dip->pending_bios); |
| 7397 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7398 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
| 7399 | if (unlikely(map_length < submit_len + bvec->bv_len || |
| 7400 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 7401 | bvec->bv_offset) < bvec->bv_len)) { |
| 7402 | /* |
| 7403 | * inc the count before we submit the bio so |
| 7404 | * we know the end IO handler won't happen before |
| 7405 | * we inc the count. Otherwise, the dip might get freed |
| 7406 | * before we're done setting it up |
| 7407 | */ |
| 7408 | atomic_inc(&dip->pending_bios); |
| 7409 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
| 7410 | file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7411 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7412 | if (ret) { |
| 7413 | bio_put(bio); |
| 7414 | atomic_dec(&dip->pending_bios); |
| 7415 | goto out_err; |
| 7416 | } |
| 7417 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7418 | start_sector += submit_len >> 9; |
| 7419 | file_offset += submit_len; |
| 7420 | |
| 7421 | submit_len = 0; |
| 7422 | nr_pages = 0; |
| 7423 | |
| 7424 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, |
| 7425 | start_sector, GFP_NOFS); |
| 7426 | if (!bio) |
| 7427 | goto out_err; |
| 7428 | bio->bi_private = dip; |
| 7429 | bio->bi_end_io = btrfs_end_dio_bio; |
| 7430 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7431 | map_length = orig_bio->bi_iter.bi_size; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7432 | ret = btrfs_map_block(root->fs_info, rw, |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 7433 | start_sector << 9, |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7434 | &map_length, NULL, 0); |
| 7435 | if (ret) { |
| 7436 | bio_put(bio); |
| 7437 | goto out_err; |
| 7438 | } |
| 7439 | } else { |
| 7440 | submit_len += bvec->bv_len; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7441 | nr_pages++; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7442 | bvec++; |
| 7443 | } |
| 7444 | } |
| 7445 | |
Josef Bacik | 02f57c7 | 2011-04-06 14:25:44 -0400 | [diff] [blame] | 7446 | submit: |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7447 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
Josef Bacik | c329861 | 2012-08-03 16:49:19 -0400 | [diff] [blame] | 7448 | async_submit); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7449 | if (!ret) |
| 7450 | return 0; |
| 7451 | |
| 7452 | bio_put(bio); |
| 7453 | out_err: |
| 7454 | dip->errors = 1; |
| 7455 | /* |
| 7456 | * before atomic variable goto zero, we must |
| 7457 | * make sure dip->errors is perceived to be set. |
| 7458 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7459 | smp_mb__before_atomic(); |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7460 | if (atomic_dec_and_test(&dip->pending_bios)) |
| 7461 | bio_io_error(dip->orig_bio); |
| 7462 | |
| 7463 | /* bio_end_io() will handle error, so we needn't return it */ |
| 7464 | return 0; |
| 7465 | } |
| 7466 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7467 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
| 7468 | struct inode *inode, loff_t file_offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7469 | { |
| 7470 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7471 | struct btrfs_dio_private *dip; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7472 | struct bio *io_bio; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7473 | int skip_sum; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7474 | int sum_len; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 7475 | int write = rw & REQ_WRITE; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7476 | int ret = 0; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7477 | u16 csum_size; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7478 | |
| 7479 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
| 7480 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7481 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7482 | if (!io_bio) { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7483 | ret = -ENOMEM; |
| 7484 | goto free_ordered; |
| 7485 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7486 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7487 | if (!skip_sum && !write) { |
| 7488 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7489 | sum_len = dio_bio->bi_iter.bi_size >> |
| 7490 | inode->i_sb->s_blocksize_bits; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 7491 | sum_len *= csum_size; |
| 7492 | } else { |
| 7493 | sum_len = 0; |
| 7494 | } |
| 7495 | |
| 7496 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7497 | if (!dip) { |
| 7498 | ret = -ENOMEM; |
| 7499 | goto free_io_bio; |
| 7500 | } |
| 7501 | |
| 7502 | dip->private = dio_bio->bi_private; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7503 | dip->inode = inode; |
| 7504 | dip->logical_offset = file_offset; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 7505 | dip->bytes = dio_bio->bi_iter.bi_size; |
| 7506 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7507 | io_bio->bi_private = dip; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7508 | dip->errors = 0; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7509 | dip->orig_bio = io_bio; |
| 7510 | dip->dio_bio = dio_bio; |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7511 | atomic_set(&dip->pending_bios, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7512 | |
| 7513 | if (write) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7514 | io_bio->bi_end_io = btrfs_endio_direct_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7515 | else |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7516 | io_bio->bi_end_io = btrfs_endio_direct_read; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7517 | |
Miao Xie | e65e153 | 2010-11-22 03:04:43 +0000 | [diff] [blame] | 7518 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
| 7519 | if (!ret) |
Chris Mason | eaf25d9 | 2010-05-25 09:48:28 -0400 | [diff] [blame] | 7520 | return; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7521 | |
| 7522 | free_io_bio: |
| 7523 | bio_put(io_bio); |
| 7524 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7525 | free_ordered: |
| 7526 | /* |
| 7527 | * If this is a write, we need to clean up the reserved space and kill |
| 7528 | * the ordered extent. |
| 7529 | */ |
| 7530 | if (write) { |
| 7531 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 955256f | 2010-11-19 09:41:10 -0500 | [diff] [blame] | 7532 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7533 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
| 7534 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) |
| 7535 | btrfs_free_reserved_extent(root, ordered->start, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7536 | ordered->disk_len, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7537 | btrfs_put_ordered_extent(ordered); |
| 7538 | btrfs_put_ordered_extent(ordered); |
| 7539 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 7540 | bio_endio(dio_bio, ret); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7541 | } |
| 7542 | |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7543 | 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] | 7544 | const struct iov_iter *iter, loff_t offset) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7545 | { |
| 7546 | int seg; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7547 | int i; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7548 | unsigned blocksize_mask = root->sectorsize - 1; |
| 7549 | ssize_t retval = -EINVAL; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7550 | |
| 7551 | if (offset & blocksize_mask) |
| 7552 | goto out; |
| 7553 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7554 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 7555 | goto out; |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7556 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7557 | /* If this is a write we don't need to check anymore */ |
| 7558 | if (rw & WRITE) |
| 7559 | return 0; |
| 7560 | /* |
| 7561 | * Check to make sure we don't have duplicate iov_base's in this |
| 7562 | * iovec, if so return EINVAL, otherwise we'll get csum errors |
| 7563 | * when reading back. |
| 7564 | */ |
| 7565 | for (seg = 0; seg < iter->nr_segs; seg++) { |
| 7566 | for (i = seg + 1; i < iter->nr_segs; i++) { |
| 7567 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) |
Josef Bacik | a1b75f7 | 2011-04-08 15:51:18 +0000 | [diff] [blame] | 7568 | goto out; |
| 7569 | } |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7570 | } |
| 7571 | retval = 0; |
| 7572 | out: |
| 7573 | return retval; |
| 7574 | } |
Josef Bacik | eb838e7 | 2012-07-31 16:28:48 -0400 | [diff] [blame] | 7575 | |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7576 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
Al Viro | d8d3d94 | 2014-03-04 21:27:34 -0500 | [diff] [blame] | 7577 | struct iov_iter *iter, loff_t offset) |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7578 | { |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7579 | struct file *file = iocb->ki_filp; |
| 7580 | struct inode *inode = file->f_mapping->host; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7581 | size_t count = 0; |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7582 | int flags = 0; |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7583 | bool wakeup = true; |
| 7584 | bool relock = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7585 | ssize_t ret; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 7586 | |
Al Viro | 28060d5 | 2014-03-22 05:15:17 -0400 | [diff] [blame] | 7587 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset)) |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7588 | return 0; |
Chris Mason | 5a5f79b | 2010-05-26 21:33:37 -0400 | [diff] [blame] | 7589 | |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7590 | atomic_inc(&inode->i_dio_count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 7591 | smp_mb__after_atomic(); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7592 | |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7593 | /* |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 7594 | * The generic stuff only does filemap_write_and_wait_range, which |
| 7595 | * isn't enough if we've written compressed pages to this area, so |
| 7596 | * we need to flush the dirty pages again to make absolutely sure |
| 7597 | * that any outstanding dirty pages are on disk. |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7598 | */ |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 7599 | count = iov_iter_count(iter); |
Miao Xie | 41bd9ca | 2014-03-06 13:54:57 +0800 | [diff] [blame] | 7600 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 7601 | &BTRFS_I(inode)->runtime_flags)) |
Wang Shilong | 9a025a0 | 2014-07-17 11:44:13 +0800 | [diff] [blame] | 7602 | filemap_fdatawrite_range(inode->i_mapping, offset, |
| 7603 | offset + count - 1); |
Josef Bacik | 0e267c4 | 2013-07-02 10:38:02 -0400 | [diff] [blame] | 7604 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7605 | if (rw & WRITE) { |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7606 | /* |
| 7607 | * If the write DIO is beyond the EOF, we need update |
| 7608 | * the isize, but it is protected by i_mutex. So we can |
| 7609 | * not unlock the i_mutex at this case. |
| 7610 | */ |
| 7611 | if (offset + count <= inode->i_size) { |
| 7612 | mutex_unlock(&inode->i_mutex); |
| 7613 | relock = true; |
| 7614 | } |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7615 | ret = btrfs_delalloc_reserve_space(inode, count); |
| 7616 | if (ret) |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7617 | goto out; |
| 7618 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
| 7619 | &BTRFS_I(inode)->runtime_flags))) { |
| 7620 | inode_dio_done(inode); |
| 7621 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
| 7622 | wakeup = false; |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7623 | } |
| 7624 | |
| 7625 | ret = __blockdev_direct_IO(rw, iocb, inode, |
| 7626 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
Al Viro | 31b1403 | 2014-03-05 01:33:16 -0500 | [diff] [blame] | 7627 | iter, offset, btrfs_get_blocks_direct, NULL, |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7628 | btrfs_submit_direct, flags); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7629 | if (rw & WRITE) { |
| 7630 | if (ret < 0 && ret != -EIOCBQUEUED) |
| 7631 | btrfs_delalloc_release_space(inode, count); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7632 | else if (ret >= 0 && (size_t)ret < count) |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7633 | btrfs_delalloc_release_space(inode, |
| 7634 | count - (size_t)ret); |
Miao Xie | 172a504 | 2013-02-21 02:48:22 -0700 | [diff] [blame] | 7635 | else |
| 7636 | btrfs_delalloc_release_metadata(inode, 0); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7637 | } |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7638 | out: |
Miao Xie | 2e60a51 | 2013-02-08 07:01:08 +0000 | [diff] [blame] | 7639 | if (wakeup) |
| 7640 | inode_dio_done(inode); |
Miao Xie | 38851cc | 2013-02-08 07:04:11 +0000 | [diff] [blame] | 7641 | if (relock) |
| 7642 | mutex_lock(&inode->i_mutex); |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 7643 | |
| 7644 | return ret; |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 7645 | } |
| 7646 | |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7647 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
| 7648 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7649 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 7650 | __u64 start, __u64 len) |
| 7651 | { |
Tsutomu Itoh | 05dadc0 | 2012-11-29 05:08:26 +0000 | [diff] [blame] | 7652 | int ret; |
| 7653 | |
| 7654 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); |
| 7655 | if (ret) |
| 7656 | return ret; |
| 7657 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 7658 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 7659 | } |
| 7660 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7661 | int btrfs_readpage(struct file *file, struct page *page) |
| 7662 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7663 | struct extent_io_tree *tree; |
| 7664 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 7665 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7666 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7667 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7668 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 7669 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7670 | struct extent_io_tree *tree; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7671 | |
| 7672 | |
| 7673 | if (current->flags & PF_MEMALLOC) { |
| 7674 | redirty_page_for_writepage(wbc, page); |
| 7675 | unlock_page(page); |
| 7676 | return 0; |
| 7677 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7678 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7679 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
| 7680 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7681 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7682 | static int btrfs_writepages(struct address_space *mapping, |
| 7683 | struct writeback_control *wbc) |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7684 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7685 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 7686 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7687 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 7688 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
| 7689 | } |
| 7690 | |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7691 | static int |
| 7692 | btrfs_readpages(struct file *file, struct address_space *mapping, |
| 7693 | struct list_head *pages, unsigned nr_pages) |
| 7694 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7695 | struct extent_io_tree *tree; |
| 7696 | tree = &BTRFS_I(mapping->host)->io_tree; |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 7697 | return extent_readpages(tree, mapping, pages, nr_pages, |
| 7698 | btrfs_get_extent); |
| 7699 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7700 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7701 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7702 | struct extent_io_tree *tree; |
| 7703 | struct extent_map_tree *map; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7704 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7705 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7706 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
| 7707 | map = &BTRFS_I(page->mapping->host)->extent_tree; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 7708 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7709 | if (ret == 1) { |
| 7710 | ClearPagePrivate(page); |
| 7711 | set_page_private(page, 0); |
| 7712 | page_cache_release(page); |
| 7713 | } |
| 7714 | return ret; |
| 7715 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7716 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7717 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
| 7718 | { |
Chris Mason | 98509cf | 2008-09-11 15:51:43 -0400 | [diff] [blame] | 7719 | if (PageWriteback(page) || PageDirty(page)) |
| 7720 | return 0; |
Yan Zheng | b335b00 | 2009-02-12 10:06:04 -0500 | [diff] [blame] | 7721 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7722 | } |
| 7723 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 7724 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
| 7725 | unsigned int length) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7726 | { |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7727 | struct inode *inode = page->mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7728 | struct extent_io_tree *tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7729 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7730 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7731 | u64 page_start = page_offset(page); |
| 7732 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7733 | int inode_evicting = inode->i_state & I_FREEING; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7734 | |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7735 | /* |
| 7736 | * we have the page locked, so new writeback can't start, |
| 7737 | * and the dirty bit won't be cleared while we are here. |
| 7738 | * |
| 7739 | * Wait for IO on this page so that we can safely clear |
| 7740 | * the PagePrivate2 bit and do ordered accounting |
| 7741 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7742 | wait_on_page_writeback(page); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7743 | |
Josef Bacik | 5fd0204 | 2012-05-02 14:00:54 -0400 | [diff] [blame] | 7744 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7745 | if (offset) { |
| 7746 | btrfs_releasepage(page, GFP_NOFS); |
| 7747 | return; |
| 7748 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7749 | |
| 7750 | if (!inode_evicting) |
| 7751 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); |
| 7752 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7753 | if (ordered) { |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7754 | /* |
| 7755 | * IO on this page will never be started, so we need |
| 7756 | * to account for any ordered extents now |
| 7757 | */ |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7758 | if (!inode_evicting) |
| 7759 | clear_extent_bit(tree, page_start, page_end, |
| 7760 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7761 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
| 7762 | EXTENT_DEFRAG, 1, 0, &cached_state, |
| 7763 | GFP_NOFS); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7764 | /* |
| 7765 | * whoever cleared the private bit is responsible |
| 7766 | * for the finish_ordered_io |
| 7767 | */ |
Josef Bacik | 77cef2e | 2013-08-29 13:57:21 -0400 | [diff] [blame] | 7768 | if (TestClearPagePrivate2(page)) { |
| 7769 | struct btrfs_ordered_inode_tree *tree; |
| 7770 | u64 new_len; |
| 7771 | |
| 7772 | tree = &BTRFS_I(inode)->ordered_tree; |
| 7773 | |
| 7774 | spin_lock_irq(&tree->lock); |
| 7775 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); |
| 7776 | new_len = page_start - ordered->file_offset; |
| 7777 | if (new_len < ordered->truncated_len) |
| 7778 | ordered->truncated_len = new_len; |
| 7779 | spin_unlock_irq(&tree->lock); |
| 7780 | |
| 7781 | if (btrfs_dec_test_ordered_pending(inode, &ordered, |
| 7782 | page_start, |
| 7783 | PAGE_CACHE_SIZE, 1)) |
| 7784 | btrfs_finish_ordered_io(ordered); |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 7785 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7786 | btrfs_put_ordered_extent(ordered); |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7787 | if (!inode_evicting) { |
| 7788 | cached_state = NULL; |
| 7789 | lock_extent_bits(tree, page_start, page_end, 0, |
| 7790 | &cached_state); |
| 7791 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7792 | } |
Filipe David Borba Manana | 131e404 | 2013-11-19 22:29:35 +0000 | [diff] [blame] | 7793 | |
| 7794 | if (!inode_evicting) { |
| 7795 | clear_extent_bit(tree, page_start, page_end, |
| 7796 | EXTENT_LOCKED | EXTENT_DIRTY | |
| 7797 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 7798 | EXTENT_DEFRAG, 1, 1, |
| 7799 | &cached_state, GFP_NOFS); |
| 7800 | |
| 7801 | __btrfs_releasepage(page, GFP_NOFS); |
| 7802 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7803 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 7804 | ClearPageChecked(page); |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7805 | if (PagePrivate(page)) { |
Chris Mason | 9ad6b7bc | 2008-04-18 16:11:30 -0400 | [diff] [blame] | 7806 | ClearPagePrivate(page); |
| 7807 | set_page_private(page, 0); |
| 7808 | page_cache_release(page); |
| 7809 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7810 | } |
| 7811 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7812 | /* |
| 7813 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 7814 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 7815 | * be careful to check for EOF conditions here. We set the page up correctly |
| 7816 | * for a written page which means we get ENOSPC checking when writing into |
| 7817 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 7818 | * support these features. |
| 7819 | * |
| 7820 | * We are not allowed to take the i_mutex here so we have to play games to |
| 7821 | * protect against truncate races as the page could now be beyond EOF. Because |
| 7822 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 7823 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 7824 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 7825 | * unlock the page. |
| 7826 | */ |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7827 | 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] | 7828 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 7829 | struct page *page = vmf->page; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 7830 | struct inode *inode = file_inode(vma->vm_file); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7831 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7832 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 7833 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7834 | struct extent_state *cached_state = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7835 | char *kaddr; |
| 7836 | unsigned long zero_start; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7837 | loff_t size; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7838 | int ret; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7839 | int reserved = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7840 | u64 page_start; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7841 | u64 page_end; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7842 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7843 | sb_start_pagefault(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 7844 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7845 | if (!ret) { |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 7846 | ret = file_update_time(vma->vm_file); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7847 | reserved = 1; |
| 7848 | } |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7849 | if (ret) { |
| 7850 | if (ret == -ENOMEM) |
| 7851 | ret = VM_FAULT_OOM; |
| 7852 | else /* -ENOSPC, -EIO, etc */ |
| 7853 | ret = VM_FAULT_SIGBUS; |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7854 | if (reserved) |
| 7855 | goto out; |
| 7856 | goto out_noreserve; |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7857 | } |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7858 | |
Nick Piggin | 56a76f8 | 2009-03-31 15:23:23 -0700 | [diff] [blame] | 7859 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7860 | again: |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7861 | lock_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7862 | size = i_size_read(inode); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7863 | page_start = page_offset(page); |
| 7864 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7865 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7866 | if ((page->mapping != inode->i_mapping) || |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7867 | (page_start >= size)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7868 | /* page got truncated out from underneath us */ |
| 7869 | goto out_unlock; |
| 7870 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7871 | wait_on_page_writeback(page); |
| 7872 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 7873 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7874 | set_page_extent_mapped(page); |
| 7875 | |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7876 | /* |
| 7877 | * we can't set the delalloc bits if there are pending ordered |
| 7878 | * extents. Drop our locks and wait for them to finish |
| 7879 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7880 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 7881 | if (ordered) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7882 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7883 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7884 | unlock_page(page); |
Chris Mason | eb84ae0 | 2008-07-17 13:53:27 -0400 | [diff] [blame] | 7885 | btrfs_start_ordered_extent(inode, ordered, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7886 | btrfs_put_ordered_extent(ordered); |
| 7887 | goto again; |
| 7888 | } |
| 7889 | |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7890 | /* |
| 7891 | * XXX - page_mkwrite gets called every time the page is dirtied, even |
| 7892 | * if it was already dirty, so for space accounting reasons we need to |
| 7893 | * clear any delalloc bits for the range we are fixing to save. There |
| 7894 | * is probably a better way to do this, but for now keep consistent with |
| 7895 | * prepare_pages in the normal write path. |
| 7896 | */ |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7897 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 7898 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 7899 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7900 | 0, 0, &cached_state, GFP_NOFS); |
Josef Bacik | fbf1908 | 2009-10-01 17:10:23 -0400 | [diff] [blame] | 7901 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7902 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
| 7903 | &cached_state); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7904 | if (ret) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7905 | unlock_extent_cached(io_tree, page_start, page_end, |
| 7906 | &cached_state, GFP_NOFS); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7907 | ret = VM_FAULT_SIGBUS; |
| 7908 | goto out_unlock; |
| 7909 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7910 | ret = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7911 | |
| 7912 | /* page is wholly or partially inside EOF */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7913 | if (page_start + PAGE_CACHE_SIZE > size) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7914 | zero_start = size & ~PAGE_CACHE_MASK; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7915 | else |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7916 | zero_start = PAGE_CACHE_SIZE; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7917 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7918 | if (zero_start != PAGE_CACHE_SIZE) { |
| 7919 | kaddr = kmap(page); |
| 7920 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
| 7921 | flush_dcache_page(page); |
| 7922 | kunmap(page); |
| 7923 | } |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 7924 | ClearPageChecked(page); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7925 | set_page_dirty(page); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7926 | SetPageUptodate(page); |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 7927 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 7928 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
| 7929 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 7930 | 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] | 7931 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7932 | 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] | 7933 | |
| 7934 | out_unlock: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7935 | if (!ret) { |
| 7936 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 50a9b21 | 2009-09-11 12:33:12 -0400 | [diff] [blame] | 7937 | return VM_FAULT_LOCKED; |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7938 | } |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7939 | unlock_page(page); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 7940 | out: |
Josef Bacik | ec39e18 | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 7941 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 9998eb7 | 2012-01-25 13:47:40 -0500 | [diff] [blame] | 7942 | out_noreserve: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 7943 | sb_end_pagefault(inode->i_sb); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 7944 | return ret; |
| 7945 | } |
| 7946 | |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 7947 | static int btrfs_truncate(struct inode *inode) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7948 | { |
| 7949 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7950 | struct btrfs_block_rsv *rsv; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 7951 | int ret = 0; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 7952 | int err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7953 | struct btrfs_trans_handle *trans; |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 7954 | u64 mask = root->sectorsize - 1; |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 7955 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7956 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 7957 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
| 7958 | (u64)-1); |
| 7959 | if (ret) |
| 7960 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 7961 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7962 | /* |
| 7963 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have |
| 7964 | * 3 things going on here |
| 7965 | * |
| 7966 | * 1) We need to reserve space for our orphan item and the space to |
| 7967 | * delete our orphan item. Lord knows we don't want to have a dangling |
| 7968 | * orphan item because we didn't reserve space to remove it. |
| 7969 | * |
| 7970 | * 2) We need to reserve space to update our inode. |
| 7971 | * |
| 7972 | * 3) We need to have something to cache all the space that is going to |
| 7973 | * be free'd up by the truncate operation, but also have some slack |
| 7974 | * space reserved in case it uses space during the truncate (thank you |
| 7975 | * very much snapshotting). |
| 7976 | * |
| 7977 | * And we need these to all be seperate. The fact is we can use alot of |
| 7978 | * space doing the truncate, and we have no earthly idea how much space |
| 7979 | * we will use, so we need the truncate reservation to be seperate so it |
| 7980 | * doesn't end up using space reserved for updating the inode or |
| 7981 | * removing the orphan item. We also need to be able to stop the |
| 7982 | * transaction and start a new one, which means we need to be able to |
| 7983 | * update the inode several times, and we have no idea of knowing how |
| 7984 | * many times that will be, so we can't just reserve 1 item for the |
| 7985 | * entirety of the opration, so that has to be done seperately as well. |
| 7986 | * Then there is the orphan item, which does indeed need to be held on |
| 7987 | * to for the whole operation, and we need nobody to touch this reserved |
| 7988 | * space except the orphan code. |
| 7989 | * |
| 7990 | * So that leaves us with |
| 7991 | * |
| 7992 | * 1) root->orphan_block_rsv - for the orphan deletion. |
| 7993 | * 2) rsv - for the truncate reservation, which we will steal from the |
| 7994 | * transaction reservation. |
| 7995 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for |
| 7996 | * updating the inode. |
| 7997 | */ |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 7998 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 7999 | if (!rsv) |
| 8000 | return -ENOMEM; |
Josef Bacik | 4a33854 | 2011-08-29 11:01:31 -0400 | [diff] [blame] | 8001 | rsv->size = min_size; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8002 | rsv->failfast = 1; |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8003 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8004 | /* |
Josef Bacik | 0712718 | 2011-08-19 10:29:59 -0400 | [diff] [blame] | 8005 | * 1 for the truncate slack space |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8006 | * 1 for updating the inode. |
| 8007 | */ |
Josef Bacik | f3fe820 | 2013-01-07 17:03:21 -0500 | [diff] [blame] | 8008 | trans = btrfs_start_transaction(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8009 | if (IS_ERR(trans)) { |
| 8010 | err = PTR_ERR(trans); |
| 8011 | goto out; |
| 8012 | } |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8013 | |
Josef Bacik | 907cbce | 2011-08-08 13:46:15 -0400 | [diff] [blame] | 8014 | /* Migrate the slack space for the truncate to our reserve */ |
| 8015 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 8016 | min_size); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8017 | BUG_ON(ret); |
Josef Bacik | f0cd846 | 2011-03-04 14:37:08 -0500 | [diff] [blame] | 8018 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8019 | /* |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8020 | * So if we truncate and then write and fsync we normally would just |
| 8021 | * write the extents that changed, which is a problem if we need to |
| 8022 | * first truncate that entire inode. So set this flag so we write out |
| 8023 | * all of the extents in the inode to the sync log so we're completely |
| 8024 | * safe. |
| 8025 | */ |
| 8026 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8027 | trans->block_rsv = rsv; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8028 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8029 | while (1) { |
| 8030 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 8031 | inode->i_size, |
| 8032 | BTRFS_EXTENT_DATA_KEY); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8033 | if (ret != -ENOSPC) { |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8034 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8035 | break; |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8036 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8037 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8038 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8039 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8040 | if (ret) { |
| 8041 | err = ret; |
| 8042 | break; |
| 8043 | } |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8044 | |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8045 | btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8046 | btrfs_btree_balance_dirty(root); |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 8047 | |
| 8048 | trans = btrfs_start_transaction(root, 2); |
| 8049 | if (IS_ERR(trans)) { |
| 8050 | ret = err = PTR_ERR(trans); |
| 8051 | trans = NULL; |
| 8052 | break; |
| 8053 | } |
| 8054 | |
| 8055 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 8056 | rsv, min_size); |
| 8057 | BUG_ON(ret); /* shouldn't happen */ |
| 8058 | trans->block_rsv = rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | if (ret == 0 && inode->i_nlink > 0) { |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8062 | trans->block_rsv = root->orphan_block_rsv; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8063 | ret = btrfs_orphan_del(trans, inode); |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8064 | if (ret) |
| 8065 | err = ret; |
Yan, Zheng | 8082510 | 2009-11-12 09:35:36 +0000 | [diff] [blame] | 8066 | } |
| 8067 | |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8068 | if (trans) { |
| 8069 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 8070 | ret = btrfs_update_inode(trans, root, inode); |
| 8071 | if (ret && !err) |
| 8072 | err = ret; |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8073 | |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8074 | ret = btrfs_end_transaction(trans, root); |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8075 | btrfs_btree_balance_dirty(root); |
Chris Mason | 917c16b | 2011-11-08 14:49:59 -0500 | [diff] [blame] | 8076 | } |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 8077 | |
| 8078 | out: |
| 8079 | btrfs_free_block_rsv(root, rsv); |
| 8080 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8081 | if (ret && !err) |
| 8082 | err = ret; |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 8083 | |
Josef Bacik | 3893e33 | 2011-01-31 16:03:11 -0500 | [diff] [blame] | 8084 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8085 | } |
| 8086 | |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8087 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8088 | * create a new subvolume directory/inode (helper for the ioctl). |
| 8089 | */ |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8090 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8091 | struct btrfs_root *new_root, |
| 8092 | struct btrfs_root *parent_root, |
| 8093 | u64 new_dirid) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8094 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8095 | struct inode *inode; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8096 | int err; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8097 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8098 | |
Florian Albrechtskirchinger | 12fc9d0 | 2012-02-10 22:15:54 +0100 | [diff] [blame] | 8099 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
| 8100 | new_dirid, new_dirid, |
| 8101 | S_IFDIR | (~current_umask() & S_IRWXUGO), |
| 8102 | &index); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8103 | if (IS_ERR(inode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8104 | return PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8105 | inode->i_op = &btrfs_dir_inode_operations; |
| 8106 | inode->i_fop = &btrfs_dir_file_operations; |
| 8107 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 8108 | set_nlink(inode, 1); |
Chris Mason | dbe674a | 2008-07-17 12:54:05 -0400 | [diff] [blame] | 8109 | btrfs_i_size_write(inode, 0); |
Sven Wegener | 3b96362 | 2008-06-09 21:57:42 -0400 | [diff] [blame] | 8110 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8111 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
| 8112 | if (err) |
| 8113 | btrfs_err(new_root->fs_info, |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 8114 | "error inheriting subvolume %llu properties: %d", |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 8115 | new_root->root_key.objectid, err); |
| 8116 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8117 | err = btrfs_update_inode(trans, new_root, inode); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 8118 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8119 | iput(inode); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 8120 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8121 | } |
| 8122 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8123 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 8124 | { |
| 8125 | struct btrfs_inode *ei; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8126 | struct inode *inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8127 | |
| 8128 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 8129 | if (!ei) |
| 8130 | return NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8131 | |
| 8132 | ei->root = NULL; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8133 | ei->generation = 0; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 8134 | ei->last_trans = 0; |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 8135 | ei->last_sub_trans = 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8136 | ei->logged_trans = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8137 | ei->delalloc_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8138 | ei->disk_i_size = 0; |
| 8139 | ei->flags = 0; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8140 | ei->csum_bytes = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8141 | ei->index_cnt = (u64)-1; |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8142 | ei->dir_index = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8143 | ei->last_unlink_trans = 0; |
Liu Bo | 46d8bc3 | 2012-08-29 01:07:55 -0600 | [diff] [blame] | 8144 | ei->last_log_commit = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8145 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8146 | spin_lock_init(&ei->lock); |
| 8147 | ei->outstanding_extents = 0; |
| 8148 | ei->reserved_extents = 0; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8149 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 8150 | ei->runtime_flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 8151 | ei->force_compress = BTRFS_COMPRESS_NONE; |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8152 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 8153 | ei->delayed_node = NULL; |
| 8154 | |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8155 | inode = &ei->vfs_inode; |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 8156 | extent_map_tree_init(&ei->extent_tree); |
David Sterba | f993c88 | 2011-04-20 23:35:57 +0200 | [diff] [blame] | 8157 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
| 8158 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 8159 | ei->io_tree.track_uptodate = 1; |
| 8160 | ei->io_failure_tree.track_uptodate = 1; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 8161 | atomic_set(&ei->sync_writers, 0); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8162 | mutex_init(&ei->log_mutex); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 8163 | mutex_init(&ei->delalloc_mutex); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8164 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8165 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
Yan, Zheng | 2ead6ae | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8166 | RB_CLEAR_NODE(&ei->rb_node); |
| 8167 | |
| 8168 | return inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8169 | } |
| 8170 | |
Josef Bacik | aaedb55 | 2013-10-11 14:44:09 -0400 | [diff] [blame] | 8171 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 8172 | void btrfs_test_destroy_inode(struct inode *inode) |
| 8173 | { |
| 8174 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
| 8175 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8176 | } |
| 8177 | #endif |
| 8178 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8179 | static void btrfs_i_callback(struct rcu_head *head) |
| 8180 | { |
| 8181 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8182 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 8183 | } |
| 8184 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8185 | void btrfs_destroy_inode(struct inode *inode) |
| 8186 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8187 | struct btrfs_ordered_extent *ordered; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8188 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8189 | |
Al Viro | b3d9b7a | 2012-06-09 13:51:19 -0400 | [diff] [blame] | 8190 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8191 | WARN_ON(inode->i_data.nrpages); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 8192 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
| 8193 | WARN_ON(BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 8194 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
| 8195 | WARN_ON(BTRFS_I(inode)->csum_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8196 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8197 | /* |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8198 | * This can happen where we create an inode, but somebody else also |
| 8199 | * created the same inode and we need to destroy the one we already |
| 8200 | * created. |
| 8201 | */ |
| 8202 | if (!root) |
| 8203 | goto free; |
| 8204 | |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8205 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
| 8206 | &BTRFS_I(inode)->runtime_flags)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8207 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8208 | btrfs_ino(inode)); |
Josef Bacik | 8a35d95 | 2012-05-23 14:26:42 -0400 | [diff] [blame] | 8209 | atomic_dec(&root->orphan_inodes); |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8210 | } |
Josef Bacik | 7b12876 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 8211 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8212 | while (1) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8213 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 8214 | if (!ordered) |
| 8215 | break; |
| 8216 | else { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8217 | 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] | 8218 | ordered->file_offset, ordered->len); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8219 | btrfs_remove_ordered_extent(inode, ordered); |
| 8220 | btrfs_put_ordered_extent(ordered); |
| 8221 | btrfs_put_ordered_extent(ordered); |
| 8222 | } |
| 8223 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8224 | inode_tree_del(inode); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8225 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
Josef Bacik | a6dbd42 | 2009-11-11 15:53:34 -0500 | [diff] [blame] | 8226 | free: |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 8227 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8228 | } |
| 8229 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8230 | int btrfs_drop_inode(struct inode *inode) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8231 | { |
| 8232 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8233 | |
Naohiro Aota | 6379ef9 | 2013-06-06 09:56:34 +0000 | [diff] [blame] | 8234 | if (root == NULL) |
| 8235 | return 1; |
| 8236 | |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 8237 | /* the snap/subvol tree is on deleting */ |
Stefan Behrens | 69e9c6c | 2013-09-05 16:58:43 +0200 | [diff] [blame] | 8238 | if (btrfs_root_refs(&root->root_item) == 0) |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8239 | return 1; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8240 | else |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 8241 | return generic_drop_inode(inode); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8242 | } |
| 8243 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 8244 | static void init_once(void *foo) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8245 | { |
| 8246 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 8247 | |
| 8248 | inode_init_once(&ei->vfs_inode); |
| 8249 | } |
| 8250 | |
| 8251 | void btrfs_destroy_cachep(void) |
| 8252 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 8253 | /* |
| 8254 | * Make sure all delayed rcu free inodes are flushed before we |
| 8255 | * destroy cache. |
| 8256 | */ |
| 8257 | rcu_barrier(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8258 | if (btrfs_inode_cachep) |
| 8259 | kmem_cache_destroy(btrfs_inode_cachep); |
| 8260 | if (btrfs_trans_handle_cachep) |
| 8261 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 8262 | if (btrfs_transaction_cachep) |
| 8263 | kmem_cache_destroy(btrfs_transaction_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8264 | if (btrfs_path_cachep) |
| 8265 | kmem_cache_destroy(btrfs_path_cachep); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8266 | if (btrfs_free_space_cachep) |
| 8267 | kmem_cache_destroy(btrfs_free_space_cachep); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8268 | if (btrfs_delalloc_work_cachep) |
| 8269 | kmem_cache_destroy(btrfs_delalloc_work_cachep); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8270 | } |
| 8271 | |
| 8272 | int btrfs_init_cachep(void) |
| 8273 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8274 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8275 | sizeof(struct btrfs_inode), 0, |
| 8276 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8277 | if (!btrfs_inode_cachep) |
| 8278 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8279 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8280 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8281 | sizeof(struct btrfs_trans_handle), 0, |
| 8282 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8283 | if (!btrfs_trans_handle_cachep) |
| 8284 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8285 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8286 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8287 | sizeof(struct btrfs_transaction), 0, |
| 8288 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8289 | if (!btrfs_transaction_cachep) |
| 8290 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8291 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8292 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8293 | sizeof(struct btrfs_path), 0, |
| 8294 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8295 | if (!btrfs_path_cachep) |
| 8296 | goto fail; |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 8297 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 8298 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 8299 | sizeof(struct btrfs_free_space), 0, |
| 8300 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 8301 | if (!btrfs_free_space_cachep) |
| 8302 | goto fail; |
| 8303 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8304 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
| 8305 | sizeof(struct btrfs_delalloc_work), 0, |
| 8306 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, |
| 8307 | NULL); |
| 8308 | if (!btrfs_delalloc_work_cachep) |
| 8309 | goto fail; |
| 8310 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8311 | return 0; |
| 8312 | fail: |
| 8313 | btrfs_destroy_cachep(); |
| 8314 | return -ENOMEM; |
| 8315 | } |
| 8316 | |
| 8317 | static int btrfs_getattr(struct vfsmount *mnt, |
| 8318 | struct dentry *dentry, struct kstat *stat) |
| 8319 | { |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8320 | u64 delalloc_bytes; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8321 | struct inode *inode = dentry->d_inode; |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8322 | u32 blocksize = inode->i_sb->s_blocksize; |
| 8323 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8324 | generic_fillattr(inode, stat); |
Al Viro | 0ee5dc6 | 2011-07-07 15:44:25 -0400 | [diff] [blame] | 8325 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 8326 | stat->blksize = PAGE_CACHE_SIZE; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8327 | |
| 8328 | spin_lock(&BTRFS_I(inode)->lock); |
| 8329 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; |
| 8330 | spin_unlock(&BTRFS_I(inode)->lock); |
David Sterba | fadc0d8 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 8331 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8332 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8333 | return 0; |
| 8334 | } |
| 8335 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8336 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 8337 | struct inode *new_dir, struct dentry *new_dentry) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8338 | { |
| 8339 | struct btrfs_trans_handle *trans; |
| 8340 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8341 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8342 | struct inode *new_inode = new_dentry->d_inode; |
| 8343 | struct inode *old_inode = old_dentry->d_inode; |
| 8344 | struct timespec ctime = CURRENT_TIME; |
Chris Mason | 00e4e6b | 2008-08-05 11:18:09 -0400 | [diff] [blame] | 8345 | u64 index = 0; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8346 | u64 root_objectid; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8347 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8348 | u64 old_ino = btrfs_ino(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8349 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8350 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
Yan, Zheng | f679a84 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8351 | return -EPERM; |
| 8352 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8353 | /* we only allow rename subvolume link between subvolumes */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8354 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 8355 | return -EXDEV; |
| 8356 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8357 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
| 8358 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8359 | return -ENOTEMPTY; |
| 8360 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8361 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8362 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8363 | return -ENOTEMPTY; |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8364 | |
| 8365 | |
| 8366 | /* check for collisions, even if the name isn't there */ |
Josef Bacik | 4871c15 | 2013-10-09 12:24:04 -0400 | [diff] [blame] | 8367 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8368 | new_dentry->d_name.name, |
| 8369 | new_dentry->d_name.len); |
| 8370 | |
| 8371 | if (ret) { |
| 8372 | if (ret == -EEXIST) { |
| 8373 | /* we shouldn't get |
| 8374 | * eexist without a new_inode */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8375 | if (WARN_ON(!new_inode)) { |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 8376 | return ret; |
| 8377 | } |
| 8378 | } else { |
| 8379 | /* maybe -EOVERFLOW */ |
| 8380 | return ret; |
| 8381 | } |
| 8382 | } |
| 8383 | ret = 0; |
| 8384 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8385 | /* |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8386 | * we're using rename to replace one file with another. Start IO on it |
| 8387 | * 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] | 8388 | */ |
Chris Mason | 8d875f9 | 2014-08-12 10:47:42 -0700 | [diff] [blame] | 8389 | 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] | 8390 | filemap_flush(old_inode->i_mapping); |
| 8391 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8392 | /* close the racy window with snapshot create/destroy ioctl */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8393 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8394 | down_read(&root->fs_info->subvol_sem); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8395 | /* |
| 8396 | * We want to reserve the absolute worst case amount of items. So if |
| 8397 | * both inodes are subvols and we need to unlink them then that would |
| 8398 | * require 4 item modifications, but if they are both normal inodes it |
| 8399 | * would require 5 item modifications, so we'll assume their normal |
| 8400 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
| 8401 | * should cover the worst case number of items we'll modify. |
| 8402 | */ |
Josef Bacik | 6e137ed | 2013-03-26 15:26:55 -0400 | [diff] [blame] | 8403 | trans = btrfs_start_transaction(root, 11); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8404 | if (IS_ERR(trans)) { |
| 8405 | ret = PTR_ERR(trans); |
| 8406 | goto out_notrans; |
| 8407 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8408 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8409 | if (dest != root) |
| 8410 | btrfs_record_root_in_trans(trans, dest); |
| 8411 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8412 | ret = btrfs_set_inode_index(new_dir, &index); |
| 8413 | if (ret) |
| 8414 | goto out_fail; |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8415 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8416 | BTRFS_I(old_inode)->dir_index = 0ULL; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8417 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8418 | /* force full log commit if subvolume involved. */ |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8419 | btrfs_set_log_full_commit(root->fs_info, trans); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8420 | } else { |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8421 | ret = btrfs_insert_inode_ref(trans, dest, |
| 8422 | new_dentry->d_name.name, |
| 8423 | new_dentry->d_name.len, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8424 | old_ino, |
| 8425 | btrfs_ino(new_dir), index); |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8426 | if (ret) |
| 8427 | goto out_fail; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8428 | /* |
| 8429 | * this is an ugly little race, but the rename is required |
| 8430 | * to make sure that if we crash, the inode is either at the |
| 8431 | * old name or the new one. pinning the log transaction lets |
| 8432 | * us make sure we don't allow a log commit to come in after |
| 8433 | * we unlink the name but before we add the new name back in. |
| 8434 | */ |
| 8435 | btrfs_pin_log_trans(root); |
| 8436 | } |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 8437 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8438 | inode_inc_iversion(old_dir); |
| 8439 | inode_inc_iversion(new_dir); |
| 8440 | inode_inc_iversion(old_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8441 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 8442 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 8443 | old_inode->i_ctime = ctime; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8444 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8445 | if (old_dentry->d_parent != new_dentry->d_parent) |
| 8446 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); |
| 8447 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8448 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8449 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
| 8450 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, |
| 8451 | old_dentry->d_name.name, |
| 8452 | old_dentry->d_name.len); |
| 8453 | } else { |
Al Viro | 9298679 | 2011-03-04 17:14:37 +0000 | [diff] [blame] | 8454 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
| 8455 | old_dentry->d_inode, |
| 8456 | old_dentry->d_name.name, |
| 8457 | old_dentry->d_name.len); |
| 8458 | if (!ret) |
| 8459 | ret = btrfs_update_inode(trans, root, old_inode); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8460 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8461 | if (ret) { |
| 8462 | btrfs_abort_transaction(trans, root, ret); |
| 8463 | goto out_fail; |
| 8464 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8465 | |
| 8466 | if (new_inode) { |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8467 | inode_inc_iversion(new_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8468 | new_inode->i_ctime = CURRENT_TIME; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8469 | if (unlikely(btrfs_ino(new_inode) == |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8470 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
| 8471 | root_objectid = BTRFS_I(new_inode)->location.objectid; |
| 8472 | ret = btrfs_unlink_subvol(trans, dest, new_dir, |
| 8473 | root_objectid, |
| 8474 | new_dentry->d_name.name, |
| 8475 | new_dentry->d_name.len); |
| 8476 | BUG_ON(new_inode->i_nlink == 0); |
| 8477 | } else { |
| 8478 | ret = btrfs_unlink_inode(trans, dest, new_dir, |
| 8479 | new_dentry->d_inode, |
| 8480 | new_dentry->d_name.name, |
| 8481 | new_dentry->d_name.len); |
| 8482 | } |
Josef Bacik | 4ef31a4 | 2013-08-13 14:10:08 -0400 | [diff] [blame] | 8483 | if (!ret && new_inode->i_nlink == 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8484 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8485 | if (ret) { |
| 8486 | btrfs_abort_transaction(trans, root, ret); |
| 8487 | goto out_fail; |
| 8488 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8489 | } |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8490 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8491 | ret = btrfs_add_link(trans, new_dir, old_inode, |
| 8492 | new_dentry->d_name.name, |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 8493 | new_dentry->d_name.len, 0, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8494 | if (ret) { |
| 8495 | btrfs_abort_transaction(trans, root, ret); |
| 8496 | goto out_fail; |
| 8497 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8498 | |
Miao Xie | 67de117 | 2013-12-26 13:07:06 +0800 | [diff] [blame] | 8499 | if (old_inode->i_nlink == 1) |
| 8500 | BTRFS_I(old_inode)->dir_index = index; |
| 8501 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8502 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
Al Viro | 10d9f30 | 2011-07-16 23:09:10 -0400 | [diff] [blame] | 8503 | struct dentry *parent = new_dentry->d_parent; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 8504 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 8505 | btrfs_end_log_trans(root); |
| 8506 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8507 | out_fail: |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8508 | btrfs_end_transaction(trans, root); |
Johann Lombardi | b44c59a | 2011-03-31 13:23:47 +0000 | [diff] [blame] | 8509 | out_notrans: |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8510 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8511 | up_read(&root->fs_info->subvol_sem); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8512 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8513 | return ret; |
| 8514 | } |
| 8515 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8516 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
| 8517 | { |
| 8518 | struct btrfs_delalloc_work *delalloc_work; |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8519 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8520 | |
| 8521 | delalloc_work = container_of(work, struct btrfs_delalloc_work, |
| 8522 | work); |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8523 | inode = delalloc_work->inode; |
| 8524 | if (delalloc_work->wait) { |
| 8525 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 8526 | } else { |
| 8527 | filemap_flush(inode->i_mapping); |
| 8528 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 8529 | &BTRFS_I(inode)->runtime_flags)) |
| 8530 | filemap_flush(inode->i_mapping); |
| 8531 | } |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8532 | |
| 8533 | if (delalloc_work->delay_iput) |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8534 | btrfs_add_delayed_iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8535 | else |
Josef Bacik | 9f23e28 | 2013-10-28 15:03:41 -0400 | [diff] [blame] | 8536 | iput(inode); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8537 | complete(&delalloc_work->completion); |
| 8538 | } |
| 8539 | |
| 8540 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, |
| 8541 | int wait, int delay_iput) |
| 8542 | { |
| 8543 | struct btrfs_delalloc_work *work; |
| 8544 | |
| 8545 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); |
| 8546 | if (!work) |
| 8547 | return NULL; |
| 8548 | |
| 8549 | init_completion(&work->completion); |
| 8550 | INIT_LIST_HEAD(&work->list); |
| 8551 | work->inode = inode; |
| 8552 | work->wait = wait; |
| 8553 | work->delay_iput = delay_iput; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 8554 | WARN_ON_ONCE(!inode); |
| 8555 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, |
| 8556 | btrfs_run_delalloc_work, NULL, NULL); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8557 | |
| 8558 | return work; |
| 8559 | } |
| 8560 | |
| 8561 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) |
| 8562 | { |
| 8563 | wait_for_completion(&work->completion); |
| 8564 | kmem_cache_free(btrfs_delalloc_work_cachep, work); |
| 8565 | } |
| 8566 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 8567 | /* |
| 8568 | * some fairly slow code that needs optimization. This walks the list |
| 8569 | * of all the inodes with pending delalloc and forces them to disk. |
| 8570 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8571 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
| 8572 | int nr) |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8573 | { |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8574 | struct btrfs_inode *binode; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8575 | struct inode *inode; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8576 | struct btrfs_delalloc_work *work, *next; |
| 8577 | struct list_head works; |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8578 | struct list_head splice; |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8579 | int ret = 0; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8580 | |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8581 | INIT_LIST_HEAD(&works); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8582 | INIT_LIST_HEAD(&splice); |
Miao Xie | 63607cc | 2013-01-22 10:50:35 +0000 | [diff] [blame] | 8583 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8584 | mutex_lock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8585 | spin_lock(&root->delalloc_lock); |
| 8586 | list_splice_init(&root->delalloc_inodes, &splice); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8587 | while (!list_empty(&splice)) { |
| 8588 | binode = list_entry(splice.next, struct btrfs_inode, |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8589 | delalloc_inodes); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8590 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8591 | list_move_tail(&binode->delalloc_inodes, |
| 8592 | &root->delalloc_inodes); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8593 | inode = igrab(&binode->vfs_inode); |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8594 | if (!inode) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8595 | cond_resched_lock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8596 | continue; |
Miao Xie | df0af1a | 2013-01-29 10:11:59 +0000 | [diff] [blame] | 8597 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8598 | spin_unlock(&root->delalloc_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8599 | |
| 8600 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); |
| 8601 | if (unlikely(!work)) { |
Josef Bacik | f4ab9ea | 2013-09-17 11:25:44 -0400 | [diff] [blame] | 8602 | if (delay_iput) |
| 8603 | btrfs_add_delayed_iput(inode); |
| 8604 | else |
| 8605 | iput(inode); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8606 | ret = -ENOMEM; |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 8607 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8608 | } |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8609 | list_add_tail(&work->list, &works); |
Qu Wenruo | a44903a | 2014-02-28 10:46:09 +0800 | [diff] [blame] | 8610 | btrfs_queue_work(root->fs_info->flush_workers, |
| 8611 | &work->work); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8612 | ret++; |
| 8613 | if (nr != -1 && ret >= nr) |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 8614 | goto out; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 8615 | cond_resched(); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8616 | spin_lock(&root->delalloc_lock); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8617 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8618 | spin_unlock(&root->delalloc_lock); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8619 | |
Wang Shilong | a1ecaab | 2014-04-02 19:53:32 +0800 | [diff] [blame] | 8620 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8621 | list_for_each_entry_safe(work, next, &works, list) { |
| 8622 | list_del_init(&work->list); |
| 8623 | btrfs_wait_and_free_delalloc_work(work); |
| 8624 | } |
| 8625 | |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8626 | if (!list_empty_careful(&splice)) { |
| 8627 | spin_lock(&root->delalloc_lock); |
| 8628 | list_splice_tail(&splice, &root->delalloc_inodes); |
| 8629 | spin_unlock(&root->delalloc_lock); |
| 8630 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8631 | mutex_unlock(&root->delalloc_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8632 | return ret; |
| 8633 | } |
| 8634 | |
| 8635 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
| 8636 | { |
| 8637 | int ret; |
| 8638 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 8639 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8640 | return -EROFS; |
| 8641 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8642 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
| 8643 | if (ret > 0) |
| 8644 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8645 | /* |
| 8646 | * the filemap_flush will queue IO into the worker threads, but |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8647 | * we have to make sure the IO is actually started and that |
| 8648 | * ordered extents get created before we return |
| 8649 | */ |
| 8650 | atomic_inc(&root->fs_info->async_submit_draining); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8651 | while (atomic_read(&root->fs_info->nr_async_submits) || |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8652 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8653 | wait_event(root->fs_info->async_submit_wait, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 8654 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 8655 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 8656 | } |
| 8657 | atomic_dec(&root->fs_info->async_submit_draining); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8658 | return ret; |
| 8659 | } |
| 8660 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8661 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
| 8662 | int nr) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8663 | { |
| 8664 | struct btrfs_root *root; |
| 8665 | struct list_head splice; |
| 8666 | int ret; |
| 8667 | |
Wang Shilong | 2c21b4d | 2014-01-14 19:42:20 +0800 | [diff] [blame] | 8668 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8669 | return -EROFS; |
| 8670 | |
| 8671 | INIT_LIST_HEAD(&splice); |
| 8672 | |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8673 | mutex_lock(&fs_info->delalloc_root_mutex); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8674 | spin_lock(&fs_info->delalloc_root_lock); |
| 8675 | list_splice_init(&fs_info->delalloc_roots, &splice); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8676 | while (!list_empty(&splice) && nr) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8677 | root = list_first_entry(&splice, struct btrfs_root, |
| 8678 | delalloc_root); |
| 8679 | root = btrfs_grab_fs_root(root); |
| 8680 | BUG_ON(!root); |
| 8681 | list_move_tail(&root->delalloc_root, |
| 8682 | &fs_info->delalloc_roots); |
| 8683 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8684 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8685 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8686 | btrfs_put_fs_root(root); |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8687 | if (ret < 0) |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8688 | goto out; |
| 8689 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8690 | if (nr != -1) { |
| 8691 | nr -= ret; |
| 8692 | WARN_ON(nr < 0); |
| 8693 | } |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8694 | spin_lock(&fs_info->delalloc_root_lock); |
| 8695 | } |
| 8696 | spin_unlock(&fs_info->delalloc_root_lock); |
| 8697 | |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 8698 | ret = 0; |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8699 | atomic_inc(&fs_info->async_submit_draining); |
| 8700 | while (atomic_read(&fs_info->nr_async_submits) || |
| 8701 | atomic_read(&fs_info->async_delalloc_pages)) { |
| 8702 | wait_event(fs_info->async_submit_wait, |
| 8703 | (atomic_read(&fs_info->nr_async_submits) == 0 && |
| 8704 | atomic_read(&fs_info->async_delalloc_pages) == 0)); |
| 8705 | } |
| 8706 | atomic_dec(&fs_info->async_submit_draining); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8707 | out: |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8708 | if (!list_empty_careful(&splice)) { |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 8709 | spin_lock(&fs_info->delalloc_root_lock); |
| 8710 | list_splice_tail(&splice, &fs_info->delalloc_roots); |
| 8711 | spin_unlock(&fs_info->delalloc_root_lock); |
Miao Xie | 1eafa6c | 2013-01-22 10:49:00 +0000 | [diff] [blame] | 8712 | } |
Miao Xie | 573bfb7 | 2014-03-06 13:55:03 +0800 | [diff] [blame] | 8713 | mutex_unlock(&fs_info->delalloc_root_mutex); |
Miao Xie | 8ccf6f19 | 2012-10-25 09:28:04 +0000 | [diff] [blame] | 8714 | return ret; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8715 | } |
| 8716 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8717 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 8718 | const char *symname) |
| 8719 | { |
| 8720 | struct btrfs_trans_handle *trans; |
| 8721 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8722 | struct btrfs_path *path; |
| 8723 | struct btrfs_key key; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8724 | struct inode *inode = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8725 | int err; |
| 8726 | int drop_inode = 0; |
| 8727 | u64 objectid; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8728 | u64 index = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8729 | int name_len; |
| 8730 | int datasize; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8731 | unsigned long ptr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8732 | struct btrfs_file_extent_item *ei; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8733 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8734 | |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8735 | name_len = strlen(symname); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8736 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 8737 | return -ENAMETOOLONG; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8738 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 8739 | /* |
| 8740 | * 2 items for inode item and ref |
| 8741 | * 2 items for dir items |
| 8742 | * 1 item for xattr if selinux is on |
| 8743 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8744 | trans = btrfs_start_transaction(root, 5); |
| 8745 | if (IS_ERR(trans)) |
| 8746 | return PTR_ERR(trans); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 8747 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 8748 | err = btrfs_find_free_ino(root, &objectid); |
| 8749 | if (err) |
| 8750 | goto out_unlock; |
| 8751 | |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 8752 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8753 | dentry->d_name.len, btrfs_ino(dir), objectid, |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 8754 | S_IFLNK|S_IRWXUGO, &index); |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8755 | if (IS_ERR(inode)) { |
| 8756 | err = PTR_ERR(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8757 | goto out_unlock; |
Tsutomu Itoh | 7cf96da | 2011-04-25 19:43:53 -0400 | [diff] [blame] | 8758 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8759 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 8760 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 8761 | if (err) { |
| 8762 | drop_inode = 1; |
| 8763 | goto out_unlock; |
| 8764 | } |
| 8765 | |
Casey Schaufler | ad19db7 | 2011-12-15 10:09:07 -0500 | [diff] [blame] | 8766 | /* |
| 8767 | * If the active LSM wants to access the inode during |
| 8768 | * d_instantiate it needs these. Smack checks to see |
| 8769 | * if the filesystem supports xattrs by looking at the |
| 8770 | * ops vector. |
| 8771 | */ |
| 8772 | inode->i_fop = &btrfs_file_operations; |
| 8773 | inode->i_op = &btrfs_file_inode_operations; |
| 8774 | |
Josef Bacik | a1b075d | 2010-11-19 20:36:11 +0000 | [diff] [blame] | 8775 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8776 | if (err) |
| 8777 | drop_inode = 1; |
| 8778 | else { |
| 8779 | inode->i_mapping->a_ops = &btrfs_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8780 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 8781 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8782 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8783 | if (drop_inode) |
| 8784 | goto out_unlock; |
| 8785 | |
| 8786 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8787 | if (!path) { |
| 8788 | err = -ENOMEM; |
| 8789 | drop_inode = 1; |
| 8790 | goto out_unlock; |
| 8791 | } |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 8792 | key.objectid = btrfs_ino(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8793 | key.offset = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8794 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 8795 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 8796 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 8797 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8798 | if (err) { |
| 8799 | drop_inode = 1; |
Julia Lawall | b083916 | 2011-05-14 07:10:51 +0000 | [diff] [blame] | 8800 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8801 | goto out_unlock; |
| 8802 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8803 | leaf = path->nodes[0]; |
| 8804 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 8805 | struct btrfs_file_extent_item); |
| 8806 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 8807 | btrfs_set_file_extent_type(leaf, ei, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8808 | BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8809 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
| 8810 | btrfs_set_file_extent_compression(leaf, ei, 0); |
| 8811 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); |
| 8812 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); |
| 8813 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8814 | ptr = btrfs_file_extent_inline_start(ei); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8815 | write_extent_buffer(leaf, symname, ptr, name_len); |
| 8816 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8817 | btrfs_free_path(path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8818 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8819 | inode->i_op = &btrfs_symlink_inode_operations; |
| 8820 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
Chris Mason | 0416008 | 2008-03-26 10:28:07 -0400 | [diff] [blame] | 8821 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8822 | inode_set_bytes(inode, name_len); |
Filipe David Borba Manana | f06becc | 2013-09-16 09:53:28 +0100 | [diff] [blame] | 8823 | btrfs_i_size_write(inode, name_len); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 8824 | err = btrfs_update_inode(trans, root, inode); |
| 8825 | if (err) |
| 8826 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8827 | |
| 8828 | out_unlock: |
Al Viro | 08c422c | 2011-12-23 07:58:13 -0500 | [diff] [blame] | 8829 | if (!err) |
| 8830 | d_instantiate(dentry, inode); |
Josef Bacik | 7ad85bb | 2012-01-12 19:10:12 -0500 | [diff] [blame] | 8831 | btrfs_end_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8832 | if (drop_inode) { |
| 8833 | inode_dec_link_count(inode); |
| 8834 | iput(inode); |
| 8835 | } |
Liu Bo | b53d3f5 | 2012-11-14 14:34:34 +0000 | [diff] [blame] | 8836 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8837 | return err; |
| 8838 | } |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 8839 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8840 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8841 | u64 start, u64 num_bytes, u64 min_size, |
| 8842 | loff_t actual_len, u64 *alloc_hint, |
| 8843 | struct btrfs_trans_handle *trans) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8844 | { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8845 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 8846 | struct extent_map *em; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8847 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 8848 | struct btrfs_key ins; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8849 | u64 cur_offset = start; |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8850 | u64 i_size; |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8851 | u64 cur_bytes; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8852 | int ret = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8853 | bool own_trans = true; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8854 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8855 | if (trans) |
| 8856 | own_trans = false; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8857 | while (num_bytes > 0) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8858 | if (own_trans) { |
| 8859 | trans = btrfs_start_transaction(root, 3); |
| 8860 | if (IS_ERR(trans)) { |
| 8861 | ret = PTR_ERR(trans); |
| 8862 | break; |
| 8863 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8864 | } |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8865 | |
Chris Mason | 154ea28 | 2013-03-05 11:11:26 -0500 | [diff] [blame] | 8866 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
| 8867 | cur_bytes = max(cur_bytes, min_size); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 8868 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8869 | *alloc_hint, &ins, 1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8870 | if (ret) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8871 | if (own_trans) |
| 8872 | btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8873 | break; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8874 | } |
| 8875 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8876 | ret = insert_reserved_file_extent(trans, inode, |
| 8877 | cur_offset, ins.objectid, |
| 8878 | ins.offset, ins.offset, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 8879 | ins.offset, 0, 0, 0, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8880 | BTRFS_FILE_EXTENT_PREALLOC); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8881 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8882 | btrfs_free_reserved_extent(root, ins.objectid, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8883 | ins.offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8884 | btrfs_abort_transaction(trans, root, ret); |
| 8885 | if (own_trans) |
| 8886 | btrfs_end_transaction(trans, root); |
| 8887 | break; |
| 8888 | } |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 8889 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8890 | cur_offset + ins.offset -1, 0); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8891 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8892 | em = alloc_extent_map(); |
| 8893 | if (!em) { |
| 8894 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 8895 | &BTRFS_I(inode)->runtime_flags); |
| 8896 | goto next; |
| 8897 | } |
| 8898 | |
| 8899 | em->start = cur_offset; |
| 8900 | em->orig_start = cur_offset; |
| 8901 | em->len = ins.offset; |
| 8902 | em->block_start = ins.objectid; |
| 8903 | em->block_len = ins.offset; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 8904 | em->orig_block_len = ins.offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 8905 | em->ram_bytes = ins.offset; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8906 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 8907 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 8908 | em->generation = trans->transid; |
| 8909 | |
| 8910 | while (1) { |
| 8911 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 8912 | ret = add_extent_mapping(em_tree, em, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 8913 | write_unlock(&em_tree->lock); |
| 8914 | if (ret != -EEXIST) |
| 8915 | break; |
| 8916 | btrfs_drop_extent_cache(inode, cur_offset, |
| 8917 | cur_offset + ins.offset - 1, |
| 8918 | 0); |
| 8919 | } |
| 8920 | free_extent_map(em); |
| 8921 | next: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8922 | num_bytes -= ins.offset; |
| 8923 | cur_offset += ins.offset; |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8924 | *alloc_hint = ins.objectid + ins.offset; |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8925 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 8926 | inode_inc_iversion(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8927 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 8928 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8929 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Yan, Zheng | efa5646 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8930 | (actual_len > inode->i_size) && |
| 8931 | (cur_offset > inode->i_size)) { |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8932 | if (cur_offset > actual_len) |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8933 | i_size = actual_len; |
Aneesh Kumar K.V | d1ea6a6 | 2010-01-20 07:28:54 +0000 | [diff] [blame] | 8934 | else |
Josef Bacik | 55a61d1 | 2010-11-22 18:50:32 +0000 | [diff] [blame] | 8935 | i_size = cur_offset; |
| 8936 | i_size_write(inode, i_size); |
| 8937 | btrfs_ordered_update_i_size(inode, i_size, NULL); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8938 | } |
| 8939 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8940 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8941 | |
| 8942 | if (ret) { |
| 8943 | btrfs_abort_transaction(trans, root, ret); |
| 8944 | if (own_trans) |
| 8945 | btrfs_end_transaction(trans, root); |
| 8946 | break; |
| 8947 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8948 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8949 | if (own_trans) |
| 8950 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 5a303d5 | 2009-11-12 09:34:52 +0000 | [diff] [blame] | 8951 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8952 | return ret; |
| 8953 | } |
| 8954 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8955 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
| 8956 | u64 start, u64 num_bytes, u64 min_size, |
| 8957 | loff_t actual_len, u64 *alloc_hint) |
| 8958 | { |
| 8959 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8960 | min_size, actual_len, alloc_hint, |
| 8961 | NULL); |
| 8962 | } |
| 8963 | |
| 8964 | int btrfs_prealloc_file_range_trans(struct inode *inode, |
| 8965 | struct btrfs_trans_handle *trans, int mode, |
| 8966 | u64 start, u64 num_bytes, u64 min_size, |
| 8967 | loff_t actual_len, u64 *alloc_hint) |
| 8968 | { |
| 8969 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, |
| 8970 | min_size, actual_len, alloc_hint, trans); |
| 8971 | } |
| 8972 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8973 | static int btrfs_set_page_dirty(struct page *page) |
| 8974 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8975 | return __set_page_dirty_nobuffers(page); |
| 8976 | } |
| 8977 | |
Al Viro | 10556cb2 | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 8978 | static int btrfs_permission(struct inode *inode, int mask) |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8979 | { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8980 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8981 | umode_t mode = inode->i_mode; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 8982 | |
Jeff Mahoney | cb6db4e | 2011-08-15 17:27:21 +0000 | [diff] [blame] | 8983 | if (mask & MAY_WRITE && |
| 8984 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { |
| 8985 | if (btrfs_root_readonly(root)) |
| 8986 | return -EROFS; |
| 8987 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) |
| 8988 | return -EACCES; |
| 8989 | } |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 8990 | return generic_permission(inode, mask); |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 8991 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 8992 | |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 8993 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 8994 | { |
| 8995 | struct btrfs_trans_handle *trans; |
| 8996 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 8997 | struct inode *inode = NULL; |
| 8998 | u64 objectid; |
| 8999 | u64 index; |
| 9000 | int ret = 0; |
| 9001 | |
| 9002 | /* |
| 9003 | * 5 units required for adding orphan entry |
| 9004 | */ |
| 9005 | trans = btrfs_start_transaction(root, 5); |
| 9006 | if (IS_ERR(trans)) |
| 9007 | return PTR_ERR(trans); |
| 9008 | |
| 9009 | ret = btrfs_find_free_ino(root, &objectid); |
| 9010 | if (ret) |
| 9011 | goto out; |
| 9012 | |
| 9013 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, |
| 9014 | btrfs_ino(dir), objectid, mode, &index); |
| 9015 | if (IS_ERR(inode)) { |
| 9016 | ret = PTR_ERR(inode); |
| 9017 | inode = NULL; |
| 9018 | goto out; |
| 9019 | } |
| 9020 | |
| 9021 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
| 9022 | if (ret) |
| 9023 | goto out; |
| 9024 | |
| 9025 | ret = btrfs_update_inode(trans, root, inode); |
| 9026 | if (ret) |
| 9027 | goto out; |
| 9028 | |
| 9029 | inode->i_fop = &btrfs_file_operations; |
| 9030 | inode->i_op = &btrfs_file_inode_operations; |
| 9031 | |
| 9032 | inode->i_mapping->a_ops = &btrfs_aops; |
| 9033 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
| 9034 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
| 9035 | |
| 9036 | ret = btrfs_orphan_add(trans, inode); |
| 9037 | if (ret) |
| 9038 | goto out; |
| 9039 | |
Filipe Manana | 5762b5c | 2014-08-01 00:10:32 +0100 | [diff] [blame] | 9040 | /* |
| 9041 | * We set number of links to 0 in btrfs_new_inode(), and here we set |
| 9042 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, |
| 9043 | * through: |
| 9044 | * |
| 9045 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() |
| 9046 | */ |
| 9047 | set_nlink(inode, 1); |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9048 | d_tmpfile(dentry, inode); |
| 9049 | mark_inode_dirty(inode); |
| 9050 | |
| 9051 | out: |
| 9052 | btrfs_end_transaction(trans, root); |
| 9053 | if (ret) |
| 9054 | iput(inode); |
| 9055 | btrfs_balance_delayed_items(root); |
| 9056 | btrfs_btree_balance_dirty(root); |
| 9057 | |
| 9058 | return ret; |
| 9059 | } |
| 9060 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9061 | static const struct inode_operations btrfs_dir_inode_operations = { |
Chris Mason | 3394e16 | 2008-11-17 20:42:26 -0500 | [diff] [blame] | 9062 | .getattr = btrfs_getattr, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9063 | .lookup = btrfs_lookup, |
| 9064 | .create = btrfs_create, |
| 9065 | .unlink = btrfs_unlink, |
| 9066 | .link = btrfs_link, |
| 9067 | .mkdir = btrfs_mkdir, |
| 9068 | .rmdir = btrfs_rmdir, |
| 9069 | .rename = btrfs_rename, |
| 9070 | .symlink = btrfs_symlink, |
| 9071 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9072 | .mknod = btrfs_mknod, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9073 | .setxattr = btrfs_setxattr, |
| 9074 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9075 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9076 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9077 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9078 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9079 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9080 | .update_time = btrfs_update_time, |
Filipe Manana | ef3b9af | 2014-04-27 20:40:45 +0100 | [diff] [blame] | 9081 | .tmpfile = btrfs_tmpfile, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9082 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9083 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9084 | .lookup = btrfs_lookup, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9085 | .permission = btrfs_permission, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9086 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9087 | .set_acl = btrfs_set_acl, |
Guangyu Sun | 93fd63c | 2013-09-16 10:42:03 -0700 | [diff] [blame] | 9088 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9089 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9090 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 9091 | static const struct file_operations btrfs_dir_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9092 | .llseek = generic_file_llseek, |
| 9093 | .read = generic_read_dir, |
Al Viro | 9cdda8d | 2013-05-22 16:48:09 -0400 | [diff] [blame] | 9094 | .iterate = btrfs_real_readdir, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9095 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9096 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 9097 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9098 | #endif |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 9099 | .release = btrfs_release_file, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9100 | .fsync = btrfs_sync_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9101 | }; |
| 9102 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 9103 | static struct extent_io_ops btrfs_extent_io_ops = { |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9104 | .fill_delalloc = run_delalloc_range, |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 9105 | .submit_bio_hook = btrfs_submit_bio_hook, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9106 | .merge_bio_hook = btrfs_merge_bio_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9107 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9108 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 9109 | .writepage_start_hook = btrfs_writepage_start_hook, |
Chris Mason | b0c68f8 | 2008-01-31 11:05:37 -0500 | [diff] [blame] | 9110 | .set_bit_hook = btrfs_set_bit_hook, |
| 9111 | .clear_bit_hook = btrfs_clear_bit_hook, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 9112 | .merge_extent_hook = btrfs_merge_extent_hook, |
| 9113 | .split_extent_hook = btrfs_split_extent_hook, |
Chris Mason | 07157aa | 2007-08-30 08:50:51 -0400 | [diff] [blame] | 9114 | }; |
| 9115 | |
Chris Mason | 3505439 | 2009-01-21 13:11:13 -0500 | [diff] [blame] | 9116 | /* |
| 9117 | * btrfs doesn't support the bmap operation because swapfiles |
| 9118 | * use bmap to make a mapping of extents in the file. They assume |
| 9119 | * these extents won't change over the life of the file and they |
| 9120 | * use the bmap result to do IO directly to the drive. |
| 9121 | * |
| 9122 | * the btrfs bmap call would return logical addresses that aren't |
| 9123 | * suitable for IO and they also will change frequently as COW |
| 9124 | * operations happen. So, swapfile + btrfs == corruption. |
| 9125 | * |
| 9126 | * For now we're avoiding this by dropping bmap. |
| 9127 | */ |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9128 | static const struct address_space_operations btrfs_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9129 | .readpage = btrfs_readpage, |
| 9130 | .writepage = btrfs_writepage, |
Chris Mason | b293f02e | 2007-11-01 19:45:34 -0400 | [diff] [blame] | 9131 | .writepages = btrfs_writepages, |
Chris Mason | 3ab2fb5 | 2007-11-08 10:59:22 -0500 | [diff] [blame] | 9132 | .readpages = btrfs_readpages, |
Chris Mason | 1643298 | 2008-04-10 10:23:21 -0400 | [diff] [blame] | 9133 | .direct_IO = btrfs_direct_IO, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9134 | .invalidatepage = btrfs_invalidatepage, |
| 9135 | .releasepage = btrfs_releasepage, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 9136 | .set_page_dirty = btrfs_set_page_dirty, |
Andi Kleen | 465fdd9 | 2009-09-16 11:50:18 +0200 | [diff] [blame] | 9137 | .error_remove_page = generic_error_remove_page, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9138 | }; |
| 9139 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 9140 | static const struct address_space_operations btrfs_symlink_aops = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9141 | .readpage = btrfs_readpage, |
| 9142 | .writepage = btrfs_writepage, |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 9143 | .invalidatepage = btrfs_invalidatepage, |
| 9144 | .releasepage = btrfs_releasepage, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9145 | }; |
| 9146 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9147 | static const struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9148 | .getattr = btrfs_getattr, |
| 9149 | .setattr = btrfs_setattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9150 | .setxattr = btrfs_setxattr, |
| 9151 | .getxattr = btrfs_getxattr, |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 9152 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9153 | .removexattr = btrfs_removexattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9154 | .permission = btrfs_permission, |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 9155 | .fiemap = btrfs_fiemap, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9156 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9157 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9158 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9159 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9160 | static const struct inode_operations btrfs_special_inode_operations = { |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9161 | .getattr = btrfs_getattr, |
| 9162 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9163 | .permission = btrfs_permission, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9164 | .setxattr = btrfs_setxattr, |
| 9165 | .getxattr = btrfs_getxattr, |
Josef Bacik | 33268ea | 2008-07-24 12:16:36 -0400 | [diff] [blame] | 9166 | .listxattr = btrfs_listxattr, |
Christoph Hellwig | 95819c0 | 2008-08-28 06:21:17 -0400 | [diff] [blame] | 9167 | .removexattr = btrfs_removexattr, |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 9168 | .get_acl = btrfs_get_acl, |
Christoph Hellwig | 996a710 | 2013-12-20 05:16:43 -0800 | [diff] [blame] | 9169 | .set_acl = btrfs_set_acl, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9170 | .update_time = btrfs_update_time, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 9171 | }; |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 9172 | static const struct inode_operations btrfs_symlink_inode_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9173 | .readlink = generic_readlink, |
| 9174 | .follow_link = page_follow_link_light, |
| 9175 | .put_link = page_put_link, |
Li Zefan | f209561 | 2010-11-19 02:05:24 +0000 | [diff] [blame] | 9176 | .getattr = btrfs_getattr, |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 9177 | .setattr = btrfs_setattr, |
Yan | fdebe2b | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 9178 | .permission = btrfs_permission, |
Jim Owens | 0279b4c | 2009-02-04 09:29:13 -0500 | [diff] [blame] | 9179 | .setxattr = btrfs_setxattr, |
| 9180 | .getxattr = btrfs_getxattr, |
| 9181 | .listxattr = btrfs_listxattr, |
| 9182 | .removexattr = btrfs_removexattr, |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 9183 | .update_time = btrfs_update_time, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 9184 | }; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9185 | |
Alexey Dobriyan | 82d339d | 2009-10-09 09:54:36 -0400 | [diff] [blame] | 9186 | const struct dentry_operations btrfs_dentry_operations = { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9187 | .d_delete = btrfs_dentry_delete, |
Josef Bacik | b4aff1f | 2011-06-28 16:18:59 -0400 | [diff] [blame] | 9188 | .d_release = btrfs_dentry_release, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9189 | }; |