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> |
| 30 | #include <linux/statfs.h> |
| 31 | #include <linux/compat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include "ctree.h" |
| 34 | #include "disk-io.h" |
| 35 | #include "transaction.h" |
| 36 | #include "btrfs_inode.h" |
| 37 | #include "ioctl.h" |
| 38 | #include "print-tree.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 39 | #include "tree-log.h" |
| 40 | #include "locking.h" |
Jeff Mahoney | 12fa8ec | 2008-05-02 15:03:58 -0400 | [diff] [blame] | 41 | #include "compat.h" |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 42 | #include "volumes.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 43 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 44 | /* |
| 45 | * when auto defrag is enabled we |
| 46 | * queue up these defrag structs to remember which |
| 47 | * inodes need defragging passes |
| 48 | */ |
| 49 | struct inode_defrag { |
| 50 | struct rb_node rb_node; |
| 51 | /* objectid */ |
| 52 | u64 ino; |
| 53 | /* |
| 54 | * transid where the defrag was added, we search for |
| 55 | * extents newer than this |
| 56 | */ |
| 57 | u64 transid; |
| 58 | |
| 59 | /* root objectid */ |
| 60 | u64 root; |
| 61 | |
| 62 | /* last offset we were able to defrag */ |
| 63 | u64 last_offset; |
| 64 | |
| 65 | /* if we've wrapped around back to zero once already */ |
| 66 | int cycled; |
| 67 | }; |
| 68 | |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 69 | static int __compare_inode_defrag(struct inode_defrag *defrag1, |
| 70 | struct inode_defrag *defrag2) |
| 71 | { |
| 72 | if (defrag1->root > defrag2->root) |
| 73 | return 1; |
| 74 | else if (defrag1->root < defrag2->root) |
| 75 | return -1; |
| 76 | else if (defrag1->ino > defrag2->ino) |
| 77 | return 1; |
| 78 | else if (defrag1->ino < defrag2->ino) |
| 79 | return -1; |
| 80 | else |
| 81 | return 0; |
| 82 | } |
| 83 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 84 | /* pop a record for an inode into the defrag tree. The lock |
| 85 | * must be held already |
| 86 | * |
| 87 | * If you're inserting a record for an older transid than an |
| 88 | * existing record, the transid already in the tree is lowered |
| 89 | * |
| 90 | * If an existing record is found the defrag item you |
| 91 | * pass in is freed |
| 92 | */ |
Wanlong Gao | a0f98dd | 2011-07-18 12:19:35 +0000 | [diff] [blame] | 93 | static void __btrfs_add_inode_defrag(struct inode *inode, |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 94 | struct inode_defrag *defrag) |
| 95 | { |
| 96 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 97 | struct inode_defrag *entry; |
| 98 | struct rb_node **p; |
| 99 | struct rb_node *parent = NULL; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 100 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 101 | |
| 102 | p = &root->fs_info->defrag_inodes.rb_node; |
| 103 | while (*p) { |
| 104 | parent = *p; |
| 105 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
| 106 | |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 107 | ret = __compare_inode_defrag(defrag, entry); |
| 108 | if (ret < 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 109 | p = &parent->rb_left; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 110 | else if (ret > 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 111 | p = &parent->rb_right; |
| 112 | else { |
| 113 | /* if we're reinserting an entry for |
| 114 | * an old defrag run, make sure to |
| 115 | * lower the transid of our existing record |
| 116 | */ |
| 117 | if (defrag->transid < entry->transid) |
| 118 | entry->transid = defrag->transid; |
| 119 | if (defrag->last_offset > entry->last_offset) |
| 120 | entry->last_offset = defrag->last_offset; |
| 121 | goto exists; |
| 122 | } |
| 123 | } |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 124 | set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 125 | rb_link_node(&defrag->rb_node, parent, p); |
| 126 | rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes); |
Wanlong Gao | a0f98dd | 2011-07-18 12:19:35 +0000 | [diff] [blame] | 127 | return; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 128 | |
| 129 | exists: |
| 130 | kfree(defrag); |
Wanlong Gao | a0f98dd | 2011-07-18 12:19:35 +0000 | [diff] [blame] | 131 | return; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 132 | |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * insert a defrag record for this inode if auto defrag is |
| 137 | * enabled |
| 138 | */ |
| 139 | int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans, |
| 140 | struct inode *inode) |
| 141 | { |
| 142 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 143 | struct inode_defrag *defrag; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 144 | u64 transid; |
| 145 | |
| 146 | if (!btrfs_test_opt(root, AUTO_DEFRAG)) |
| 147 | return 0; |
| 148 | |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 149 | if (btrfs_fs_closing(root->fs_info)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 150 | return 0; |
| 151 | |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 152 | if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 153 | return 0; |
| 154 | |
| 155 | if (trans) |
| 156 | transid = trans->transid; |
| 157 | else |
| 158 | transid = BTRFS_I(inode)->root->last_trans; |
| 159 | |
| 160 | defrag = kzalloc(sizeof(*defrag), GFP_NOFS); |
| 161 | if (!defrag) |
| 162 | return -ENOMEM; |
| 163 | |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 164 | defrag->ino = btrfs_ino(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 165 | defrag->transid = transid; |
| 166 | defrag->root = root->root_key.objectid; |
| 167 | |
| 168 | spin_lock(&root->fs_info->defrag_inodes_lock); |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 169 | if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) |
Wanlong Gao | a0f98dd | 2011-07-18 12:19:35 +0000 | [diff] [blame] | 170 | __btrfs_add_inode_defrag(inode, defrag); |
Dan Carpenter | f4ac904 | 2011-08-05 14:19:00 +0000 | [diff] [blame] | 171 | else |
| 172 | kfree(defrag); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 173 | spin_unlock(&root->fs_info->defrag_inodes_lock); |
Wanlong Gao | a0f98dd | 2011-07-18 12:19:35 +0000 | [diff] [blame] | 174 | return 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * must be called with the defrag_inodes lock held |
| 179 | */ |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 180 | struct inode_defrag *btrfs_find_defrag_inode(struct btrfs_fs_info *info, |
| 181 | u64 root, u64 ino, |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 182 | struct rb_node **next) |
| 183 | { |
| 184 | struct inode_defrag *entry = NULL; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 185 | struct inode_defrag tmp; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 186 | struct rb_node *p; |
| 187 | struct rb_node *parent = NULL; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | tmp.ino = ino; |
| 191 | tmp.root = root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 192 | |
| 193 | p = info->defrag_inodes.rb_node; |
| 194 | while (p) { |
| 195 | parent = p; |
| 196 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
| 197 | |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 198 | ret = __compare_inode_defrag(&tmp, entry); |
| 199 | if (ret < 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 200 | p = parent->rb_left; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 201 | else if (ret > 0) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 202 | p = parent->rb_right; |
| 203 | else |
| 204 | return entry; |
| 205 | } |
| 206 | |
| 207 | if (next) { |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 208 | while (parent && __compare_inode_defrag(&tmp, entry) > 0) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 209 | parent = rb_next(parent); |
| 210 | entry = rb_entry(parent, struct inode_defrag, rb_node); |
| 211 | } |
| 212 | *next = parent; |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * run through the list of inodes in the FS that need |
| 219 | * defragging |
| 220 | */ |
| 221 | int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info) |
| 222 | { |
| 223 | struct inode_defrag *defrag; |
| 224 | struct btrfs_root *inode_root; |
| 225 | struct inode *inode; |
| 226 | struct rb_node *n; |
| 227 | struct btrfs_key key; |
| 228 | struct btrfs_ioctl_defrag_range_args range; |
| 229 | u64 first_ino = 0; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 230 | u64 root_objectid = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 231 | int num_defrag; |
| 232 | int defrag_batch = 1024; |
| 233 | |
| 234 | memset(&range, 0, sizeof(range)); |
| 235 | range.len = (u64)-1; |
| 236 | |
| 237 | atomic_inc(&fs_info->defrag_running); |
| 238 | spin_lock(&fs_info->defrag_inodes_lock); |
| 239 | while(1) { |
| 240 | n = NULL; |
| 241 | |
| 242 | /* find an inode to defrag */ |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 243 | defrag = btrfs_find_defrag_inode(fs_info, root_objectid, |
| 244 | first_ino, &n); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 245 | if (!defrag) { |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 246 | if (n) { |
| 247 | defrag = rb_entry(n, struct inode_defrag, |
| 248 | rb_node); |
| 249 | } else if (root_objectid || first_ino) { |
| 250 | root_objectid = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 251 | first_ino = 0; |
| 252 | continue; |
| 253 | } else { |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* remove it from the rbtree */ |
| 259 | first_ino = defrag->ino + 1; |
Miao Xie | 762f226 | 2012-05-24 18:58:27 +0800 | [diff] [blame] | 260 | root_objectid = defrag->root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 261 | rb_erase(&defrag->rb_node, &fs_info->defrag_inodes); |
| 262 | |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 263 | if (btrfs_fs_closing(fs_info)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 264 | goto next_free; |
| 265 | |
| 266 | spin_unlock(&fs_info->defrag_inodes_lock); |
| 267 | |
| 268 | /* get the inode */ |
| 269 | key.objectid = defrag->root; |
| 270 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 271 | key.offset = (u64)-1; |
| 272 | inode_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 273 | if (IS_ERR(inode_root)) |
| 274 | goto next; |
| 275 | |
| 276 | key.objectid = defrag->ino; |
| 277 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 278 | key.offset = 0; |
| 279 | |
| 280 | inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL); |
| 281 | if (IS_ERR(inode)) |
| 282 | goto next; |
| 283 | |
| 284 | /* do a chunk of defrag */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 285 | clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 286 | range.start = defrag->last_offset; |
| 287 | num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid, |
| 288 | defrag_batch); |
| 289 | /* |
| 290 | * if we filled the whole defrag batch, there |
| 291 | * must be more work to do. Queue this defrag |
| 292 | * again |
| 293 | */ |
| 294 | if (num_defrag == defrag_batch) { |
| 295 | defrag->last_offset = range.start; |
| 296 | __btrfs_add_inode_defrag(inode, defrag); |
| 297 | /* |
| 298 | * we don't want to kfree defrag, we added it back to |
| 299 | * the rbtree |
| 300 | */ |
| 301 | defrag = NULL; |
| 302 | } else if (defrag->last_offset && !defrag->cycled) { |
| 303 | /* |
| 304 | * we didn't fill our defrag batch, but |
| 305 | * we didn't start at zero. Make sure we loop |
| 306 | * around to the start of the file. |
| 307 | */ |
| 308 | defrag->last_offset = 0; |
| 309 | defrag->cycled = 1; |
| 310 | __btrfs_add_inode_defrag(inode, defrag); |
| 311 | defrag = NULL; |
| 312 | } |
| 313 | |
| 314 | iput(inode); |
| 315 | next: |
| 316 | spin_lock(&fs_info->defrag_inodes_lock); |
| 317 | next_free: |
| 318 | kfree(defrag); |
| 319 | } |
| 320 | spin_unlock(&fs_info->defrag_inodes_lock); |
| 321 | |
| 322 | atomic_dec(&fs_info->defrag_running); |
| 323 | |
| 324 | /* |
| 325 | * during unmount, we use the transaction_wait queue to |
| 326 | * wait for the defragger to stop |
| 327 | */ |
| 328 | wake_up(&fs_info->transaction_wait); |
| 329 | return 0; |
| 330 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 331 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 332 | /* simple helper to fault in pages and copy. This should go away |
| 333 | * and be replaced with calls into generic code. |
| 334 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 335 | static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 336 | size_t write_bytes, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 337 | struct page **prepared_pages, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 338 | struct iov_iter *i) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 339 | { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 340 | size_t copied = 0; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 341 | size_t total_copied = 0; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 342 | int pg = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 343 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 344 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 345 | while (write_bytes > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 346 | size_t count = min_t(size_t, |
| 347 | PAGE_CACHE_SIZE - offset, write_bytes); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 348 | struct page *page = prepared_pages[pg]; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 349 | /* |
| 350 | * Copy data from userspace to the current page |
| 351 | * |
| 352 | * Disable pagefault to avoid recursive lock since |
| 353 | * the pages are already locked |
| 354 | */ |
| 355 | pagefault_disable(); |
| 356 | copied = iov_iter_copy_from_user_atomic(page, i, offset, count); |
| 357 | pagefault_enable(); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 358 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 359 | /* Flush processor's dcache for this page */ |
| 360 | flush_dcache_page(page); |
Chris Mason | 31339ac | 2011-03-07 11:10:24 -0500 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * if we get a partial write, we can end up with |
| 364 | * partially up to date pages. These add |
| 365 | * a lot of complexity, so make sure they don't |
| 366 | * happen by forcing this copy to be retried. |
| 367 | * |
| 368 | * The rest of the btrfs_file_write code will fall |
| 369 | * back to page at a time copies after we return 0. |
| 370 | */ |
| 371 | if (!PageUptodate(page) && copied < count) |
| 372 | copied = 0; |
| 373 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 374 | iov_iter_advance(i, copied); |
| 375 | write_bytes -= copied; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 376 | total_copied += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 377 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 378 | /* Return to btrfs_file_aio_write to fault page */ |
Josef Bacik | 9f570b8 | 2011-01-25 12:42:37 -0500 | [diff] [blame] | 379 | if (unlikely(copied == 0)) |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 380 | break; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 381 | |
| 382 | if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { |
| 383 | offset += copied; |
| 384 | } else { |
| 385 | pg++; |
| 386 | offset = 0; |
| 387 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 388 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 389 | return total_copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 392 | /* |
| 393 | * unlocks pages after btrfs_file_write is done with them |
| 394 | */ |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 395 | void btrfs_drop_pages(struct page **pages, size_t num_pages) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 396 | { |
| 397 | size_t i; |
| 398 | for (i = 0; i < num_pages; i++) { |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 399 | /* page checked is some magic around finding pages that |
| 400 | * have been modified without going through btrfs_set_page_dirty |
| 401 | * clear it here |
| 402 | */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 403 | ClearPageChecked(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 404 | unlock_page(pages[i]); |
| 405 | mark_page_accessed(pages[i]); |
| 406 | page_cache_release(pages[i]); |
| 407 | } |
| 408 | } |
| 409 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 410 | /* |
| 411 | * after copy_from_user, pages need to be dirtied and we need to make |
| 412 | * sure holes are created between the current EOF and the start of |
| 413 | * any next extents (if required). |
| 414 | * |
| 415 | * this also makes the decision about creating an inline extent vs |
| 416 | * doing real data extents, marking pages dirty and delalloc as required. |
| 417 | */ |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 418 | int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode, |
| 419 | struct page **pages, size_t num_pages, |
| 420 | loff_t pos, size_t write_bytes, |
| 421 | struct extent_state **cached) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 422 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 423 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 424 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 425 | u64 num_bytes; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 426 | u64 start_pos; |
| 427 | u64 end_of_last_block; |
| 428 | u64 end_pos = pos + write_bytes; |
| 429 | loff_t isize = i_size_read(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 430 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 431 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 432 | num_bytes = (write_bytes + pos - start_pos + |
| 433 | root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 434 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 435 | end_of_last_block = start_pos + num_bytes - 1; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 436 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 437 | cached); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 438 | if (err) |
| 439 | return err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 440 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 441 | for (i = 0; i < num_pages; i++) { |
| 442 | struct page *p = pages[i]; |
| 443 | SetPageUptodate(p); |
| 444 | ClearPageChecked(p); |
| 445 | set_page_dirty(p); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 446 | } |
Josef Bacik | 9f570b8 | 2011-01-25 12:42:37 -0500 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * we've only changed i_size in ram, and we haven't updated |
| 450 | * the disk i_size. There is no need to log the inode |
| 451 | * at this time. |
| 452 | */ |
| 453 | if (end_pos > isize) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 454 | i_size_write(inode, end_pos); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 455 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 458 | /* |
| 459 | * this drops all the extents in the cache that intersect the range |
| 460 | * [start, end]. Existing extents are split as required. |
| 461 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 462 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end, |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 463 | int skip_pinned) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 464 | { |
| 465 | struct extent_map *em; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 466 | struct extent_map *split = NULL; |
| 467 | struct extent_map *split2 = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 468 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 469 | u64 len = end - start + 1; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 470 | u64 gen; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 471 | int ret; |
| 472 | int testend = 1; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 473 | unsigned long flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 474 | int compressed = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 475 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 476 | WARN_ON(end < start); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 477 | if (end == (u64)-1) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 478 | len = (u64)-1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 479 | testend = 0; |
| 480 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 481 | while (1) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 482 | if (!split) |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 483 | split = alloc_extent_map(); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 484 | if (!split2) |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 485 | split2 = alloc_extent_map(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 486 | BUG_ON(!split || !split2); /* -ENOMEM */ |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 487 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 488 | write_lock(&em_tree->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 489 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 490 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 491 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 492 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 493 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 494 | flags = em->flags; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 495 | gen = em->generation; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 496 | if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) { |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 497 | if (testend && em->start + em->len >= start + len) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 498 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 499 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 500 | break; |
| 501 | } |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 502 | start = em->start + em->len; |
| 503 | if (testend) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 504 | len = start + len - (em->start + em->len); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 505 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 506 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 507 | continue; |
| 508 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 509 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | 3ce7e67 | 2008-07-31 15:42:54 -0400 | [diff] [blame] | 510 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 511 | remove_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 512 | |
| 513 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 514 | em->start < start) { |
| 515 | split->start = em->start; |
| 516 | split->len = start - em->start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 517 | split->orig_start = em->orig_start; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 518 | split->block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 519 | |
| 520 | if (compressed) |
| 521 | split->block_len = em->block_len; |
| 522 | else |
| 523 | split->block_len = split->len; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 524 | split->generation = gen; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 525 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 526 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 527 | split->compress_type = em->compress_type; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 528 | ret = add_extent_mapping(em_tree, split); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 529 | BUG_ON(ret); /* Logic error */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 530 | list_move(&split->list, &em_tree->modified_extents); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 531 | free_extent_map(split); |
| 532 | split = split2; |
| 533 | split2 = NULL; |
| 534 | } |
| 535 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 536 | testend && em->start + em->len > start + len) { |
| 537 | u64 diff = start + len - em->start; |
| 538 | |
| 539 | split->start = start + len; |
| 540 | split->len = em->start + em->len - (start + len); |
| 541 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 542 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 543 | split->compress_type = em->compress_type; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 544 | split->generation = gen; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 545 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 546 | if (compressed) { |
| 547 | split->block_len = em->block_len; |
| 548 | split->block_start = em->block_start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 549 | split->orig_start = em->orig_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 550 | } else { |
| 551 | split->block_len = split->len; |
| 552 | split->block_start = em->block_start + diff; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 553 | split->orig_start = split->start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 554 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 555 | |
| 556 | ret = add_extent_mapping(em_tree, split); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 557 | BUG_ON(ret); /* Logic error */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 558 | list_move(&split->list, &em_tree->modified_extents); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 559 | free_extent_map(split); |
| 560 | split = NULL; |
| 561 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 562 | write_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 563 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 564 | /* once for us */ |
| 565 | free_extent_map(em); |
| 566 | /* once for the tree*/ |
| 567 | free_extent_map(em); |
| 568 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 569 | if (split) |
| 570 | free_extent_map(split); |
| 571 | if (split2) |
| 572 | free_extent_map(split2); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 576 | /* |
| 577 | * this is very complex, but the basic idea is to drop all extents |
| 578 | * in the range start - end. hint_block is filled in with a block number |
| 579 | * that would be a good hint to the block allocator for this file. |
| 580 | * |
| 581 | * If an extent intersects the range but is not entirely inside the range |
| 582 | * it is either truncated or split. Anything entirely inside the range |
| 583 | * is deleted from the tree. |
| 584 | */ |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 585 | int __btrfs_drop_extents(struct btrfs_trans_handle *trans, |
| 586 | struct btrfs_root *root, struct inode *inode, |
| 587 | struct btrfs_path *path, u64 start, u64 end, |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 588 | u64 *drop_end, int drop_cache) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 589 | { |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 590 | struct extent_buffer *leaf; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 591 | struct btrfs_file_extent_item *fi; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 592 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 593 | struct btrfs_key new_key; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 594 | u64 ino = btrfs_ino(inode); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 595 | u64 search_start = start; |
| 596 | u64 disk_bytenr = 0; |
| 597 | u64 num_bytes = 0; |
| 598 | u64 extent_offset = 0; |
| 599 | u64 extent_end = 0; |
| 600 | int del_nr = 0; |
| 601 | int del_slot = 0; |
| 602 | int extent_type; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 603 | int recow; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 604 | int ret; |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 605 | int modify_tree = -1; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 606 | int update_refs = (root->ref_cows || root == root->fs_info->tree_root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 607 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 608 | if (drop_cache) |
| 609 | btrfs_drop_extent_cache(inode, start, end - 1, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 610 | |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 611 | if (start >= BTRFS_I(inode)->disk_i_size) |
| 612 | modify_tree = 0; |
| 613 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 614 | while (1) { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 615 | recow = 0; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 616 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 617 | search_start, modify_tree); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 618 | if (ret < 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 619 | break; |
| 620 | if (ret > 0 && path->slots[0] > 0 && search_start == start) { |
| 621 | leaf = path->nodes[0]; |
| 622 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 623 | if (key.objectid == ino && |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 624 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 625 | path->slots[0]--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 626 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 627 | ret = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 628 | next_slot: |
| 629 | leaf = path->nodes[0]; |
| 630 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 631 | BUG_ON(del_nr > 0); |
| 632 | ret = btrfs_next_leaf(root, path); |
| 633 | if (ret < 0) |
| 634 | break; |
| 635 | if (ret > 0) { |
| 636 | ret = 0; |
| 637 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 638 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 639 | leaf = path->nodes[0]; |
| 640 | recow = 1; |
| 641 | } |
| 642 | |
| 643 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 644 | if (key.objectid > ino || |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 645 | key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end) |
| 646 | break; |
| 647 | |
| 648 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 649 | struct btrfs_file_extent_item); |
| 650 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 651 | |
| 652 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 653 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 654 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 655 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 656 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
| 657 | extent_end = key.offset + |
| 658 | btrfs_file_extent_num_bytes(leaf, fi); |
| 659 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 660 | extent_end = key.offset + |
| 661 | btrfs_file_extent_inline_len(leaf, fi); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 662 | } else { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 663 | WARN_ON(1); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 664 | extent_end = search_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 667 | if (extent_end <= search_start) { |
| 668 | path->slots[0]++; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 669 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 670 | } |
| 671 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 672 | search_start = max(key.offset, start); |
Chris Mason | dc7fdde | 2012-04-27 14:31:29 -0400 | [diff] [blame] | 673 | if (recow || !modify_tree) { |
| 674 | modify_tree = -1; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 675 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 676 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 677 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 678 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 679 | /* |
| 680 | * | - range to drop - | |
| 681 | * | -------- extent -------- | |
| 682 | */ |
| 683 | if (start > key.offset && end < extent_end) { |
| 684 | BUG_ON(del_nr > 0); |
| 685 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 686 | |
| 687 | memcpy(&new_key, &key, sizeof(new_key)); |
| 688 | new_key.offset = start; |
| 689 | ret = btrfs_duplicate_item(trans, root, path, |
| 690 | &new_key); |
| 691 | if (ret == -EAGAIN) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 692 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 693 | continue; |
| 694 | } |
| 695 | if (ret < 0) |
| 696 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 697 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 698 | leaf = path->nodes[0]; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 699 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 700 | struct btrfs_file_extent_item); |
| 701 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 702 | start - key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 703 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 704 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 705 | struct btrfs_file_extent_item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 706 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 707 | extent_offset += start - key.offset; |
| 708 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 709 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 710 | extent_end - start); |
| 711 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 712 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 713 | if (update_refs && disk_bytenr > 0) { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 714 | ret = btrfs_inc_extent_ref(trans, root, |
| 715 | disk_bytenr, num_bytes, 0, |
| 716 | root->root_key.objectid, |
| 717 | new_key.objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 718 | start - extent_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 719 | BUG_ON(ret); /* -ENOMEM */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 720 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 721 | key.offset = start; |
| 722 | } |
| 723 | /* |
| 724 | * | ---- range to drop ----- | |
| 725 | * | -------- extent -------- | |
| 726 | */ |
| 727 | if (start <= key.offset && end < extent_end) { |
| 728 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 729 | |
| 730 | memcpy(&new_key, &key, sizeof(new_key)); |
| 731 | new_key.offset = end; |
| 732 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 733 | |
| 734 | extent_offset += end - key.offset; |
| 735 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 736 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 737 | extent_end - end); |
| 738 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 739 | if (update_refs && disk_bytenr > 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 740 | inode_sub_bytes(inode, end - key.offset); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 741 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 744 | search_start = extent_end; |
| 745 | /* |
| 746 | * | ---- range to drop ----- | |
| 747 | * | -------- extent -------- | |
| 748 | */ |
| 749 | if (start > key.offset && end >= extent_end) { |
| 750 | BUG_ON(del_nr > 0); |
| 751 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 752 | |
| 753 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 754 | start - key.offset); |
| 755 | btrfs_mark_buffer_dirty(leaf); |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 756 | if (update_refs && disk_bytenr > 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 757 | inode_sub_bytes(inode, extent_end - start); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 758 | if (end == extent_end) |
| 759 | break; |
| 760 | |
| 761 | path->slots[0]++; |
| 762 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 763 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 764 | |
| 765 | /* |
| 766 | * | ---- range to drop ----- | |
| 767 | * | ------ extent ------ | |
| 768 | */ |
| 769 | if (start <= key.offset && end >= extent_end) { |
| 770 | if (del_nr == 0) { |
| 771 | del_slot = path->slots[0]; |
| 772 | del_nr = 1; |
| 773 | } else { |
| 774 | BUG_ON(del_slot + del_nr != path->slots[0]); |
| 775 | del_nr++; |
| 776 | } |
| 777 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 778 | if (update_refs && |
| 779 | extent_type == BTRFS_FILE_EXTENT_INLINE) { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 780 | inode_sub_bytes(inode, |
| 781 | extent_end - key.offset); |
| 782 | extent_end = ALIGN(extent_end, |
| 783 | root->sectorsize); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 784 | } else if (update_refs && disk_bytenr > 0) { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 785 | ret = btrfs_free_extent(trans, root, |
| 786 | disk_bytenr, num_bytes, 0, |
| 787 | root->root_key.objectid, |
| 788 | key.objectid, key.offset - |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 789 | extent_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 790 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 791 | inode_sub_bytes(inode, |
| 792 | extent_end - key.offset); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | if (end == extent_end) |
| 796 | break; |
| 797 | |
| 798 | if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) { |
| 799 | path->slots[0]++; |
| 800 | goto next_slot; |
| 801 | } |
| 802 | |
| 803 | ret = btrfs_del_items(trans, root, path, del_slot, |
| 804 | del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 805 | if (ret) { |
| 806 | btrfs_abort_transaction(trans, root, ret); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 807 | break; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 808 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 809 | |
| 810 | del_nr = 0; |
| 811 | del_slot = 0; |
| 812 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 813 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 814 | continue; |
| 815 | } |
| 816 | |
| 817 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 818 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 819 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 820 | if (!ret && del_nr > 0) { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 821 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 822 | if (ret) |
| 823 | btrfs_abort_transaction(trans, root, ret); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 826 | if (drop_end) |
| 827 | *drop_end = min(end, extent_end); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 828 | btrfs_release_path(path); |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, |
| 833 | struct btrfs_root *root, struct inode *inode, u64 start, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 834 | u64 end, int drop_cache) |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 835 | { |
| 836 | struct btrfs_path *path; |
| 837 | int ret; |
| 838 | |
| 839 | path = btrfs_alloc_path(); |
| 840 | if (!path) |
| 841 | return -ENOMEM; |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 842 | ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 843 | drop_cache); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 844 | btrfs_free_path(path); |
| 845 | return ret; |
| 846 | } |
| 847 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 848 | static int extent_mergeable(struct extent_buffer *leaf, int slot, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 849 | u64 objectid, u64 bytenr, u64 orig_offset, |
| 850 | u64 *start, u64 *end) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 851 | { |
| 852 | struct btrfs_file_extent_item *fi; |
| 853 | struct btrfs_key key; |
| 854 | u64 extent_end; |
| 855 | |
| 856 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) |
| 857 | return 0; |
| 858 | |
| 859 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 860 | if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY) |
| 861 | return 0; |
| 862 | |
| 863 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 864 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG || |
| 865 | btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr || |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 866 | btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset || |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 867 | btrfs_file_extent_compression(leaf, fi) || |
| 868 | btrfs_file_extent_encryption(leaf, fi) || |
| 869 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 870 | return 0; |
| 871 | |
| 872 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 873 | if ((*start && *start != key.offset) || (*end && *end != extent_end)) |
| 874 | return 0; |
| 875 | |
| 876 | *start = key.offset; |
| 877 | *end = extent_end; |
| 878 | return 1; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Mark extent in the range start - end as written. |
| 883 | * |
| 884 | * This changes extent type from 'pre-allocated' to 'regular'. If only |
| 885 | * part of extent is marked as written, the extent will be split into |
| 886 | * two or three. |
| 887 | */ |
| 888 | int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 889 | struct inode *inode, u64 start, u64 end) |
| 890 | { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 891 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 892 | struct extent_buffer *leaf; |
| 893 | struct btrfs_path *path; |
| 894 | struct btrfs_file_extent_item *fi; |
| 895 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 896 | struct btrfs_key new_key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 897 | u64 bytenr; |
| 898 | u64 num_bytes; |
| 899 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 900 | u64 orig_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 901 | u64 other_start; |
| 902 | u64 other_end; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 903 | u64 split; |
| 904 | int del_nr = 0; |
| 905 | int del_slot = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 906 | int recow; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 907 | int ret; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 908 | u64 ino = btrfs_ino(inode); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 909 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 910 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 911 | if (!path) |
| 912 | return -ENOMEM; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 913 | again: |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 914 | recow = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 915 | split = start; |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 916 | key.objectid = ino; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 917 | key.type = BTRFS_EXTENT_DATA_KEY; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 918 | key.offset = split; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 919 | |
| 920 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Josef Bacik | 4141573 | 2011-03-16 13:59:32 -0400 | [diff] [blame] | 921 | if (ret < 0) |
| 922 | goto out; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 923 | if (ret > 0 && path->slots[0] > 0) |
| 924 | path->slots[0]--; |
| 925 | |
| 926 | leaf = path->nodes[0]; |
| 927 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 928 | BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 929 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 930 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 931 | BUG_ON(btrfs_file_extent_type(leaf, fi) != |
| 932 | BTRFS_FILE_EXTENT_PREALLOC); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 933 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 934 | BUG_ON(key.offset > start || extent_end < end); |
| 935 | |
| 936 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 937 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 938 | orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 939 | memcpy(&new_key, &key, sizeof(new_key)); |
| 940 | |
| 941 | if (start == key.offset && end < extent_end) { |
| 942 | other_start = 0; |
| 943 | other_end = start; |
| 944 | if (extent_mergeable(leaf, path->slots[0] - 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 945 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 946 | &other_start, &other_end)) { |
| 947 | new_key.offset = end; |
| 948 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 949 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 950 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 951 | btrfs_set_file_extent_generation(leaf, fi, |
| 952 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 953 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 954 | extent_end - end); |
| 955 | btrfs_set_file_extent_offset(leaf, fi, |
| 956 | end - orig_offset); |
| 957 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 958 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 959 | btrfs_set_file_extent_generation(leaf, fi, |
| 960 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 961 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 962 | end - other_start); |
| 963 | btrfs_mark_buffer_dirty(leaf); |
| 964 | goto out; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if (start > key.offset && end == extent_end) { |
| 969 | other_start = end; |
| 970 | other_end = 0; |
| 971 | if (extent_mergeable(leaf, path->slots[0] + 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 972 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 973 | &other_start, &other_end)) { |
| 974 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 975 | struct btrfs_file_extent_item); |
| 976 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 977 | start - key.offset); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 978 | btrfs_set_file_extent_generation(leaf, fi, |
| 979 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 980 | path->slots[0]++; |
| 981 | new_key.offset = start; |
| 982 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 983 | |
| 984 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 985 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 986 | btrfs_set_file_extent_generation(leaf, fi, |
| 987 | trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 988 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 989 | other_end - start); |
| 990 | btrfs_set_file_extent_offset(leaf, fi, |
| 991 | start - orig_offset); |
| 992 | btrfs_mark_buffer_dirty(leaf); |
| 993 | goto out; |
| 994 | } |
| 995 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 996 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 997 | while (start > key.offset || end < extent_end) { |
| 998 | if (key.offset == start) |
| 999 | split = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1000 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1001 | new_key.offset = split; |
| 1002 | ret = btrfs_duplicate_item(trans, root, path, &new_key); |
| 1003 | if (ret == -EAGAIN) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1004 | btrfs_release_path(path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1005 | goto again; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1006 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1007 | if (ret < 0) { |
| 1008 | btrfs_abort_transaction(trans, root, ret); |
| 1009 | goto out; |
| 1010 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1011 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1012 | leaf = path->nodes[0]; |
| 1013 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1014 | struct btrfs_file_extent_item); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1015 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1016 | btrfs_set_file_extent_num_bytes(leaf, fi, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1017 | split - key.offset); |
| 1018 | |
| 1019 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1020 | struct btrfs_file_extent_item); |
| 1021 | |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1022 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1023 | btrfs_set_file_extent_offset(leaf, fi, split - orig_offset); |
| 1024 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1025 | extent_end - split); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1026 | btrfs_mark_buffer_dirty(leaf); |
| 1027 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1028 | ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, |
| 1029 | root->root_key.objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1030 | ino, orig_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1031 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1032 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1033 | if (split == start) { |
| 1034 | key.offset = start; |
| 1035 | } else { |
| 1036 | BUG_ON(start != key.offset); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1037 | path->slots[0]--; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1038 | extent_end = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1039 | } |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1040 | recow = 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1041 | } |
| 1042 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1043 | other_start = end; |
| 1044 | other_end = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1045 | if (extent_mergeable(leaf, path->slots[0] + 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1046 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1047 | &other_start, &other_end)) { |
| 1048 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1049 | btrfs_release_path(path); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1050 | goto again; |
| 1051 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1052 | extent_end = other_end; |
| 1053 | del_slot = path->slots[0] + 1; |
| 1054 | del_nr++; |
| 1055 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 1056 | 0, root->root_key.objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1057 | ino, orig_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1058 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1059 | } |
| 1060 | other_start = 0; |
| 1061 | other_end = start; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1062 | if (extent_mergeable(leaf, path->slots[0] - 1, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1063 | ino, bytenr, orig_offset, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1064 | &other_start, &other_end)) { |
| 1065 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1066 | btrfs_release_path(path); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1067 | goto again; |
| 1068 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1069 | key.offset = other_start; |
| 1070 | del_slot = path->slots[0]; |
| 1071 | del_nr++; |
| 1072 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 1073 | 0, root->root_key.objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1074 | ino, orig_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1075 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1076 | } |
| 1077 | if (del_nr == 0) { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 1078 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1079 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1080 | btrfs_set_file_extent_type(leaf, fi, |
| 1081 | BTRFS_FILE_EXTENT_REG); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1082 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1083 | btrfs_mark_buffer_dirty(leaf); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1084 | } else { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 1085 | fi = btrfs_item_ptr(leaf, del_slot - 1, |
| 1086 | struct btrfs_file_extent_item); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1087 | btrfs_set_file_extent_type(leaf, fi, |
| 1088 | BTRFS_FILE_EXTENT_REG); |
Josef Bacik | 224ecce | 2012-08-16 16:32:06 -0400 | [diff] [blame] | 1089 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 1090 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 1091 | extent_end - key.offset); |
| 1092 | btrfs_mark_buffer_dirty(leaf); |
| 1093 | |
| 1094 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1095 | if (ret < 0) { |
| 1096 | btrfs_abort_transaction(trans, root, ret); |
| 1097 | goto out; |
| 1098 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1099 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1100 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 1101 | btrfs_free_path(path); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1105 | /* |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1106 | * on error we return an unlocked page and the error value |
| 1107 | * on success we return a locked page and 0 |
| 1108 | */ |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1109 | static int prepare_uptodate_page(struct page *page, u64 pos, |
| 1110 | bool force_uptodate) |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1111 | { |
| 1112 | int ret = 0; |
| 1113 | |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1114 | if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) && |
| 1115 | !PageUptodate(page)) { |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1116 | ret = btrfs_readpage(NULL, page); |
| 1117 | if (ret) |
| 1118 | return ret; |
| 1119 | lock_page(page); |
| 1120 | if (!PageUptodate(page)) { |
| 1121 | unlock_page(page); |
| 1122 | return -EIO; |
| 1123 | } |
| 1124 | } |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1129 | * this gets pages into the page cache and locks them down, it also properly |
| 1130 | * waits for data=ordered extents to finish before allowing the pages to be |
| 1131 | * modified. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1132 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1133 | static noinline int prepare_pages(struct btrfs_root *root, struct file *file, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1134 | struct page **pages, size_t num_pages, |
| 1135 | loff_t pos, unsigned long first_index, |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1136 | size_t write_bytes, bool force_uptodate) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1137 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1138 | struct extent_state *cached_state = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1139 | int i; |
| 1140 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 1141 | struct inode *inode = fdentry(file)->d_inode; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 1142 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1143 | int err = 0; |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1144 | int faili = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1145 | u64 start_pos; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1146 | u64 last_pos; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1147 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1148 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1149 | last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1150 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1151 | again: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1152 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | a94733d | 2011-07-11 10:47:06 -0400 | [diff] [blame] | 1153 | pages[i] = find_or_create_page(inode->i_mapping, index + i, |
Johannes Weiner | e3a41a5 | 2012-01-10 15:07:55 -0800 | [diff] [blame] | 1154 | mask | __GFP_WRITE); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1155 | if (!pages[i]) { |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1156 | faili = i - 1; |
| 1157 | err = -ENOMEM; |
| 1158 | goto fail; |
| 1159 | } |
| 1160 | |
| 1161 | if (i == 0) |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1162 | err = prepare_uptodate_page(pages[i], pos, |
| 1163 | force_uptodate); |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1164 | if (i == num_pages - 1) |
| 1165 | err = prepare_uptodate_page(pages[i], |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1166 | pos + write_bytes, false); |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1167 | if (err) { |
| 1168 | page_cache_release(pages[i]); |
| 1169 | faili = i - 1; |
| 1170 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1171 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1172 | wait_on_page_writeback(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1173 | } |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1174 | err = 0; |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 1175 | if (start_pos < inode->i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1176 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1177 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1178 | start_pos, last_pos - 1, 0, &cached_state); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1179 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 1180 | last_pos - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1181 | if (ordered && |
| 1182 | ordered->file_offset + ordered->len > start_pos && |
| 1183 | ordered->file_offset < last_pos) { |
| 1184 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1185 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 1186 | start_pos, last_pos - 1, |
| 1187 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1188 | for (i = 0; i < num_pages; i++) { |
| 1189 | unlock_page(pages[i]); |
| 1190 | page_cache_release(pages[i]); |
| 1191 | } |
| 1192 | btrfs_wait_ordered_range(inode, start_pos, |
| 1193 | last_pos - start_pos); |
| 1194 | goto again; |
| 1195 | } |
| 1196 | if (ordered) |
| 1197 | btrfs_put_ordered_extent(ordered); |
| 1198 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1199 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 1200 | last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1201 | EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 1202 | GFP_NOFS); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 1203 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 1204 | start_pos, last_pos - 1, &cached_state, |
| 1205 | GFP_NOFS); |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 1206 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1207 | for (i = 0; i < num_pages; i++) { |
Wu Fengguang | 32c7f20 | 2011-08-08 15:19:47 -0600 | [diff] [blame] | 1208 | if (clear_page_dirty_for_io(pages[i])) |
| 1209 | account_page_redirty(pages[i]); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 1210 | set_page_extent_mapped(pages[i]); |
| 1211 | WARN_ON(!PageLocked(pages[i])); |
| 1212 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1213 | return 0; |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1214 | fail: |
| 1215 | while (faili >= 0) { |
| 1216 | unlock_page(pages[faili]); |
| 1217 | page_cache_release(pages[faili]); |
| 1218 | faili--; |
| 1219 | } |
| 1220 | return err; |
| 1221 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1222 | } |
| 1223 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1224 | static noinline ssize_t __btrfs_buffered_write(struct file *file, |
| 1225 | struct iov_iter *i, |
| 1226 | loff_t pos) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1227 | { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1228 | struct inode *inode = fdentry(file)->d_inode; |
| 1229 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1230 | struct page **pages = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1231 | unsigned long first_index; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1232 | size_t num_written = 0; |
| 1233 | int nrptrs; |
Tsutomu Itoh | c914923 | 2011-03-30 00:57:23 +0000 | [diff] [blame] | 1234 | int ret = 0; |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1235 | bool force_page_uptodate = false; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1236 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1237 | nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) / |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1238 | PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / |
| 1239 | (sizeof(struct page *))); |
Wu Fengguang | 142349f | 2011-12-16 12:32:57 -0500 | [diff] [blame] | 1240 | nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied); |
| 1241 | nrptrs = max(nrptrs, 8); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1242 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1243 | if (!pages) |
| 1244 | return -ENOMEM; |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 1245 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1246 | first_index = pos >> PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1247 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1248 | while (iov_iter_count(i) > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1249 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1250 | size_t write_bytes = min(iov_iter_count(i), |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1251 | nrptrs * (size_t)PAGE_CACHE_SIZE - |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1252 | offset); |
Yan, Zheng | 3a90983 | 2011-01-18 13:34:40 +0800 | [diff] [blame] | 1253 | size_t num_pages = (write_bytes + offset + |
| 1254 | PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1255 | size_t dirty_pages; |
| 1256 | size_t copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1257 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1258 | WARN_ON(num_pages > nrptrs); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1259 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1260 | /* |
| 1261 | * Fault pages before locking them in prepare_pages |
| 1262 | * to avoid recursive lock |
| 1263 | */ |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1264 | if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1265 | ret = -EFAULT; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1266 | break; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | ret = btrfs_delalloc_reserve_space(inode, |
| 1270 | num_pages << PAGE_CACHE_SHIFT); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1271 | if (ret) |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1272 | break; |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1273 | |
Josef Bacik | 4a64001 | 2011-01-25 15:10:08 -0500 | [diff] [blame] | 1274 | /* |
| 1275 | * This is going to setup the pages array with the number of |
| 1276 | * pages we want, so we don't really need to worry about the |
| 1277 | * contents of pages from loop to loop |
| 1278 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1279 | ret = prepare_pages(root, file, pages, num_pages, |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1280 | pos, first_index, write_bytes, |
| 1281 | force_page_uptodate); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1282 | if (ret) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1283 | btrfs_delalloc_release_space(inode, |
| 1284 | num_pages << PAGE_CACHE_SHIFT); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1285 | break; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1286 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1287 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1288 | copied = btrfs_copy_from_user(pos, num_pages, |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1289 | write_bytes, pages, i); |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1290 | |
| 1291 | /* |
| 1292 | * if we have trouble faulting in the pages, fall |
| 1293 | * back to one page at a time |
| 1294 | */ |
| 1295 | if (copied < write_bytes) |
| 1296 | nrptrs = 1; |
| 1297 | |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1298 | if (copied == 0) { |
| 1299 | force_page_uptodate = true; |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1300 | dirty_pages = 0; |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1301 | } else { |
| 1302 | force_page_uptodate = false; |
Chris Mason | b1bf862 | 2011-02-28 09:52:08 -0500 | [diff] [blame] | 1303 | dirty_pages = (copied + offset + |
| 1304 | PAGE_CACHE_SIZE - 1) >> |
| 1305 | PAGE_CACHE_SHIFT; |
Josef Bacik | b6316429 | 2011-09-30 15:23:54 -0400 | [diff] [blame] | 1306 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1307 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1308 | /* |
| 1309 | * If we had a short copy we need to release the excess delaloc |
| 1310 | * bytes we reserved. We need to increment outstanding_extents |
| 1311 | * because btrfs_delalloc_release_space will decrement it, but |
| 1312 | * we still have an outstanding extent for the chunk we actually |
| 1313 | * managed to copy. |
| 1314 | */ |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1315 | if (num_pages > dirty_pages) { |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1316 | if (copied > 0) { |
| 1317 | spin_lock(&BTRFS_I(inode)->lock); |
| 1318 | BTRFS_I(inode)->outstanding_extents++; |
| 1319 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1320 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1321 | btrfs_delalloc_release_space(inode, |
| 1322 | (num_pages - dirty_pages) << |
| 1323 | PAGE_CACHE_SHIFT); |
| 1324 | } |
| 1325 | |
| 1326 | if (copied > 0) { |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 1327 | ret = btrfs_dirty_pages(root, inode, pages, |
| 1328 | dirty_pages, pos, copied, |
| 1329 | NULL); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1330 | if (ret) { |
| 1331 | btrfs_delalloc_release_space(inode, |
| 1332 | dirty_pages << PAGE_CACHE_SHIFT); |
| 1333 | btrfs_drop_pages(pages, num_pages); |
| 1334 | break; |
| 1335 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1336 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1337 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1338 | btrfs_drop_pages(pages, num_pages); |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1339 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1340 | cond_resched(); |
| 1341 | |
| 1342 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, |
| 1343 | dirty_pages); |
| 1344 | if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1) |
| 1345 | btrfs_btree_balance_dirty(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1346 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1347 | pos += copied; |
| 1348 | num_written += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1349 | } |
Chris Mason | 5b92ee7 | 2008-01-03 13:46:11 -0500 | [diff] [blame] | 1350 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1351 | kfree(pages); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1352 | |
| 1353 | return num_written ? num_written : ret; |
| 1354 | } |
| 1355 | |
| 1356 | static ssize_t __btrfs_direct_write(struct kiocb *iocb, |
| 1357 | const struct iovec *iov, |
| 1358 | unsigned long nr_segs, loff_t pos, |
| 1359 | loff_t *ppos, size_t count, size_t ocount) |
| 1360 | { |
| 1361 | struct file *file = iocb->ki_filp; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1362 | struct iov_iter i; |
| 1363 | ssize_t written; |
| 1364 | ssize_t written_buffered; |
| 1365 | loff_t endbyte; |
| 1366 | int err; |
| 1367 | |
| 1368 | written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos, |
| 1369 | count, ocount); |
| 1370 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1371 | if (written < 0 || written == count) |
| 1372 | return written; |
| 1373 | |
| 1374 | pos += written; |
| 1375 | count -= written; |
| 1376 | iov_iter_init(&i, iov, nr_segs, count, written); |
| 1377 | written_buffered = __btrfs_buffered_write(file, &i, pos); |
| 1378 | if (written_buffered < 0) { |
| 1379 | err = written_buffered; |
| 1380 | goto out; |
| 1381 | } |
| 1382 | endbyte = pos + written_buffered - 1; |
| 1383 | err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte); |
| 1384 | if (err) |
| 1385 | goto out; |
| 1386 | written += written_buffered; |
| 1387 | *ppos = pos + written_buffered; |
| 1388 | invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT, |
| 1389 | endbyte >> PAGE_CACHE_SHIFT); |
| 1390 | out: |
| 1391 | return written ? written : err; |
| 1392 | } |
| 1393 | |
| 1394 | static ssize_t btrfs_file_aio_write(struct kiocb *iocb, |
| 1395 | const struct iovec *iov, |
| 1396 | unsigned long nr_segs, loff_t pos) |
| 1397 | { |
| 1398 | struct file *file = iocb->ki_filp; |
| 1399 | struct inode *inode = fdentry(file)->d_inode; |
| 1400 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1401 | loff_t *ppos = &iocb->ki_pos; |
Miao Xie | 0c1a98c | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 1402 | u64 start_pos; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1403 | ssize_t num_written = 0; |
| 1404 | ssize_t err = 0; |
| 1405 | size_t count, ocount; |
| 1406 | |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 1407 | sb_start_write(inode->i_sb); |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1408 | |
| 1409 | mutex_lock(&inode->i_mutex); |
| 1410 | |
| 1411 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
| 1412 | if (err) { |
| 1413 | mutex_unlock(&inode->i_mutex); |
| 1414 | goto out; |
| 1415 | } |
| 1416 | count = ocount; |
| 1417 | |
| 1418 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1419 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1420 | if (err) { |
| 1421 | mutex_unlock(&inode->i_mutex); |
| 1422 | goto out; |
| 1423 | } |
| 1424 | |
| 1425 | if (count == 0) { |
| 1426 | mutex_unlock(&inode->i_mutex); |
| 1427 | goto out; |
| 1428 | } |
| 1429 | |
| 1430 | err = file_remove_suid(file); |
| 1431 | if (err) { |
| 1432 | mutex_unlock(&inode->i_mutex); |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * If BTRFS flips readonly due to some impossible error |
| 1438 | * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), |
| 1439 | * although we have opened a file as writable, we have |
| 1440 | * to stop this write operation to ensure FS consistency. |
| 1441 | */ |
| 1442 | if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { |
| 1443 | mutex_unlock(&inode->i_mutex); |
| 1444 | err = -EROFS; |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
Josef Bacik | e41f941 | 2012-03-26 09:46:47 -0400 | [diff] [blame] | 1448 | err = file_update_time(file); |
Josef Bacik | 22c44fe | 2011-11-30 10:45:38 -0500 | [diff] [blame] | 1449 | if (err) { |
| 1450 | mutex_unlock(&inode->i_mutex); |
| 1451 | goto out; |
| 1452 | } |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1453 | |
Miao Xie | 0c1a98c | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 1454 | start_pos = round_down(pos, root->sectorsize); |
| 1455 | if (start_pos > i_size_read(inode)) { |
| 1456 | err = btrfs_cont_expand(inode, i_size_read(inode), start_pos); |
| 1457 | if (err) { |
| 1458 | mutex_unlock(&inode->i_mutex); |
| 1459 | goto out; |
| 1460 | } |
| 1461 | } |
| 1462 | |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1463 | if (unlikely(file->f_flags & O_DIRECT)) { |
| 1464 | num_written = __btrfs_direct_write(iocb, iov, nr_segs, |
| 1465 | pos, ppos, count, ocount); |
| 1466 | } else { |
| 1467 | struct iov_iter i; |
| 1468 | |
| 1469 | iov_iter_init(&i, iov, nr_segs, count, num_written); |
| 1470 | |
| 1471 | num_written = __btrfs_buffered_write(file, &i, pos); |
| 1472 | if (num_written > 0) |
| 1473 | *ppos = pos + num_written; |
| 1474 | } |
| 1475 | |
| 1476 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1477 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1478 | /* |
| 1479 | * we want to make sure fsync finds this change |
| 1480 | * but we haven't joined a transaction running right now. |
| 1481 | * |
| 1482 | * Later on, someone is sure to update the inode and get the |
| 1483 | * real transid recorded. |
| 1484 | * |
| 1485 | * We set last_trans now to the fs_info generation + 1, |
| 1486 | * this will either be one more than the running transaction |
| 1487 | * or the generation used for the next transaction if there isn't |
| 1488 | * one running right now. |
| 1489 | */ |
| 1490 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1491 | if (num_written > 0 || num_written == -EIOCBQUEUED) { |
| 1492 | err = generic_write_sync(file, pos, num_written); |
| 1493 | if (err < 0 && num_written > 0) |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1494 | num_written = err; |
| 1495 | } |
Josef Bacik | d0215f3 | 2011-01-25 14:57:24 -0500 | [diff] [blame] | 1496 | out: |
Jan Kara | b2b5ef5 | 2012-06-12 16:20:45 +0200 | [diff] [blame] | 1497 | sb_end_write(inode->i_sb); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1498 | current->backing_dev_info = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1499 | return num_written ? num_written : err; |
| 1500 | } |
| 1501 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1502 | int btrfs_release_file(struct inode *inode, struct file *filp) |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1503 | { |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1504 | /* |
| 1505 | * ordered_data_close is set by settattr when we are about to truncate |
| 1506 | * a file from a non-zero size to a zero size. This tries to |
| 1507 | * flush down new bytes that may have been written if the |
| 1508 | * application were using truncate to replace a file in place. |
| 1509 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 1510 | if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
| 1511 | &BTRFS_I(inode)->runtime_flags)) { |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1512 | btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode); |
| 1513 | if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 1514 | filemap_flush(inode->i_mapping); |
| 1515 | } |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1516 | if (filp->private_data) |
| 1517 | btrfs_ioctl_trans_end(filp); |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1518 | return 0; |
| 1519 | } |
| 1520 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1521 | /* |
| 1522 | * fsync call for both files and directories. This logs the inode into |
| 1523 | * the tree log instead of forcing full commits whenever possible. |
| 1524 | * |
| 1525 | * It needs to call filemap_fdatawait so that all ordered extent updates are |
| 1526 | * in the metadata btree are up to date for copying to the log. |
| 1527 | * |
| 1528 | * It drops the inode mutex before doing the tree log commit. This is an |
| 1529 | * important optimization for directories because holding the mutex prevents |
| 1530 | * new operations on the dir while we write to disk. |
| 1531 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1532 | 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] | 1533 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1534 | struct dentry *dentry = file->f_path.dentry; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1535 | struct inode *inode = dentry->d_inode; |
| 1536 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1537 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1538 | struct btrfs_trans_handle *trans; |
| 1539 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1540 | trace_btrfs_sync_file(file, datasync); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1541 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1542 | mutex_lock(&inode->i_mutex); |
| 1543 | |
Josef Bacik | 0885ef5 | 2012-04-23 15:09:39 -0400 | [diff] [blame] | 1544 | /* |
| 1545 | * we wait first, since the writeback may change the inode, also wait |
| 1546 | * ordered range does a filemape_write_and_wait_range which is why we |
| 1547 | * don't do it above like other file systems. |
| 1548 | */ |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1549 | root->log_batch++; |
Josef Bacik | 0885ef5 | 2012-04-23 15:09:39 -0400 | [diff] [blame] | 1550 | btrfs_wait_ordered_range(inode, start, end); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1551 | root->log_batch++; |
| 1552 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1553 | /* |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1554 | * check the transaction that last modified this inode |
| 1555 | * and see if its already been committed |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1556 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1557 | if (!BTRFS_I(inode)->last_trans) { |
| 1558 | mutex_unlock(&inode->i_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1559 | goto out; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1560 | } |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1561 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1562 | /* |
| 1563 | * if the last transaction that changed this file was before |
| 1564 | * the current transaction, we can bail out now without any |
| 1565 | * syncing |
| 1566 | */ |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 1567 | smp_mb(); |
Josef Bacik | 22ee698 | 2012-05-29 16:57:49 -0400 | [diff] [blame] | 1568 | if (btrfs_inode_in_log(inode, root->fs_info->generation) || |
| 1569 | BTRFS_I(inode)->last_trans <= |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1570 | root->fs_info->last_trans_committed) { |
| 1571 | BTRFS_I(inode)->last_trans = 0; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 1572 | |
| 1573 | /* |
| 1574 | * We'v had everything committed since the last time we were |
| 1575 | * modified so clear this flag in case it was set for whatever |
| 1576 | * reason, it's no longer relevant. |
| 1577 | */ |
| 1578 | clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 1579 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1580 | mutex_unlock(&inode->i_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1581 | goto out; |
| 1582 | } |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1583 | |
| 1584 | /* |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1585 | * ok we haven't committed the transaction yet, lets do a commit |
| 1586 | */ |
Dan Carpenter | 6f902af | 2010-05-29 09:49:07 +0000 | [diff] [blame] | 1587 | if (file->private_data) |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1588 | btrfs_ioctl_trans_end(file); |
| 1589 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1590 | trans = btrfs_start_transaction(root, 0); |
| 1591 | if (IS_ERR(trans)) { |
| 1592 | ret = PTR_ERR(trans); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1593 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1594 | goto out; |
| 1595 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1596 | |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1597 | ret = btrfs_log_dentry_safe(trans, root, dentry); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1598 | if (ret < 0) { |
| 1599 | mutex_unlock(&inode->i_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1600 | goto out; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1601 | } |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1602 | |
| 1603 | /* we've logged all the items and now have a consistent |
| 1604 | * version of the file in the log. It is possible that |
| 1605 | * someone will come in and modify the file, but that's |
| 1606 | * fine because the log is consistent on disk, and we |
| 1607 | * have references to all of the file's extents |
| 1608 | * |
| 1609 | * It is possible that someone will come in and log the |
| 1610 | * file again, but that will end up using the synchronization |
| 1611 | * inside btrfs_sync_log to keep things safe. |
| 1612 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1613 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1614 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1615 | if (ret != BTRFS_NO_LOG_SYNC) { |
| 1616 | if (ret > 0) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1617 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1618 | } else { |
| 1619 | ret = btrfs_sync_log(trans, root); |
| 1620 | if (ret == 0) |
| 1621 | ret = btrfs_end_transaction(trans, root); |
| 1622 | else |
| 1623 | ret = btrfs_commit_transaction(trans, root); |
| 1624 | } |
| 1625 | } else { |
| 1626 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1627 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1628 | out: |
Roel Kluin | 014e4ac | 2010-01-29 10:42:11 +0000 | [diff] [blame] | 1629 | return ret > 0 ? -EIO : ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1630 | } |
| 1631 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 1632 | static const struct vm_operations_struct btrfs_file_vm_ops = { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 1633 | .fault = filemap_fault, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1634 | .page_mkwrite = btrfs_page_mkwrite, |
| 1635 | }; |
| 1636 | |
| 1637 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1638 | { |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 1639 | struct address_space *mapping = filp->f_mapping; |
| 1640 | |
| 1641 | if (!mapping->a_ops->readpage) |
| 1642 | return -ENOEXEC; |
| 1643 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1644 | file_accessed(filp); |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 1645 | vma->vm_ops = &btrfs_file_vm_ops; |
| 1646 | vma->vm_flags |= VM_CAN_NONLINEAR; |
| 1647 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1648 | return 0; |
| 1649 | } |
| 1650 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 1651 | static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf, |
| 1652 | int slot, u64 start, u64 end) |
| 1653 | { |
| 1654 | struct btrfs_file_extent_item *fi; |
| 1655 | struct btrfs_key key; |
| 1656 | |
| 1657 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) |
| 1658 | return 0; |
| 1659 | |
| 1660 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 1661 | if (key.objectid != btrfs_ino(inode) || |
| 1662 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 1663 | return 0; |
| 1664 | |
| 1665 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 1666 | |
| 1667 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) |
| 1668 | return 0; |
| 1669 | |
| 1670 | if (btrfs_file_extent_disk_bytenr(leaf, fi)) |
| 1671 | return 0; |
| 1672 | |
| 1673 | if (key.offset == end) |
| 1674 | return 1; |
| 1675 | if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start) |
| 1676 | return 1; |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode, |
| 1681 | struct btrfs_path *path, u64 offset, u64 end) |
| 1682 | { |
| 1683 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1684 | struct extent_buffer *leaf; |
| 1685 | struct btrfs_file_extent_item *fi; |
| 1686 | struct extent_map *hole_em; |
| 1687 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 1688 | struct btrfs_key key; |
| 1689 | int ret; |
| 1690 | |
| 1691 | key.objectid = btrfs_ino(inode); |
| 1692 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1693 | key.offset = offset; |
| 1694 | |
| 1695 | |
| 1696 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1697 | if (ret < 0) |
| 1698 | return ret; |
| 1699 | BUG_ON(!ret); |
| 1700 | |
| 1701 | leaf = path->nodes[0]; |
| 1702 | if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) { |
| 1703 | u64 num_bytes; |
| 1704 | |
| 1705 | path->slots[0]--; |
| 1706 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1707 | struct btrfs_file_extent_item); |
| 1708 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + |
| 1709 | end - offset; |
| 1710 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1711 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); |
| 1712 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1713 | btrfs_mark_buffer_dirty(leaf); |
| 1714 | goto out; |
| 1715 | } |
| 1716 | |
| 1717 | if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) { |
| 1718 | u64 num_bytes; |
| 1719 | |
| 1720 | path->slots[0]++; |
| 1721 | key.offset = offset; |
| 1722 | btrfs_set_item_key_safe(trans, root, path, &key); |
| 1723 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 1724 | struct btrfs_file_extent_item); |
| 1725 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end - |
| 1726 | offset; |
| 1727 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); |
| 1728 | btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes); |
| 1729 | btrfs_set_file_extent_offset(leaf, fi, 0); |
| 1730 | btrfs_mark_buffer_dirty(leaf); |
| 1731 | goto out; |
| 1732 | } |
| 1733 | btrfs_release_path(path); |
| 1734 | |
| 1735 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, |
| 1736 | 0, 0, end - offset, 0, end - offset, |
| 1737 | 0, 0, 0); |
| 1738 | if (ret) |
| 1739 | return ret; |
| 1740 | |
| 1741 | out: |
| 1742 | btrfs_release_path(path); |
| 1743 | |
| 1744 | hole_em = alloc_extent_map(); |
| 1745 | if (!hole_em) { |
| 1746 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); |
| 1747 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 1748 | &BTRFS_I(inode)->runtime_flags); |
| 1749 | } else { |
| 1750 | hole_em->start = offset; |
| 1751 | hole_em->len = end - offset; |
| 1752 | hole_em->orig_start = offset; |
| 1753 | |
| 1754 | hole_em->block_start = EXTENT_MAP_HOLE; |
| 1755 | hole_em->block_len = 0; |
| 1756 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 1757 | hole_em->compress_type = BTRFS_COMPRESS_NONE; |
| 1758 | hole_em->generation = trans->transid; |
| 1759 | |
| 1760 | do { |
| 1761 | btrfs_drop_extent_cache(inode, offset, end - 1, 0); |
| 1762 | write_lock(&em_tree->lock); |
| 1763 | ret = add_extent_mapping(em_tree, hole_em); |
| 1764 | if (!ret) |
| 1765 | list_move(&hole_em->list, |
| 1766 | &em_tree->modified_extents); |
| 1767 | write_unlock(&em_tree->lock); |
| 1768 | } while (ret == -EEXIST); |
| 1769 | free_extent_map(hole_em); |
| 1770 | if (ret) |
| 1771 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 1772 | &BTRFS_I(inode)->runtime_flags); |
| 1773 | } |
| 1774 | |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
| 1778 | static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) |
| 1779 | { |
| 1780 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1781 | struct extent_state *cached_state = NULL; |
| 1782 | struct btrfs_path *path; |
| 1783 | struct btrfs_block_rsv *rsv; |
| 1784 | struct btrfs_trans_handle *trans; |
| 1785 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 1786 | u64 lockstart = (offset + mask) & ~mask; |
| 1787 | u64 lockend = ((offset + len) & ~mask) - 1; |
| 1788 | u64 cur_offset = lockstart; |
| 1789 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
| 1790 | u64 drop_end; |
| 1791 | unsigned long nr; |
| 1792 | int ret = 0; |
| 1793 | int err = 0; |
| 1794 | bool same_page = (offset >> PAGE_CACHE_SHIFT) == |
| 1795 | ((offset + len) >> PAGE_CACHE_SHIFT); |
| 1796 | |
| 1797 | btrfs_wait_ordered_range(inode, offset, len); |
| 1798 | |
| 1799 | mutex_lock(&inode->i_mutex); |
| 1800 | if (offset >= inode->i_size) { |
| 1801 | mutex_unlock(&inode->i_mutex); |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | /* |
| 1806 | * Only do this if we are in the same page and we aren't doing the |
| 1807 | * entire page. |
| 1808 | */ |
| 1809 | if (same_page && len < PAGE_CACHE_SIZE) { |
| 1810 | ret = btrfs_truncate_page(inode, offset, len, 0); |
| 1811 | mutex_unlock(&inode->i_mutex); |
| 1812 | return ret; |
| 1813 | } |
| 1814 | |
| 1815 | /* zero back part of the first page */ |
| 1816 | ret = btrfs_truncate_page(inode, offset, 0, 0); |
| 1817 | if (ret) { |
| 1818 | mutex_unlock(&inode->i_mutex); |
| 1819 | return ret; |
| 1820 | } |
| 1821 | |
| 1822 | /* zero the front end of the last page */ |
| 1823 | ret = btrfs_truncate_page(inode, offset + len, 0, 1); |
| 1824 | if (ret) { |
| 1825 | mutex_unlock(&inode->i_mutex); |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
| 1829 | if (lockend < lockstart) { |
| 1830 | mutex_unlock(&inode->i_mutex); |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
| 1834 | while (1) { |
| 1835 | struct btrfs_ordered_extent *ordered; |
| 1836 | |
| 1837 | truncate_pagecache_range(inode, lockstart, lockend); |
| 1838 | |
| 1839 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 1840 | 0, &cached_state); |
| 1841 | ordered = btrfs_lookup_first_ordered_extent(inode, lockend); |
| 1842 | |
| 1843 | /* |
| 1844 | * We need to make sure we have no ordered extents in this range |
| 1845 | * and nobody raced in and read a page in this range, if we did |
| 1846 | * we need to try again. |
| 1847 | */ |
| 1848 | if ((!ordered || |
| 1849 | (ordered->file_offset + ordered->len < lockstart || |
| 1850 | ordered->file_offset > lockend)) && |
| 1851 | !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart, |
| 1852 | lockend, EXTENT_UPTODATE, 0, |
| 1853 | cached_state)) { |
| 1854 | if (ordered) |
| 1855 | btrfs_put_ordered_extent(ordered); |
| 1856 | break; |
| 1857 | } |
| 1858 | if (ordered) |
| 1859 | btrfs_put_ordered_extent(ordered); |
| 1860 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, |
| 1861 | lockend, &cached_state, GFP_NOFS); |
| 1862 | btrfs_wait_ordered_range(inode, lockstart, |
| 1863 | lockend - lockstart + 1); |
| 1864 | } |
| 1865 | |
| 1866 | path = btrfs_alloc_path(); |
| 1867 | if (!path) { |
| 1868 | ret = -ENOMEM; |
| 1869 | goto out; |
| 1870 | } |
| 1871 | |
| 1872 | rsv = btrfs_alloc_block_rsv(root); |
| 1873 | if (!rsv) { |
| 1874 | ret = -ENOMEM; |
| 1875 | goto out_free; |
| 1876 | } |
| 1877 | rsv->size = btrfs_calc_trunc_metadata_size(root, 1); |
| 1878 | rsv->failfast = 1; |
| 1879 | |
| 1880 | /* |
| 1881 | * 1 - update the inode |
| 1882 | * 1 - removing the extents in the range |
| 1883 | * 1 - adding the hole extent |
| 1884 | */ |
| 1885 | trans = btrfs_start_transaction(root, 3); |
| 1886 | if (IS_ERR(trans)) { |
| 1887 | err = PTR_ERR(trans); |
| 1888 | goto out_free; |
| 1889 | } |
| 1890 | |
| 1891 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, |
| 1892 | min_size); |
| 1893 | BUG_ON(ret); |
| 1894 | trans->block_rsv = rsv; |
| 1895 | |
| 1896 | while (cur_offset < lockend) { |
| 1897 | ret = __btrfs_drop_extents(trans, root, inode, path, |
| 1898 | cur_offset, lockend + 1, |
| 1899 | &drop_end, 1); |
| 1900 | if (ret != -ENOSPC) |
| 1901 | break; |
| 1902 | |
| 1903 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 1904 | |
| 1905 | ret = fill_holes(trans, inode, path, cur_offset, drop_end); |
| 1906 | if (ret) { |
| 1907 | err = ret; |
| 1908 | break; |
| 1909 | } |
| 1910 | |
| 1911 | cur_offset = drop_end; |
| 1912 | |
| 1913 | ret = btrfs_update_inode(trans, root, inode); |
| 1914 | if (ret) { |
| 1915 | err = ret; |
| 1916 | break; |
| 1917 | } |
| 1918 | |
| 1919 | nr = trans->blocks_used; |
| 1920 | btrfs_end_transaction(trans, root); |
| 1921 | btrfs_btree_balance_dirty(root, nr); |
| 1922 | |
| 1923 | trans = btrfs_start_transaction(root, 3); |
| 1924 | if (IS_ERR(trans)) { |
| 1925 | ret = PTR_ERR(trans); |
| 1926 | trans = NULL; |
| 1927 | break; |
| 1928 | } |
| 1929 | |
| 1930 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, |
| 1931 | rsv, min_size); |
| 1932 | BUG_ON(ret); /* shouldn't happen */ |
| 1933 | trans->block_rsv = rsv; |
| 1934 | } |
| 1935 | |
| 1936 | if (ret) { |
| 1937 | err = ret; |
| 1938 | goto out_trans; |
| 1939 | } |
| 1940 | |
| 1941 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 1942 | ret = fill_holes(trans, inode, path, cur_offset, drop_end); |
| 1943 | if (ret) { |
| 1944 | err = ret; |
| 1945 | goto out_trans; |
| 1946 | } |
| 1947 | |
| 1948 | out_trans: |
| 1949 | if (!trans) |
| 1950 | goto out_free; |
| 1951 | |
| 1952 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 1953 | ret = btrfs_update_inode(trans, root, inode); |
| 1954 | nr = trans->blocks_used; |
| 1955 | btrfs_end_transaction(trans, root); |
| 1956 | btrfs_btree_balance_dirty(root, nr); |
| 1957 | out_free: |
| 1958 | btrfs_free_path(path); |
| 1959 | btrfs_free_block_rsv(root, rsv); |
| 1960 | out: |
| 1961 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 1962 | &cached_state, GFP_NOFS); |
| 1963 | mutex_unlock(&inode->i_mutex); |
| 1964 | if (ret && !err) |
| 1965 | err = ret; |
| 1966 | return err; |
| 1967 | } |
| 1968 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 1969 | static long btrfs_fallocate(struct file *file, int mode, |
| 1970 | loff_t offset, loff_t len) |
| 1971 | { |
| 1972 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1973 | struct extent_state *cached_state = NULL; |
| 1974 | u64 cur_offset; |
| 1975 | u64 last_byte; |
| 1976 | u64 alloc_start; |
| 1977 | u64 alloc_end; |
| 1978 | u64 alloc_hint = 0; |
| 1979 | u64 locked_end; |
| 1980 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; |
| 1981 | struct extent_map *em; |
| 1982 | int ret; |
| 1983 | |
| 1984 | alloc_start = offset & ~mask; |
| 1985 | alloc_end = (offset + len + mask) & ~mask; |
| 1986 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 1987 | /* Make sure we aren't being give some crap mode */ |
| 1988 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 1989 | return -EOPNOTSUPP; |
| 1990 | |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame^] | 1991 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 1992 | return btrfs_punch_hole(inode, offset, len); |
| 1993 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 1994 | /* |
Chris Mason | d98456f | 2012-01-31 20:27:41 -0500 | [diff] [blame] | 1995 | * Make sure we have enough space before we do the |
| 1996 | * allocation. |
| 1997 | */ |
| 1998 | ret = btrfs_check_data_free_space(inode, len); |
| 1999 | if (ret) |
| 2000 | return ret; |
| 2001 | |
| 2002 | /* |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2003 | * wait for ordered IO before we have any locks. We'll loop again |
| 2004 | * below with the locks held. |
| 2005 | */ |
| 2006 | btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start); |
| 2007 | |
| 2008 | mutex_lock(&inode->i_mutex); |
| 2009 | ret = inode_newsize_ok(inode, alloc_end); |
| 2010 | if (ret) |
| 2011 | goto out; |
| 2012 | |
| 2013 | if (alloc_start > inode->i_size) { |
Josef Bacik | a41ad39 | 2011-01-31 15:30:16 -0500 | [diff] [blame] | 2014 | ret = btrfs_cont_expand(inode, i_size_read(inode), |
| 2015 | alloc_start); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2016 | if (ret) |
| 2017 | goto out; |
| 2018 | } |
| 2019 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2020 | locked_end = alloc_end - 1; |
| 2021 | while (1) { |
| 2022 | struct btrfs_ordered_extent *ordered; |
| 2023 | |
| 2024 | /* the extent lock is ordered inside the running |
| 2025 | * transaction |
| 2026 | */ |
| 2027 | lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2028 | locked_end, 0, &cached_state); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2029 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 2030 | alloc_end - 1); |
| 2031 | if (ordered && |
| 2032 | ordered->file_offset + ordered->len > alloc_start && |
| 2033 | ordered->file_offset < alloc_end) { |
| 2034 | btrfs_put_ordered_extent(ordered); |
| 2035 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 2036 | alloc_start, locked_end, |
| 2037 | &cached_state, GFP_NOFS); |
| 2038 | /* |
| 2039 | * we can't wait on the range with the transaction |
| 2040 | * running or with the extent lock held |
| 2041 | */ |
| 2042 | btrfs_wait_ordered_range(inode, alloc_start, |
| 2043 | alloc_end - alloc_start); |
| 2044 | } else { |
| 2045 | if (ordered) |
| 2046 | btrfs_put_ordered_extent(ordered); |
| 2047 | break; |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | cur_offset = alloc_start; |
| 2052 | while (1) { |
Josef Bacik | f1e490a | 2011-08-18 10:36:39 -0400 | [diff] [blame] | 2053 | u64 actual_end; |
| 2054 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2055 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, |
| 2056 | alloc_end - cur_offset, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2057 | if (IS_ERR_OR_NULL(em)) { |
| 2058 | if (!em) |
| 2059 | ret = -ENOMEM; |
| 2060 | else |
| 2061 | ret = PTR_ERR(em); |
| 2062 | break; |
| 2063 | } |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2064 | last_byte = min(extent_map_end(em), alloc_end); |
Josef Bacik | f1e490a | 2011-08-18 10:36:39 -0400 | [diff] [blame] | 2065 | actual_end = min_t(u64, extent_map_end(em), offset + len); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2066 | last_byte = (last_byte + mask) & ~mask; |
Josef Bacik | f1e490a | 2011-08-18 10:36:39 -0400 | [diff] [blame] | 2067 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2068 | if (em->block_start == EXTENT_MAP_HOLE || |
| 2069 | (cur_offset >= inode->i_size && |
| 2070 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
| 2071 | ret = btrfs_prealloc_file_range(inode, mode, cur_offset, |
| 2072 | last_byte - cur_offset, |
| 2073 | 1 << inode->i_blkbits, |
| 2074 | offset + len, |
| 2075 | &alloc_hint); |
Josef Bacik | 1b9c332 | 2011-08-17 10:19:52 -0400 | [diff] [blame] | 2076 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2077 | if (ret < 0) { |
| 2078 | free_extent_map(em); |
| 2079 | break; |
| 2080 | } |
Josef Bacik | f1e490a | 2011-08-18 10:36:39 -0400 | [diff] [blame] | 2081 | } else if (actual_end > inode->i_size && |
| 2082 | !(mode & FALLOC_FL_KEEP_SIZE)) { |
| 2083 | /* |
| 2084 | * We didn't need to allocate any more space, but we |
| 2085 | * still extended the size of the file so we need to |
| 2086 | * update i_size. |
| 2087 | */ |
| 2088 | inode->i_ctime = CURRENT_TIME; |
| 2089 | i_size_write(inode, actual_end); |
| 2090 | btrfs_ordered_update_i_size(inode, actual_end, NULL); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2091 | } |
| 2092 | free_extent_map(em); |
| 2093 | |
| 2094 | cur_offset = last_byte; |
| 2095 | if (cur_offset >= alloc_end) { |
| 2096 | ret = 0; |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
| 2100 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, |
| 2101 | &cached_state, GFP_NOFS); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2102 | out: |
| 2103 | mutex_unlock(&inode->i_mutex); |
Chris Mason | d98456f | 2012-01-31 20:27:41 -0500 | [diff] [blame] | 2104 | /* Let go of our reservation. */ |
| 2105 | btrfs_free_reserved_data_space(inode, len); |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2106 | return ret; |
| 2107 | } |
| 2108 | |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2109 | static int find_desired_extent(struct inode *inode, loff_t *offset, int origin) |
| 2110 | { |
| 2111 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2112 | struct extent_map *em; |
| 2113 | struct extent_state *cached_state = NULL; |
| 2114 | u64 lockstart = *offset; |
| 2115 | u64 lockend = i_size_read(inode); |
| 2116 | u64 start = *offset; |
| 2117 | u64 orig_start = *offset; |
| 2118 | u64 len = i_size_read(inode); |
| 2119 | u64 last_end = 0; |
| 2120 | int ret = 0; |
| 2121 | |
| 2122 | lockend = max_t(u64, root->sectorsize, lockend); |
| 2123 | if (lockend <= lockstart) |
| 2124 | lockend = lockstart + root->sectorsize; |
| 2125 | |
| 2126 | len = lockend - lockstart + 1; |
| 2127 | |
| 2128 | len = max_t(u64, len, root->sectorsize); |
| 2129 | if (inode->i_size == 0) |
| 2130 | return -ENXIO; |
| 2131 | |
| 2132 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 2133 | &cached_state); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2134 | |
| 2135 | /* |
| 2136 | * Delalloc is such a pain. If we have a hole and we have pending |
| 2137 | * delalloc for a portion of the hole we will get back a hole that |
| 2138 | * exists for the entire range since it hasn't been actually written |
| 2139 | * yet. So to take care of this case we need to look for an extent just |
| 2140 | * before the position we want in case there is outstanding delalloc |
| 2141 | * going on here. |
| 2142 | */ |
| 2143 | if (origin == SEEK_HOLE && start != 0) { |
| 2144 | if (start <= root->sectorsize) |
| 2145 | em = btrfs_get_extent_fiemap(inode, NULL, 0, 0, |
| 2146 | root->sectorsize, 0); |
| 2147 | else |
| 2148 | em = btrfs_get_extent_fiemap(inode, NULL, 0, |
| 2149 | start - root->sectorsize, |
| 2150 | root->sectorsize, 0); |
| 2151 | if (IS_ERR(em)) { |
Jeff Liu | 6af021d | 2012-02-09 14:25:50 +0800 | [diff] [blame] | 2152 | ret = PTR_ERR(em); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2153 | goto out; |
| 2154 | } |
| 2155 | last_end = em->start + em->len; |
| 2156 | if (em->block_start == EXTENT_MAP_DELALLOC) |
| 2157 | last_end = min_t(u64, last_end, inode->i_size); |
| 2158 | free_extent_map(em); |
| 2159 | } |
| 2160 | |
| 2161 | while (1) { |
| 2162 | em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0); |
| 2163 | if (IS_ERR(em)) { |
Jeff Liu | 6af021d | 2012-02-09 14:25:50 +0800 | [diff] [blame] | 2164 | ret = PTR_ERR(em); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2165 | break; |
| 2166 | } |
| 2167 | |
| 2168 | if (em->block_start == EXTENT_MAP_HOLE) { |
| 2169 | if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { |
| 2170 | if (last_end <= orig_start) { |
| 2171 | free_extent_map(em); |
| 2172 | ret = -ENXIO; |
| 2173 | break; |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | if (origin == SEEK_HOLE) { |
| 2178 | *offset = start; |
| 2179 | free_extent_map(em); |
| 2180 | break; |
| 2181 | } |
| 2182 | } else { |
| 2183 | if (origin == SEEK_DATA) { |
| 2184 | if (em->block_start == EXTENT_MAP_DELALLOC) { |
| 2185 | if (start >= inode->i_size) { |
| 2186 | free_extent_map(em); |
| 2187 | ret = -ENXIO; |
| 2188 | break; |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | *offset = start; |
| 2193 | free_extent_map(em); |
| 2194 | break; |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | start = em->start + em->len; |
| 2199 | last_end = em->start + em->len; |
| 2200 | |
| 2201 | if (em->block_start == EXTENT_MAP_DELALLOC) |
| 2202 | last_end = min_t(u64, last_end, inode->i_size); |
| 2203 | |
| 2204 | if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { |
| 2205 | free_extent_map(em); |
| 2206 | ret = -ENXIO; |
| 2207 | break; |
| 2208 | } |
| 2209 | free_extent_map(em); |
| 2210 | cond_resched(); |
| 2211 | } |
| 2212 | if (!ret) |
| 2213 | *offset = min(*offset, inode->i_size); |
| 2214 | out: |
| 2215 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
| 2216 | &cached_state, GFP_NOFS); |
| 2217 | return ret; |
| 2218 | } |
| 2219 | |
| 2220 | static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin) |
| 2221 | { |
| 2222 | struct inode *inode = file->f_mapping->host; |
| 2223 | int ret; |
| 2224 | |
| 2225 | mutex_lock(&inode->i_mutex); |
| 2226 | switch (origin) { |
| 2227 | case SEEK_END: |
| 2228 | case SEEK_CUR: |
Andi Kleen | ef3d0fd | 2011-09-15 16:06:48 -0700 | [diff] [blame] | 2229 | offset = generic_file_llseek(file, offset, origin); |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2230 | goto out; |
| 2231 | case SEEK_DATA: |
| 2232 | case SEEK_HOLE: |
Jeff Liu | 48802c8 | 2011-09-18 10:34:02 -0400 | [diff] [blame] | 2233 | if (offset >= i_size_read(inode)) { |
| 2234 | mutex_unlock(&inode->i_mutex); |
| 2235 | return -ENXIO; |
| 2236 | } |
| 2237 | |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2238 | ret = find_desired_extent(inode, &offset, origin); |
| 2239 | if (ret) { |
| 2240 | mutex_unlock(&inode->i_mutex); |
| 2241 | return ret; |
| 2242 | } |
| 2243 | } |
| 2244 | |
Dan Carpenter | 9a4327c | 2011-08-18 10:16:05 -0400 | [diff] [blame] | 2245 | if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) { |
Jeff Liu | 48802c8 | 2011-09-18 10:34:02 -0400 | [diff] [blame] | 2246 | offset = -EINVAL; |
Dan Carpenter | 9a4327c | 2011-08-18 10:16:05 -0400 | [diff] [blame] | 2247 | goto out; |
| 2248 | } |
| 2249 | if (offset > inode->i_sb->s_maxbytes) { |
Jeff Liu | 48802c8 | 2011-09-18 10:34:02 -0400 | [diff] [blame] | 2250 | offset = -EINVAL; |
Dan Carpenter | 9a4327c | 2011-08-18 10:16:05 -0400 | [diff] [blame] | 2251 | goto out; |
| 2252 | } |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2253 | |
| 2254 | /* Special lock needed here? */ |
| 2255 | if (offset != file->f_pos) { |
| 2256 | file->f_pos = offset; |
| 2257 | file->f_version = 0; |
| 2258 | } |
| 2259 | out: |
| 2260 | mutex_unlock(&inode->i_mutex); |
| 2261 | return offset; |
| 2262 | } |
| 2263 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 2264 | const struct file_operations btrfs_file_operations = { |
Josef Bacik | b267515 | 2011-07-18 13:21:36 -0400 | [diff] [blame] | 2265 | .llseek = btrfs_file_llseek, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2266 | .read = do_sync_read, |
Miao Xie | 4a001071 | 2010-06-07 03:38:51 +0000 | [diff] [blame] | 2267 | .write = do_sync_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2268 | .aio_read = generic_file_aio_read, |
Chris Mason | e9906a9 | 2007-12-14 12:56:58 -0500 | [diff] [blame] | 2269 | .splice_read = generic_file_splice_read, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 2270 | .aio_write = btrfs_file_aio_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 2271 | .mmap = btrfs_file_mmap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2272 | .open = generic_file_open, |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 2273 | .release = btrfs_release_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2274 | .fsync = btrfs_sync_file, |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 2275 | .fallocate = btrfs_fallocate, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 2276 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2277 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 2278 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2279 | #endif |
| 2280 | }; |