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 | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/highmem.h> |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/string.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
| 26 | #include <linux/mpage.h> |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 27 | #include <linux/falloc.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 28 | #include <linux/swap.h> |
| 29 | #include <linux/writeback.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 30 | #include <linux/compat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 32 | #include <linux/btrfs.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 33 | #include <linux/uio.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame^] | 34 | #include <linux/iversion.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 35 | #include "ctree.h" |
| 36 | #include "disk-io.h" |
| 37 | #include "transaction.h" |
| 38 | #include "btrfs_inode.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 39 | #include "print-tree.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 40 | #include "tree-log.h" |
| 41 | #include "locking.h" |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 42 | #include "volumes.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 43 | #include "qgroup.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 44 | #include "compression.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 45 | |
Miao Xie | 9247f31 | 2012-11-26 09:24:43 +0000 | [diff] [blame] | 46 | static struct kmem_cache *btrfs_inode_defrag_cachep; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 47 | /* |
| 48 | * when auto defrag is enabled we |
| 49 | * queue up these defrag structs to remember which |
| 50 | * inodes need defragging passes |
| 51 | */ |
| 52 | struct inode_defrag { |
| 53 | struct rb_node rb_node; |
| 54 | /* objectid */ |
| 55 | u64 ino; |
| 56 | /* |
| 57 | * transid where the defrag was added, we search for |
| 58 | * extents newer than this |
| 59 | */ |
| 60 | u64 transid; |
| 61 | |
| 62 | /* root objectid */ |
| 63 | u64 root; |
| 64 | |
| 65 | /* last offset we were able to defrag */ |
| 66 | u64 last_offset; |
| 67 | |
| 68 | /* if we've wrapped around back to zero once already */ |
| 69 | int cycled; |
| 70 | }; |
| 71 | |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 72 | static int __compare_inode_defrag(struct inode_defrag *defrag1, |
| 73 | struct inode_defrag *defrag2) |
| 74 | { |
| 75 | if (defrag1->root > defrag2->root) |
| 76 | return 1; |
| 77 | else if (defrag1->root < defrag2->root) |
| 78 | return -1; |
| 79 | else if (defrag1->ino > defrag2->ino) |
| 80 | return 1; |
| 81 | else if (defrag1->ino < defrag2->ino) |
| 82 | return -1; |
| 83 | else |
| 84 | return 0; |
| 85 | } |
| 86 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 87 | /* pop a record for an inode into the defrag tree. The lock |
| 88 | * must be held already |
| 89 | * |
| 90 | * If you're inserting a record for an older transid than an |
| 91 | * existing record, the transid already in the tree is lowered |
| 92 | * |
| 93 | * If an existing record is found the defrag item you |
| 94 | * pass in is freed |
| 95 | */ |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 96 | static int __btrfs_add_inode_defrag(struct btrfs_inode *inode, |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 97 | struct inode_defrag *defrag) |
| 98 | { |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 99 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 100 | struct inode_defrag *entry; |
| 101 | struct rb_node **p; |
| 102 | struct rb_node *parent = NULL; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 103 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 104 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 105 | p = &fs_info->defrag_inodes.rb_node; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 106 | while (*p) { |
| 107 | parent = *p; |
| 108 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
| 109 | |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 110 | ret = __compare_inode_defrag(defrag, entry); |
| 111 | if (ret < 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 112 | p = &parent->rb_left; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 113 | else if (ret > 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 114 | p = &parent->rb_right; |
| 115 | else { |
| 116 | /* if we're reinserting an entry for |
| 117 | * an old defrag run, make sure to |
| 118 | * lower the transid of our existing record |
| 119 | */ |
| 120 | if (defrag->transid < entry->transid) |
| 121 | entry->transid = defrag->transid; |
| 122 | if (defrag->last_offset > entry->last_offset) |
| 123 | entry->last_offset = defrag->last_offset; |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 124 | return -EEXIST; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 125 | } |
| 126 | } |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 127 | set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 128 | rb_link_node(&defrag->rb_node, parent, p); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 129 | rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes); |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 130 | return 0; |
| 131 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 132 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 133 | static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info) |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 134 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 135 | if (!btrfs_test_opt(fs_info, AUTO_DEFRAG)) |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 136 | return 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 137 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 138 | if (btrfs_fs_closing(fs_info)) |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | |
| 141 | return 1; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * insert a defrag record for this inode if auto defrag is |
| 146 | * enabled |
| 147 | */ |
| 148 | int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 149 | struct btrfs_inode *inode) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 150 | { |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 151 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 152 | struct btrfs_root *root = inode->root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 153 | struct inode_defrag *defrag; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 154 | u64 transid; |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 155 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 156 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 157 | if (!__need_auto_defrag(fs_info)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 158 | return 0; |
| 159 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 160 | if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 161 | return 0; |
| 162 | |
| 163 | if (trans) |
| 164 | transid = trans->transid; |
| 165 | else |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 166 | transid = inode->root->last_trans; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 167 | |
Miao Xie | 9247f31 | 2012-11-26 09:24:43 +0000 | [diff] [blame] | 168 | defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 169 | if (!defrag) |
| 170 | return -ENOMEM; |
| 171 | |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 172 | defrag->ino = btrfs_ino(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 173 | defrag->transid = transid; |
| 174 | defrag->root = root->root_key.objectid; |
| 175 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 176 | spin_lock(&fs_info->defrag_inodes_lock); |
Nikolay Borisov | 6158e1c | 2017-02-20 13:50:43 +0200 | [diff] [blame] | 177 | if (!test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) { |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 178 | /* |
| 179 | * If we set IN_DEFRAG flag and evict the inode from memory, |
| 180 | * and then re-read this inode, this new inode doesn't have |
| 181 | * IN_DEFRAG flag. At the case, we may find the existed defrag. |
| 182 | */ |
| 183 | ret = __btrfs_add_inode_defrag(inode, defrag); |
| 184 | if (ret) |
| 185 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
| 186 | } else { |
Miao Xie | 9247f31 | 2012-11-26 09:24:43 +0000 | [diff] [blame] | 187 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 188 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 189 | spin_unlock(&fs_info->defrag_inodes_lock); |
Wanlong Gao | a0f98dd | 2011-07-18 12:19:35 +0000 | [diff] [blame] | 190 | return 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 194 | * Requeue the defrag object. If there is a defrag object that points to |
| 195 | * the same inode in the tree, we will merge them together (by |
| 196 | * __btrfs_add_inode_defrag()) and free the one that we want to requeue. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 197 | */ |
Nikolay Borisov | 46e5979 | 2017-02-20 13:50:44 +0200 | [diff] [blame] | 198 | static void btrfs_requeue_inode_defrag(struct btrfs_inode *inode, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 199 | struct inode_defrag *defrag) |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 200 | { |
Nikolay Borisov | 46e5979 | 2017-02-20 13:50:44 +0200 | [diff] [blame] | 201 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 202 | int ret; |
| 203 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 204 | if (!__need_auto_defrag(fs_info)) |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 205 | goto out; |
| 206 | |
| 207 | /* |
| 208 | * Here we don't check the IN_DEFRAG flag, because we need merge |
| 209 | * them together. |
| 210 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 211 | spin_lock(&fs_info->defrag_inodes_lock); |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 212 | ret = __btrfs_add_inode_defrag(inode, defrag); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 213 | spin_unlock(&fs_info->defrag_inodes_lock); |
Miao Xie | 8ddc473 | 2012-11-26 09:25:38 +0000 | [diff] [blame] | 214 | if (ret) |
| 215 | goto out; |
| 216 | return; |
| 217 | out: |
| 218 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
| 219 | } |
| 220 | |
| 221 | /* |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 222 | * pick the defragable inode that we want, if it doesn't exist, we will get |
| 223 | * the next one. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 224 | */ |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 225 | static struct inode_defrag * |
| 226 | btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 227 | { |
| 228 | struct inode_defrag *entry = NULL; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 229 | struct inode_defrag tmp; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 230 | struct rb_node *p; |
| 231 | struct rb_node *parent = NULL; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 232 | int ret; |
| 233 | |
| 234 | tmp.ino = ino; |
| 235 | tmp.root = root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 236 | |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 237 | spin_lock(&fs_info->defrag_inodes_lock); |
| 238 | p = fs_info->defrag_inodes.rb_node; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 239 | while (p) { |
| 240 | parent = p; |
| 241 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
| 242 | |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 243 | ret = __compare_inode_defrag(&tmp, entry); |
| 244 | if (ret < 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 245 | p = parent->rb_left; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 246 | else if (ret > 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 247 | p = parent->rb_right; |
| 248 | else |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 249 | goto out; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 252 | if (parent && __compare_inode_defrag(&tmp, entry) > 0) { |
| 253 | parent = rb_next(parent); |
| 254 | if (parent) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 255 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 256 | else |
| 257 | entry = NULL; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 258 | } |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 259 | out: |
| 260 | if (entry) |
| 261 | rb_erase(parent, &fs_info->defrag_inodes); |
| 262 | spin_unlock(&fs_info->defrag_inodes_lock); |
| 263 | return entry; |
| 264 | } |
| 265 | |
| 266 | void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info) |
| 267 | { |
| 268 | struct inode_defrag *defrag; |
| 269 | struct rb_node *node; |
| 270 | |
| 271 | spin_lock(&fs_info->defrag_inodes_lock); |
| 272 | node = rb_first(&fs_info->defrag_inodes); |
| 273 | while (node) { |
| 274 | rb_erase(node, &fs_info->defrag_inodes); |
| 275 | defrag = rb_entry(node, struct inode_defrag, rb_node); |
| 276 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
| 277 | |
David Sterba | 351810c | 2015-01-08 15:20:54 +0100 | [diff] [blame] | 278 | cond_resched_lock(&fs_info->defrag_inodes_lock); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 279 | |
| 280 | node = rb_first(&fs_info->defrag_inodes); |
| 281 | } |
| 282 | spin_unlock(&fs_info->defrag_inodes_lock); |
| 283 | } |
| 284 | |
| 285 | #define BTRFS_DEFRAG_BATCH 1024 |
| 286 | |
| 287 | static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info, |
| 288 | struct inode_defrag *defrag) |
| 289 | { |
| 290 | struct btrfs_root *inode_root; |
| 291 | struct inode *inode; |
| 292 | struct btrfs_key key; |
| 293 | struct btrfs_ioctl_defrag_range_args range; |
| 294 | int num_defrag; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 295 | int index; |
| 296 | int ret; |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 297 | |
| 298 | /* get the inode */ |
| 299 | key.objectid = defrag->root; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 300 | key.type = BTRFS_ROOT_ITEM_KEY; |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 301 | key.offset = (u64)-1; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 302 | |
| 303 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 304 | |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 305 | inode_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 306 | if (IS_ERR(inode_root)) { |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 307 | ret = PTR_ERR(inode_root); |
| 308 | goto cleanup; |
| 309 | } |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 310 | |
| 311 | key.objectid = defrag->ino; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 312 | key.type = BTRFS_INODE_ITEM_KEY; |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 313 | key.offset = 0; |
| 314 | inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL); |
| 315 | if (IS_ERR(inode)) { |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 316 | ret = PTR_ERR(inode); |
| 317 | goto cleanup; |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 318 | } |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 319 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 320 | |
| 321 | /* do a chunk of defrag */ |
| 322 | clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags); |
| 323 | memset(&range, 0, sizeof(range)); |
| 324 | range.len = (u64)-1; |
| 325 | range.start = defrag->last_offset; |
Miao Xie | b66f00d | 2012-11-26 09:27:29 +0000 | [diff] [blame] | 326 | |
| 327 | sb_start_write(fs_info->sb); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 328 | num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid, |
| 329 | BTRFS_DEFRAG_BATCH); |
Miao Xie | b66f00d | 2012-11-26 09:27:29 +0000 | [diff] [blame] | 330 | sb_end_write(fs_info->sb); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 331 | /* |
| 332 | * if we filled the whole defrag batch, there |
| 333 | * must be more work to do. Queue this defrag |
| 334 | * again |
| 335 | */ |
| 336 | if (num_defrag == BTRFS_DEFRAG_BATCH) { |
| 337 | defrag->last_offset = range.start; |
Nikolay Borisov | 46e5979 | 2017-02-20 13:50:44 +0200 | [diff] [blame] | 338 | btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 339 | } else if (defrag->last_offset && !defrag->cycled) { |
| 340 | /* |
| 341 | * we didn't fill our defrag batch, but |
| 342 | * we didn't start at zero. Make sure we loop |
| 343 | * around to the start of the file. |
| 344 | */ |
| 345 | defrag->last_offset = 0; |
| 346 | defrag->cycled = 1; |
Nikolay Borisov | 46e5979 | 2017-02-20 13:50:44 +0200 | [diff] [blame] | 347 | btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag); |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 348 | } else { |
| 349 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
| 350 | } |
| 351 | |
| 352 | iput(inode); |
| 353 | return 0; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 354 | cleanup: |
| 355 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 356 | kmem_cache_free(btrfs_inode_defrag_cachep, defrag); |
| 357 | return ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * run through the list of inodes in the FS that need |
| 362 | * defragging |
| 363 | */ |
| 364 | int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info) |
| 365 | { |
| 366 | struct inode_defrag *defrag; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 367 | u64 first_ino = 0; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 368 | u64 root_objectid = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 369 | |
| 370 | atomic_inc(&fs_info->defrag_running); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 371 | while (1) { |
Miao Xie | dc81cdc | 2013-02-20 23:32:52 -0700 | [diff] [blame] | 372 | /* Pause the auto defragger. */ |
| 373 | if (test_bit(BTRFS_FS_STATE_REMOUNTING, |
| 374 | &fs_info->fs_state)) |
| 375 | break; |
| 376 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 377 | if (!__need_auto_defrag(fs_info)) |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 378 | break; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 379 | |
| 380 | /* find an inode to defrag */ |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 381 | defrag = btrfs_pick_defrag_inode(fs_info, root_objectid, |
| 382 | first_ino); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 383 | if (!defrag) { |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 384 | if (root_objectid || first_ino) { |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 385 | root_objectid = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 386 | first_ino = 0; |
| 387 | continue; |
| 388 | } else { |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 393 | first_ino = defrag->ino + 1; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 394 | root_objectid = defrag->root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 395 | |
Miao Xie | 26176e7 | 2012-11-26 09:26:20 +0000 | [diff] [blame] | 396 | __btrfs_run_defrag_inode(fs_info, defrag); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 397 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 398 | atomic_dec(&fs_info->defrag_running); |
| 399 | |
| 400 | /* |
| 401 | * during unmount, we use the transaction_wait queue to |
| 402 | * wait for the defragger to stop |
| 403 | */ |
| 404 | wake_up(&fs_info->transaction_wait); |
| 405 | return 0; |
| 406 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 407 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 408 | /* simple helper to fault in pages and copy. This should go away |
| 409 | * and be replaced with calls into generic code. |
| 410 | */ |
Zhao Lei | ee22f0c | 2016-01-06 18:47:31 +0800 | [diff] [blame] | 411 | static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 412 | struct page **prepared_pages, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 413 | struct iov_iter *i) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 414 | { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 415 | size_t copied = 0; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 416 | size_t total_copied = 0; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 417 | int pg = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 418 | int offset = pos & (PAGE_SIZE - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 419 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 420 | while (write_bytes > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 421 | size_t count = min_t(size_t, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 422 | PAGE_SIZE - offset, write_bytes); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 423 | struct page *page = prepared_pages[pg]; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 424 | /* |
| 425 | * Copy data from userspace to the current page |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 426 | */ |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 427 | copied = iov_iter_copy_from_user_atomic(page, i, offset, count); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 428 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 429 | /* Flush processor's dcache for this page */ |
| 430 | flush_dcache_page(page); |
Chris Mason | 31339ac | 2011-03-07 11:10:24 -0500 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * if we get a partial write, we can end up with |
| 434 | * partially up to date pages. These add |
| 435 | * a lot of complexity, so make sure they don't |
| 436 | * happen by forcing this copy to be retried. |
| 437 | * |
| 438 | * The rest of the btrfs_file_write code will fall |
| 439 | * back to page at a time copies after we return 0. |
| 440 | */ |
| 441 | if (!PageUptodate(page) && copied < count) |
| 442 | copied = 0; |
| 443 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 444 | iov_iter_advance(i, copied); |
| 445 | write_bytes -= copied; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 446 | total_copied += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 447 | |
Al Viro | b30ac0f | 2014-04-03 14:29:04 -0400 | [diff] [blame] | 448 | /* Return to btrfs_file_write_iter to fault page */ |
Josef Bacik | 9f570b8 | 2011-01-25 12:42:37 -0500 | [diff] [blame] | 449 | if (unlikely(copied == 0)) |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 450 | break; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 451 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 452 | if (copied < PAGE_SIZE - offset) { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 453 | offset += copied; |
| 454 | } else { |
| 455 | pg++; |
| 456 | offset = 0; |
| 457 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 458 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 459 | return total_copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 460 | } |
| 461 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 462 | /* |
| 463 | * unlocks pages after btrfs_file_write is done with them |
| 464 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 465 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 466 | { |
| 467 | size_t i; |
| 468 | for (i = 0; i < num_pages; i++) { |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 469 | /* page checked is some magic around finding pages that |
| 470 | * have been modified without going through btrfs_set_page_dirty |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 471 | * clear it here. There should be no need to mark the pages |
| 472 | * accessed as prepare_pages should have marked them accessed |
| 473 | * in prepare_pages via find_or_create_page() |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 474 | */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 475 | ClearPageChecked(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 476 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 477 | put_page(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Filipe Manana | f48bf66 | 2017-11-03 22:26:44 +0000 | [diff] [blame] | 481 | static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode, |
| 482 | const u64 start, |
| 483 | const u64 len, |
| 484 | struct extent_state **cached_state) |
| 485 | { |
| 486 | u64 search_start = start; |
| 487 | const u64 end = start + len - 1; |
| 488 | |
| 489 | while (search_start < end) { |
| 490 | const u64 search_len = end - search_start + 1; |
| 491 | struct extent_map *em; |
| 492 | u64 em_len; |
| 493 | int ret = 0; |
| 494 | |
| 495 | em = btrfs_get_extent(inode, NULL, 0, search_start, |
| 496 | search_len, 0); |
| 497 | if (IS_ERR(em)) |
| 498 | return PTR_ERR(em); |
| 499 | |
| 500 | if (em->block_start != EXTENT_MAP_HOLE) |
| 501 | goto next; |
| 502 | |
| 503 | em_len = em->len; |
| 504 | if (em->start < search_start) |
| 505 | em_len -= search_start - em->start; |
| 506 | if (em_len > search_len) |
| 507 | em_len = search_len; |
| 508 | |
| 509 | ret = set_extent_bit(&inode->io_tree, search_start, |
| 510 | search_start + em_len - 1, |
| 511 | EXTENT_DELALLOC_NEW, |
| 512 | NULL, cached_state, GFP_NOFS); |
| 513 | next: |
| 514 | search_start = extent_map_end(em); |
| 515 | free_extent_map(em); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | } |
| 519 | return 0; |
| 520 | } |
| 521 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 522 | /* |
| 523 | * after copy_from_user, pages need to be dirtied and we need to make |
| 524 | * sure holes are created between the current EOF and the start of |
| 525 | * any next extents (if required). |
| 526 | * |
| 527 | * this also makes the decision about creating an inline extent vs |
| 528 | * doing real data extents, marking pages dirty and delalloc as required. |
| 529 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 530 | int btrfs_dirty_pages(struct inode *inode, struct page **pages, |
| 531 | size_t num_pages, loff_t pos, size_t write_bytes, |
| 532 | struct extent_state **cached) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 533 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 534 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 535 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 536 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 537 | u64 num_bytes; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 538 | u64 start_pos; |
| 539 | u64 end_of_last_block; |
| 540 | u64 end_pos = pos + write_bytes; |
| 541 | loff_t isize = i_size_read(inode); |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 542 | unsigned int extra_bits = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 543 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 544 | start_pos = pos & ~((u64) fs_info->sectorsize - 1); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 545 | num_bytes = round_up(write_bytes + pos - start_pos, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 546 | fs_info->sectorsize); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 547 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 548 | end_of_last_block = start_pos + num_bytes - 1; |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 549 | |
| 550 | if (!btrfs_is_free_space_inode(BTRFS_I(inode))) { |
| 551 | if (start_pos >= isize && |
| 552 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) { |
| 553 | /* |
| 554 | * There can't be any extents following eof in this case |
| 555 | * so just set the delalloc new bit for the range |
| 556 | * directly. |
| 557 | */ |
| 558 | extra_bits |= EXTENT_DELALLOC_NEW; |
| 559 | } else { |
| 560 | err = btrfs_find_new_delalloc_bytes(BTRFS_I(inode), |
| 561 | start_pos, |
| 562 | num_bytes, cached); |
| 563 | if (err) |
| 564 | return err; |
| 565 | } |
| 566 | } |
| 567 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 568 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 569 | extra_bits, cached, 0); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 570 | if (err) |
| 571 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 572 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 573 | for (i = 0; i < num_pages; i++) { |
| 574 | struct page *p = pages[i]; |
| 575 | SetPageUptodate(p); |
| 576 | ClearPageChecked(p); |
| 577 | set_page_dirty(p); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 578 | } |
Josef Bacik | 9f570b8 | 2011-01-25 12:42:37 -0500 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * we've only changed i_size in ram, and we haven't updated |
| 582 | * the disk i_size. There is no need to log the inode |
| 583 | * at this time. |
| 584 | */ |
| 585 | if (end_pos > isize) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 586 | i_size_write(inode, end_pos); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 587 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 590 | /* |
| 591 | * this drops all the extents in the cache that intersect the range |
| 592 | * [start, end]. Existing extents are split as required. |
| 593 | */ |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 594 | void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end, |
Josef Bacik | 7014cdb | 2012-08-30 20:06:49 -0400 | [diff] [blame] | 595 | int skip_pinned) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 596 | { |
| 597 | struct extent_map *em; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 598 | struct extent_map *split = NULL; |
| 599 | struct extent_map *split2 = NULL; |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 600 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 601 | u64 len = end - start + 1; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 602 | u64 gen; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 603 | int ret; |
| 604 | int testend = 1; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 605 | unsigned long flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 606 | int compressed = 0; |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 607 | bool modified; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 608 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 609 | WARN_ON(end < start); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 610 | if (end == (u64)-1) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 611 | len = (u64)-1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 612 | testend = 0; |
| 613 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 614 | while (1) { |
Josef Bacik | 7014cdb | 2012-08-30 20:06:49 -0400 | [diff] [blame] | 615 | int no_splits = 0; |
| 616 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 617 | modified = false; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 618 | if (!split) |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 619 | split = alloc_extent_map(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 620 | if (!split2) |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 621 | split2 = alloc_extent_map(); |
Josef Bacik | 7014cdb | 2012-08-30 20:06:49 -0400 | [diff] [blame] | 622 | if (!split || !split2) |
| 623 | no_splits = 1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 624 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 625 | write_lock(&em_tree->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 626 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 627 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 628 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 629 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 630 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 631 | flags = em->flags; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 632 | gen = em->generation; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 633 | if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) { |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 634 | if (testend && em->start + em->len >= start + len) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 635 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 636 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 637 | break; |
| 638 | } |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 639 | start = em->start + em->len; |
| 640 | if (testend) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 641 | len = start + len - (em->start + em->len); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 642 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 643 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 644 | continue; |
| 645 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 646 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | 3ce7e67 | 2008-07-31 15:42:54 -0400 | [diff] [blame] | 647 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 3b27759 | 2013-03-15 08:46:39 -0600 | [diff] [blame] | 648 | clear_bit(EXTENT_FLAG_LOGGING, &flags); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 649 | modified = !list_empty(&em->list); |
Josef Bacik | 7014cdb | 2012-08-30 20:06:49 -0400 | [diff] [blame] | 650 | if (no_splits) |
| 651 | goto next; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 652 | |
Josef Bacik | ee20a98 | 2013-07-11 10:34:59 -0400 | [diff] [blame] | 653 | if (em->start < start) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 654 | split->start = em->start; |
| 655 | split->len = start - em->start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 656 | |
Josef Bacik | ee20a98 | 2013-07-11 10:34:59 -0400 | [diff] [blame] | 657 | if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 658 | split->orig_start = em->orig_start; |
| 659 | split->block_start = em->block_start; |
| 660 | |
| 661 | if (compressed) |
| 662 | split->block_len = em->block_len; |
| 663 | else |
| 664 | split->block_len = split->len; |
| 665 | split->orig_block_len = max(split->block_len, |
| 666 | em->orig_block_len); |
| 667 | split->ram_bytes = em->ram_bytes; |
| 668 | } else { |
| 669 | split->orig_start = split->start; |
| 670 | split->block_len = 0; |
| 671 | split->block_start = em->block_start; |
| 672 | split->orig_block_len = 0; |
| 673 | split->ram_bytes = split->len; |
| 674 | } |
| 675 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 676 | split->generation = gen; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 677 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 678 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 679 | split->compress_type = em->compress_type; |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 680 | replace_extent_mapping(em_tree, em, split, modified); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 681 | free_extent_map(split); |
| 682 | split = split2; |
| 683 | split2 = NULL; |
| 684 | } |
Josef Bacik | ee20a98 | 2013-07-11 10:34:59 -0400 | [diff] [blame] | 685 | if (testend && em->start + em->len > start + len) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 686 | u64 diff = start + len - em->start; |
| 687 | |
| 688 | split->start = start + len; |
| 689 | split->len = em->start + em->len - (start + len); |
| 690 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 691 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 692 | split->compress_type = em->compress_type; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 693 | split->generation = gen; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 694 | |
Josef Bacik | ee20a98 | 2013-07-11 10:34:59 -0400 | [diff] [blame] | 695 | if (em->block_start < EXTENT_MAP_LAST_BYTE) { |
| 696 | split->orig_block_len = max(em->block_len, |
| 697 | em->orig_block_len); |
| 698 | |
| 699 | split->ram_bytes = em->ram_bytes; |
| 700 | if (compressed) { |
| 701 | split->block_len = em->block_len; |
| 702 | split->block_start = em->block_start; |
| 703 | split->orig_start = em->orig_start; |
| 704 | } else { |
| 705 | split->block_len = split->len; |
| 706 | split->block_start = em->block_start |
| 707 | + diff; |
| 708 | split->orig_start = em->orig_start; |
| 709 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 710 | } else { |
Josef Bacik | ee20a98 | 2013-07-11 10:34:59 -0400 | [diff] [blame] | 711 | split->ram_bytes = split->len; |
| 712 | split->orig_start = split->start; |
| 713 | split->block_len = 0; |
| 714 | split->block_start = em->block_start; |
| 715 | split->orig_block_len = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 716 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 717 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 718 | if (extent_map_in_tree(em)) { |
| 719 | replace_extent_mapping(em_tree, em, split, |
| 720 | modified); |
| 721 | } else { |
| 722 | ret = add_extent_mapping(em_tree, split, |
| 723 | modified); |
| 724 | ASSERT(ret == 0); /* Logic error */ |
| 725 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 726 | free_extent_map(split); |
| 727 | split = NULL; |
| 728 | } |
Josef Bacik | 7014cdb | 2012-08-30 20:06:49 -0400 | [diff] [blame] | 729 | next: |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 730 | if (extent_map_in_tree(em)) |
| 731 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 732 | write_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 733 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 734 | /* once for us */ |
| 735 | free_extent_map(em); |
| 736 | /* once for the tree*/ |
| 737 | free_extent_map(em); |
| 738 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 739 | if (split) |
| 740 | free_extent_map(split); |
| 741 | if (split2) |
| 742 | free_extent_map(split2); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 745 | /* |
| 746 | * this is very complex, but the basic idea is to drop all extents |
| 747 | * in the range start - end. hint_block is filled in with a block number |
| 748 | * that would be a good hint to the block allocator for this file. |
| 749 | * |
| 750 | * If an extent intersects the range but is not entirely inside the range |
| 751 | * it is either truncated or split. Anything entirely inside the range |
| 752 | * is deleted from the tree. |
| 753 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 754 | int __btrfs_drop_extents(struct btrfs_trans_handle *trans, |
| 755 | struct btrfs_root *root, struct inode *inode, |
| 756 | struct btrfs_path *path, u64 start, u64 end, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 757 | u64 *drop_end, int drop_cache, |
| 758 | int replace_extent, |
| 759 | u32 extent_item_size, |
| 760 | int *key_inserted) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 761 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 762 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 763 | struct extent_buffer *leaf; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 764 | struct btrfs_file_extent_item *fi; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 765 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 766 | struct btrfs_key new_key; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 767 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 768 | u64 search_start = start; |
| 769 | u64 disk_bytenr = 0; |
| 770 | u64 num_bytes = 0; |
| 771 | u64 extent_offset = 0; |
| 772 | u64 extent_end = 0; |
Josef Bacik | 62fe51c1 | 2016-11-16 09:13:39 -0500 | [diff] [blame] | 773 | u64 last_end = start; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 774 | int del_nr = 0; |
| 775 | int del_slot = 0; |
| 776 | int extent_type; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 777 | int recow; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 778 | int ret; |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 779 | int modify_tree = -1; |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 780 | int update_refs; |
Josef Bacik | c3308f8 | 2012-09-14 14:51:22 -0400 | [diff] [blame] | 781 | int found = 0; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 782 | int leafs_visited = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 783 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 784 | if (drop_cache) |
Nikolay Borisov | dcdbc05 | 2017-02-20 13:50:45 +0200 | [diff] [blame] | 785 | btrfs_drop_extent_cache(BTRFS_I(inode), start, end - 1, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 786 | |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 787 | if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent) |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 788 | modify_tree = 0; |
| 789 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 790 | update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 791 | root == fs_info->tree_root); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 792 | while (1) { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 793 | recow = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 794 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 795 | search_start, modify_tree); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 796 | if (ret < 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 797 | break; |
| 798 | if (ret > 0 && path->slots[0] > 0 && search_start == start) { |
| 799 | leaf = path->nodes[0]; |
| 800 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 801 | if (key.objectid == ino && |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 802 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 803 | path->slots[0]--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 804 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 805 | ret = 0; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 806 | leafs_visited++; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 807 | next_slot: |
| 808 | leaf = path->nodes[0]; |
| 809 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 810 | BUG_ON(del_nr > 0); |
| 811 | ret = btrfs_next_leaf(root, path); |
| 812 | if (ret < 0) |
| 813 | break; |
| 814 | if (ret > 0) { |
| 815 | ret = 0; |
| 816 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 817 | } |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 818 | leafs_visited++; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 819 | leaf = path->nodes[0]; |
| 820 | recow = 1; |
| 821 | } |
| 822 | |
| 823 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Filipe Manana | aeafbf8 | 2015-11-06 13:33:33 +0000 | [diff] [blame] | 824 | |
| 825 | if (key.objectid > ino) |
| 826 | break; |
| 827 | if (WARN_ON_ONCE(key.objectid < ino) || |
| 828 | key.type < BTRFS_EXTENT_DATA_KEY) { |
| 829 | ASSERT(del_nr == 0); |
| 830 | path->slots[0]++; |
| 831 | goto next_slot; |
| 832 | } |
| 833 | if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 834 | break; |
| 835 | |
| 836 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 837 | struct btrfs_file_extent_item); |
| 838 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 839 | |
| 840 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 841 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 842 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 843 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 844 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
| 845 | extent_end = key.offset + |
| 846 | btrfs_file_extent_num_bytes(leaf, fi); |
| 847 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 848 | extent_end = key.offset + |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 849 | btrfs_file_extent_inline_len(leaf, |
| 850 | path->slots[0], fi); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 851 | } else { |
Filipe Manana | aeafbf8 | 2015-11-06 13:33:33 +0000 | [diff] [blame] | 852 | /* can't happen */ |
| 853 | BUG(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 854 | } |
| 855 | |
Filipe Manana | fc19c5e | 2014-04-29 13:18:40 +0100 | [diff] [blame] | 856 | /* |
| 857 | * Don't skip extent items representing 0 byte lengths. They |
| 858 | * used to be created (bug) if while punching holes we hit |
| 859 | * -ENOSPC condition. So if we find one here, just ensure we |
| 860 | * delete it, otherwise we would insert a new file extent item |
| 861 | * with the same key (offset) as that 0 bytes length file |
| 862 | * extent item in the call to setup_items_for_insert() later |
| 863 | * in this function. |
| 864 | */ |
Josef Bacik | 62fe51c1 | 2016-11-16 09:13:39 -0500 | [diff] [blame] | 865 | if (extent_end == key.offset && extent_end >= search_start) { |
| 866 | last_end = extent_end; |
Filipe Manana | fc19c5e | 2014-04-29 13:18:40 +0100 | [diff] [blame] | 867 | goto delete_extent_item; |
Josef Bacik | 62fe51c1 | 2016-11-16 09:13:39 -0500 | [diff] [blame] | 868 | } |
Filipe Manana | fc19c5e | 2014-04-29 13:18:40 +0100 | [diff] [blame] | 869 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 870 | if (extent_end <= search_start) { |
| 871 | path->slots[0]++; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 872 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Josef Bacik | c3308f8 | 2012-09-14 14:51:22 -0400 | [diff] [blame] | 875 | found = 1; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 876 | search_start = max(key.offset, start); |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 877 | if (recow || !modify_tree) { |
| 878 | modify_tree = -1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 879 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 880 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 881 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 882 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 883 | /* |
| 884 | * | - range to drop - | |
| 885 | * | -------- extent -------- | |
| 886 | */ |
| 887 | if (start > key.offset && end < extent_end) { |
| 888 | BUG_ON(del_nr > 0); |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 889 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
David Sterba | 3f9e3df | 2014-04-15 18:50:17 +0200 | [diff] [blame] | 890 | ret = -EOPNOTSUPP; |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 891 | break; |
| 892 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 893 | |
| 894 | memcpy(&new_key, &key, sizeof(new_key)); |
| 895 | new_key.offset = start; |
| 896 | ret = btrfs_duplicate_item(trans, root, path, |
| 897 | &new_key); |
| 898 | if (ret == -EAGAIN) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 899 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 900 | continue; |
| 901 | } |
| 902 | if (ret < 0) |
| 903 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 904 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 905 | leaf = path->nodes[0]; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 906 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 907 | struct btrfs_file_extent_item); |
| 908 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 909 | start - key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 910 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 911 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 912 | struct btrfs_file_extent_item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 913 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 914 | extent_offset += start - key.offset; |
| 915 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 916 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 917 | extent_end - start); |
| 918 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 919 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 920 | if (update_refs && disk_bytenr > 0) { |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 921 | ret = btrfs_inc_extent_ref(trans, root, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 922 | disk_bytenr, num_bytes, 0, |
| 923 | root->root_key.objectid, |
| 924 | new_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 925 | start - extent_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 926 | BUG_ON(ret); /* -ENOMEM */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 927 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 928 | key.offset = start; |
| 929 | } |
| 930 | /* |
Josef Bacik | 62fe51c1 | 2016-11-16 09:13:39 -0500 | [diff] [blame] | 931 | * From here on out we will have actually dropped something, so |
| 932 | * last_end can be updated. |
| 933 | */ |
| 934 | last_end = extent_end; |
| 935 | |
| 936 | /* |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 937 | * | ---- range to drop ----- | |
| 938 | * | -------- extent -------- | |
| 939 | */ |
| 940 | if (start <= key.offset && end < extent_end) { |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 941 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
David Sterba | 3f9e3df | 2014-04-15 18:50:17 +0200 | [diff] [blame] | 942 | ret = -EOPNOTSUPP; |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 943 | break; |
| 944 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 945 | |
| 946 | memcpy(&new_key, &key, sizeof(new_key)); |
| 947 | new_key.offset = end; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 948 | btrfs_set_item_key_safe(fs_info, path, &new_key); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 949 | |
| 950 | extent_offset += end - key.offset; |
| 951 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 952 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 953 | extent_end - end); |
| 954 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 955 | if (update_refs && disk_bytenr > 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 956 | inode_sub_bytes(inode, end - key.offset); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 957 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 958 | } |
| 959 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 960 | search_start = extent_end; |
| 961 | /* |
| 962 | * | ---- range to drop ----- | |
| 963 | * | -------- extent -------- | |
| 964 | */ |
| 965 | if (start > key.offset && end >= extent_end) { |
| 966 | BUG_ON(del_nr > 0); |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 967 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
David Sterba | 3f9e3df | 2014-04-15 18:50:17 +0200 | [diff] [blame] | 968 | ret = -EOPNOTSUPP; |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 969 | break; |
| 970 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 971 | |
| 972 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 973 | start - key.offset); |
| 974 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 975 | if (update_refs && disk_bytenr > 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 976 | inode_sub_bytes(inode, extent_end - start); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 977 | if (end == extent_end) |
| 978 | break; |
| 979 | |
| 980 | path->slots[0]++; |
| 981 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 982 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 983 | |
| 984 | /* |
| 985 | * | ---- range to drop ----- | |
| 986 | * | ------ extent ------ | |
| 987 | */ |
| 988 | if (start <= key.offset && end >= extent_end) { |
Filipe Manana | fc19c5e | 2014-04-29 13:18:40 +0100 | [diff] [blame] | 989 | delete_extent_item: |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 990 | if (del_nr == 0) { |
| 991 | del_slot = path->slots[0]; |
| 992 | del_nr = 1; |
| 993 | } else { |
| 994 | BUG_ON(del_slot + del_nr != path->slots[0]); |
| 995 | del_nr++; |
| 996 | } |
| 997 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 998 | if (update_refs && |
| 999 | extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1000 | inode_sub_bytes(inode, |
| 1001 | extent_end - key.offset); |
| 1002 | extent_end = ALIGN(extent_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1003 | fs_info->sectorsize); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1004 | } else if (update_refs && disk_bytenr > 0) { |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 1005 | ret = btrfs_free_extent(trans, root, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1006 | disk_bytenr, num_bytes, 0, |
| 1007 | root->root_key.objectid, |
| 1008 | key.objectid, key.offset - |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 1009 | extent_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1010 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1011 | inode_sub_bytes(inode, |
| 1012 | extent_end - key.offset); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | if (end == extent_end) |
| 1016 | break; |
| 1017 | |
| 1018 | if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) { |
| 1019 | path->slots[0]++; |
| 1020 | goto next_slot; |
| 1021 | } |
| 1022 | |
| 1023 | ret = btrfs_del_items(trans, root, path, del_slot, |
| 1024 | del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1025 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1026 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1027 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1028 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1029 | |
| 1030 | del_nr = 0; |
| 1031 | del_slot = 0; |
| 1032 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1033 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1034 | continue; |
| 1035 | } |
| 1036 | |
| 1037 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1038 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1039 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1040 | if (!ret && del_nr > 0) { |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1041 | /* |
| 1042 | * Set path->slots[0] to first slot, so that after the delete |
| 1043 | * if items are move off from our leaf to its immediate left or |
| 1044 | * right neighbor leafs, we end up with a correct and adjusted |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 1045 | * path->slots[0] for our insertion (if replace_extent != 0). |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1046 | */ |
| 1047 | path->slots[0] = del_slot; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1048 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1049 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1050 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 1051 | } |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1052 | |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 1053 | leaf = path->nodes[0]; |
| 1054 | /* |
| 1055 | * If btrfs_del_items() was called, it might have deleted a leaf, in |
| 1056 | * which case it unlocked our path, so check path->locks[0] matches a |
| 1057 | * write lock. |
| 1058 | */ |
| 1059 | if (!ret && replace_extent && leafs_visited == 1 && |
| 1060 | (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING || |
| 1061 | path->locks[0] == BTRFS_WRITE_LOCK) && |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1062 | btrfs_leaf_free_space(fs_info, leaf) >= |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 1063 | sizeof(struct btrfs_item) + extent_item_size) { |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1064 | |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 1065 | key.objectid = ino; |
| 1066 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1067 | key.offset = start; |
| 1068 | if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) { |
| 1069 | struct btrfs_key slot_key; |
| 1070 | |
| 1071 | btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]); |
| 1072 | if (btrfs_comp_cpu_keys(&key, &slot_key) > 0) |
| 1073 | path->slots[0]++; |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1074 | } |
Filipe David Borba Manana | d5f3752 | 2014-02-09 23:45:12 +0000 | [diff] [blame] | 1075 | setup_items_for_insert(root, path, &key, |
| 1076 | &extent_item_size, |
| 1077 | extent_item_size, |
| 1078 | sizeof(struct btrfs_item) + |
| 1079 | extent_item_size, 1); |
| 1080 | *key_inserted = 1; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1083 | if (!replace_extent || !(*key_inserted)) |
| 1084 | btrfs_release_path(path); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 1085 | if (drop_end) |
Josef Bacik | 62fe51c1 | 2016-11-16 09:13:39 -0500 | [diff] [blame] | 1086 | *drop_end = found ? min(end, last_end) : end; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1087 | return ret; |
| 1088 | } |
| 1089 | |
| 1090 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, |
| 1091 | struct btrfs_root *root, struct inode *inode, u64 start, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 1092 | u64 end, int drop_cache) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1093 | { |
| 1094 | struct btrfs_path *path; |
| 1095 | int ret; |
| 1096 | |
| 1097 | path = btrfs_alloc_path(); |
| 1098 | if (!path) |
| 1099 | return -ENOMEM; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 1100 | ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 1101 | drop_cache, 0, 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1102 | btrfs_free_path(path); |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1106 | static int extent_mergeable(struct extent_buffer *leaf, int slot, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1107 | u64 objectid, u64 bytenr, u64 orig_offset, |
| 1108 | u64 *start, u64 *end) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1109 | { |
| 1110 | struct btrfs_file_extent_item *fi; |
| 1111 | struct btrfs_key key; |
| 1112 | u64 extent_end; |
| 1113 | |
| 1114 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) |
| 1115 | return 0; |
| 1116 | |
| 1117 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 1118 | if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY) |
| 1119 | return 0; |
| 1120 | |
| 1121 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 1122 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG || |
| 1123 | btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr || |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1124 | btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset || |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1125 | btrfs_file_extent_compression(leaf, fi) || |
| 1126 | btrfs_file_extent_encryption(leaf, fi) || |
| 1127 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 1128 | return 0; |
| 1129 | |
| 1130 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 1131 | if ((*start && *start != key.offset) || (*end && *end != extent_end)) |
| 1132 | return 0; |
| 1133 | |
| 1134 | *start = key.offset; |
| 1135 | *end = extent_end; |
| 1136 | return 1; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * Mark extent in the range start - end as written. |
| 1141 | * |
| 1142 | * This changes extent type from 'pre-allocated' to 'regular'. If only |
| 1143 | * part of extent is marked as written, the extent will be split into |
| 1144 | * two or three. |
| 1145 | */ |
| 1146 | int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 1147 | struct btrfs_inode *inode, u64 start, u64 end) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1148 | { |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 1149 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 1150 | struct btrfs_root *root = inode->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1151 | struct extent_buffer *leaf; |
| 1152 | struct btrfs_path *path; |
| 1153 | struct btrfs_file_extent_item *fi; |
| 1154 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1155 | struct btrfs_key new_key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1156 | u64 bytenr; |
| 1157 | u64 num_bytes; |
| 1158 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1159 | u64 orig_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1160 | u64 other_start; |
| 1161 | u64 other_end; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1162 | u64 split; |
| 1163 | int del_nr = 0; |
| 1164 | int del_slot = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1165 | int recow; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1166 | int ret; |
Nikolay Borisov | 7a6d706 | 2017-02-20 13:50:48 +0200 | [diff] [blame] | 1167 | u64 ino = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1168 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1169 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 1170 | if (!path) |
| 1171 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1172 | again: |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1173 | recow = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1174 | split = start; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1175 | key.objectid = ino; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1176 | key.type = BTRFS_EXTENT_DATA_KEY; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1177 | key.offset = split; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1178 | |
| 1179 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Josef Bacik | 4141573 | 2011-03-16 13:59:32 -0400 | [diff] [blame] | 1180 | if (ret < 0) |
| 1181 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1182 | if (ret > 0 && path->slots[0] > 0) |
| 1183 | path->slots[0]--; |
| 1184 | |
| 1185 | leaf = path->nodes[0]; |
| 1186 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1187 | if (key.objectid != ino || |
| 1188 | key.type != BTRFS_EXTENT_DATA_KEY) { |
| 1189 | ret = -EINVAL; |
| 1190 | btrfs_abort_transaction(trans, ret); |
| 1191 | goto out; |
| 1192 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1193 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1194 | struct btrfs_file_extent_item); |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1195 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) { |
| 1196 | ret = -EINVAL; |
| 1197 | btrfs_abort_transaction(trans, ret); |
| 1198 | goto out; |
| 1199 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1200 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1201 | if (key.offset > start || extent_end < end) { |
| 1202 | ret = -EINVAL; |
| 1203 | btrfs_abort_transaction(trans, ret); |
| 1204 | goto out; |
| 1205 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1206 | |
| 1207 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1208 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1209 | orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1210 | memcpy(&new_key, &key, sizeof(new_key)); |
| 1211 | |
| 1212 | if (start == key.offset && end < extent_end) { |
| 1213 | other_start = 0; |
| 1214 | other_end = start; |
| 1215 | if (extent_mergeable(leaf, path->slots[0] - 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1216 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1217 | &other_start, &other_end)) { |
| 1218 | new_key.offset = end; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1219 | btrfs_set_item_key_safe(fs_info, path, &new_key); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1220 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1221 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1222 | btrfs_set_file_extent_generation(leaf, fi, |
| 1223 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1224 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1225 | extent_end - end); |
| 1226 | btrfs_set_file_extent_offset(leaf, fi, |
| 1227 | end - orig_offset); |
| 1228 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 1229 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1230 | btrfs_set_file_extent_generation(leaf, fi, |
| 1231 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1232 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1233 | end - other_start); |
| 1234 | btrfs_mark_buffer_dirty(leaf); |
| 1235 | goto out; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | if (start > key.offset && end == extent_end) { |
| 1240 | other_start = end; |
| 1241 | other_end = 0; |
| 1242 | if (extent_mergeable(leaf, path->slots[0] + 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1243 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1244 | &other_start, &other_end)) { |
| 1245 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1246 | struct btrfs_file_extent_item); |
| 1247 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1248 | start - key.offset); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1249 | btrfs_set_file_extent_generation(leaf, fi, |
| 1250 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1251 | path->slots[0]++; |
| 1252 | new_key.offset = start; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1253 | btrfs_set_item_key_safe(fs_info, path, &new_key); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1254 | |
| 1255 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1256 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1257 | btrfs_set_file_extent_generation(leaf, fi, |
| 1258 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1259 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1260 | other_end - start); |
| 1261 | btrfs_set_file_extent_offset(leaf, fi, |
| 1262 | start - orig_offset); |
| 1263 | btrfs_mark_buffer_dirty(leaf); |
| 1264 | goto out; |
| 1265 | } |
| 1266 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1267 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1268 | while (start > key.offset || end < extent_end) { |
| 1269 | if (key.offset == start) |
| 1270 | split = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1271 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1272 | new_key.offset = split; |
| 1273 | ret = btrfs_duplicate_item(trans, root, path, &new_key); |
| 1274 | if (ret == -EAGAIN) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1275 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1276 | goto again; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1277 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1278 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1279 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1280 | goto out; |
| 1281 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1282 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1283 | leaf = path->nodes[0]; |
| 1284 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1285 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1286 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1287 | btrfs_set_file_extent_num_bytes(leaf, fi, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1288 | split - key.offset); |
| 1289 | |
| 1290 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1291 | struct btrfs_file_extent_item); |
| 1292 | |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1293 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1294 | btrfs_set_file_extent_offset(leaf, fi, split - orig_offset); |
| 1295 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1296 | extent_end - split); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1297 | btrfs_mark_buffer_dirty(leaf); |
| 1298 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 1299 | ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1300 | 0, root->root_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 1301 | ino, orig_offset); |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1302 | if (ret) { |
| 1303 | btrfs_abort_transaction(trans, ret); |
| 1304 | goto out; |
| 1305 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1306 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1307 | if (split == start) { |
| 1308 | key.offset = start; |
| 1309 | } else { |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1310 | if (start != key.offset) { |
| 1311 | ret = -EINVAL; |
| 1312 | btrfs_abort_transaction(trans, ret); |
| 1313 | goto out; |
| 1314 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1315 | path->slots[0]--; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1316 | extent_end = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1317 | } |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1318 | recow = 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1319 | } |
| 1320 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1321 | other_start = end; |
| 1322 | other_end = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1323 | if (extent_mergeable(leaf, path->slots[0] + 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1324 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1325 | &other_start, &other_end)) { |
| 1326 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1327 | btrfs_release_path(path); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1328 | goto again; |
| 1329 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1330 | extent_end = other_end; |
| 1331 | del_slot = path->slots[0] + 1; |
| 1332 | del_nr++; |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 1333 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1334 | 0, root->root_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 1335 | ino, orig_offset); |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1336 | if (ret) { |
| 1337 | btrfs_abort_transaction(trans, ret); |
| 1338 | goto out; |
| 1339 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1340 | } |
| 1341 | other_start = 0; |
| 1342 | other_end = start; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1343 | if (extent_mergeable(leaf, path->slots[0] - 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1344 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1345 | &other_start, &other_end)) { |
| 1346 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1347 | btrfs_release_path(path); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1348 | goto again; |
| 1349 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1350 | key.offset = other_start; |
| 1351 | del_slot = path->slots[0]; |
| 1352 | del_nr++; |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 1353 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1354 | 0, root->root_key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 1355 | ino, orig_offset); |
Josef Bacik | 9c8e63d | 2016-09-02 15:40:06 -0400 | [diff] [blame] | 1356 | if (ret) { |
| 1357 | btrfs_abort_transaction(trans, ret); |
| 1358 | goto out; |
| 1359 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1360 | } |
| 1361 | if (del_nr == 0) { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 1362 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1363 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1364 | btrfs_set_file_extent_type(leaf, fi, |
| 1365 | BTRFS_FILE_EXTENT_REG); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1366 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1367 | btrfs_mark_buffer_dirty(leaf); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1368 | } else { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 1369 | fi = btrfs_item_ptr(leaf, del_slot - 1, |
| 1370 | struct btrfs_file_extent_item); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1371 | btrfs_set_file_extent_type(leaf, fi, |
| 1372 | BTRFS_FILE_EXTENT_REG); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1373 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1374 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1375 | extent_end - key.offset); |
| 1376 | btrfs_mark_buffer_dirty(leaf); |
| 1377 | |
| 1378 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1379 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1380 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1381 | goto out; |
| 1382 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1383 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1384 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1385 | btrfs_free_path(path); |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1389 | /* |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1390 | * on error we return an unlocked page and the error value |
| 1391 | * on success we return a locked page and 0 |
| 1392 | */ |
Chris Mason | bb1591b4 | 2015-12-14 15:40:44 -0800 | [diff] [blame] | 1393 | static int prepare_uptodate_page(struct inode *inode, |
| 1394 | struct page *page, u64 pos, |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1395 | bool force_uptodate) |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1396 | { |
| 1397 | int ret = 0; |
| 1398 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1399 | if (((pos & (PAGE_SIZE - 1)) || force_uptodate) && |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1400 | !PageUptodate(page)) { |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1401 | ret = btrfs_readpage(NULL, page); |
| 1402 | if (ret) |
| 1403 | return ret; |
| 1404 | lock_page(page); |
| 1405 | if (!PageUptodate(page)) { |
| 1406 | unlock_page(page); |
| 1407 | return -EIO; |
| 1408 | } |
Chris Mason | bb1591b4 | 2015-12-14 15:40:44 -0800 | [diff] [blame] | 1409 | if (page->mapping != inode->i_mapping) { |
| 1410 | unlock_page(page); |
| 1411 | return -EAGAIN; |
| 1412 | } |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1413 | } |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1418 | * this just gets pages into the page cache and locks them down. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1419 | */ |
Miao Xie | b37392e | 2013-12-10 19:25:03 +0800 | [diff] [blame] | 1420 | static noinline int prepare_pages(struct inode *inode, struct page **pages, |
| 1421 | size_t num_pages, loff_t pos, |
| 1422 | size_t write_bytes, bool force_uptodate) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1423 | { |
| 1424 | int i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1425 | unsigned long index = pos >> PAGE_SHIFT; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 1426 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
Filipe David Borba Manana | fc28b62 | 2013-12-13 19:39:34 +0000 | [diff] [blame] | 1427 | int err = 0; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1428 | int faili; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1429 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1430 | for (i = 0; i < num_pages; i++) { |
Chris Mason | bb1591b4 | 2015-12-14 15:40:44 -0800 | [diff] [blame] | 1431 | again: |
Josef Bacik | a94733d | 2011-07-11 10:47:06 -0400 | [diff] [blame] | 1432 | pages[i] = find_or_create_page(inode->i_mapping, index + i, |
Johannes Weiner | e3a41a5 | 2012-01-10 15:07:55 -0800 | [diff] [blame] | 1433 | mask | __GFP_WRITE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1434 | if (!pages[i]) { |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1435 | faili = i - 1; |
| 1436 | err = -ENOMEM; |
| 1437 | goto fail; |
| 1438 | } |
| 1439 | |
| 1440 | if (i == 0) |
Chris Mason | bb1591b4 | 2015-12-14 15:40:44 -0800 | [diff] [blame] | 1441 | err = prepare_uptodate_page(inode, pages[i], pos, |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1442 | force_uptodate); |
Chris Mason | bb1591b4 | 2015-12-14 15:40:44 -0800 | [diff] [blame] | 1443 | if (!err && i == num_pages - 1) |
| 1444 | err = prepare_uptodate_page(inode, pages[i], |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1445 | pos + write_bytes, false); |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1446 | if (err) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1447 | put_page(pages[i]); |
Chris Mason | bb1591b4 | 2015-12-14 15:40:44 -0800 | [diff] [blame] | 1448 | if (err == -EAGAIN) { |
| 1449 | err = 0; |
| 1450 | goto again; |
| 1451 | } |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1452 | faili = i - 1; |
| 1453 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1454 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1455 | wait_on_page_writeback(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1456 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1457 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1458 | return 0; |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1459 | fail: |
| 1460 | while (faili >= 0) { |
| 1461 | unlock_page(pages[faili]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1462 | put_page(pages[faili]); |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1463 | faili--; |
| 1464 | } |
| 1465 | return err; |
| 1466 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1467 | } |
| 1468 | |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1469 | /* |
| 1470 | * This function locks the extent and properly waits for data=ordered extents |
| 1471 | * to finish before allowing the pages to be modified if need. |
| 1472 | * |
| 1473 | * The return value: |
| 1474 | * 1 - the extent is locked |
| 1475 | * 0 - the extent is not locked, and everything is OK |
| 1476 | * -EAGAIN - need re-prepare the pages |
| 1477 | * the other < 0 number - Something wrong happens |
| 1478 | */ |
| 1479 | static noinline int |
Nikolay Borisov | 2cff578c | 2017-02-20 13:50:51 +0200 | [diff] [blame] | 1480 | lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages, |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1481 | size_t num_pages, loff_t pos, |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1482 | size_t write_bytes, |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1483 | u64 *lockstart, u64 *lockend, |
| 1484 | struct extent_state **cached_state) |
| 1485 | { |
Nikolay Borisov | 2cff578c | 2017-02-20 13:50:51 +0200 | [diff] [blame] | 1486 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1487 | u64 start_pos; |
| 1488 | u64 last_pos; |
| 1489 | int i; |
| 1490 | int ret = 0; |
| 1491 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1492 | start_pos = round_down(pos, fs_info->sectorsize); |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1493 | last_pos = start_pos |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1494 | + round_up(pos + write_bytes - start_pos, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1495 | fs_info->sectorsize) - 1; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1496 | |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 1497 | if (start_pos < inode->vfs_inode.i_size) { |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1498 | struct btrfs_ordered_extent *ordered; |
Filipe Manana | a7e3b97 | 2017-04-03 10:45:46 +0100 | [diff] [blame] | 1499 | |
Nikolay Borisov | 2cff578c | 2017-02-20 13:50:51 +0200 | [diff] [blame] | 1500 | lock_extent_bits(&inode->io_tree, start_pos, last_pos, |
| 1501 | cached_state); |
Miao Xie | b88935b | 2014-03-06 13:54:58 +0800 | [diff] [blame] | 1502 | ordered = btrfs_lookup_ordered_range(inode, start_pos, |
| 1503 | last_pos - start_pos + 1); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1504 | if (ordered && |
| 1505 | ordered->file_offset + ordered->len > start_pos && |
| 1506 | ordered->file_offset <= last_pos) { |
Nikolay Borisov | 2cff578c | 2017-02-20 13:50:51 +0200 | [diff] [blame] | 1507 | unlock_extent_cached(&inode->io_tree, start_pos, |
| 1508 | last_pos, cached_state, GFP_NOFS); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1509 | for (i = 0; i < num_pages; i++) { |
| 1510 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1511 | put_page(pages[i]); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1512 | } |
Nikolay Borisov | 2cff578c | 2017-02-20 13:50:51 +0200 | [diff] [blame] | 1513 | btrfs_start_ordered_extent(&inode->vfs_inode, |
| 1514 | ordered, 1); |
Miao Xie | b88935b | 2014-03-06 13:54:58 +0800 | [diff] [blame] | 1515 | btrfs_put_ordered_extent(ordered); |
| 1516 | return -EAGAIN; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1517 | } |
| 1518 | if (ordered) |
| 1519 | btrfs_put_ordered_extent(ordered); |
Filipe Manana | e3b8a48 | 2017-11-04 00:16:59 +0000 | [diff] [blame] | 1520 | clear_extent_bit(&inode->io_tree, start_pos, last_pos, |
| 1521 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 1522 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, |
| 1523 | 0, 0, cached_state, GFP_NOFS); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1524 | *lockstart = start_pos; |
| 1525 | *lockend = last_pos; |
| 1526 | ret = 1; |
| 1527 | } |
| 1528 | |
| 1529 | for (i = 0; i < num_pages; i++) { |
| 1530 | if (clear_page_dirty_for_io(pages[i])) |
| 1531 | account_page_redirty(pages[i]); |
| 1532 | set_page_extent_mapped(pages[i]); |
| 1533 | WARN_ON(!PageLocked(pages[i])); |
| 1534 | } |
| 1535 | |
| 1536 | return ret; |
| 1537 | } |
| 1538 | |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1539 | static noinline int check_can_nocow(struct btrfs_inode *inode, loff_t pos, |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1540 | size_t *write_bytes) |
| 1541 | { |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1542 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 1543 | struct btrfs_root *root = inode->root; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1544 | struct btrfs_ordered_extent *ordered; |
| 1545 | u64 lockstart, lockend; |
| 1546 | u64 num_bytes; |
| 1547 | int ret; |
| 1548 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1549 | ret = btrfs_start_write_no_snapshotting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1550 | if (!ret) |
| 1551 | return -ENOSPC; |
| 1552 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1553 | lockstart = round_down(pos, fs_info->sectorsize); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1554 | lockend = round_up(pos + *write_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1555 | fs_info->sectorsize) - 1; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1556 | |
| 1557 | while (1) { |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1558 | lock_extent(&inode->io_tree, lockstart, lockend); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1559 | ordered = btrfs_lookup_ordered_range(inode, lockstart, |
| 1560 | lockend - lockstart + 1); |
| 1561 | if (!ordered) { |
| 1562 | break; |
| 1563 | } |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1564 | unlock_extent(&inode->io_tree, lockstart, lockend); |
| 1565 | btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1566 | btrfs_put_ordered_extent(ordered); |
| 1567 | } |
| 1568 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1569 | num_bytes = lockend - lockstart + 1; |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1570 | ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes, |
| 1571 | NULL, NULL, NULL); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1572 | if (ret <= 0) { |
| 1573 | ret = 0; |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1574 | btrfs_end_write_no_snapshotting(root); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1575 | } else { |
Miao Xie | c933956 | 2014-02-27 13:58:04 +0800 | [diff] [blame] | 1576 | *write_bytes = min_t(size_t, *write_bytes , |
| 1577 | num_bytes - pos + lockstart); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1578 | } |
| 1579 | |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1580 | unlock_extent(&inode->io_tree, lockstart, lockend); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1581 | |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1585 | static noinline ssize_t __btrfs_buffered_write(struct file *file, |
| 1586 | struct iov_iter *i, |
| 1587 | loff_t pos) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1588 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1589 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1590 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1591 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1592 | struct page **pages = NULL; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1593 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1594 | struct extent_changeset *data_reserved = NULL; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1595 | u64 release_bytes = 0; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1596 | u64 lockstart; |
| 1597 | u64 lockend; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1598 | size_t num_written = 0; |
| 1599 | int nrptrs; |
Tsutomu Itoh | c914923 | 2011-03-30 00:57:23 +0000 | [diff] [blame] | 1600 | int ret = 0; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1601 | bool only_release_metadata = false; |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1602 | bool force_page_uptodate = false; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1603 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1604 | nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE), |
| 1605 | PAGE_SIZE / (sizeof(struct page *))); |
Wu Fengguang | 142349f | 2011-12-16 12:32:57 -0500 | [diff] [blame] | 1606 | nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied); |
| 1607 | nrptrs = max(nrptrs, 8); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 1608 | pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1609 | if (!pages) |
| 1610 | return -ENOMEM; |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 1611 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1612 | while (iov_iter_count(i) > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1613 | size_t offset = pos & (PAGE_SIZE - 1); |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1614 | size_t sector_offset; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1615 | size_t write_bytes = min(iov_iter_count(i), |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1616 | nrptrs * (size_t)PAGE_SIZE - |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1617 | offset); |
David Sterba | ed6078f | 2014-06-05 01:59:57 +0200 | [diff] [blame] | 1618 | size_t num_pages = DIV_ROUND_UP(write_bytes + offset, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1619 | PAGE_SIZE); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1620 | size_t reserve_bytes; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1621 | size_t dirty_pages; |
| 1622 | size_t copied; |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1623 | size_t dirty_sectors; |
| 1624 | size_t num_sectors; |
Goldwyn Rodrigues | 79f015f | 2017-10-16 05:43:21 -0500 | [diff] [blame] | 1625 | int extents_locked; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1626 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1627 | WARN_ON(num_pages > nrptrs); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1628 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1629 | /* |
| 1630 | * Fault pages before locking them in prepare_pages |
| 1631 | * to avoid recursive lock |
| 1632 | */ |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1633 | if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1634 | ret = -EFAULT; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1635 | break; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1638 | sector_offset = pos & (fs_info->sectorsize - 1); |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1639 | reserve_bytes = round_up(write_bytes + sector_offset, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1640 | fs_info->sectorsize); |
Qu Wenruo | d9d8b2a | 2015-09-08 17:22:43 +0800 | [diff] [blame] | 1641 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1642 | extent_changeset_release(data_reserved); |
| 1643 | ret = btrfs_check_data_free_space(inode, &data_reserved, pos, |
| 1644 | write_bytes); |
Josef Bacik | c6887cd | 2016-03-25 13:26:00 -0400 | [diff] [blame] | 1645 | if (ret < 0) { |
| 1646 | if ((BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | |
| 1647 | BTRFS_INODE_PREALLOC)) && |
Nikolay Borisov | 85b7ab6 | 2017-02-20 13:50:50 +0200 | [diff] [blame] | 1648 | check_can_nocow(BTRFS_I(inode), pos, |
| 1649 | &write_bytes) > 0) { |
Josef Bacik | c6887cd | 2016-03-25 13:26:00 -0400 | [diff] [blame] | 1650 | /* |
| 1651 | * For nodata cow case, no need to reserve |
| 1652 | * data space. |
| 1653 | */ |
| 1654 | only_release_metadata = true; |
| 1655 | /* |
| 1656 | * our prealloc extent may be smaller than |
| 1657 | * write_bytes, so scale down. |
| 1658 | */ |
| 1659 | num_pages = DIV_ROUND_UP(write_bytes + offset, |
| 1660 | PAGE_SIZE); |
| 1661 | reserve_bytes = round_up(write_bytes + |
| 1662 | sector_offset, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1663 | fs_info->sectorsize); |
Josef Bacik | c6887cd | 2016-03-25 13:26:00 -0400 | [diff] [blame] | 1664 | } else { |
| 1665 | break; |
| 1666 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1667 | } |
Zhao Lei | 4da2e26 | 2016-01-06 18:24:43 +0800 | [diff] [blame] | 1668 | |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1669 | WARN_ON(reserve_bytes == 0); |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 1670 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), |
| 1671 | reserve_bytes); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1672 | if (ret) { |
| 1673 | if (!only_release_metadata) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 1674 | btrfs_free_reserved_data_space(inode, |
| 1675 | data_reserved, pos, |
| 1676 | write_bytes); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1677 | else |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1678 | btrfs_end_write_no_snapshotting(root); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1679 | break; |
| 1680 | } |
| 1681 | |
| 1682 | release_bytes = reserve_bytes; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1683 | again: |
Josef Bacik | 4a64001 | 2011-01-25 15:10:08 -0500 | [diff] [blame] | 1684 | /* |
| 1685 | * This is going to setup the pages array with the number of |
| 1686 | * pages we want, so we don't really need to worry about the |
| 1687 | * contents of pages from loop to loop |
| 1688 | */ |
Miao Xie | b37392e | 2013-12-10 19:25:03 +0800 | [diff] [blame] | 1689 | ret = prepare_pages(inode, pages, num_pages, |
| 1690 | pos, write_bytes, |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1691 | force_page_uptodate); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1692 | if (ret) { |
| 1693 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
| 1694 | reserve_bytes); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1695 | break; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1696 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1697 | |
Goldwyn Rodrigues | 79f015f | 2017-10-16 05:43:21 -0500 | [diff] [blame] | 1698 | extents_locked = lock_and_cleanup_extent_if_need( |
| 1699 | BTRFS_I(inode), pages, |
Nikolay Borisov | 2cff578c | 2017-02-20 13:50:51 +0200 | [diff] [blame] | 1700 | num_pages, pos, write_bytes, &lockstart, |
| 1701 | &lockend, &cached_state); |
Goldwyn Rodrigues | 79f015f | 2017-10-16 05:43:21 -0500 | [diff] [blame] | 1702 | if (extents_locked < 0) { |
| 1703 | if (extents_locked == -EAGAIN) |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1704 | goto again; |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1705 | btrfs_delalloc_release_extents(BTRFS_I(inode), |
| 1706 | reserve_bytes); |
Goldwyn Rodrigues | 79f015f | 2017-10-16 05:43:21 -0500 | [diff] [blame] | 1707 | ret = extents_locked; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1708 | break; |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1709 | } |
| 1710 | |
Zhao Lei | ee22f0c | 2016-01-06 18:47:31 +0800 | [diff] [blame] | 1711 | copied = btrfs_copy_from_user(pos, write_bytes, pages, i); |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1712 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1713 | num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes); |
Chris Mason | 56244ef | 2016-05-16 09:21:01 -0700 | [diff] [blame] | 1714 | dirty_sectors = round_up(copied + sector_offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1715 | fs_info->sectorsize); |
| 1716 | dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors); |
Chris Mason | 56244ef | 2016-05-16 09:21:01 -0700 | [diff] [blame] | 1717 | |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1718 | /* |
| 1719 | * if we have trouble faulting in the pages, fall |
| 1720 | * back to one page at a time |
| 1721 | */ |
| 1722 | if (copied < write_bytes) |
| 1723 | nrptrs = 1; |
| 1724 | |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1725 | if (copied == 0) { |
| 1726 | force_page_uptodate = true; |
Chris Mason | 56244ef | 2016-05-16 09:21:01 -0700 | [diff] [blame] | 1727 | dirty_sectors = 0; |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1728 | dirty_pages = 0; |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1729 | } else { |
| 1730 | force_page_uptodate = false; |
David Sterba | ed6078f | 2014-06-05 01:59:57 +0200 | [diff] [blame] | 1731 | dirty_pages = DIV_ROUND_UP(copied + offset, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1732 | PAGE_SIZE); |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1733 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1734 | |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1735 | if (num_sectors > dirty_sectors) { |
Chris Mason | 8b8b08c | 2016-07-19 05:52:36 -0700 | [diff] [blame] | 1736 | /* release everything except the sectors we dirtied */ |
| 1737 | release_bytes -= dirty_sectors << |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1738 | fs_info->sb->s_blocksize_bits; |
Qu Wenruo | 485290a | 2015-10-29 17:28:46 +0800 | [diff] [blame] | 1739 | if (only_release_metadata) { |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 1740 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1741 | release_bytes); |
Qu Wenruo | 485290a | 2015-10-29 17:28:46 +0800 | [diff] [blame] | 1742 | } else { |
| 1743 | u64 __pos; |
| 1744 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1745 | __pos = round_down(pos, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1746 | fs_info->sectorsize) + |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1747 | (dirty_pages << PAGE_SHIFT); |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 1748 | btrfs_delalloc_release_space(inode, |
| 1749 | data_reserved, __pos, |
| 1750 | release_bytes); |
Qu Wenruo | 485290a | 2015-10-29 17:28:46 +0800 | [diff] [blame] | 1751 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Chandan Rajendra | 2e78c92 | 2016-01-21 15:55:53 +0530 | [diff] [blame] | 1754 | release_bytes = round_up(copied + sector_offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1755 | fs_info->sectorsize); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1756 | |
| 1757 | if (copied > 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1758 | ret = btrfs_dirty_pages(inode, pages, dirty_pages, |
| 1759 | pos, copied, NULL); |
Goldwyn Rodrigues | 79f015f | 2017-10-16 05:43:21 -0500 | [diff] [blame] | 1760 | if (extents_locked) |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1761 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 1762 | lockstart, lockend, &cached_state, |
| 1763 | GFP_NOFS); |
Josef Bacik | 8b62f87 | 2017-10-19 14:15:55 -0400 | [diff] [blame] | 1764 | btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes); |
Miao Xie | f1de968 | 2014-01-09 10:06:10 +0800 | [diff] [blame] | 1765 | if (ret) { |
| 1766 | btrfs_drop_pages(pages, num_pages); |
Miao Xie | 376cc68 | 2013-12-10 19:25:04 +0800 | [diff] [blame] | 1767 | break; |
Miao Xie | f1de968 | 2014-01-09 10:06:10 +0800 | [diff] [blame] | 1768 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1769 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1770 | release_bytes = 0; |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1771 | if (only_release_metadata) |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1772 | btrfs_end_write_no_snapshotting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1773 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1774 | if (only_release_metadata && copied > 0) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1775 | lockstart = round_down(pos, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1776 | fs_info->sectorsize); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1777 | lockend = round_up(pos + copied, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1778 | fs_info->sectorsize) - 1; |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1779 | |
| 1780 | set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 1781 | lockend, EXTENT_NORESERVE, NULL, |
| 1782 | NULL, GFP_NOFS); |
| 1783 | only_release_metadata = false; |
| 1784 | } |
| 1785 | |
Miao Xie | f1de968 | 2014-01-09 10:06:10 +0800 | [diff] [blame] | 1786 | btrfs_drop_pages(pages, num_pages); |
| 1787 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1788 | cond_resched(); |
| 1789 | |
Namjae Jeon | d0e1d66 | 2012-12-11 16:00:21 -0800 | [diff] [blame] | 1790 | balance_dirty_pages_ratelimited(inode->i_mapping); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1791 | if (dirty_pages < (fs_info->nodesize >> PAGE_SHIFT) + 1) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1792 | btrfs_btree_balance_dirty(fs_info); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1793 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1794 | pos += copied; |
| 1795 | num_written += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1796 | } |
Chris Mason | 5b92ee7 | 2008-01-03 13:46:11 -0500 | [diff] [blame] | 1797 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1798 | kfree(pages); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1799 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1800 | if (release_bytes) { |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1801 | if (only_release_metadata) { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 1802 | btrfs_end_write_no_snapshotting(root); |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 1803 | btrfs_delalloc_release_metadata(BTRFS_I(inode), |
| 1804 | release_bytes); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1805 | } else { |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 1806 | btrfs_delalloc_release_space(inode, data_reserved, |
| 1807 | round_down(pos, fs_info->sectorsize), |
| 1808 | release_bytes); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 1809 | } |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1812 | extent_changeset_free(data_reserved); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1813 | return num_written ? num_written : ret; |
| 1814 | } |
| 1815 | |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 1816 | static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1817 | { |
| 1818 | struct file *file = iocb->ki_filp; |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 1819 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 1820 | loff_t pos = iocb->ki_pos; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1821 | ssize_t written; |
| 1822 | ssize_t written_buffered; |
| 1823 | loff_t endbyte; |
| 1824 | int err; |
| 1825 | |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 1826 | written = generic_file_direct_write(iocb, from); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1827 | |
Al Viro | 0c94933 | 2014-03-22 06:51:37 -0400 | [diff] [blame] | 1828 | if (written < 0 || !iov_iter_count(from)) |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1829 | return written; |
| 1830 | |
| 1831 | pos += written; |
Al Viro | 0ae5e4d | 2014-03-03 22:09:39 -0500 | [diff] [blame] | 1832 | written_buffered = __btrfs_buffered_write(file, from, pos); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1833 | if (written_buffered < 0) { |
| 1834 | err = written_buffered; |
| 1835 | goto out; |
| 1836 | } |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 1837 | /* |
| 1838 | * Ensure all data is persisted. We want the next direct IO read to be |
| 1839 | * able to read what was just written. |
| 1840 | */ |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1841 | endbyte = pos + written_buffered - 1; |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 1842 | err = btrfs_fdatawrite_range(inode, pos, endbyte); |
Filipe Manana | 075bdbd | 2014-10-09 21:18:55 +0100 | [diff] [blame] | 1843 | if (err) |
| 1844 | goto out; |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 1845 | err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1846 | if (err) |
| 1847 | goto out; |
| 1848 | written += written_buffered; |
Al Viro | 867c4f9 | 2014-02-11 19:31:06 -0500 | [diff] [blame] | 1849 | iocb->ki_pos = pos + written_buffered; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1850 | invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT, |
| 1851 | endbyte >> PAGE_SHIFT); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1852 | out: |
| 1853 | return written ? written : err; |
| 1854 | } |
| 1855 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1856 | static void update_time_for_write(struct inode *inode) |
| 1857 | { |
| 1858 | struct timespec now; |
| 1859 | |
| 1860 | if (IS_NOCMTIME(inode)) |
| 1861 | return; |
| 1862 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 1863 | now = current_time(inode); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1864 | if (!timespec_equal(&inode->i_mtime, &now)) |
| 1865 | inode->i_mtime = now; |
| 1866 | |
| 1867 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 1868 | inode->i_ctime = now; |
| 1869 | |
| 1870 | if (IS_I_VERSION(inode)) |
| 1871 | inode_inc_iversion(inode); |
| 1872 | } |
| 1873 | |
Al Viro | b30ac0f | 2014-04-03 14:29:04 -0400 | [diff] [blame] | 1874 | static ssize_t btrfs_file_write_iter(struct kiocb *iocb, |
| 1875 | struct iov_iter *from) |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1876 | { |
| 1877 | struct file *file = iocb->ki_filp; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1878 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1879 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Miao Xie | 0c1a98c | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 1881 | u64 start_pos; |
Qu Wenruo | 3ac0d7b | 2014-03-27 02:51:58 +0000 | [diff] [blame] | 1882 | u64 end_pos; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1883 | ssize_t num_written = 0; |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1884 | bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 1885 | ssize_t err; |
Goldwyn Rodrigues | ff0fa73 | 2017-07-04 22:33:07 -0500 | [diff] [blame] | 1886 | loff_t pos; |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 1887 | size_t count = iov_iter_count(from); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 1888 | loff_t oldsize; |
| 1889 | int clean_page = 0; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1890 | |
Christoph Hellwig | 91f9943 | 2017-08-29 16:13:20 +0200 | [diff] [blame] | 1891 | if (!(iocb->ki_flags & IOCB_DIRECT) && |
| 1892 | (iocb->ki_flags & IOCB_NOWAIT)) |
| 1893 | return -EOPNOTSUPP; |
| 1894 | |
Goldwyn Rodrigues | ff0fa73 | 2017-07-04 22:33:07 -0500 | [diff] [blame] | 1895 | if (!inode_trylock(inode)) { |
| 1896 | if (iocb->ki_flags & IOCB_NOWAIT) |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 1897 | return -EAGAIN; |
Goldwyn Rodrigues | ff0fa73 | 2017-07-04 22:33:07 -0500 | [diff] [blame] | 1898 | inode_lock(inode); |
| 1899 | } |
| 1900 | |
| 1901 | err = generic_write_checks(iocb, from); |
| 1902 | if (err <= 0) { |
| 1903 | inode_unlock(inode); |
| 1904 | return err; |
| 1905 | } |
| 1906 | |
| 1907 | pos = iocb->ki_pos; |
| 1908 | if (iocb->ki_flags & IOCB_NOWAIT) { |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 1909 | /* |
| 1910 | * We will allocate space in case nodatacow is not set, |
| 1911 | * so bail |
| 1912 | */ |
| 1913 | if (!(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | |
| 1914 | BTRFS_INODE_PREALLOC)) || |
| 1915 | check_can_nocow(BTRFS_I(inode), pos, &count) <= 0) { |
| 1916 | inode_unlock(inode); |
| 1917 | return -EAGAIN; |
| 1918 | } |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 1919 | } |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1920 | |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 1921 | current->backing_dev_info = inode_to_bdi(inode); |
Jan Kara | 5fa8e0a | 2015-05-21 16:05:53 +0200 | [diff] [blame] | 1922 | err = file_remove_privs(file); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1923 | if (err) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1924 | inode_unlock(inode); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1925 | goto out; |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * If BTRFS flips readonly due to some impossible error |
| 1930 | * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), |
| 1931 | * although we have opened a file as writable, we have |
| 1932 | * to stop this write operation to ensure FS consistency. |
| 1933 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1934 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1935 | inode_unlock(inode); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1936 | err = -EROFS; |
| 1937 | goto out; |
| 1938 | } |
| 1939 | |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1940 | /* |
| 1941 | * We reserve space for updating the inode when we reserve space for the |
| 1942 | * extent we are going to write, so we will enospc out there. We don't |
| 1943 | * need to start yet another transaction to update the inode as we will |
| 1944 | * update the inode when we finish writing whatever data we write. |
| 1945 | */ |
| 1946 | update_time_for_write(inode); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1947 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1948 | start_pos = round_down(pos, fs_info->sectorsize); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 1949 | oldsize = i_size_read(inode); |
| 1950 | if (start_pos > oldsize) { |
Qu Wenruo | 3ac0d7b | 2014-03-27 02:51:58 +0000 | [diff] [blame] | 1951 | /* Expand hole size to cover write data, preventing empty gap */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1952 | end_pos = round_up(pos + count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1953 | fs_info->sectorsize); |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 1954 | err = btrfs_cont_expand(inode, oldsize, end_pos); |
Miao Xie | 0c1a98c | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 1955 | if (err) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1956 | inode_unlock(inode); |
Miao Xie | 0c1a98c | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 1957 | goto out; |
| 1958 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1959 | if (start_pos > round_up(oldsize, fs_info->sectorsize)) |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 1960 | clean_page = 1; |
Miao Xie | 0c1a98c | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 1961 | } |
| 1962 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1963 | if (sync) |
| 1964 | atomic_inc(&BTRFS_I(inode)->sync_writers); |
| 1965 | |
Al Viro | 2ba48ce | 2015-04-09 13:52:01 -0400 | [diff] [blame] | 1966 | if (iocb->ki_flags & IOCB_DIRECT) { |
Christoph Hellwig | 1af5bb4 | 2016-04-07 08:51:56 -0700 | [diff] [blame] | 1967 | num_written = __btrfs_direct_write(iocb, from); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1968 | } else { |
Al Viro | b30ac0f | 2014-04-03 14:29:04 -0400 | [diff] [blame] | 1969 | num_written = __btrfs_buffered_write(file, from, pos); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1970 | if (num_written > 0) |
Al Viro | 867c4f9 | 2014-02-11 19:31:06 -0500 | [diff] [blame] | 1971 | iocb->ki_pos = pos + num_written; |
Chandan Rajendra | 27772b6 | 2016-01-21 15:56:03 +0530 | [diff] [blame] | 1972 | if (clean_page) |
| 1973 | pagecache_isize_extended(inode, oldsize, |
| 1974 | i_size_read(inode)); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1975 | } |
| 1976 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1977 | inode_unlock(inode); |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1978 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1979 | /* |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1980 | * We also have to set last_sub_trans to the current log transid, |
| 1981 | * otherwise subsequent syncs to a file that's been synced in this |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 1982 | * transaction will appear to have already occurred. |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1983 | */ |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 1984 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 6c760c0 | 2012-11-09 10:53:21 -0500 | [diff] [blame] | 1985 | BTRFS_I(inode)->last_sub_trans = root->log_transid; |
Filipe Manana | 2f2ff0e | 2015-03-20 17:19:46 +0000 | [diff] [blame] | 1986 | spin_unlock(&BTRFS_I(inode)->lock); |
Christoph Hellwig | e259221 | 2016-04-07 08:52:01 -0700 | [diff] [blame] | 1987 | if (num_written > 0) |
| 1988 | num_written = generic_write_sync(iocb, num_written); |
Miao Xie | 0a3404d | 2013-01-28 12:34:55 +0000 | [diff] [blame] | 1989 | |
Josef Bacik | b812ce2 | 2012-11-16 13:56:32 -0500 | [diff] [blame] | 1990 | if (sync) |
| 1991 | atomic_dec(&BTRFS_I(inode)->sync_writers); |
Miao Xie | 0a3404d | 2013-01-28 12:34:55 +0000 | [diff] [blame] | 1992 | out: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1993 | current->backing_dev_info = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1994 | return num_written ? num_written : err; |
| 1995 | } |
| 1996 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1997 | int btrfs_release_file(struct inode *inode, struct file *filp) |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1998 | { |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 1999 | struct btrfs_file_private *private = filp->private_data; |
| 2000 | |
| 2001 | if (private && private->trans) |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 2002 | btrfs_ioctl_trans_end(filp); |
Josef Bacik | 23b5ec7 | 2017-07-24 15:14:25 -0400 | [diff] [blame] | 2003 | if (private && private->filldir_buf) |
| 2004 | kfree(private->filldir_buf); |
| 2005 | kfree(private); |
| 2006 | filp->private_data = NULL; |
| 2007 | |
Chris Mason | f6dc45c | 2014-08-20 07:15:33 -0700 | [diff] [blame] | 2008 | /* |
| 2009 | * ordered_data_close is set by settattr when we are about to truncate |
| 2010 | * a file from a non-zero size to a zero size. This tries to |
| 2011 | * flush down new bytes that may have been written if the |
| 2012 | * application were using truncate to replace a file in place. |
| 2013 | */ |
| 2014 | if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 2015 | &BTRFS_I(inode)->runtime_flags)) |
| 2016 | filemap_flush(inode->i_mapping); |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 2017 | return 0; |
| 2018 | } |
| 2019 | |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2020 | static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end) |
| 2021 | { |
| 2022 | int ret; |
| 2023 | |
| 2024 | atomic_inc(&BTRFS_I(inode)->sync_writers); |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 2025 | ret = btrfs_fdatawrite_range(inode, start, end); |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2026 | atomic_dec(&BTRFS_I(inode)->sync_writers); |
| 2027 | |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2031 | /* |
| 2032 | * fsync call for both files and directories. This logs the inode into |
| 2033 | * the tree log instead of forcing full commits whenever possible. |
| 2034 | * |
| 2035 | * It needs to call filemap_fdatawait so that all ordered extent updates are |
| 2036 | * in the metadata btree are up to date for copying to the log. |
| 2037 | * |
| 2038 | * It drops the inode mutex before doing the tree log commit. This is an |
| 2039 | * important optimization for directories because holding the mutex prevents |
| 2040 | * new operations on the dir while we write to disk. |
| 2041 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2042 | int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2043 | { |
Filipe Manana | de17e79 | 2016-03-30 19:03:13 -0400 | [diff] [blame] | 2044 | struct dentry *dentry = file_dentry(file); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2045 | struct inode *inode = d_inode(dentry); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2046 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2047 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2048 | struct btrfs_trans_handle *trans; |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2049 | struct btrfs_log_ctx ctx; |
Jeff Layton | 333427a | 2017-07-06 07:02:31 -0400 | [diff] [blame] | 2050 | int ret = 0, err; |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 2051 | bool full_sync = false; |
David Sterba | 9dcbeed | 2015-11-09 11:44:45 +0100 | [diff] [blame] | 2052 | u64 len; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2053 | |
David Sterba | 9dcbeed | 2015-11-09 11:44:45 +0100 | [diff] [blame] | 2054 | /* |
| 2055 | * The range length can be represented by u64, we have to do the typecasts |
| 2056 | * to avoid signed overflow if it's [0, LLONG_MAX] eg. from fsync() |
| 2057 | */ |
| 2058 | len = (u64)end - (u64)start + 1; |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2059 | trace_btrfs_sync_file(file, datasync); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 2060 | |
Liu Bo | ebb7044 | 2017-11-21 14:35:40 -0700 | [diff] [blame] | 2061 | btrfs_init_log_ctx(&ctx, inode); |
| 2062 | |
Miao Xie | 90abccf | 2012-09-13 04:53:47 -0600 | [diff] [blame] | 2063 | /* |
| 2064 | * We write the dirty pages in the range and wait until they complete |
| 2065 | * out of the ->i_mutex. If so, we can flush the dirty pages by |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2066 | * multi-task, and make the performance up. See |
| 2067 | * btrfs_wait_ordered_range for an explanation of the ASYNC check. |
Miao Xie | 90abccf | 2012-09-13 04:53:47 -0600 | [diff] [blame] | 2068 | */ |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2069 | ret = start_ordered_ops(inode, start, end); |
Miao Xie | 90abccf | 2012-09-13 04:53:47 -0600 | [diff] [blame] | 2070 | if (ret) |
Jeff Layton | 333427a | 2017-07-06 07:02:31 -0400 | [diff] [blame] | 2071 | goto out; |
Miao Xie | 90abccf | 2012-09-13 04:53:47 -0600 | [diff] [blame] | 2072 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2073 | inode_lock(inode); |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2074 | atomic_inc(&root->log_batch); |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2075 | full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 2076 | &BTRFS_I(inode)->runtime_flags); |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2077 | /* |
| 2078 | * We might have have had more pages made dirty after calling |
| 2079 | * start_ordered_ops and before acquiring the inode's i_mutex. |
| 2080 | */ |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2081 | if (full_sync) { |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2082 | /* |
| 2083 | * For a full sync, we need to make sure any ordered operations |
| 2084 | * start and finish before we start logging the inode, so that |
| 2085 | * all extents are persisted and the respective file extent |
| 2086 | * items are in the fs/subvol btree. |
| 2087 | */ |
Filipe Manana | b659ef0 | 2015-03-31 14:16:52 +0100 | [diff] [blame] | 2088 | ret = btrfs_wait_ordered_range(inode, start, len); |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2089 | } else { |
| 2090 | /* |
| 2091 | * Start any new ordered operations before starting to log the |
| 2092 | * inode. We will wait for them to finish in btrfs_sync_log(). |
| 2093 | * |
| 2094 | * Right before acquiring the inode's mutex, we might have new |
| 2095 | * writes dirtying pages, which won't immediately start the |
| 2096 | * respective ordered operations - that is done through the |
| 2097 | * fill_delalloc callbacks invoked from the writepage and |
| 2098 | * writepages address space operations. So make sure we start |
| 2099 | * all ordered operations before starting to log our inode. Not |
| 2100 | * doing this means that while logging the inode, writeback |
| 2101 | * could start and invoke writepage/writepages, which would call |
| 2102 | * the fill_delalloc callbacks (cow_file_range, |
| 2103 | * submit_compressed_extents). These callbacks add first an |
| 2104 | * extent map to the modified list of extents and then create |
| 2105 | * the respective ordered operation, which means in |
| 2106 | * tree-log.c:btrfs_log_inode() we might capture all existing |
| 2107 | * ordered operations (with btrfs_get_logged_extents()) before |
| 2108 | * the fill_delalloc callback adds its ordered operation, and by |
| 2109 | * the time we visit the modified list of extent maps (with |
| 2110 | * btrfs_log_changed_extents()), we see and process the extent |
| 2111 | * map they created. We then use the extent map to construct a |
| 2112 | * file extent item for logging without waiting for the |
| 2113 | * respective ordered operation to finish - this file extent |
| 2114 | * item points to a disk location that might not have yet been |
| 2115 | * written to, containing random data - so after a crash a log |
| 2116 | * replay will make our inode have file extent items that point |
| 2117 | * to disk locations containing invalid data, as we returned |
| 2118 | * success to userspace without waiting for the respective |
| 2119 | * ordered operation to finish, because it wasn't captured by |
| 2120 | * btrfs_get_logged_extents(). |
| 2121 | */ |
| 2122 | ret = start_ordered_ops(inode, start, end); |
| 2123 | } |
| 2124 | if (ret) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2125 | inode_unlock(inode); |
Filipe Manana | 669249e | 2014-09-02 11:09:58 +0100 | [diff] [blame] | 2126 | goto out; |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2127 | } |
Miao Xie | 2ecb792 | 2012-09-06 04:04:27 -0600 | [diff] [blame] | 2128 | atomic_inc(&root->log_batch); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 2129 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2130 | /* |
Filipe Manana | 3a8b36f | 2015-03-01 20:36:00 +0000 | [diff] [blame] | 2131 | * If the last transaction that changed this file was before the current |
| 2132 | * transaction and we have the full sync flag set in our inode, we can |
| 2133 | * bail out now without any syncing. |
| 2134 | * |
| 2135 | * Note that we can't bail out if the full sync flag isn't set. This is |
| 2136 | * because when the full sync flag is set we start all ordered extents |
| 2137 | * and wait for them to fully complete - when they complete they update |
| 2138 | * the inode's last_trans field through: |
| 2139 | * |
| 2140 | * btrfs_finish_ordered_io() -> |
| 2141 | * btrfs_update_inode_fallback() -> |
| 2142 | * btrfs_update_inode() -> |
| 2143 | * btrfs_set_inode_last_trans() |
| 2144 | * |
| 2145 | * So we are sure that last_trans is up to date and can do this check to |
| 2146 | * bail out safely. For the fast path, when the full sync flag is not |
| 2147 | * set in our inode, we can not do it because we start only our ordered |
| 2148 | * extents and don't wait for them to complete (that is when |
| 2149 | * btrfs_finish_ordered_io runs), so here at this point their last_trans |
| 2150 | * value might be less than or equals to fs_info->last_trans_committed, |
| 2151 | * and setting a speculative last_trans for an inode when a buffered |
| 2152 | * write is made (such as fs_info->generation + 1 for example) would not |
| 2153 | * be reliable since after setting the value and before fsync is called |
| 2154 | * any number of transactions can start and commit (transaction kthread |
| 2155 | * commits the current transaction periodically), and a transaction |
| 2156 | * commit does not start nor waits for ordered extents to complete. |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 2157 | */ |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 2158 | smp_mb(); |
Nikolay Borisov | 0f8939b | 2017-01-18 00:31:30 +0200 | [diff] [blame] | 2159 | if (btrfs_inode_in_log(BTRFS_I(inode), fs_info->generation) || |
Filipe Manana | affc0ff | 2016-02-24 07:35:05 +0000 | [diff] [blame] | 2160 | (full_sync && BTRFS_I(inode)->last_trans <= |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2161 | fs_info->last_trans_committed) || |
Filipe Manana | affc0ff | 2016-02-24 07:35:05 +0000 | [diff] [blame] | 2162 | (!btrfs_have_ordered_extents_in_range(inode, start, len) && |
| 2163 | BTRFS_I(inode)->last_trans |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2164 | <= fs_info->last_trans_committed)) { |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2165 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2166 | * We've had everything committed since the last time we were |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2167 | * modified so clear this flag in case it was set for whatever |
| 2168 | * reason, it's no longer relevant. |
| 2169 | */ |
| 2170 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 2171 | &BTRFS_I(inode)->runtime_flags); |
Filipe Manana | 0596a90 | 2016-06-14 14:18:27 +0100 | [diff] [blame] | 2172 | /* |
| 2173 | * An ordered extent might have started before and completed |
| 2174 | * already with io errors, in which case the inode was not |
| 2175 | * updated and we end up here. So check the inode's mapping |
Jeff Layton | 333427a | 2017-07-06 07:02:31 -0400 | [diff] [blame] | 2176 | * for any errors that might have happened since we last |
| 2177 | * checked called fsync. |
Filipe Manana | 0596a90 | 2016-06-14 14:18:27 +0100 | [diff] [blame] | 2178 | */ |
Jeff Layton | 333427a | 2017-07-06 07:02:31 -0400 | [diff] [blame] | 2179 | ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2180 | inode_unlock(inode); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2181 | goto out; |
| 2182 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 2183 | |
| 2184 | /* |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2185 | * ok we haven't committed the transaction yet, lets do a commit |
| 2186 | */ |
Dan Carpenter | 6f902af | 2010-05-29 09:49:07 +0000 | [diff] [blame] | 2187 | if (file->private_data) |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 2188 | btrfs_ioctl_trans_end(file); |
| 2189 | |
Josef Bacik | 5039edd | 2014-01-15 13:34:13 -0500 | [diff] [blame] | 2190 | /* |
| 2191 | * We use start here because we will need to wait on the IO to complete |
| 2192 | * in btrfs_sync_log, which could require joining a transaction (for |
| 2193 | * example checking cross references in the nocow path). If we use join |
| 2194 | * here we could get into a situation where we're waiting on IO to |
| 2195 | * happen that is blocked on a transaction trying to commit. With start |
| 2196 | * we inc the extwriter counter, so we wait for all extwriters to exit |
| 2197 | * before we start blocking join'ers. This comment is to keep somebody |
| 2198 | * from thinking they are super smart and changing this to |
| 2199 | * btrfs_join_transaction *cough*Josef*cough*. |
| 2200 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2201 | trans = btrfs_start_transaction(root, 0); |
| 2202 | if (IS_ERR(trans)) { |
| 2203 | ret = PTR_ERR(trans); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2204 | inode_unlock(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2205 | goto out; |
| 2206 | } |
Josef Bacik | 5039edd | 2014-01-15 13:34:13 -0500 | [diff] [blame] | 2207 | trans->sync = true; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2208 | |
Filipe Manana | 49dae1b | 2014-09-06 22:34:39 +0100 | [diff] [blame] | 2209 | ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2210 | if (ret < 0) { |
Filipe David Borba Manana | a0634be | 2013-09-11 20:36:44 +0100 | [diff] [blame] | 2211 | /* Fallthrough and commit/free transaction. */ |
| 2212 | ret = 1; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2213 | } |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2214 | |
| 2215 | /* we've logged all the items and now have a consistent |
| 2216 | * version of the file in the log. It is possible that |
| 2217 | * someone will come in and modify the file, but that's |
| 2218 | * fine because the log is consistent on disk, and we |
| 2219 | * have references to all of the file's extents |
| 2220 | * |
| 2221 | * It is possible that someone will come in and log the |
| 2222 | * file again, but that will end up using the synchronization |
| 2223 | * inside btrfs_sync_log to keep things safe. |
| 2224 | */ |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2225 | inode_unlock(inode); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 2226 | |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 2227 | /* |
| 2228 | * If any of the ordered extents had an error, just return it to user |
| 2229 | * space, so that the application knows some writes didn't succeed and |
| 2230 | * can take proper action (retry for e.g.). Blindly committing the |
| 2231 | * transaction in this case, would fool userspace that everything was |
| 2232 | * successful. And we also want to make sure our log doesn't contain |
| 2233 | * file extent items pointing to extents that weren't fully written to - |
| 2234 | * just like in the non fast fsync path, where we check for the ordered |
| 2235 | * operation's error flag before writing to the log tree and return -EIO |
| 2236 | * if any of them had this flag set (btrfs_wait_ordered_range) - |
| 2237 | * therefore we need to check for errors in the ordered operations, |
| 2238 | * which are indicated by ctx.io_err. |
| 2239 | */ |
| 2240 | if (ctx.io_err) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2241 | btrfs_end_transaction(trans); |
Filipe Manana | 8407f55 | 2014-09-05 15:14:39 +0100 | [diff] [blame] | 2242 | ret = ctx.io_err; |
| 2243 | goto out; |
| 2244 | } |
| 2245 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 2246 | if (ret != BTRFS_NO_LOG_SYNC) { |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2247 | if (!ret) { |
Miao Xie | 8b050d3 | 2014-02-20 18:08:58 +0800 | [diff] [blame] | 2248 | ret = btrfs_sync_log(trans, root, &ctx); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2249 | if (!ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2250 | ret = btrfs_end_transaction(trans); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2251 | goto out; |
Josef Bacik | 2ab28f3 | 2012-10-12 15:27:49 -0400 | [diff] [blame] | 2252 | } |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 2253 | } |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2254 | if (!full_sync) { |
David Sterba | 9dcbeed | 2015-11-09 11:44:45 +0100 | [diff] [blame] | 2255 | ret = btrfs_wait_ordered_range(inode, start, len); |
Filipe Manana | b05fd87 | 2014-05-29 23:31:39 +0100 | [diff] [blame] | 2256 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2257 | btrfs_end_transaction(trans); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2258 | goto out; |
Filipe Manana | b05fd87 | 2014-05-29 23:31:39 +0100 | [diff] [blame] | 2259 | } |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2260 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2261 | ret = btrfs_commit_transaction(trans); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 2262 | } else { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2263 | ret = btrfs_end_transaction(trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2264 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2265 | out: |
Liu Bo | ebb7044 | 2017-11-21 14:35:40 -0700 | [diff] [blame] | 2266 | ASSERT(list_empty(&ctx.list)); |
Jeff Layton | 333427a | 2017-07-06 07:02:31 -0400 | [diff] [blame] | 2267 | err = file_check_and_advance_wb_err(file); |
| 2268 | if (!ret) |
| 2269 | ret = err; |
Roel Kluin | 014e4ac | 2010-01-29 10:42:11 +0000 | [diff] [blame] | 2270 | return ret > 0 ? -EIO : ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2273 | static const struct vm_operations_struct btrfs_file_vm_ops = { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 2274 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 2275 | .map_pages = filemap_map_pages, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2276 | .page_mkwrite = btrfs_page_mkwrite, |
| 2277 | }; |
| 2278 | |
| 2279 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 2280 | { |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 2281 | struct address_space *mapping = filp->f_mapping; |
| 2282 | |
| 2283 | if (!mapping->a_ops->readpage) |
| 2284 | return -ENOEXEC; |
| 2285 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2286 | file_accessed(filp); |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 2287 | vma->vm_ops = &btrfs_file_vm_ops; |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 2288 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2289 | return 0; |
| 2290 | } |
| 2291 | |
Nikolay Borisov | 35339c2 | 2017-02-20 13:50:46 +0200 | [diff] [blame] | 2292 | static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf, |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2293 | int slot, u64 start, u64 end) |
| 2294 | { |
| 2295 | struct btrfs_file_extent_item *fi; |
| 2296 | struct btrfs_key key; |
| 2297 | |
| 2298 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) |
| 2299 | return 0; |
| 2300 | |
| 2301 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Nikolay Borisov | 35339c2 | 2017-02-20 13:50:46 +0200 | [diff] [blame] | 2302 | if (key.objectid != btrfs_ino(inode) || |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2303 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 2304 | return 0; |
| 2305 | |
| 2306 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 2307 | |
| 2308 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 2309 | return 0; |
| 2310 | |
| 2311 | if (btrfs_file_extent_disk_bytenr(leaf, fi)) |
| 2312 | return 0; |
| 2313 | |
| 2314 | if (key.offset == end) |
| 2315 | return 1; |
| 2316 | if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start) |
| 2317 | return 1; |
| 2318 | return 0; |
| 2319 | } |
| 2320 | |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2321 | static int fill_holes(struct btrfs_trans_handle *trans, |
| 2322 | struct btrfs_inode *inode, |
| 2323 | struct btrfs_path *path, u64 offset, u64 end) |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2324 | { |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2325 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 2326 | struct btrfs_root *root = inode->root; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2327 | struct extent_buffer *leaf; |
| 2328 | struct btrfs_file_extent_item *fi; |
| 2329 | struct extent_map *hole_em; |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2330 | struct extent_map_tree *em_tree = &inode->extent_tree; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2331 | struct btrfs_key key; |
| 2332 | int ret; |
| 2333 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2334 | if (btrfs_fs_incompat(fs_info, NO_HOLES)) |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 2335 | goto out; |
| 2336 | |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2337 | key.objectid = btrfs_ino(inode); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2338 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2339 | key.offset = offset; |
| 2340 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2341 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Josef Bacik | f94480b | 2016-11-14 14:06:22 -0500 | [diff] [blame] | 2342 | if (ret <= 0) { |
| 2343 | /* |
| 2344 | * We should have dropped this offset, so if we find it then |
| 2345 | * something has gone horribly wrong. |
| 2346 | */ |
| 2347 | if (ret == 0) |
| 2348 | ret = -EINVAL; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2349 | return ret; |
Josef Bacik | f94480b | 2016-11-14 14:06:22 -0500 | [diff] [blame] | 2350 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2351 | |
| 2352 | leaf = path->nodes[0]; |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2353 | if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2354 | u64 num_bytes; |
| 2355 | |
| 2356 | path->slots[0]--; |
| 2357 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2358 | struct btrfs_file_extent_item); |
| 2359 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + |
| 2360 | end - offset; |
| 2361 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2362 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); |
| 2363 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2364 | btrfs_mark_buffer_dirty(leaf); |
| 2365 | goto out; |
| 2366 | } |
| 2367 | |
chandan | 1707e26 | 2014-07-01 12:04:28 +0530 | [diff] [blame] | 2368 | if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2369 | u64 num_bytes; |
| 2370 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2371 | key.offset = offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2372 | btrfs_set_item_key_safe(fs_info, path, &key); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2373 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 2374 | struct btrfs_file_extent_item); |
| 2375 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end - |
| 2376 | offset; |
| 2377 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 2378 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); |
| 2379 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 2380 | btrfs_mark_buffer_dirty(leaf); |
| 2381 | goto out; |
| 2382 | } |
| 2383 | btrfs_release_path(path); |
| 2384 | |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2385 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 2386 | offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2387 | if (ret) |
| 2388 | return ret; |
| 2389 | |
| 2390 | out: |
| 2391 | btrfs_release_path(path); |
| 2392 | |
| 2393 | hole_em = alloc_extent_map(); |
| 2394 | if (!hole_em) { |
| 2395 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2396 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2397 | } else { |
| 2398 | hole_em->start = offset; |
| 2399 | hole_em->len = end - offset; |
Josef Bacik | cc95bef | 2013-04-04 14:31:27 -0400 | [diff] [blame] | 2400 | hole_em->ram_bytes = hole_em->len; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2401 | hole_em->orig_start = offset; |
| 2402 | |
| 2403 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 2404 | hole_em->block_len = 0; |
Josef Bacik | b493968 | 2012-12-03 10:31:19 -0500 | [diff] [blame] | 2405 | hole_em->orig_block_len = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2406 | hole_em->bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2407 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 2408 | hole_em->generation = trans->transid; |
| 2409 | |
| 2410 | do { |
| 2411 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); |
| 2412 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2413 | ret = add_extent_mapping(em_tree, hole_em, 1); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2414 | write_unlock(&em_tree->lock); |
| 2415 | } while (ret == -EEXIST); |
| 2416 | free_extent_map(hole_em); |
| 2417 | if (ret) |
| 2418 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2419 | &inode->runtime_flags); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2425 | /* |
| 2426 | * Find a hole extent on given inode and change start/len to the end of hole |
| 2427 | * extent.(hole/vacuum extent whose em->start <= start && |
| 2428 | * em->start + em->len > start) |
| 2429 | * When a hole extent is found, return 1 and modify start/len. |
| 2430 | */ |
| 2431 | static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len) |
| 2432 | { |
Filipe Manana | 609805d | 2017-05-30 05:29:09 +0100 | [diff] [blame] | 2433 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2434 | struct extent_map *em; |
| 2435 | int ret = 0; |
| 2436 | |
Filipe Manana | 609805d | 2017-05-30 05:29:09 +0100 | [diff] [blame] | 2437 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, |
| 2438 | round_down(*start, fs_info->sectorsize), |
| 2439 | round_up(*len, fs_info->sectorsize), 0); |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 2440 | if (IS_ERR(em)) |
| 2441 | return PTR_ERR(em); |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2442 | |
| 2443 | /* Hole or vacuum extent(only exists in no-hole mode) */ |
| 2444 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 2445 | ret = 1; |
| 2446 | *len = em->start + em->len > *start + *len ? |
| 2447 | 0 : *start + *len - em->start - em->len; |
| 2448 | *start = em->start + em->len; |
| 2449 | } |
| 2450 | free_extent_map(em); |
| 2451 | return ret; |
| 2452 | } |
| 2453 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2454 | static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) |
| 2455 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2456 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2457 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2458 | struct extent_state *cached_state = NULL; |
| 2459 | struct btrfs_path *path; |
| 2460 | struct btrfs_block_rsv *rsv; |
| 2461 | struct btrfs_trans_handle *trans; |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2462 | u64 lockstart; |
| 2463 | u64 lockend; |
| 2464 | u64 tail_start; |
| 2465 | u64 tail_len; |
| 2466 | u64 orig_start = offset; |
| 2467 | u64 cur_offset; |
Chris Mason | 5f52a2c | 2016-12-13 09:14:42 -0800 | [diff] [blame] | 2468 | u64 min_size = btrfs_calc_trans_metadata_size(fs_info, 1); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2469 | u64 drop_end; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2470 | int ret = 0; |
| 2471 | int err = 0; |
Alexandru Moise | 6e4d6fa | 2015-09-22 21:00:07 +0000 | [diff] [blame] | 2472 | unsigned int rsv_count; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2473 | bool same_block; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2474 | bool no_holes = btrfs_fs_incompat(fs_info, NO_HOLES); |
Filipe Manana | a1a50f6 | 2014-04-26 01:35:31 +0100 | [diff] [blame] | 2475 | u64 ino_size; |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2476 | bool truncated_block = false; |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2477 | bool updated_inode = false; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2478 | |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2479 | ret = btrfs_wait_ordered_range(inode, offset, len); |
| 2480 | if (ret) |
| 2481 | return ret; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2482 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2483 | inode_lock(inode); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2484 | ino_size = round_up(inode->i_size, fs_info->sectorsize); |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2485 | ret = find_first_non_hole(inode, &offset, &len); |
| 2486 | if (ret < 0) |
| 2487 | goto out_only_mutex; |
| 2488 | if (ret && !len) { |
| 2489 | /* Already in a large hole */ |
| 2490 | ret = 0; |
| 2491 | goto out_only_mutex; |
| 2492 | } |
| 2493 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 2494 | lockstart = round_up(offset, btrfs_inode_sectorsize(inode)); |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2495 | lockend = round_down(offset + len, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 2496 | btrfs_inode_sectorsize(inode)) - 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2497 | same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset)) |
| 2498 | == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)); |
Miao Xie | 7426cc0 | 2012-12-05 10:54:52 +0000 | [diff] [blame] | 2499 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2500 | * We needn't truncate any block which is beyond the end of the file |
Miao Xie | 7426cc0 | 2012-12-05 10:54:52 +0000 | [diff] [blame] | 2501 | * because we are sure there is no data there. |
| 2502 | */ |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2503 | /* |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2504 | * Only do this if we are in the same block and we aren't doing the |
| 2505 | * entire block. |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2506 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2507 | if (same_block && len < fs_info->sectorsize) { |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2508 | if (offset < ino_size) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2509 | truncated_block = true; |
| 2510 | ret = btrfs_truncate_block(inode, offset, len, 0); |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2511 | } else { |
| 2512 | ret = 0; |
| 2513 | } |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2514 | goto out_only_mutex; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2515 | } |
| 2516 | |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2517 | /* zero back part of the first block */ |
Filipe Manana | 12870f1 | 2014-02-15 15:55:58 +0000 | [diff] [blame] | 2518 | if (offset < ino_size) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2519 | truncated_block = true; |
| 2520 | ret = btrfs_truncate_block(inode, offset, 0, 0); |
Miao Xie | 7426cc0 | 2012-12-05 10:54:52 +0000 | [diff] [blame] | 2521 | if (ret) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2522 | inode_unlock(inode); |
Miao Xie | 7426cc0 | 2012-12-05 10:54:52 +0000 | [diff] [blame] | 2523 | return ret; |
| 2524 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2525 | } |
| 2526 | |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2527 | /* Check the aligned pages after the first unaligned page, |
| 2528 | * if offset != orig_start, which means the first unaligned page |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2529 | * including several following pages are already in holes, |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2530 | * the extra check can be skipped */ |
| 2531 | if (offset == orig_start) { |
| 2532 | /* after truncate page, check hole again */ |
| 2533 | len = offset + len - lockstart; |
| 2534 | offset = lockstart; |
| 2535 | ret = find_first_non_hole(inode, &offset, &len); |
| 2536 | if (ret < 0) |
| 2537 | goto out_only_mutex; |
| 2538 | if (ret && !len) { |
| 2539 | ret = 0; |
| 2540 | goto out_only_mutex; |
| 2541 | } |
| 2542 | lockstart = offset; |
| 2543 | } |
| 2544 | |
| 2545 | /* Check the tail unaligned part is in a hole */ |
| 2546 | tail_start = lockend + 1; |
| 2547 | tail_len = offset + len - tail_start; |
| 2548 | if (tail_len) { |
| 2549 | ret = find_first_non_hole(inode, &tail_start, &tail_len); |
| 2550 | if (unlikely(ret < 0)) |
| 2551 | goto out_only_mutex; |
| 2552 | if (!ret) { |
| 2553 | /* zero the front end of the last page */ |
| 2554 | if (tail_start + tail_len < ino_size) { |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2555 | truncated_block = true; |
| 2556 | ret = btrfs_truncate_block(inode, |
| 2557 | tail_start + tail_len, |
| 2558 | 0, 1); |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2559 | if (ret) |
| 2560 | goto out_only_mutex; |
Qu Wenruo | 51f395a | 2014-08-08 13:06:20 +0800 | [diff] [blame] | 2561 | } |
Miao Xie | 0061280 | 2012-12-05 10:54:12 +0000 | [diff] [blame] | 2562 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | if (lockend < lockstart) { |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2566 | ret = 0; |
| 2567 | goto out_only_mutex; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | while (1) { |
| 2571 | struct btrfs_ordered_extent *ordered; |
| 2572 | |
| 2573 | truncate_pagecache_range(inode, lockstart, lockend); |
| 2574 | |
| 2575 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2576 | &cached_state); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2577 | ordered = btrfs_lookup_first_ordered_extent(inode, lockend); |
| 2578 | |
| 2579 | /* |
| 2580 | * We need to make sure we have no ordered extents in this range |
| 2581 | * and nobody raced in and read a page in this range, if we did |
| 2582 | * we need to try again. |
| 2583 | */ |
| 2584 | if ((!ordered || |
Filipe David Borba Manana | 6126e3c | 2013-11-19 16:19:24 +0000 | [diff] [blame] | 2585 | (ordered->file_offset + ordered->len <= lockstart || |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2586 | ordered->file_offset > lockend)) && |
Alex Gartrell | fc4adbff | 2014-05-20 13:07:56 -0700 | [diff] [blame] | 2587 | !btrfs_page_exists_in_range(inode, lockstart, lockend)) { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2588 | if (ordered) |
| 2589 | btrfs_put_ordered_extent(ordered); |
| 2590 | break; |
| 2591 | } |
| 2592 | if (ordered) |
| 2593 | btrfs_put_ordered_extent(ordered); |
| 2594 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, |
| 2595 | lockend, &cached_state, GFP_NOFS); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2596 | ret = btrfs_wait_ordered_range(inode, lockstart, |
| 2597 | lockend - lockstart + 1); |
| 2598 | if (ret) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2599 | inode_unlock(inode); |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2600 | return ret; |
| 2601 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | path = btrfs_alloc_path(); |
| 2605 | if (!path) { |
| 2606 | ret = -ENOMEM; |
| 2607 | goto out; |
| 2608 | } |
| 2609 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2610 | rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2611 | if (!rsv) { |
| 2612 | ret = -ENOMEM; |
| 2613 | goto out_free; |
| 2614 | } |
Chris Mason | 5f52a2c | 2016-12-13 09:14:42 -0800 | [diff] [blame] | 2615 | rsv->size = btrfs_calc_trans_metadata_size(fs_info, 1); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2616 | rsv->failfast = 1; |
| 2617 | |
| 2618 | /* |
| 2619 | * 1 - update the inode |
| 2620 | * 1 - removing the extents in the range |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 2621 | * 1 - adding the hole extent if no_holes isn't set |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2622 | */ |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 2623 | rsv_count = no_holes ? 2 : 3; |
| 2624 | trans = btrfs_start_transaction(root, rsv_count); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2625 | if (IS_ERR(trans)) { |
| 2626 | err = PTR_ERR(trans); |
| 2627 | goto out_free; |
| 2628 | } |
| 2629 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2630 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 2631 | min_size, 0); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2632 | BUG_ON(ret); |
| 2633 | trans->block_rsv = rsv; |
| 2634 | |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2635 | cur_offset = lockstart; |
| 2636 | len = lockend - cur_offset; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2637 | while (cur_offset < lockend) { |
| 2638 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 2639 | cur_offset, lockend + 1, |
Filipe David Borba Manana | 1acae57 | 2014-01-07 11:42:27 +0000 | [diff] [blame] | 2640 | &drop_end, 1, 0, 0, NULL); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2641 | if (ret != -ENOSPC) |
| 2642 | break; |
| 2643 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2644 | trans->block_rsv = &fs_info->trans_block_rsv; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2645 | |
Josef Bacik | 62fe51c1 | 2016-11-16 09:13:39 -0500 | [diff] [blame] | 2646 | if (cur_offset < drop_end && cur_offset < ino_size) { |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2647 | ret = fill_holes(trans, BTRFS_I(inode), path, |
| 2648 | cur_offset, drop_end); |
Filipe Manana | 12870f1 | 2014-02-15 15:55:58 +0000 | [diff] [blame] | 2649 | if (ret) { |
Josef Bacik | f94480b | 2016-11-14 14:06:22 -0500 | [diff] [blame] | 2650 | /* |
| 2651 | * If we failed then we didn't insert our hole |
| 2652 | * entries for the area we dropped, so now the |
| 2653 | * fs is corrupted, so we must abort the |
| 2654 | * transaction. |
| 2655 | */ |
| 2656 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 12870f1 | 2014-02-15 15:55:58 +0000 | [diff] [blame] | 2657 | err = ret; |
| 2658 | break; |
| 2659 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | cur_offset = drop_end; |
| 2663 | |
| 2664 | ret = btrfs_update_inode(trans, root, inode); |
| 2665 | if (ret) { |
| 2666 | err = ret; |
| 2667 | break; |
| 2668 | } |
| 2669 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2670 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2671 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2672 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 2673 | trans = btrfs_start_transaction(root, rsv_count); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2674 | if (IS_ERR(trans)) { |
| 2675 | ret = PTR_ERR(trans); |
| 2676 | trans = NULL; |
| 2677 | break; |
| 2678 | } |
| 2679 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2680 | ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 2681 | rsv, min_size, 0); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2682 | BUG_ON(ret); /* shouldn't happen */ |
| 2683 | trans->block_rsv = rsv; |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2684 | |
| 2685 | ret = find_first_non_hole(inode, &cur_offset, &len); |
| 2686 | if (unlikely(ret < 0)) |
| 2687 | break; |
| 2688 | if (ret && !len) { |
| 2689 | ret = 0; |
| 2690 | break; |
| 2691 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | if (ret) { |
| 2695 | err = ret; |
| 2696 | goto out_trans; |
| 2697 | } |
| 2698 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2699 | trans->block_rsv = &fs_info->trans_block_rsv; |
Filipe Manana | fc19c5e | 2014-04-29 13:18:40 +0100 | [diff] [blame] | 2700 | /* |
Filipe Manana | 2959a32 | 2015-11-02 12:32:44 +0000 | [diff] [blame] | 2701 | * If we are using the NO_HOLES feature we might have had already an |
| 2702 | * hole that overlaps a part of the region [lockstart, lockend] and |
| 2703 | * ends at (or beyond) lockend. Since we have no file extent items to |
| 2704 | * represent holes, drop_end can be less than lockend and so we must |
| 2705 | * make sure we have an extent map representing the existing hole (the |
| 2706 | * call to __btrfs_drop_extents() might have dropped the existing extent |
| 2707 | * map representing the existing hole), otherwise the fast fsync path |
| 2708 | * will not record the existence of the hole region |
| 2709 | * [existing_hole_start, lockend]. |
| 2710 | */ |
| 2711 | if (drop_end <= lockend) |
| 2712 | drop_end = lockend + 1; |
| 2713 | /* |
Filipe Manana | fc19c5e | 2014-04-29 13:18:40 +0100 | [diff] [blame] | 2714 | * Don't insert file hole extent item if it's for a range beyond eof |
| 2715 | * (because it's useless) or if it represents a 0 bytes range (when |
| 2716 | * cur_offset == drop_end). |
| 2717 | */ |
| 2718 | if (cur_offset < ino_size && cur_offset < drop_end) { |
Nikolay Borisov | a012a74 | 2017-02-20 13:50:47 +0200 | [diff] [blame] | 2719 | ret = fill_holes(trans, BTRFS_I(inode), path, |
| 2720 | cur_offset, drop_end); |
Filipe Manana | 12870f1 | 2014-02-15 15:55:58 +0000 | [diff] [blame] | 2721 | if (ret) { |
Josef Bacik | f94480b | 2016-11-14 14:06:22 -0500 | [diff] [blame] | 2722 | /* Same comment as above. */ |
| 2723 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | 12870f1 | 2014-02-15 15:55:58 +0000 | [diff] [blame] | 2724 | err = ret; |
| 2725 | goto out_trans; |
| 2726 | } |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | out_trans: |
| 2730 | if (!trans) |
| 2731 | goto out_free; |
| 2732 | |
Tsutomu Itoh | e1f5790 | 2012-11-08 04:47:33 +0000 | [diff] [blame] | 2733 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 2734 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Tsutomu Itoh | e1f5790 | 2012-11-08 04:47:33 +0000 | [diff] [blame] | 2735 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2736 | trans->block_rsv = &fs_info->trans_block_rsv; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2737 | ret = btrfs_update_inode(trans, root, inode); |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2738 | updated_inode = true; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2739 | btrfs_end_transaction(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2740 | btrfs_btree_balance_dirty(fs_info); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2741 | out_free: |
| 2742 | btrfs_free_path(path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2743 | btrfs_free_block_rsv(fs_info, rsv); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2744 | out: |
| 2745 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 2746 | &cached_state, GFP_NOFS); |
Qu Wenruo | d778154 | 2014-05-30 15:16:10 +0800 | [diff] [blame] | 2747 | out_only_mutex: |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2748 | if (!updated_inode && truncated_block && !ret && !err) { |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2749 | /* |
| 2750 | * If we only end up zeroing part of a page, we still need to |
| 2751 | * update the inode item, so that all the time fields are |
| 2752 | * updated as well as the necessary btrfs inode in memory fields |
| 2753 | * for detecting, at fsync time, if the inode isn't yet in the |
| 2754 | * log tree or it's there but not up to date. |
| 2755 | */ |
| 2756 | trans = btrfs_start_transaction(root, 1); |
| 2757 | if (IS_ERR(trans)) { |
| 2758 | err = PTR_ERR(trans); |
| 2759 | } else { |
| 2760 | err = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2761 | ret = btrfs_end_transaction(trans); |
Filipe Manana | e8c1c76 | 2015-02-15 22:38:54 +0000 | [diff] [blame] | 2762 | } |
| 2763 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2764 | inode_unlock(inode); |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2765 | if (ret && !err) |
| 2766 | err = ret; |
| 2767 | return err; |
| 2768 | } |
| 2769 | |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2770 | /* Helper structure to record which range is already reserved */ |
| 2771 | struct falloc_range { |
| 2772 | struct list_head list; |
| 2773 | u64 start; |
| 2774 | u64 len; |
| 2775 | }; |
| 2776 | |
| 2777 | /* |
| 2778 | * Helper function to add falloc range |
| 2779 | * |
| 2780 | * Caller should have locked the larger range of extent containing |
| 2781 | * [start, len) |
| 2782 | */ |
| 2783 | static int add_falloc_range(struct list_head *head, u64 start, u64 len) |
| 2784 | { |
| 2785 | struct falloc_range *prev = NULL; |
| 2786 | struct falloc_range *range = NULL; |
| 2787 | |
| 2788 | if (list_empty(head)) |
| 2789 | goto insert; |
| 2790 | |
| 2791 | /* |
| 2792 | * As fallocate iterate by bytenr order, we only need to check |
| 2793 | * the last range. |
| 2794 | */ |
| 2795 | prev = list_entry(head->prev, struct falloc_range, list); |
| 2796 | if (prev->start + prev->len == start) { |
| 2797 | prev->len += len; |
| 2798 | return 0; |
| 2799 | } |
| 2800 | insert: |
David Sterba | 32fc932 | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2801 | range = kmalloc(sizeof(*range), GFP_KERNEL); |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2802 | if (!range) |
| 2803 | return -ENOMEM; |
| 2804 | range->start = start; |
| 2805 | range->len = len; |
| 2806 | list_add_tail(&range->list, head); |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2810 | static long btrfs_fallocate(struct file *file, int mode, |
| 2811 | loff_t offset, loff_t len) |
| 2812 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2813 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2814 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2815 | struct extent_changeset *data_reserved = NULL; |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2816 | struct falloc_range *range; |
| 2817 | struct falloc_range *tmp; |
| 2818 | struct list_head reserve_list; |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2819 | u64 cur_offset; |
| 2820 | u64 last_byte; |
| 2821 | u64 alloc_start; |
| 2822 | u64 alloc_end; |
| 2823 | u64 alloc_hint = 0; |
| 2824 | u64 locked_end; |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2825 | u64 actual_end = 0; |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2826 | struct extent_map *em; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 2827 | int blocksize = btrfs_inode_sectorsize(inode); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2828 | int ret; |
| 2829 | |
Miao Xie | 797f427 | 2012-11-28 10:28:07 +0000 | [diff] [blame] | 2830 | alloc_start = round_down(offset, blocksize); |
| 2831 | alloc_end = round_up(offset + len, blocksize); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 2832 | cur_offset = alloc_start; |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2833 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2834 | /* Make sure we aren't being give some crap mode */ |
| 2835 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2836 | return -EOPNOTSUPP; |
| 2837 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 2838 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 2839 | return btrfs_punch_hole(inode, offset, len); |
| 2840 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2841 | /* |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2842 | * Only trigger disk allocation, don't trigger qgroup reserve |
| 2843 | * |
| 2844 | * For qgroup space, it will be checked later. |
Chris Mason | d98456f | 2012-01-31 20:27:41 -0500 | [diff] [blame] | 2845 | */ |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 2846 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), |
| 2847 | alloc_end - alloc_start); |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2848 | if (ret < 0) |
Chris Mason | d98456f | 2012-01-31 20:27:41 -0500 | [diff] [blame] | 2849 | return ret; |
| 2850 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2851 | inode_lock(inode); |
Davide Italiano | 2a162ce | 2015-04-06 22:09:15 -0700 | [diff] [blame] | 2852 | |
| 2853 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) { |
| 2854 | ret = inode_newsize_ok(inode, offset + len); |
| 2855 | if (ret) |
| 2856 | goto out; |
| 2857 | } |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2858 | |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2859 | /* |
| 2860 | * TODO: Move these two operations after we have checked |
| 2861 | * accurate reserved space, or fallocate can still fail but |
| 2862 | * with page truncated or size expanded. |
| 2863 | * |
| 2864 | * But that's a minor problem and won't do much harm BTW. |
| 2865 | */ |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2866 | if (alloc_start > inode->i_size) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2867 | ret = btrfs_cont_expand(inode, i_size_read(inode), |
| 2868 | alloc_start); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2869 | if (ret) |
| 2870 | goto out; |
Qu Wenruo | 0f6925f | 2015-10-14 15:26:13 +0800 | [diff] [blame] | 2871 | } else if (offset + len > inode->i_size) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2872 | /* |
| 2873 | * If we are fallocating from the end of the file onward we |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2874 | * need to zero out the end of the block if i_size lands in the |
| 2875 | * middle of a block. |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2876 | */ |
Chandan Rajendra | 9703fef | 2016-01-21 15:55:56 +0530 | [diff] [blame] | 2877 | ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2878 | if (ret) |
| 2879 | goto out; |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2880 | } |
| 2881 | |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2882 | /* |
| 2883 | * wait for ordered IO before we have any locks. We'll loop again |
| 2884 | * below with the locks held. |
| 2885 | */ |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2886 | ret = btrfs_wait_ordered_range(inode, alloc_start, |
| 2887 | alloc_end - alloc_start); |
| 2888 | if (ret) |
| 2889 | goto out; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2890 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2891 | locked_end = alloc_end - 1; |
| 2892 | while (1) { |
| 2893 | struct btrfs_ordered_extent *ordered; |
| 2894 | |
| 2895 | /* the extent lock is ordered inside the running |
| 2896 | * transaction |
| 2897 | */ |
| 2898 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2899 | locked_end, &cached_state); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2900 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 2901 | alloc_end - 1); |
| 2902 | if (ordered && |
| 2903 | ordered->file_offset + ordered->len > alloc_start && |
| 2904 | ordered->file_offset < alloc_end) { |
| 2905 | btrfs_put_ordered_extent(ordered); |
| 2906 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 2907 | alloc_start, locked_end, |
David Sterba | 32fc932 | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2908 | &cached_state, GFP_KERNEL); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2909 | /* |
| 2910 | * we can't wait on the range with the transaction |
| 2911 | * running or with the extent lock held |
| 2912 | */ |
Josef Bacik | 0ef8b72 | 2013-10-25 16:13:35 -0400 | [diff] [blame] | 2913 | ret = btrfs_wait_ordered_range(inode, alloc_start, |
| 2914 | alloc_end - alloc_start); |
| 2915 | if (ret) |
| 2916 | goto out; |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2917 | } else { |
| 2918 | if (ordered) |
| 2919 | btrfs_put_ordered_extent(ordered); |
| 2920 | break; |
| 2921 | } |
| 2922 | } |
| 2923 | |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2924 | /* First, check if we exceed the qgroup limit */ |
| 2925 | INIT_LIST_HEAD(&reserve_list); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2926 | while (1) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 2927 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset, |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2928 | alloc_end - cur_offset, 0); |
Dan Carpenter | 9986277 | 2017-04-11 11:57:15 +0300 | [diff] [blame] | 2929 | if (IS_ERR(em)) { |
| 2930 | ret = PTR_ERR(em); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2931 | break; |
| 2932 | } |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2933 | last_byte = min(extent_map_end(em), alloc_end); |
Josef Bacik | f1e490a | 2011-08-18 10:36:39 -0400 | [diff] [blame] | 2934 | actual_end = min_t(u64, extent_map_end(em), offset + len); |
Miao Xie | 797f427 | 2012-11-28 10:28:07 +0000 | [diff] [blame] | 2935 | last_byte = ALIGN(last_byte, blocksize); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2936 | if (em->block_start == EXTENT_MAP_HOLE || |
| 2937 | (cur_offset >= inode->i_size && |
| 2938 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2939 | ret = add_falloc_range(&reserve_list, cur_offset, |
| 2940 | last_byte - cur_offset); |
| 2941 | if (ret < 0) { |
| 2942 | free_extent_map(em); |
| 2943 | break; |
Filipe Manana | 3d850dd | 2015-03-12 23:23:13 +0000 | [diff] [blame] | 2944 | } |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 2945 | ret = btrfs_qgroup_reserve_data(inode, &data_reserved, |
| 2946 | cur_offset, last_byte - cur_offset); |
Filipe Manana | be2d253 | 2017-04-03 15:57:17 +0100 | [diff] [blame] | 2947 | if (ret < 0) { |
| 2948 | free_extent_map(em); |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2949 | break; |
Filipe Manana | be2d253 | 2017-04-03 15:57:17 +0100 | [diff] [blame] | 2950 | } |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 2951 | } else { |
| 2952 | /* |
| 2953 | * Do not need to reserve unwritten extent for this |
| 2954 | * range, free reserved data space first, otherwise |
| 2955 | * it'll result in false ENOSPC error. |
| 2956 | */ |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 2957 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 2958 | cur_offset, last_byte - cur_offset); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2959 | } |
| 2960 | free_extent_map(em); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2961 | cur_offset = last_byte; |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2962 | if (cur_offset >= alloc_end) |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2963 | break; |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * If ret is still 0, means we're OK to fallocate. |
| 2968 | * Or just cleanup the list and exit. |
| 2969 | */ |
| 2970 | list_for_each_entry_safe(range, tmp, &reserve_list, list) { |
| 2971 | if (!ret) |
| 2972 | ret = btrfs_prealloc_file_range(inode, mode, |
| 2973 | range->start, |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 2974 | range->len, i_blocksize(inode), |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2975 | offset + len, &alloc_hint); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 2976 | else |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 2977 | btrfs_free_reserved_data_space(inode, |
| 2978 | data_reserved, range->start, |
| 2979 | range->len); |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 2980 | list_del(&range->list); |
| 2981 | kfree(range); |
| 2982 | } |
| 2983 | if (ret < 0) |
| 2984 | goto out_unlock; |
| 2985 | |
| 2986 | if (actual_end > inode->i_size && |
| 2987 | !(mode & FALLOC_FL_KEEP_SIZE)) { |
| 2988 | struct btrfs_trans_handle *trans; |
| 2989 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2990 | |
| 2991 | /* |
| 2992 | * We didn't need to allocate any more space, but we |
| 2993 | * still extended the size of the file so we need to |
| 2994 | * update i_size and the inode item. |
| 2995 | */ |
| 2996 | trans = btrfs_start_transaction(root, 1); |
| 2997 | if (IS_ERR(trans)) { |
| 2998 | ret = PTR_ERR(trans); |
| 2999 | } else { |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3000 | inode->i_ctime = current_time(inode); |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 3001 | i_size_write(inode, actual_end); |
| 3002 | btrfs_ordered_update_i_size(inode, actual_end, NULL); |
| 3003 | ret = btrfs_update_inode(trans, root, inode); |
| 3004 | if (ret) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3005 | btrfs_end_transaction(trans); |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 3006 | else |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3007 | ret = btrfs_end_transaction(trans); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3008 | } |
| 3009 | } |
Qu Wenruo | 14524a8 | 2015-09-08 17:22:44 +0800 | [diff] [blame] | 3010 | out_unlock: |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3011 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
David Sterba | 32fc932 | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 3012 | &cached_state, GFP_KERNEL); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3013 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3014 | inode_unlock(inode); |
Chris Mason | d98456f | 2012-01-31 20:27:41 -0500 | [diff] [blame] | 3015 | /* Let go of our reservation. */ |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 3016 | if (ret != 0) |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3017 | btrfs_free_reserved_data_space(inode, data_reserved, |
| 3018 | alloc_start, alloc_end - cur_offset); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3019 | extent_changeset_free(data_reserved); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3020 | return ret; |
| 3021 | } |
| 3022 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 3023 | static int find_desired_extent(struct inode *inode, loff_t *offset, int whence) |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3024 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3025 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 7f4ca37 | 2013-10-18 11:44:46 -0400 | [diff] [blame] | 3026 | struct extent_map *em = NULL; |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3027 | struct extent_state *cached_state = NULL; |
Liu Bo | 4d1a40c | 2014-09-16 17:49:30 +0800 | [diff] [blame] | 3028 | u64 lockstart; |
| 3029 | u64 lockend; |
| 3030 | u64 start; |
| 3031 | u64 len; |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3032 | int ret = 0; |
| 3033 | |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3034 | if (inode->i_size == 0) |
| 3035 | return -ENXIO; |
| 3036 | |
Liu Bo | 4d1a40c | 2014-09-16 17:49:30 +0800 | [diff] [blame] | 3037 | /* |
| 3038 | * *offset can be negative, in this case we start finding DATA/HOLE from |
| 3039 | * the very start of the file. |
| 3040 | */ |
| 3041 | start = max_t(loff_t, 0, *offset); |
| 3042 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3043 | lockstart = round_down(start, fs_info->sectorsize); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 3044 | lockend = round_up(i_size_read(inode), |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3045 | fs_info->sectorsize); |
Liu Bo | 4d1a40c | 2014-09-16 17:49:30 +0800 | [diff] [blame] | 3046 | if (lockend <= lockstart) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3047 | lockend = lockstart + fs_info->sectorsize; |
Liu Bo | 4d1a40c | 2014-09-16 17:49:30 +0800 | [diff] [blame] | 3048 | lockend--; |
| 3049 | len = lockend - lockstart + 1; |
| 3050 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 3051 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 3052 | &cached_state); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3053 | |
Josef Bacik | 7f4ca37 | 2013-10-18 11:44:46 -0400 | [diff] [blame] | 3054 | while (start < inode->i_size) { |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 3055 | em = btrfs_get_extent_fiemap(BTRFS_I(inode), NULL, 0, |
| 3056 | start, len, 0); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3057 | if (IS_ERR(em)) { |
Jeff Liu | 6af021d | 2012-02-09 14:25:50 +0800 | [diff] [blame] | 3058 | ret = PTR_ERR(em); |
Josef Bacik | 7f4ca37 | 2013-10-18 11:44:46 -0400 | [diff] [blame] | 3059 | em = NULL; |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3060 | break; |
| 3061 | } |
| 3062 | |
Josef Bacik | 7f4ca37 | 2013-10-18 11:44:46 -0400 | [diff] [blame] | 3063 | if (whence == SEEK_HOLE && |
| 3064 | (em->block_start == EXTENT_MAP_HOLE || |
| 3065 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) |
| 3066 | break; |
| 3067 | else if (whence == SEEK_DATA && |
| 3068 | (em->block_start != EXTENT_MAP_HOLE && |
| 3069 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) |
| 3070 | break; |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3071 | |
| 3072 | start = em->start + em->len; |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3073 | free_extent_map(em); |
Josef Bacik | 7f4ca37 | 2013-10-18 11:44:46 -0400 | [diff] [blame] | 3074 | em = NULL; |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3075 | cond_resched(); |
| 3076 | } |
Josef Bacik | 7f4ca37 | 2013-10-18 11:44:46 -0400 | [diff] [blame] | 3077 | free_extent_map(em); |
| 3078 | if (!ret) { |
| 3079 | if (whence == SEEK_DATA && start >= inode->i_size) |
| 3080 | ret = -ENXIO; |
| 3081 | else |
| 3082 | *offset = min_t(loff_t, start, inode->i_size); |
| 3083 | } |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3084 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 3085 | &cached_state, GFP_NOFS); |
| 3086 | return ret; |
| 3087 | } |
| 3088 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 3089 | static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence) |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3090 | { |
| 3091 | struct inode *inode = file->f_mapping->host; |
| 3092 | int ret; |
| 3093 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3094 | inode_lock(inode); |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 3095 | switch (whence) { |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3096 | case SEEK_END: |
| 3097 | case SEEK_CUR: |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 3098 | offset = generic_file_llseek(file, offset, whence); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3099 | goto out; |
| 3100 | case SEEK_DATA: |
| 3101 | case SEEK_HOLE: |
Jeff Liu | 48802c8 | 2011-09-18 10:34:02 -0400 | [diff] [blame] | 3102 | if (offset >= i_size_read(inode)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3103 | inode_unlock(inode); |
Jeff Liu | 48802c8 | 2011-09-18 10:34:02 -0400 | [diff] [blame] | 3104 | return -ENXIO; |
| 3105 | } |
| 3106 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 3107 | ret = find_desired_extent(inode, &offset, whence); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3108 | if (ret) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3109 | inode_unlock(inode); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3110 | return ret; |
| 3111 | } |
| 3112 | } |
| 3113 | |
Jie Liu | 46a1c2c | 2013-06-25 12:02:13 +0800 | [diff] [blame] | 3114 | offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3115 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3116 | inode_unlock(inode); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3117 | return offset; |
| 3118 | } |
| 3119 | |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 3120 | static int btrfs_file_open(struct inode *inode, struct file *filp) |
| 3121 | { |
Christoph Hellwig | 91f9943 | 2017-08-29 16:13:20 +0200 | [diff] [blame] | 3122 | filp->f_mode |= FMODE_NOWAIT; |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 3123 | return generic_file_open(inode, filp); |
| 3124 | } |
| 3125 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 3126 | const struct file_operations btrfs_file_operations = { |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 3127 | .llseek = btrfs_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 3128 | .read_iter = generic_file_read_iter, |
Chris Mason | e9906a9 | 2007-12-14 12:56:58 -0500 | [diff] [blame] | 3129 | .splice_read = generic_file_splice_read, |
Al Viro | b30ac0f | 2014-04-03 14:29:04 -0400 | [diff] [blame] | 3130 | .write_iter = btrfs_file_write_iter, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 3131 | .mmap = btrfs_file_mmap, |
Goldwyn Rodrigues | edf064e | 2017-06-20 07:05:49 -0500 | [diff] [blame] | 3132 | .open = btrfs_file_open, |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 3133 | .release = btrfs_release_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3134 | .fsync = btrfs_sync_file, |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3135 | .fallocate = btrfs_fallocate, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 3136 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3137 | #ifdef CONFIG_COMPAT |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 3138 | .compat_ioctl = btrfs_compat_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3139 | #endif |
Christoph Hellwig | 04b38d6 | 2015-12-03 12:59:50 +0100 | [diff] [blame] | 3140 | .clone_file_range = btrfs_clone_file_range, |
Darrick J. Wong | 2b3909f | 2015-12-19 00:56:05 -0800 | [diff] [blame] | 3141 | .dedupe_file_range = btrfs_dedupe_file_range, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 3142 | }; |
Miao Xie | 9247f31 | 2012-11-26 09:24:43 +0000 | [diff] [blame] | 3143 | |
| 3144 | void btrfs_auto_defrag_exit(void) |
| 3145 | { |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 3146 | kmem_cache_destroy(btrfs_inode_defrag_cachep); |
Miao Xie | 9247f31 | 2012-11-26 09:24:43 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | int btrfs_auto_defrag_init(void) |
| 3150 | { |
| 3151 | btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag", |
| 3152 | sizeof(struct inode_defrag), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 3153 | SLAB_MEM_SPREAD, |
Miao Xie | 9247f31 | 2012-11-26 09:24:43 +0000 | [diff] [blame] | 3154 | NULL); |
| 3155 | if (!btrfs_inode_defrag_cachep) |
| 3156 | return -ENOMEM; |
| 3157 | |
| 3158 | return 0; |
| 3159 | } |
Filipe Manana | 728404d | 2014-10-10 09:43:11 +0100 | [diff] [blame] | 3160 | |
| 3161 | int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end) |
| 3162 | { |
| 3163 | int ret; |
| 3164 | |
| 3165 | /* |
| 3166 | * So with compression we will find and lock a dirty page and clear the |
| 3167 | * first one as dirty, setup an async extent, and immediately return |
| 3168 | * with the entire range locked but with nobody actually marked with |
| 3169 | * writeback. So we can't just filemap_write_and_wait_range() and |
| 3170 | * expect it to work since it will just kick off a thread to do the |
| 3171 | * actual work. So we need to call filemap_fdatawrite_range _again_ |
| 3172 | * since it will wait on the page lock, which won't be unlocked until |
| 3173 | * after the pages have been marked as writeback and so we're good to go |
| 3174 | * from there. We have to do this otherwise we'll miss the ordered |
| 3175 | * extents and that results in badness. Please Josef, do not think you |
| 3176 | * know better and pull this out at some point in the future, it is |
| 3177 | * right and you are wrong. |
| 3178 | */ |
| 3179 | ret = filemap_fdatawrite_range(inode->i_mapping, start, end); |
| 3180 | if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 3181 | &BTRFS_I(inode)->runtime_flags)) |
| 3182 | ret = filemap_fdatawrite_range(inode->i_mapping, start, end); |
| 3183 | |
| 3184 | return ret; |
| 3185 | } |