blob: 75d0fe134be31cb6b23e9a0091263cd993a1f48c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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 Mason39279cc2007-06-12 06:35:45 -040019#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 Mason39279cc2007-06-12 06:35:45 -040025#include <linux/backing-dev.h>
26#include <linux/mpage.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010027#include <linux/falloc.h>
Chris Mason39279cc2007-06-12 06:35:45 -040028#include <linux/swap.h>
29#include <linux/writeback.h>
30#include <linux/statfs.h>
31#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000033#include <linux/btrfs.h>
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
40#include "locking.h"
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -040041#include "compat.h"
Josef Bacik2aaa6652012-08-29 14:27:18 -040042#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043
Miao Xie9247f312012-11-26 09:24:43 +000044static struct kmem_cache *btrfs_inode_defrag_cachep;
Chris Mason4cb53002011-05-24 15:35:30 -040045/*
46 * when auto defrag is enabled we
47 * queue up these defrag structs to remember which
48 * inodes need defragging passes
49 */
50struct inode_defrag {
51 struct rb_node rb_node;
52 /* objectid */
53 u64 ino;
54 /*
55 * transid where the defrag was added, we search for
56 * extents newer than this
57 */
58 u64 transid;
59
60 /* root objectid */
61 u64 root;
62
63 /* last offset we were able to defrag */
64 u64 last_offset;
65
66 /* if we've wrapped around back to zero once already */
67 int cycled;
68};
69
Miao Xie762f2262012-05-24 18:58:27 +080070static int __compare_inode_defrag(struct inode_defrag *defrag1,
71 struct inode_defrag *defrag2)
72{
73 if (defrag1->root > defrag2->root)
74 return 1;
75 else if (defrag1->root < defrag2->root)
76 return -1;
77 else if (defrag1->ino > defrag2->ino)
78 return 1;
79 else if (defrag1->ino < defrag2->ino)
80 return -1;
81 else
82 return 0;
83}
84
Chris Mason4cb53002011-05-24 15:35:30 -040085/* pop a record for an inode into the defrag tree. The lock
86 * must be held already
87 *
88 * If you're inserting a record for an older transid than an
89 * existing record, the transid already in the tree is lowered
90 *
91 * If an existing record is found the defrag item you
92 * pass in is freed
93 */
Miao Xie8ddc4732012-11-26 09:25:38 +000094static int __btrfs_add_inode_defrag(struct inode *inode,
Chris Mason4cb53002011-05-24 15:35:30 -040095 struct inode_defrag *defrag)
96{
97 struct btrfs_root *root = BTRFS_I(inode)->root;
98 struct inode_defrag *entry;
99 struct rb_node **p;
100 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800101 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400102
103 p = &root->fs_info->defrag_inodes.rb_node;
104 while (*p) {
105 parent = *p;
106 entry = rb_entry(parent, struct inode_defrag, rb_node);
107
Miao Xie762f2262012-05-24 18:58:27 +0800108 ret = __compare_inode_defrag(defrag, entry);
109 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400110 p = &parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800111 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400112 p = &parent->rb_right;
113 else {
114 /* if we're reinserting an entry for
115 * an old defrag run, make sure to
116 * lower the transid of our existing record
117 */
118 if (defrag->transid < entry->transid)
119 entry->transid = defrag->transid;
120 if (defrag->last_offset > entry->last_offset)
121 entry->last_offset = defrag->last_offset;
Miao Xie8ddc4732012-11-26 09:25:38 +0000122 return -EEXIST;
Chris Mason4cb53002011-05-24 15:35:30 -0400123 }
124 }
Josef Bacik72ac3c02012-05-23 14:13:11 -0400125 set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
Chris Mason4cb53002011-05-24 15:35:30 -0400126 rb_link_node(&defrag->rb_node, parent, p);
127 rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
Miao Xie8ddc4732012-11-26 09:25:38 +0000128 return 0;
129}
Chris Mason4cb53002011-05-24 15:35:30 -0400130
Miao Xie8ddc4732012-11-26 09:25:38 +0000131static inline int __need_auto_defrag(struct btrfs_root *root)
132{
133 if (!btrfs_test_opt(root, AUTO_DEFRAG))
134 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400135
Miao Xie8ddc4732012-11-26 09:25:38 +0000136 if (btrfs_fs_closing(root->fs_info))
137 return 0;
138
139 return 1;
Chris Mason4cb53002011-05-24 15:35:30 -0400140}
141
142/*
143 * insert a defrag record for this inode if auto defrag is
144 * enabled
145 */
146int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
147 struct inode *inode)
148{
149 struct btrfs_root *root = BTRFS_I(inode)->root;
150 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400151 u64 transid;
Miao Xie8ddc4732012-11-26 09:25:38 +0000152 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400153
Miao Xie8ddc4732012-11-26 09:25:38 +0000154 if (!__need_auto_defrag(root))
Chris Mason4cb53002011-05-24 15:35:30 -0400155 return 0;
156
Josef Bacik72ac3c02012-05-23 14:13:11 -0400157 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
Chris Mason4cb53002011-05-24 15:35:30 -0400158 return 0;
159
160 if (trans)
161 transid = trans->transid;
162 else
163 transid = BTRFS_I(inode)->root->last_trans;
164
Miao Xie9247f312012-11-26 09:24:43 +0000165 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -0400166 if (!defrag)
167 return -ENOMEM;
168
David Sterbaa4689d22011-05-31 17:08:14 +0000169 defrag->ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400170 defrag->transid = transid;
171 defrag->root = root->root_key.objectid;
172
173 spin_lock(&root->fs_info->defrag_inodes_lock);
Miao Xie8ddc4732012-11-26 09:25:38 +0000174 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
175 /*
176 * If we set IN_DEFRAG flag and evict the inode from memory,
177 * and then re-read this inode, this new inode doesn't have
178 * IN_DEFRAG flag. At the case, we may find the existed defrag.
179 */
180 ret = __btrfs_add_inode_defrag(inode, defrag);
181 if (ret)
182 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
183 } else {
Miao Xie9247f312012-11-26 09:24:43 +0000184 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
Miao Xie8ddc4732012-11-26 09:25:38 +0000185 }
Chris Mason4cb53002011-05-24 15:35:30 -0400186 spin_unlock(&root->fs_info->defrag_inodes_lock);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000187 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400188}
189
190/*
Miao Xie8ddc4732012-11-26 09:25:38 +0000191 * Requeue the defrag object. If there is a defrag object that points to
192 * the same inode in the tree, we will merge them together (by
193 * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
194 */
195void btrfs_requeue_inode_defrag(struct inode *inode,
196 struct inode_defrag *defrag)
197{
198 struct btrfs_root *root = BTRFS_I(inode)->root;
199 int ret;
200
201 if (!__need_auto_defrag(root))
202 goto out;
203
204 /*
205 * Here we don't check the IN_DEFRAG flag, because we need merge
206 * them together.
207 */
208 spin_lock(&root->fs_info->defrag_inodes_lock);
209 ret = __btrfs_add_inode_defrag(inode, defrag);
210 spin_unlock(&root->fs_info->defrag_inodes_lock);
211 if (ret)
212 goto out;
213 return;
214out:
215 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
216}
217
218/*
Miao Xie26176e72012-11-26 09:26:20 +0000219 * pick the defragable inode that we want, if it doesn't exist, we will get
220 * the next one.
Chris Mason4cb53002011-05-24 15:35:30 -0400221 */
Miao Xie26176e72012-11-26 09:26:20 +0000222static struct inode_defrag *
223btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400224{
225 struct inode_defrag *entry = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800226 struct inode_defrag tmp;
Chris Mason4cb53002011-05-24 15:35:30 -0400227 struct rb_node *p;
228 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800229 int ret;
230
231 tmp.ino = ino;
232 tmp.root = root;
Chris Mason4cb53002011-05-24 15:35:30 -0400233
Miao Xie26176e72012-11-26 09:26:20 +0000234 spin_lock(&fs_info->defrag_inodes_lock);
235 p = fs_info->defrag_inodes.rb_node;
Chris Mason4cb53002011-05-24 15:35:30 -0400236 while (p) {
237 parent = p;
238 entry = rb_entry(parent, struct inode_defrag, rb_node);
239
Miao Xie762f2262012-05-24 18:58:27 +0800240 ret = __compare_inode_defrag(&tmp, entry);
241 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400242 p = parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800243 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400244 p = parent->rb_right;
245 else
Miao Xie26176e72012-11-26 09:26:20 +0000246 goto out;
Chris Mason4cb53002011-05-24 15:35:30 -0400247 }
248
Miao Xie26176e72012-11-26 09:26:20 +0000249 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
250 parent = rb_next(parent);
251 if (parent)
Chris Mason4cb53002011-05-24 15:35:30 -0400252 entry = rb_entry(parent, struct inode_defrag, rb_node);
Miao Xie26176e72012-11-26 09:26:20 +0000253 else
254 entry = NULL;
Chris Mason4cb53002011-05-24 15:35:30 -0400255 }
Miao Xie26176e72012-11-26 09:26:20 +0000256out:
257 if (entry)
258 rb_erase(parent, &fs_info->defrag_inodes);
259 spin_unlock(&fs_info->defrag_inodes_lock);
260 return entry;
261}
262
263void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
264{
265 struct inode_defrag *defrag;
266 struct rb_node *node;
267
268 spin_lock(&fs_info->defrag_inodes_lock);
269 node = rb_first(&fs_info->defrag_inodes);
270 while (node) {
271 rb_erase(node, &fs_info->defrag_inodes);
272 defrag = rb_entry(node, struct inode_defrag, rb_node);
273 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
274
275 if (need_resched()) {
276 spin_unlock(&fs_info->defrag_inodes_lock);
277 cond_resched();
278 spin_lock(&fs_info->defrag_inodes_lock);
279 }
280
281 node = rb_first(&fs_info->defrag_inodes);
282 }
283 spin_unlock(&fs_info->defrag_inodes_lock);
284}
285
286#define BTRFS_DEFRAG_BATCH 1024
287
288static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
289 struct inode_defrag *defrag)
290{
291 struct btrfs_root *inode_root;
292 struct inode *inode;
293 struct btrfs_key key;
294 struct btrfs_ioctl_defrag_range_args range;
295 int num_defrag;
Liu Bo6f1c3602013-01-29 03:22:10 +0000296 int index;
297 int ret;
Miao Xie26176e72012-11-26 09:26:20 +0000298
299 /* get the inode */
300 key.objectid = defrag->root;
301 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
302 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000303
304 index = srcu_read_lock(&fs_info->subvol_srcu);
305
Miao Xie26176e72012-11-26 09:26:20 +0000306 inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
307 if (IS_ERR(inode_root)) {
Liu Bo6f1c3602013-01-29 03:22:10 +0000308 ret = PTR_ERR(inode_root);
309 goto cleanup;
310 }
311 if (btrfs_root_refs(&inode_root->root_item) == 0) {
312 ret = -ENOENT;
313 goto cleanup;
Miao Xie26176e72012-11-26 09:26:20 +0000314 }
315
316 key.objectid = defrag->ino;
317 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
318 key.offset = 0;
319 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
320 if (IS_ERR(inode)) {
Liu Bo6f1c3602013-01-29 03:22:10 +0000321 ret = PTR_ERR(inode);
322 goto cleanup;
Miao Xie26176e72012-11-26 09:26:20 +0000323 }
Liu Bo6f1c3602013-01-29 03:22:10 +0000324 srcu_read_unlock(&fs_info->subvol_srcu, index);
Miao Xie26176e72012-11-26 09:26:20 +0000325
326 /* do a chunk of defrag */
327 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
328 memset(&range, 0, sizeof(range));
329 range.len = (u64)-1;
330 range.start = defrag->last_offset;
Miao Xieb66f00d2012-11-26 09:27:29 +0000331
332 sb_start_write(fs_info->sb);
Miao Xie26176e72012-11-26 09:26:20 +0000333 num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
334 BTRFS_DEFRAG_BATCH);
Miao Xieb66f00d2012-11-26 09:27:29 +0000335 sb_end_write(fs_info->sb);
Miao Xie26176e72012-11-26 09:26:20 +0000336 /*
337 * if we filled the whole defrag batch, there
338 * must be more work to do. Queue this defrag
339 * again
340 */
341 if (num_defrag == BTRFS_DEFRAG_BATCH) {
342 defrag->last_offset = range.start;
343 btrfs_requeue_inode_defrag(inode, defrag);
344 } else if (defrag->last_offset && !defrag->cycled) {
345 /*
346 * we didn't fill our defrag batch, but
347 * we didn't start at zero. Make sure we loop
348 * around to the start of the file.
349 */
350 defrag->last_offset = 0;
351 defrag->cycled = 1;
352 btrfs_requeue_inode_defrag(inode, defrag);
353 } else {
354 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
355 }
356
357 iput(inode);
358 return 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000359cleanup:
360 srcu_read_unlock(&fs_info->subvol_srcu, index);
361 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
362 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400363}
364
365/*
366 * run through the list of inodes in the FS that need
367 * defragging
368 */
369int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
370{
371 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400372 u64 first_ino = 0;
Miao Xie762f2262012-05-24 18:58:27 +0800373 u64 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400374
375 atomic_inc(&fs_info->defrag_running);
Chris Mason4cb53002011-05-24 15:35:30 -0400376 while(1) {
Miao Xie26176e72012-11-26 09:26:20 +0000377 if (!__need_auto_defrag(fs_info->tree_root))
378 break;
Chris Mason4cb53002011-05-24 15:35:30 -0400379
380 /* find an inode to defrag */
Miao Xie26176e72012-11-26 09:26:20 +0000381 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
382 first_ino);
Chris Mason4cb53002011-05-24 15:35:30 -0400383 if (!defrag) {
Miao Xie26176e72012-11-26 09:26:20 +0000384 if (root_objectid || first_ino) {
Miao Xie762f2262012-05-24 18:58:27 +0800385 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400386 first_ino = 0;
387 continue;
388 } else {
389 break;
390 }
391 }
392
Chris Mason4cb53002011-05-24 15:35:30 -0400393 first_ino = defrag->ino + 1;
Miao Xie762f2262012-05-24 18:58:27 +0800394 root_objectid = defrag->root;
Chris Mason4cb53002011-05-24 15:35:30 -0400395
Miao Xie26176e72012-11-26 09:26:20 +0000396 __btrfs_run_defrag_inode(fs_info, defrag);
Chris Mason4cb53002011-05-24 15:35:30 -0400397 }
Chris Mason4cb53002011-05-24 15:35:30 -0400398 atomic_dec(&fs_info->defrag_running);
399
400 /*
401 * during unmount, we use the transaction_wait queue to
402 * wait for the defragger to stop
403 */
404 wake_up(&fs_info->transaction_wait);
405 return 0;
406}
Chris Mason39279cc2007-06-12 06:35:45 -0400407
Chris Masond352ac62008-09-29 15:18:18 -0400408/* simple helper to fault in pages and copy. This should go away
409 * and be replaced with calls into generic code.
410 */
Chris Masond3977122009-01-05 21:25:51 -0500411static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -0500412 size_t write_bytes,
Chris Masona1b32a52008-09-05 16:09:51 -0400413 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -0400414 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -0400415{
Xin Zhong914ee292010-12-09 09:30:14 +0000416 size_t copied = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -0500417 size_t total_copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400418 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400419 int offset = pos & (PAGE_CACHE_SIZE - 1);
420
Josef Bacik11c65dc2010-05-23 11:07:21 -0400421 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400422 size_t count = min_t(size_t,
423 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400424 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +0000425 /*
426 * Copy data from userspace to the current page
427 *
428 * Disable pagefault to avoid recursive lock since
429 * the pages are already locked
430 */
431 pagefault_disable();
432 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
433 pagefault_enable();
Josef Bacik11c65dc2010-05-23 11:07:21 -0400434
Chris Mason39279cc2007-06-12 06:35:45 -0400435 /* Flush processor's dcache for this page */
436 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -0500437
438 /*
439 * if we get a partial write, we can end up with
440 * partially up to date pages. These add
441 * a lot of complexity, so make sure they don't
442 * happen by forcing this copy to be retried.
443 *
444 * The rest of the btrfs_file_write code will fall
445 * back to page at a time copies after we return 0.
446 */
447 if (!PageUptodate(page) && copied < count)
448 copied = 0;
449
Josef Bacik11c65dc2010-05-23 11:07:21 -0400450 iov_iter_advance(i, copied);
451 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +0000452 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400453
Xin Zhong914ee292010-12-09 09:30:14 +0000454 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik9f570b82011-01-25 12:42:37 -0500455 if (unlikely(copied == 0))
Xin Zhong914ee292010-12-09 09:30:14 +0000456 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400457
458 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
459 offset += copied;
460 } else {
461 pg++;
462 offset = 0;
463 }
Chris Mason39279cc2007-06-12 06:35:45 -0400464 }
Xin Zhong914ee292010-12-09 09:30:14 +0000465 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400466}
467
Chris Masond352ac62008-09-29 15:18:18 -0400468/*
469 * unlocks pages after btrfs_file_write is done with them
470 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400471void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400472{
473 size_t i;
474 for (i = 0; i < num_pages; i++) {
Chris Masond352ac62008-09-29 15:18:18 -0400475 /* page checked is some magic around finding pages that
476 * have been modified without going through btrfs_set_page_dirty
477 * clear it here
478 */
Chris Mason4a096752008-07-21 10:29:44 -0400479 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400480 unlock_page(pages[i]);
481 mark_page_accessed(pages[i]);
482 page_cache_release(pages[i]);
483 }
484}
485
Chris Masond352ac62008-09-29 15:18:18 -0400486/*
487 * after copy_from_user, pages need to be dirtied and we need to make
488 * sure holes are created between the current EOF and the start of
489 * any next extents (if required).
490 *
491 * this also makes the decision about creating an inline extent vs
492 * doing real data extents, marking pages dirty and delalloc as required.
493 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400494int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
495 struct page **pages, size_t num_pages,
496 loff_t pos, size_t write_bytes,
497 struct extent_state **cached)
Chris Mason39279cc2007-06-12 06:35:45 -0400498{
Chris Mason39279cc2007-06-12 06:35:45 -0400499 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400500 int i;
Chris Masondb945352007-10-15 16:15:53 -0400501 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400502 u64 start_pos;
503 u64 end_of_last_block;
504 u64 end_pos = pos + write_bytes;
505 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400506
Chris Mason5f39d392007-10-15 16:14:19 -0400507 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400508 num_bytes = (write_bytes + pos - start_pos +
509 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400510
Chris Masondb945352007-10-15 16:15:53 -0400511 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000512 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400513 cached);
Josef Bacikd0215f32011-01-25 14:57:24 -0500514 if (err)
515 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400516
Chris Masonc8b97812008-10-29 14:49:59 -0400517 for (i = 0; i < num_pages; i++) {
518 struct page *p = pages[i];
519 SetPageUptodate(p);
520 ClearPageChecked(p);
521 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400522 }
Josef Bacik9f570b82011-01-25 12:42:37 -0500523
524 /*
525 * we've only changed i_size in ram, and we haven't updated
526 * the disk i_size. There is no need to log the inode
527 * at this time.
528 */
529 if (end_pos > isize)
Chris Masona52d9a82007-08-27 16:49:44 -0400530 i_size_write(inode, end_pos);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400531 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400532}
533
Chris Masond352ac62008-09-29 15:18:18 -0400534/*
535 * this drops all the extents in the cache that intersect the range
536 * [start, end]. Existing extents are split as required.
537 */
Josef Bacik7014cdb2012-08-30 20:06:49 -0400538void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
539 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400540{
541 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400542 struct extent_map *split = NULL;
543 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400544 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500545 u64 len = end - start + 1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400546 u64 gen;
Chris Mason3b951512008-04-17 11:29:12 -0400547 int ret;
548 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400549 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400550 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400551
Chris Masone6dcd2d2008-07-17 12:53:50 -0400552 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400553 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500554 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400555 testend = 0;
556 }
Chris Masond3977122009-01-05 21:25:51 -0500557 while (1) {
Josef Bacik7014cdb2012-08-30 20:06:49 -0400558 int no_splits = 0;
559
Chris Mason3b951512008-04-17 11:29:12 -0400560 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200561 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400562 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200563 split2 = alloc_extent_map();
Josef Bacik7014cdb2012-08-30 20:06:49 -0400564 if (!split || !split2)
565 no_splits = 1;
Chris Mason3b951512008-04-17 11:29:12 -0400566
Chris Mason890871b2009-09-02 16:24:52 -0400567 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500568 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500569 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400570 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400571 break;
Chris Masond1310b22008-01-24 16:13:08 -0500572 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400573 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400574 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400575 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000576 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400577 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400578 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400579 break;
580 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000581 start = em->start + em->len;
582 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400583 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400584 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400585 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400586 continue;
587 }
Chris Masonc8b97812008-10-29 14:49:59 -0400588 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400589 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400590 remove_extent_mapping(em_tree, em);
Josef Bacik7014cdb2012-08-30 20:06:49 -0400591 if (no_splits)
592 goto next;
Chris Mason3b951512008-04-17 11:29:12 -0400593
594 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
595 em->start < start) {
596 split->start = em->start;
597 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500598 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400599 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400600
601 if (compressed)
602 split->block_len = em->block_len;
603 else
604 split->block_len = split->len;
Josef Bacikb4939682012-12-03 10:31:19 -0500605 split->orig_block_len = max(split->block_len,
606 em->orig_block_len);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400607 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400608 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400609 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800610 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400611 ret = add_extent_mapping(em_tree, split);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100612 BUG_ON(ret); /* Logic error */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400613 list_move(&split->list, &em_tree->modified_extents);
Chris Mason3b951512008-04-17 11:29:12 -0400614 free_extent_map(split);
615 split = split2;
616 split2 = NULL;
617 }
618 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
619 testend && em->start + em->len > start + len) {
620 u64 diff = start + len - em->start;
621
622 split->start = start + len;
623 split->len = em->start + em->len - (start + len);
624 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400625 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800626 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400627 split->generation = gen;
Josef Bacikb4939682012-12-03 10:31:19 -0500628 split->orig_block_len = max(em->block_len,
629 em->orig_block_len);
Chris Mason3b951512008-04-17 11:29:12 -0400630
Chris Masonc8b97812008-10-29 14:49:59 -0400631 if (compressed) {
632 split->block_len = em->block_len;
633 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500634 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400635 } else {
636 split->block_len = split->len;
637 split->block_start = em->block_start + diff;
Josef Bacik70c8a912012-10-11 16:54:30 -0400638 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400639 }
Chris Mason3b951512008-04-17 11:29:12 -0400640
641 ret = add_extent_mapping(em_tree, split);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100642 BUG_ON(ret); /* Logic error */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400643 list_move(&split->list, &em_tree->modified_extents);
Chris Mason3b951512008-04-17 11:29:12 -0400644 free_extent_map(split);
645 split = NULL;
646 }
Josef Bacik7014cdb2012-08-30 20:06:49 -0400647next:
Chris Mason890871b2009-09-02 16:24:52 -0400648 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500649
Chris Masona52d9a82007-08-27 16:49:44 -0400650 /* once for us */
651 free_extent_map(em);
652 /* once for the tree*/
653 free_extent_map(em);
654 }
Chris Mason3b951512008-04-17 11:29:12 -0400655 if (split)
656 free_extent_map(split);
657 if (split2)
658 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400659}
660
Chris Mason39279cc2007-06-12 06:35:45 -0400661/*
662 * this is very complex, but the basic idea is to drop all extents
663 * in the range start - end. hint_block is filled in with a block number
664 * that would be a good hint to the block allocator for this file.
665 *
666 * If an extent intersects the range but is not entirely inside the range
667 * it is either truncated or split. Anything entirely inside the range
668 * is deleted from the tree.
669 */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400670int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
671 struct btrfs_root *root, struct inode *inode,
672 struct btrfs_path *path, u64 start, u64 end,
Josef Bacik2aaa6652012-08-29 14:27:18 -0400673 u64 *drop_end, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400674{
Chris Mason00f5c792007-11-30 10:09:33 -0500675 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000676 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500677 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000678 struct btrfs_key new_key;
Li Zefan33345d012011-04-20 10:31:50 +0800679 u64 ino = btrfs_ino(inode);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000680 u64 search_start = start;
681 u64 disk_bytenr = 0;
682 u64 num_bytes = 0;
683 u64 extent_offset = 0;
684 u64 extent_end = 0;
685 int del_nr = 0;
686 int del_slot = 0;
687 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400688 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500689 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400690 int modify_tree = -1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400691 int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
Josef Bacikc3308f82012-09-14 14:51:22 -0400692 int found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400693
Chris Masona1ed8352009-09-11 12:27:37 -0400694 if (drop_cache)
695 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400696
Chris Masondc7fdde2012-04-27 14:31:29 -0400697 if (start >= BTRFS_I(inode)->disk_i_size)
698 modify_tree = 0;
699
Chris Masond3977122009-01-05 21:25:51 -0500700 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400701 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800702 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400703 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400704 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000705 break;
706 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
707 leaf = path->nodes[0];
708 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800709 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000710 key.type == BTRFS_EXTENT_DATA_KEY)
711 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400712 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400713 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000714next_slot:
715 leaf = path->nodes[0];
716 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
717 BUG_ON(del_nr > 0);
718 ret = btrfs_next_leaf(root, path);
719 if (ret < 0)
720 break;
721 if (ret > 0) {
722 ret = 0;
723 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400724 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000725 leaf = path->nodes[0];
726 recow = 1;
727 }
728
729 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800730 if (key.objectid > ino ||
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000731 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
732 break;
733
734 fi = btrfs_item_ptr(leaf, path->slots[0],
735 struct btrfs_file_extent_item);
736 extent_type = btrfs_file_extent_type(leaf, fi);
737
738 if (extent_type == BTRFS_FILE_EXTENT_REG ||
739 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
740 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
741 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
742 extent_offset = btrfs_file_extent_offset(leaf, fi);
743 extent_end = key.offset +
744 btrfs_file_extent_num_bytes(leaf, fi);
745 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
746 extent_end = key.offset +
747 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400748 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000749 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400750 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400751 }
752
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000753 if (extent_end <= search_start) {
754 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400755 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400756 }
757
Josef Bacikc3308f82012-09-14 14:51:22 -0400758 found = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000759 search_start = max(key.offset, start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400760 if (recow || !modify_tree) {
761 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200762 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000763 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400764 }
Chris Mason771ed682008-11-06 22:02:51 -0500765
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000766 /*
767 * | - range to drop - |
768 * | -------- extent -------- |
769 */
770 if (start > key.offset && end < extent_end) {
771 BUG_ON(del_nr > 0);
772 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
773
774 memcpy(&new_key, &key, sizeof(new_key));
775 new_key.offset = start;
776 ret = btrfs_duplicate_item(trans, root, path,
777 &new_key);
778 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200779 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000780 continue;
781 }
782 if (ret < 0)
783 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400784
Chris Mason5f39d392007-10-15 16:14:19 -0400785 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000786 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
787 struct btrfs_file_extent_item);
788 btrfs_set_file_extent_num_bytes(leaf, fi,
789 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400790
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000791 fi = btrfs_item_ptr(leaf, path->slots[0],
792 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400793
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000794 extent_offset += start - key.offset;
795 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
796 btrfs_set_file_extent_num_bytes(leaf, fi,
797 extent_end - start);
798 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400799
Josef Bacik5dc562c2012-08-17 13:14:17 -0400800 if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000801 ret = btrfs_inc_extent_ref(trans, root,
802 disk_bytenr, num_bytes, 0,
803 root->root_key.objectid,
804 new_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200805 start - extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100806 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400807 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000808 key.offset = start;
809 }
810 /*
811 * | ---- range to drop ----- |
812 * | -------- extent -------- |
813 */
814 if (start <= key.offset && end < extent_end) {
815 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
816
817 memcpy(&new_key, &key, sizeof(new_key));
818 new_key.offset = end;
819 btrfs_set_item_key_safe(trans, root, path, &new_key);
820
821 extent_offset += end - key.offset;
822 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
823 btrfs_set_file_extent_num_bytes(leaf, fi,
824 extent_end - end);
825 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400826 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000827 inode_sub_bytes(inode, end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000828 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400829 }
830
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000831 search_start = extent_end;
832 /*
833 * | ---- range to drop ----- |
834 * | -------- extent -------- |
835 */
836 if (start > key.offset && end >= extent_end) {
837 BUG_ON(del_nr > 0);
838 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
839
840 btrfs_set_file_extent_num_bytes(leaf, fi,
841 start - key.offset);
842 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400843 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000844 inode_sub_bytes(inode, extent_end - start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000845 if (end == extent_end)
846 break;
847
848 path->slots[0]++;
849 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400850 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000851
852 /*
853 * | ---- range to drop ----- |
854 * | ------ extent ------ |
855 */
856 if (start <= key.offset && end >= extent_end) {
857 if (del_nr == 0) {
858 del_slot = path->slots[0];
859 del_nr = 1;
860 } else {
861 BUG_ON(del_slot + del_nr != path->slots[0]);
862 del_nr++;
863 }
864
Josef Bacik5dc562c2012-08-17 13:14:17 -0400865 if (update_refs &&
866 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000867 inode_sub_bytes(inode,
868 extent_end - key.offset);
869 extent_end = ALIGN(extent_end,
870 root->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400871 } else if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000872 ret = btrfs_free_extent(trans, root,
873 disk_bytenr, num_bytes, 0,
874 root->root_key.objectid,
875 key.objectid, key.offset -
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200876 extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100877 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000878 inode_sub_bytes(inode,
879 extent_end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000880 }
881
882 if (end == extent_end)
883 break;
884
885 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
886 path->slots[0]++;
887 goto next_slot;
888 }
889
890 ret = btrfs_del_items(trans, root, path, del_slot,
891 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 if (ret) {
893 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400894 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000896
897 del_nr = 0;
898 del_slot = 0;
899
David Sterbab3b4aa72011-04-21 01:20:15 +0200900 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000901 continue;
902 }
903
904 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400905 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000906
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100907 if (!ret && del_nr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000908 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100909 if (ret)
910 btrfs_abort_transaction(trans, root, ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000911 }
912
Josef Bacik2aaa6652012-08-29 14:27:18 -0400913 if (drop_end)
Josef Bacikc3308f82012-09-14 14:51:22 -0400914 *drop_end = found ? min(end, extent_end) : end;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400915 btrfs_release_path(path);
916 return ret;
917}
918
919int btrfs_drop_extents(struct btrfs_trans_handle *trans,
920 struct btrfs_root *root, struct inode *inode, u64 start,
Josef Bacik26714852012-08-29 12:24:27 -0400921 u64 end, int drop_cache)
Josef Bacik5dc562c2012-08-17 13:14:17 -0400922{
923 struct btrfs_path *path;
924 int ret;
925
926 path = btrfs_alloc_path();
927 if (!path)
928 return -ENOMEM;
Josef Bacik2aaa6652012-08-29 14:27:18 -0400929 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
Josef Bacik26714852012-08-29 12:24:27 -0400930 drop_cache);
Chris Mason39279cc2007-06-12 06:35:45 -0400931 btrfs_free_path(path);
932 return ret;
933}
934
Yan Zhengd899e052008-10-30 14:25:28 -0400935static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000936 u64 objectid, u64 bytenr, u64 orig_offset,
937 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400938{
939 struct btrfs_file_extent_item *fi;
940 struct btrfs_key key;
941 u64 extent_end;
942
943 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
944 return 0;
945
946 btrfs_item_key_to_cpu(leaf, &key, slot);
947 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
948 return 0;
949
950 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
951 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
952 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000953 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400954 btrfs_file_extent_compression(leaf, fi) ||
955 btrfs_file_extent_encryption(leaf, fi) ||
956 btrfs_file_extent_other_encoding(leaf, fi))
957 return 0;
958
959 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
960 if ((*start && *start != key.offset) || (*end && *end != extent_end))
961 return 0;
962
963 *start = key.offset;
964 *end = extent_end;
965 return 1;
966}
967
968/*
969 * Mark extent in the range start - end as written.
970 *
971 * This changes extent type from 'pre-allocated' to 'regular'. If only
972 * part of extent is marked as written, the extent will be split into
973 * two or three.
974 */
975int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400976 struct inode *inode, u64 start, u64 end)
977{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000978 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400979 struct extent_buffer *leaf;
980 struct btrfs_path *path;
981 struct btrfs_file_extent_item *fi;
982 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000983 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400984 u64 bytenr;
985 u64 num_bytes;
986 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400988 u64 other_start;
989 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000990 u64 split;
991 int del_nr = 0;
992 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000993 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400994 int ret;
Li Zefan33345d012011-04-20 10:31:50 +0800995 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -0400996
Yan Zhengd899e052008-10-30 14:25:28 -0400997 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700998 if (!path)
999 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001000again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001001 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001002 split = start;
Li Zefan33345d012011-04-20 10:31:50 +08001003 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -04001004 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001005 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -04001006
1007 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -04001008 if (ret < 0)
1009 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001010 if (ret > 0 && path->slots[0] > 0)
1011 path->slots[0]--;
1012
1013 leaf = path->nodes[0];
1014 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001015 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
Yan Zhengd899e052008-10-30 14:25:28 -04001016 fi = btrfs_item_ptr(leaf, path->slots[0],
1017 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001018 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
1019 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -04001020 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1021 BUG_ON(key.offset > start || extent_end < end);
1022
1023 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1024 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001026 memcpy(&new_key, &key, sizeof(new_key));
1027
1028 if (start == key.offset && end < extent_end) {
1029 other_start = 0;
1030 other_end = start;
1031 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001032 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001033 &other_start, &other_end)) {
1034 new_key.offset = end;
1035 btrfs_set_item_key_safe(trans, root, path, &new_key);
1036 fi = btrfs_item_ptr(leaf, path->slots[0],
1037 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001038 btrfs_set_file_extent_generation(leaf, fi,
1039 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001040 btrfs_set_file_extent_num_bytes(leaf, fi,
1041 extent_end - end);
1042 btrfs_set_file_extent_offset(leaf, fi,
1043 end - orig_offset);
1044 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1045 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001046 btrfs_set_file_extent_generation(leaf, fi,
1047 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001048 btrfs_set_file_extent_num_bytes(leaf, fi,
1049 end - other_start);
1050 btrfs_mark_buffer_dirty(leaf);
1051 goto out;
1052 }
1053 }
1054
1055 if (start > key.offset && end == extent_end) {
1056 other_start = end;
1057 other_end = 0;
1058 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001059 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001060 &other_start, &other_end)) {
1061 fi = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_file_extent_item);
1063 btrfs_set_file_extent_num_bytes(leaf, fi,
1064 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -04001065 btrfs_set_file_extent_generation(leaf, fi,
1066 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001067 path->slots[0]++;
1068 new_key.offset = start;
1069 btrfs_set_item_key_safe(trans, root, path, &new_key);
1070
1071 fi = btrfs_item_ptr(leaf, path->slots[0],
1072 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001073 btrfs_set_file_extent_generation(leaf, fi,
1074 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001075 btrfs_set_file_extent_num_bytes(leaf, fi,
1076 other_end - start);
1077 btrfs_set_file_extent_offset(leaf, fi,
1078 start - orig_offset);
1079 btrfs_mark_buffer_dirty(leaf);
1080 goto out;
1081 }
1082 }
Yan Zhengd899e052008-10-30 14:25:28 -04001083
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001084 while (start > key.offset || end < extent_end) {
1085 if (key.offset == start)
1086 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001087
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001088 new_key.offset = split;
1089 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1090 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001091 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001092 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001093 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001094 if (ret < 0) {
1095 btrfs_abort_transaction(trans, root, ret);
1096 goto out;
1097 }
Yan Zhengd899e052008-10-30 14:25:28 -04001098
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001099 leaf = path->nodes[0];
1100 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001101 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001102 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001103 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001104 split - key.offset);
1105
1106 fi = btrfs_item_ptr(leaf, path->slots[0],
1107 struct btrfs_file_extent_item);
1108
Josef Bacik224ecce2012-08-16 16:32:06 -04001109 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001110 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1111 btrfs_set_file_extent_num_bytes(leaf, fi,
1112 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001113 btrfs_mark_buffer_dirty(leaf);
1114
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001115 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1116 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001117 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118 BUG_ON(ret); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001119
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001120 if (split == start) {
1121 key.offset = start;
1122 } else {
1123 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -04001124 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001125 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001126 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001127 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001128 }
1129
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001130 other_start = end;
1131 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001132 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001133 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001134 &other_start, &other_end)) {
1135 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001136 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001137 goto again;
1138 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001139 extent_end = other_end;
1140 del_slot = path->slots[0] + 1;
1141 del_nr++;
1142 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1143 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001144 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001145 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001146 }
1147 other_start = 0;
1148 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001149 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001150 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001151 &other_start, &other_end)) {
1152 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001153 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001154 goto again;
1155 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001156 key.offset = other_start;
1157 del_slot = path->slots[0];
1158 del_nr++;
1159 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1160 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001161 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001162 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001163 }
1164 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001165 fi = btrfs_item_ptr(leaf, path->slots[0],
1166 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001167 btrfs_set_file_extent_type(leaf, fi,
1168 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001169 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001170 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001171 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001172 fi = btrfs_item_ptr(leaf, del_slot - 1,
1173 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001174 btrfs_set_file_extent_type(leaf, fi,
1175 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001176 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001177 btrfs_set_file_extent_num_bytes(leaf, fi,
1178 extent_end - key.offset);
1179 btrfs_mark_buffer_dirty(leaf);
1180
1181 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001182 if (ret < 0) {
1183 btrfs_abort_transaction(trans, root, ret);
1184 goto out;
1185 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001186 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001187out:
Yan Zhengd899e052008-10-30 14:25:28 -04001188 btrfs_free_path(path);
1189 return 0;
1190}
1191
Chris Mason39279cc2007-06-12 06:35:45 -04001192/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001193 * on error we return an unlocked page and the error value
1194 * on success we return a locked page and 0
1195 */
Josef Bacikb63164292011-09-30 15:23:54 -04001196static int prepare_uptodate_page(struct page *page, u64 pos,
1197 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001198{
1199 int ret = 0;
1200
Josef Bacikb63164292011-09-30 15:23:54 -04001201 if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1202 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001203 ret = btrfs_readpage(NULL, page);
1204 if (ret)
1205 return ret;
1206 lock_page(page);
1207 if (!PageUptodate(page)) {
1208 unlock_page(page);
1209 return -EIO;
1210 }
1211 }
1212 return 0;
1213}
1214
1215/*
Chris Masond352ac62008-09-29 15:18:18 -04001216 * this gets pages into the page cache and locks them down, it also properly
1217 * waits for data=ordered extents to finish before allowing the pages to be
1218 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -04001219 */
Chris Masond3977122009-01-05 21:25:51 -05001220static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -05001221 struct page **pages, size_t num_pages,
1222 loff_t pos, unsigned long first_index,
Josef Bacikb63164292011-09-30 15:23:54 -04001223 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001224{
Josef Bacik2ac55d42010-02-03 19:33:23 +00001225 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001226 int i;
1227 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -05001228 struct inode *inode = fdentry(file)->d_inode;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001229 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason39279cc2007-06-12 06:35:45 -04001230 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001231 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -04001232 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001233 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -04001234
Chris Mason5f39d392007-10-15 16:14:19 -04001235 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001236 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001237
Chris Masone6dcd2d2008-07-17 12:53:50 -04001238again:
Chris Mason39279cc2007-06-12 06:35:45 -04001239 for (i = 0; i < num_pages; i++) {
Josef Bacika94733d2011-07-11 10:47:06 -04001240 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001241 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001242 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001243 faili = i - 1;
1244 err = -ENOMEM;
1245 goto fail;
1246 }
1247
1248 if (i == 0)
Josef Bacikb63164292011-09-30 15:23:54 -04001249 err = prepare_uptodate_page(pages[i], pos,
1250 force_uptodate);
Chris Masonb1bf8622011-02-28 09:52:08 -05001251 if (i == num_pages - 1)
1252 err = prepare_uptodate_page(pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001253 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001254 if (err) {
1255 page_cache_release(pages[i]);
1256 faili = i - 1;
1257 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001258 }
Chris Masonccd467d2007-06-28 15:57:36 -04001259 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001260 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001261 err = 0;
Chris Mason07627042008-02-19 11:29:24 -05001262 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04001263 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001264 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001265 start_pos, last_pos - 1, 0, &cached_state);
Chris Masond3977122009-01-05 21:25:51 -05001266 ordered = btrfs_lookup_first_ordered_extent(inode,
1267 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001268 if (ordered &&
1269 ordered->file_offset + ordered->len > start_pos &&
1270 ordered->file_offset < last_pos) {
1271 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001272 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1273 start_pos, last_pos - 1,
1274 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001275 for (i = 0; i < num_pages; i++) {
1276 unlock_page(pages[i]);
1277 page_cache_release(pages[i]);
1278 }
1279 btrfs_wait_ordered_range(inode, start_pos,
1280 last_pos - start_pos);
1281 goto again;
1282 }
1283 if (ordered)
1284 btrfs_put_ordered_extent(ordered);
1285
Josef Bacik2ac55d42010-02-03 19:33:23 +00001286 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -04001287 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001288 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1289 0, 0, &cached_state, GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001290 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1291 start_pos, last_pos - 1, &cached_state,
1292 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -05001293 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001294 for (i = 0; i < num_pages; i++) {
Wu Fengguang32c7f202011-08-08 15:19:47 -06001295 if (clear_page_dirty_for_io(pages[i]))
1296 account_page_redirty(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001297 set_page_extent_mapped(pages[i]);
1298 WARN_ON(!PageLocked(pages[i]));
1299 }
Chris Mason39279cc2007-06-12 06:35:45 -04001300 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001301fail:
1302 while (faili >= 0) {
1303 unlock_page(pages[faili]);
1304 page_cache_release(pages[faili]);
1305 faili--;
1306 }
1307 return err;
1308
Chris Mason39279cc2007-06-12 06:35:45 -04001309}
1310
Josef Bacikd0215f32011-01-25 14:57:24 -05001311static noinline ssize_t __btrfs_buffered_write(struct file *file,
1312 struct iov_iter *i,
1313 loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001314{
Josef Bacik11c65dc2010-05-23 11:07:21 -04001315 struct inode *inode = fdentry(file)->d_inode;
1316 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001317 struct page **pages = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001318 unsigned long first_index;
Josef Bacikd0215f32011-01-25 14:57:24 -05001319 size_t num_written = 0;
1320 int nrptrs;
Tsutomu Itohc9149232011-03-30 00:57:23 +00001321 int ret = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001322 bool force_page_uptodate = false;
Chris Masoncb843a62008-10-03 12:30:02 -04001323
Josef Bacikd0215f32011-01-25 14:57:24 -05001324 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
Josef Bacik11c65dc2010-05-23 11:07:21 -04001325 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1326 (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001327 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1328 nrptrs = max(nrptrs, 8);
Chris Mason8c2383c2007-06-18 09:57:58 -04001329 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001330 if (!pages)
1331 return -ENOMEM;
Chris Masonab93dbe2009-10-01 12:29:10 -04001332
Chris Mason39279cc2007-06-12 06:35:45 -04001333 first_index = pos >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001334
Josef Bacikd0215f32011-01-25 14:57:24 -05001335 while (iov_iter_count(i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001336 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacikd0215f32011-01-25 14:57:24 -05001337 size_t write_bytes = min(iov_iter_count(i),
Josef Bacik11c65dc2010-05-23 11:07:21 -04001338 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001339 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001340 size_t num_pages = (write_bytes + offset +
1341 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001342 size_t dirty_pages;
1343 size_t copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001344
Chris Mason8c2383c2007-06-18 09:57:58 -04001345 WARN_ON(num_pages > nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001346
Xin Zhong914ee292010-12-09 09:30:14 +00001347 /*
1348 * Fault pages before locking them in prepare_pages
1349 * to avoid recursive lock
1350 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001351 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001352 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001353 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001354 }
1355
1356 ret = btrfs_delalloc_reserve_space(inode,
1357 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001358 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001359 break;
Chris Mason1832a6d2007-12-21 16:27:21 -05001360
Josef Bacik4a640012011-01-25 15:10:08 -05001361 /*
1362 * This is going to setup the pages array with the number of
1363 * pages we want, so we don't really need to worry about the
1364 * contents of pages from loop to loop
1365 */
Chris Mason39279cc2007-06-12 06:35:45 -04001366 ret = prepare_pages(root, file, pages, num_pages,
Josef Bacikb63164292011-09-30 15:23:54 -04001367 pos, first_index, write_bytes,
1368 force_page_uptodate);
Josef Bacik6a632092009-02-20 11:00:09 -05001369 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001370 btrfs_delalloc_release_space(inode,
1371 num_pages << PAGE_CACHE_SHIFT);
Josef Bacikd0215f32011-01-25 14:57:24 -05001372 break;
Josef Bacik6a632092009-02-20 11:00:09 -05001373 }
Chris Mason39279cc2007-06-12 06:35:45 -04001374
Xin Zhong914ee292010-12-09 09:30:14 +00001375 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -05001376 write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001377
1378 /*
1379 * if we have trouble faulting in the pages, fall
1380 * back to one page at a time
1381 */
1382 if (copied < write_bytes)
1383 nrptrs = 1;
1384
Josef Bacikb63164292011-09-30 15:23:54 -04001385 if (copied == 0) {
1386 force_page_uptodate = true;
Chris Masonb1bf8622011-02-28 09:52:08 -05001387 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001388 } else {
1389 force_page_uptodate = false;
Chris Masonb1bf8622011-02-28 09:52:08 -05001390 dirty_pages = (copied + offset +
1391 PAGE_CACHE_SIZE - 1) >>
1392 PAGE_CACHE_SHIFT;
Josef Bacikb63164292011-09-30 15:23:54 -04001393 }
Xin Zhong914ee292010-12-09 09:30:14 +00001394
Josef Bacikd0215f32011-01-25 14:57:24 -05001395 /*
1396 * If we had a short copy we need to release the excess delaloc
1397 * bytes we reserved. We need to increment outstanding_extents
1398 * because btrfs_delalloc_release_space will decrement it, but
1399 * we still have an outstanding extent for the chunk we actually
1400 * managed to copy.
1401 */
Xin Zhong914ee292010-12-09 09:30:14 +00001402 if (num_pages > dirty_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001403 if (copied > 0) {
1404 spin_lock(&BTRFS_I(inode)->lock);
1405 BTRFS_I(inode)->outstanding_extents++;
1406 spin_unlock(&BTRFS_I(inode)->lock);
1407 }
Xin Zhong914ee292010-12-09 09:30:14 +00001408 btrfs_delalloc_release_space(inode,
1409 (num_pages - dirty_pages) <<
1410 PAGE_CACHE_SHIFT);
1411 }
1412
1413 if (copied > 0) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001414 ret = btrfs_dirty_pages(root, inode, pages,
1415 dirty_pages, pos, copied,
1416 NULL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001417 if (ret) {
1418 btrfs_delalloc_release_space(inode,
1419 dirty_pages << PAGE_CACHE_SHIFT);
1420 btrfs_drop_pages(pages, num_pages);
1421 break;
1422 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001423 }
Chris Mason39279cc2007-06-12 06:35:45 -04001424
Chris Mason39279cc2007-06-12 06:35:45 -04001425 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001426
Josef Bacikd0215f32011-01-25 14:57:24 -05001427 cond_resched();
1428
1429 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1430 dirty_pages);
1431 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
Liu Bob53d3f52012-11-14 14:34:34 +00001432 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001433
Xin Zhong914ee292010-12-09 09:30:14 +00001434 pos += copied;
1435 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001436 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001437
Chris Mason8c2383c2007-06-18 09:57:58 -04001438 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001439
1440 return num_written ? num_written : ret;
1441}
1442
1443static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1444 const struct iovec *iov,
1445 unsigned long nr_segs, loff_t pos,
1446 loff_t *ppos, size_t count, size_t ocount)
1447{
1448 struct file *file = iocb->ki_filp;
Josef Bacikd0215f32011-01-25 14:57:24 -05001449 struct iov_iter i;
1450 ssize_t written;
1451 ssize_t written_buffered;
1452 loff_t endbyte;
1453 int err;
1454
1455 written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
1456 count, ocount);
1457
Josef Bacikd0215f32011-01-25 14:57:24 -05001458 if (written < 0 || written == count)
1459 return written;
1460
1461 pos += written;
1462 count -= written;
1463 iov_iter_init(&i, iov, nr_segs, count, written);
1464 written_buffered = __btrfs_buffered_write(file, &i, pos);
1465 if (written_buffered < 0) {
1466 err = written_buffered;
1467 goto out;
1468 }
1469 endbyte = pos + written_buffered - 1;
1470 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1471 if (err)
1472 goto out;
1473 written += written_buffered;
1474 *ppos = pos + written_buffered;
1475 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1476 endbyte >> PAGE_CACHE_SHIFT);
1477out:
1478 return written ? written : err;
1479}
1480
Josef Bacik6c760c02012-11-09 10:53:21 -05001481static void update_time_for_write(struct inode *inode)
1482{
1483 struct timespec now;
1484
1485 if (IS_NOCMTIME(inode))
1486 return;
1487
1488 now = current_fs_time(inode->i_sb);
1489 if (!timespec_equal(&inode->i_mtime, &now))
1490 inode->i_mtime = now;
1491
1492 if (!timespec_equal(&inode->i_ctime, &now))
1493 inode->i_ctime = now;
1494
1495 if (IS_I_VERSION(inode))
1496 inode_inc_iversion(inode);
1497}
1498
Josef Bacikd0215f32011-01-25 14:57:24 -05001499static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1500 const struct iovec *iov,
1501 unsigned long nr_segs, loff_t pos)
1502{
1503 struct file *file = iocb->ki_filp;
1504 struct inode *inode = fdentry(file)->d_inode;
1505 struct btrfs_root *root = BTRFS_I(inode)->root;
1506 loff_t *ppos = &iocb->ki_pos;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001507 u64 start_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001508 ssize_t num_written = 0;
1509 ssize_t err = 0;
1510 size_t count, ocount;
Josef Bacikb812ce22012-11-16 13:56:32 -05001511 bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
Josef Bacikd0215f32011-01-25 14:57:24 -05001512
Jan Karab2b5ef52012-06-12 16:20:45 +02001513 sb_start_write(inode->i_sb);
Josef Bacikd0215f32011-01-25 14:57:24 -05001514
1515 mutex_lock(&inode->i_mutex);
1516
1517 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1518 if (err) {
1519 mutex_unlock(&inode->i_mutex);
1520 goto out;
1521 }
1522 count = ocount;
1523
1524 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1525 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1526 if (err) {
1527 mutex_unlock(&inode->i_mutex);
1528 goto out;
1529 }
1530
1531 if (count == 0) {
1532 mutex_unlock(&inode->i_mutex);
1533 goto out;
1534 }
1535
1536 err = file_remove_suid(file);
1537 if (err) {
1538 mutex_unlock(&inode->i_mutex);
1539 goto out;
1540 }
1541
1542 /*
1543 * If BTRFS flips readonly due to some impossible error
1544 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1545 * although we have opened a file as writable, we have
1546 * to stop this write operation to ensure FS consistency.
1547 */
Miao Xie87533c42013-01-29 10:14:48 +00001548 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
Josef Bacikd0215f32011-01-25 14:57:24 -05001549 mutex_unlock(&inode->i_mutex);
1550 err = -EROFS;
1551 goto out;
1552 }
1553
Josef Bacik6c760c02012-11-09 10:53:21 -05001554 /*
1555 * We reserve space for updating the inode when we reserve space for the
1556 * extent we are going to write, so we will enospc out there. We don't
1557 * need to start yet another transaction to update the inode as we will
1558 * update the inode when we finish writing whatever data we write.
1559 */
1560 update_time_for_write(inode);
Josef Bacikd0215f32011-01-25 14:57:24 -05001561
Miao Xie0c1a98c2011-09-11 10:52:24 -04001562 start_pos = round_down(pos, root->sectorsize);
1563 if (start_pos > i_size_read(inode)) {
1564 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1565 if (err) {
1566 mutex_unlock(&inode->i_mutex);
1567 goto out;
1568 }
1569 }
1570
Josef Bacikb812ce22012-11-16 13:56:32 -05001571 if (sync)
1572 atomic_inc(&BTRFS_I(inode)->sync_writers);
1573
Josef Bacikd0215f32011-01-25 14:57:24 -05001574 if (unlikely(file->f_flags & O_DIRECT)) {
1575 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1576 pos, ppos, count, ocount);
1577 } else {
1578 struct iov_iter i;
1579
1580 iov_iter_init(&i, iov, nr_segs, count, num_written);
1581
1582 num_written = __btrfs_buffered_write(file, &i, pos);
1583 if (num_written > 0)
1584 *ppos = pos + num_written;
1585 }
1586
1587 mutex_unlock(&inode->i_mutex);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001588
Chris Mason5a3f23d2009-03-31 13:27:11 -04001589 /*
1590 * we want to make sure fsync finds this change
1591 * but we haven't joined a transaction running right now.
1592 *
1593 * Later on, someone is sure to update the inode and get the
1594 * real transid recorded.
1595 *
1596 * We set last_trans now to the fs_info generation + 1,
1597 * this will either be one more than the running transaction
1598 * or the generation used for the next transaction if there isn't
1599 * one running right now.
Josef Bacik6c760c02012-11-09 10:53:21 -05001600 *
1601 * We also have to set last_sub_trans to the current log transid,
1602 * otherwise subsequent syncs to a file that's been synced in this
1603 * transaction will appear to have already occured.
Chris Mason5a3f23d2009-03-31 13:27:11 -04001604 */
1605 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Josef Bacik6c760c02012-11-09 10:53:21 -05001606 BTRFS_I(inode)->last_sub_trans = root->log_transid;
Josef Bacikd0215f32011-01-25 14:57:24 -05001607 if (num_written > 0 || num_written == -EIOCBQUEUED) {
1608 err = generic_write_sync(file, pos, num_written);
1609 if (err < 0 && num_written > 0)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001610 num_written = err;
1611 }
Miao Xie0a3404d2013-01-28 12:34:55 +00001612
Josef Bacikb812ce22012-11-16 13:56:32 -05001613 if (sync)
1614 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie0a3404d2013-01-28 12:34:55 +00001615out:
Jan Karab2b5ef52012-06-12 16:20:45 +02001616 sb_end_write(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04001617 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001618 return num_written ? num_written : err;
1619}
1620
Chris Masond3977122009-01-05 21:25:51 -05001621int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001622{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001623 /*
1624 * ordered_data_close is set by settattr when we are about to truncate
1625 * a file from a non-zero size to a zero size. This tries to
1626 * flush down new bytes that may have been written if the
1627 * application were using truncate to replace a file in place.
1628 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04001629 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1630 &BTRFS_I(inode)->runtime_flags)) {
Chris Mason5a3f23d2009-03-31 13:27:11 -04001631 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1632 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1633 filemap_flush(inode->i_mapping);
1634 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001635 if (filp->private_data)
1636 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001637 return 0;
1638}
1639
Chris Masond352ac62008-09-29 15:18:18 -04001640/*
1641 * fsync call for both files and directories. This logs the inode into
1642 * the tree log instead of forcing full commits whenever possible.
1643 *
1644 * It needs to call filemap_fdatawait so that all ordered extent updates are
1645 * in the metadata btree are up to date for copying to the log.
1646 *
1647 * It drops the inode mutex before doing the tree log commit. This is an
1648 * important optimization for directories because holding the mutex prevents
1649 * new operations on the dir while we write to disk.
1650 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001651int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001652{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001653 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001654 struct inode *inode = dentry->d_inode;
1655 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001656 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001657 struct btrfs_trans_handle *trans;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001658 bool full_sync = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001659
liubo1abe9b82011-03-24 11:18:59 +00001660 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04001661
Miao Xie90abccf2012-09-13 04:53:47 -06001662 /*
1663 * We write the dirty pages in the range and wait until they complete
1664 * out of the ->i_mutex. If so, we can flush the dirty pages by
Josef Bacik2ab28f32012-10-12 15:27:49 -04001665 * multi-task, and make the performance up. See
1666 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
Miao Xie90abccf2012-09-13 04:53:47 -06001667 */
Josef Bacikb812ce22012-11-16 13:56:32 -05001668 atomic_inc(&BTRFS_I(inode)->sync_writers);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001669 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1670 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1671 &BTRFS_I(inode)->runtime_flags))
1672 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
Josef Bacikb812ce22012-11-16 13:56:32 -05001673 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie90abccf2012-09-13 04:53:47 -06001674 if (ret)
1675 return ret;
1676
Josef Bacik02c24a82011-07-16 20:44:56 -04001677 mutex_lock(&inode->i_mutex);
1678
Josef Bacik0885ef52012-04-23 15:09:39 -04001679 /*
Miao Xie90abccf2012-09-13 04:53:47 -06001680 * We flush the dirty pages again to avoid some dirty pages in the
1681 * range being left.
Josef Bacik0885ef52012-04-23 15:09:39 -04001682 */
Miao Xie2ecb7922012-09-06 04:04:27 -06001683 atomic_inc(&root->log_batch);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001684 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1685 &BTRFS_I(inode)->runtime_flags);
1686 if (full_sync)
1687 btrfs_wait_ordered_range(inode, start, end - start + 1);
Miao Xie2ecb7922012-09-06 04:04:27 -06001688 atomic_inc(&root->log_batch);
Chris Mason257c62e2009-10-13 13:21:08 -04001689
Chris Mason39279cc2007-06-12 06:35:45 -04001690 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001691 * check the transaction that last modified this inode
1692 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001693 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001694 if (!BTRFS_I(inode)->last_trans) {
1695 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001696 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001697 }
Chris Masona2135012008-06-25 16:01:30 -04001698
Chris Mason257c62e2009-10-13 13:21:08 -04001699 /*
1700 * if the last transaction that changed this file was before
1701 * the current transaction, we can bail out now without any
1702 * syncing
1703 */
Josef Bacika4abeea2011-04-11 17:25:13 -04001704 smp_mb();
Josef Bacik22ee6985d2012-05-29 16:57:49 -04001705 if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1706 BTRFS_I(inode)->last_trans <=
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001707 root->fs_info->last_trans_committed) {
1708 BTRFS_I(inode)->last_trans = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001709
1710 /*
1711 * We'v had everything committed since the last time we were
1712 * modified so clear this flag in case it was set for whatever
1713 * reason, it's no longer relevant.
1714 */
1715 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1716 &BTRFS_I(inode)->runtime_flags);
Josef Bacik02c24a82011-07-16 20:44:56 -04001717 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001718 goto out;
1719 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001720
1721 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001722 * ok we haven't committed the transaction yet, lets do a commit
1723 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001724 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001725 btrfs_ioctl_trans_end(file);
1726
Yan, Zhenga22285a2010-05-16 10:48:46 -04001727 trans = btrfs_start_transaction(root, 0);
1728 if (IS_ERR(trans)) {
1729 ret = PTR_ERR(trans);
Josef Bacik02c24a82011-07-16 20:44:56 -04001730 mutex_unlock(&inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001731 goto out;
1732 }
Chris Masone02119d2008-09-05 16:13:11 -04001733
Chris Mason2cfbd502009-02-20 10:55:10 -05001734 ret = btrfs_log_dentry_safe(trans, root, dentry);
Josef Bacik02c24a82011-07-16 20:44:56 -04001735 if (ret < 0) {
1736 mutex_unlock(&inode->i_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001737 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001738 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001739
1740 /* we've logged all the items and now have a consistent
1741 * version of the file in the log. It is possible that
1742 * someone will come in and modify the file, but that's
1743 * fine because the log is consistent on disk, and we
1744 * have references to all of the file's extents
1745 *
1746 * It is possible that someone will come in and log the
1747 * file again, but that will end up using the synchronization
1748 * inside btrfs_sync_log to keep things safe.
1749 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001750 mutex_unlock(&inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001751
Chris Mason257c62e2009-10-13 13:21:08 -04001752 if (ret != BTRFS_NO_LOG_SYNC) {
1753 if (ret > 0) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04001754 /*
1755 * If we didn't already wait for ordered extents we need
1756 * to do that now.
1757 */
1758 if (!full_sync)
1759 btrfs_wait_ordered_range(inode, start,
1760 end - start + 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04001761 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001762 } else {
1763 ret = btrfs_sync_log(trans, root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001764 if (ret == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04001765 ret = btrfs_end_transaction(trans, root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001766 } else {
1767 if (!full_sync)
1768 btrfs_wait_ordered_range(inode, start,
1769 end -
1770 start + 1);
Chris Mason257c62e2009-10-13 13:21:08 -04001771 ret = btrfs_commit_transaction(trans, root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001772 }
Chris Mason257c62e2009-10-13 13:21:08 -04001773 }
1774 } else {
1775 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001776 }
Chris Mason39279cc2007-06-12 06:35:45 -04001777out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001778 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001779}
1780
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001781static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001782 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001783 .page_mkwrite = btrfs_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001784 .remap_pages = generic_file_remap_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04001785};
1786
1787static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1788{
Miao Xie058a4572010-05-20 07:21:50 +00001789 struct address_space *mapping = filp->f_mapping;
1790
1791 if (!mapping->a_ops->readpage)
1792 return -ENOEXEC;
1793
Chris Mason9ebefb182007-06-15 13:50:00 -04001794 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001795 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00001796
Chris Mason9ebefb182007-06-15 13:50:00 -04001797 return 0;
1798}
1799
Josef Bacik2aaa6652012-08-29 14:27:18 -04001800static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
1801 int slot, u64 start, u64 end)
1802{
1803 struct btrfs_file_extent_item *fi;
1804 struct btrfs_key key;
1805
1806 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1807 return 0;
1808
1809 btrfs_item_key_to_cpu(leaf, &key, slot);
1810 if (key.objectid != btrfs_ino(inode) ||
1811 key.type != BTRFS_EXTENT_DATA_KEY)
1812 return 0;
1813
1814 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1815
1816 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1817 return 0;
1818
1819 if (btrfs_file_extent_disk_bytenr(leaf, fi))
1820 return 0;
1821
1822 if (key.offset == end)
1823 return 1;
1824 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1825 return 1;
1826 return 0;
1827}
1828
1829static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
1830 struct btrfs_path *path, u64 offset, u64 end)
1831{
1832 struct btrfs_root *root = BTRFS_I(inode)->root;
1833 struct extent_buffer *leaf;
1834 struct btrfs_file_extent_item *fi;
1835 struct extent_map *hole_em;
1836 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1837 struct btrfs_key key;
1838 int ret;
1839
1840 key.objectid = btrfs_ino(inode);
1841 key.type = BTRFS_EXTENT_DATA_KEY;
1842 key.offset = offset;
1843
1844
1845 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1846 if (ret < 0)
1847 return ret;
1848 BUG_ON(!ret);
1849
1850 leaf = path->nodes[0];
1851 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
1852 u64 num_bytes;
1853
1854 path->slots[0]--;
1855 fi = btrfs_item_ptr(leaf, path->slots[0],
1856 struct btrfs_file_extent_item);
1857 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
1858 end - offset;
1859 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1860 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1861 btrfs_set_file_extent_offset(leaf, fi, 0);
1862 btrfs_mark_buffer_dirty(leaf);
1863 goto out;
1864 }
1865
1866 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
1867 u64 num_bytes;
1868
1869 path->slots[0]++;
1870 key.offset = offset;
1871 btrfs_set_item_key_safe(trans, root, path, &key);
1872 fi = btrfs_item_ptr(leaf, path->slots[0],
1873 struct btrfs_file_extent_item);
1874 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
1875 offset;
1876 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1877 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1878 btrfs_set_file_extent_offset(leaf, fi, 0);
1879 btrfs_mark_buffer_dirty(leaf);
1880 goto out;
1881 }
1882 btrfs_release_path(path);
1883
1884 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
1885 0, 0, end - offset, 0, end - offset,
1886 0, 0, 0);
1887 if (ret)
1888 return ret;
1889
1890out:
1891 btrfs_release_path(path);
1892
1893 hole_em = alloc_extent_map();
1894 if (!hole_em) {
1895 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1896 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1897 &BTRFS_I(inode)->runtime_flags);
1898 } else {
1899 hole_em->start = offset;
1900 hole_em->len = end - offset;
1901 hole_em->orig_start = offset;
1902
1903 hole_em->block_start = EXTENT_MAP_HOLE;
1904 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05001905 hole_em->orig_block_len = 0;
Josef Bacik2aaa6652012-08-29 14:27:18 -04001906 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
1907 hole_em->compress_type = BTRFS_COMPRESS_NONE;
1908 hole_em->generation = trans->transid;
1909
1910 do {
1911 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1912 write_lock(&em_tree->lock);
1913 ret = add_extent_mapping(em_tree, hole_em);
1914 if (!ret)
1915 list_move(&hole_em->list,
1916 &em_tree->modified_extents);
1917 write_unlock(&em_tree->lock);
1918 } while (ret == -EEXIST);
1919 free_extent_map(hole_em);
1920 if (ret)
1921 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1922 &BTRFS_I(inode)->runtime_flags);
1923 }
1924
1925 return 0;
1926}
1927
1928static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1929{
1930 struct btrfs_root *root = BTRFS_I(inode)->root;
1931 struct extent_state *cached_state = NULL;
1932 struct btrfs_path *path;
1933 struct btrfs_block_rsv *rsv;
1934 struct btrfs_trans_handle *trans;
Miao Xie00612802012-12-05 10:54:12 +00001935 u64 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize);
1936 u64 lockend = round_down(offset + len,
1937 BTRFS_I(inode)->root->sectorsize) - 1;
Josef Bacik2aaa6652012-08-29 14:27:18 -04001938 u64 cur_offset = lockstart;
1939 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
1940 u64 drop_end;
Josef Bacik2aaa6652012-08-29 14:27:18 -04001941 int ret = 0;
1942 int err = 0;
Miao Xie6347b3c2012-12-05 10:53:45 +00001943 bool same_page = ((offset >> PAGE_CACHE_SHIFT) ==
1944 ((offset + len - 1) >> PAGE_CACHE_SHIFT));
Josef Bacik2aaa6652012-08-29 14:27:18 -04001945
1946 btrfs_wait_ordered_range(inode, offset, len);
1947
1948 mutex_lock(&inode->i_mutex);
Miao Xie7426cc02012-12-05 10:54:52 +00001949 /*
1950 * We needn't truncate any page which is beyond the end of the file
1951 * because we are sure there is no data there.
1952 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04001953 /*
1954 * Only do this if we are in the same page and we aren't doing the
1955 * entire page.
1956 */
1957 if (same_page && len < PAGE_CACHE_SIZE) {
Miao Xie7426cc02012-12-05 10:54:52 +00001958 if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE))
1959 ret = btrfs_truncate_page(inode, offset, len, 0);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001960 mutex_unlock(&inode->i_mutex);
1961 return ret;
1962 }
1963
1964 /* zero back part of the first page */
Miao Xie7426cc02012-12-05 10:54:52 +00001965 if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
1966 ret = btrfs_truncate_page(inode, offset, 0, 0);
1967 if (ret) {
1968 mutex_unlock(&inode->i_mutex);
1969 return ret;
1970 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04001971 }
1972
1973 /* zero the front end of the last page */
Miao Xie00612802012-12-05 10:54:12 +00001974 if (offset + len < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
1975 ret = btrfs_truncate_page(inode, offset + len, 0, 1);
1976 if (ret) {
1977 mutex_unlock(&inode->i_mutex);
1978 return ret;
1979 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04001980 }
1981
1982 if (lockend < lockstart) {
1983 mutex_unlock(&inode->i_mutex);
1984 return 0;
1985 }
1986
1987 while (1) {
1988 struct btrfs_ordered_extent *ordered;
1989
1990 truncate_pagecache_range(inode, lockstart, lockend);
1991
1992 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1993 0, &cached_state);
1994 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
1995
1996 /*
1997 * We need to make sure we have no ordered extents in this range
1998 * and nobody raced in and read a page in this range, if we did
1999 * we need to try again.
2000 */
2001 if ((!ordered ||
2002 (ordered->file_offset + ordered->len < lockstart ||
2003 ordered->file_offset > lockend)) &&
2004 !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
2005 lockend, EXTENT_UPTODATE, 0,
2006 cached_state)) {
2007 if (ordered)
2008 btrfs_put_ordered_extent(ordered);
2009 break;
2010 }
2011 if (ordered)
2012 btrfs_put_ordered_extent(ordered);
2013 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2014 lockend, &cached_state, GFP_NOFS);
2015 btrfs_wait_ordered_range(inode, lockstart,
2016 lockend - lockstart + 1);
2017 }
2018
2019 path = btrfs_alloc_path();
2020 if (!path) {
2021 ret = -ENOMEM;
2022 goto out;
2023 }
2024
Miao Xie66d8f3d2012-09-06 04:02:28 -06002025 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002026 if (!rsv) {
2027 ret = -ENOMEM;
2028 goto out_free;
2029 }
2030 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
2031 rsv->failfast = 1;
2032
2033 /*
2034 * 1 - update the inode
2035 * 1 - removing the extents in the range
2036 * 1 - adding the hole extent
2037 */
2038 trans = btrfs_start_transaction(root, 3);
2039 if (IS_ERR(trans)) {
2040 err = PTR_ERR(trans);
2041 goto out_free;
2042 }
2043
2044 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
2045 min_size);
2046 BUG_ON(ret);
2047 trans->block_rsv = rsv;
2048
2049 while (cur_offset < lockend) {
2050 ret = __btrfs_drop_extents(trans, root, inode, path,
2051 cur_offset, lockend + 1,
2052 &drop_end, 1);
2053 if (ret != -ENOSPC)
2054 break;
2055
2056 trans->block_rsv = &root->fs_info->trans_block_rsv;
2057
2058 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2059 if (ret) {
2060 err = ret;
2061 break;
2062 }
2063
2064 cur_offset = drop_end;
2065
2066 ret = btrfs_update_inode(trans, root, inode);
2067 if (ret) {
2068 err = ret;
2069 break;
2070 }
2071
Josef Bacik2aaa6652012-08-29 14:27:18 -04002072 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002073 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002074
2075 trans = btrfs_start_transaction(root, 3);
2076 if (IS_ERR(trans)) {
2077 ret = PTR_ERR(trans);
2078 trans = NULL;
2079 break;
2080 }
2081
2082 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
2083 rsv, min_size);
2084 BUG_ON(ret); /* shouldn't happen */
2085 trans->block_rsv = rsv;
2086 }
2087
2088 if (ret) {
2089 err = ret;
2090 goto out_trans;
2091 }
2092
2093 trans->block_rsv = &root->fs_info->trans_block_rsv;
2094 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2095 if (ret) {
2096 err = ret;
2097 goto out_trans;
2098 }
2099
2100out_trans:
2101 if (!trans)
2102 goto out_free;
2103
Tsutomu Itohe1f57902012-11-08 04:47:33 +00002104 inode_inc_iversion(inode);
2105 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2106
Josef Bacik2aaa6652012-08-29 14:27:18 -04002107 trans->block_rsv = &root->fs_info->trans_block_rsv;
2108 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002109 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002110 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002111out_free:
2112 btrfs_free_path(path);
2113 btrfs_free_block_rsv(root, rsv);
2114out:
2115 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2116 &cached_state, GFP_NOFS);
2117 mutex_unlock(&inode->i_mutex);
2118 if (ret && !err)
2119 err = ret;
2120 return err;
2121}
2122
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002123static long btrfs_fallocate(struct file *file, int mode,
2124 loff_t offset, loff_t len)
2125{
2126 struct inode *inode = file->f_path.dentry->d_inode;
2127 struct extent_state *cached_state = NULL;
2128 u64 cur_offset;
2129 u64 last_byte;
2130 u64 alloc_start;
2131 u64 alloc_end;
2132 u64 alloc_hint = 0;
2133 u64 locked_end;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002134 struct extent_map *em;
Miao Xie797f4272012-11-28 10:28:07 +00002135 int blocksize = BTRFS_I(inode)->root->sectorsize;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002136 int ret;
2137
Miao Xie797f4272012-11-28 10:28:07 +00002138 alloc_start = round_down(offset, blocksize);
2139 alloc_end = round_up(offset + len, blocksize);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002140
Josef Bacik2aaa6652012-08-29 14:27:18 -04002141 /* Make sure we aren't being give some crap mode */
2142 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002143 return -EOPNOTSUPP;
2144
Josef Bacik2aaa6652012-08-29 14:27:18 -04002145 if (mode & FALLOC_FL_PUNCH_HOLE)
2146 return btrfs_punch_hole(inode, offset, len);
2147
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002148 /*
Chris Masond98456f2012-01-31 20:27:41 -05002149 * Make sure we have enough space before we do the
2150 * allocation.
2151 */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002152 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Chris Masond98456f2012-01-31 20:27:41 -05002153 if (ret)
2154 return ret;
2155
2156 /*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002157 * wait for ordered IO before we have any locks. We'll loop again
2158 * below with the locks held.
2159 */
2160 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2161
2162 mutex_lock(&inode->i_mutex);
2163 ret = inode_newsize_ok(inode, alloc_end);
2164 if (ret)
2165 goto out;
2166
2167 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002168 ret = btrfs_cont_expand(inode, i_size_read(inode),
2169 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002170 if (ret)
2171 goto out;
2172 }
2173
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002174 locked_end = alloc_end - 1;
2175 while (1) {
2176 struct btrfs_ordered_extent *ordered;
2177
2178 /* the extent lock is ordered inside the running
2179 * transaction
2180 */
2181 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002182 locked_end, 0, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002183 ordered = btrfs_lookup_first_ordered_extent(inode,
2184 alloc_end - 1);
2185 if (ordered &&
2186 ordered->file_offset + ordered->len > alloc_start &&
2187 ordered->file_offset < alloc_end) {
2188 btrfs_put_ordered_extent(ordered);
2189 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2190 alloc_start, locked_end,
2191 &cached_state, GFP_NOFS);
2192 /*
2193 * we can't wait on the range with the transaction
2194 * running or with the extent lock held
2195 */
2196 btrfs_wait_ordered_range(inode, alloc_start,
2197 alloc_end - alloc_start);
2198 } else {
2199 if (ordered)
2200 btrfs_put_ordered_extent(ordered);
2201 break;
2202 }
2203 }
2204
2205 cur_offset = alloc_start;
2206 while (1) {
Josef Bacikf1e490a2011-08-18 10:36:39 -04002207 u64 actual_end;
2208
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002209 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2210 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002211 if (IS_ERR_OR_NULL(em)) {
2212 if (!em)
2213 ret = -ENOMEM;
2214 else
2215 ret = PTR_ERR(em);
2216 break;
2217 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002218 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002219 actual_end = min_t(u64, extent_map_end(em), offset + len);
Miao Xie797f4272012-11-28 10:28:07 +00002220 last_byte = ALIGN(last_byte, blocksize);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002221
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002222 if (em->block_start == EXTENT_MAP_HOLE ||
2223 (cur_offset >= inode->i_size &&
2224 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2225 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2226 last_byte - cur_offset,
2227 1 << inode->i_blkbits,
2228 offset + len,
2229 &alloc_hint);
Josef Bacik1b9c3322011-08-17 10:19:52 -04002230
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002231 if (ret < 0) {
2232 free_extent_map(em);
2233 break;
2234 }
Josef Bacikf1e490a2011-08-18 10:36:39 -04002235 } else if (actual_end > inode->i_size &&
2236 !(mode & FALLOC_FL_KEEP_SIZE)) {
2237 /*
2238 * We didn't need to allocate any more space, but we
2239 * still extended the size of the file so we need to
2240 * update i_size.
2241 */
2242 inode->i_ctime = CURRENT_TIME;
2243 i_size_write(inode, actual_end);
2244 btrfs_ordered_update_i_size(inode, actual_end, NULL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002245 }
2246 free_extent_map(em);
2247
2248 cur_offset = last_byte;
2249 if (cur_offset >= alloc_end) {
2250 ret = 0;
2251 break;
2252 }
2253 }
2254 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2255 &cached_state, GFP_NOFS);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002256out:
2257 mutex_unlock(&inode->i_mutex);
Chris Masond98456f2012-01-31 20:27:41 -05002258 /* Let go of our reservation. */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002259 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002260 return ret;
2261}
2262
Josef Bacikb2675152011-07-18 13:21:36 -04002263static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
2264{
2265 struct btrfs_root *root = BTRFS_I(inode)->root;
2266 struct extent_map *em;
2267 struct extent_state *cached_state = NULL;
2268 u64 lockstart = *offset;
2269 u64 lockend = i_size_read(inode);
2270 u64 start = *offset;
2271 u64 orig_start = *offset;
2272 u64 len = i_size_read(inode);
2273 u64 last_end = 0;
2274 int ret = 0;
2275
2276 lockend = max_t(u64, root->sectorsize, lockend);
2277 if (lockend <= lockstart)
2278 lockend = lockstart + root->sectorsize;
2279
Liu Bo1214b532013-01-07 03:53:08 +00002280 lockend--;
Josef Bacikb2675152011-07-18 13:21:36 -04002281 len = lockend - lockstart + 1;
2282
2283 len = max_t(u64, len, root->sectorsize);
2284 if (inode->i_size == 0)
2285 return -ENXIO;
2286
2287 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002288 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002289
2290 /*
2291 * Delalloc is such a pain. If we have a hole and we have pending
2292 * delalloc for a portion of the hole we will get back a hole that
2293 * exists for the entire range since it hasn't been actually written
2294 * yet. So to take care of this case we need to look for an extent just
2295 * before the position we want in case there is outstanding delalloc
2296 * going on here.
2297 */
2298 if (origin == SEEK_HOLE && start != 0) {
2299 if (start <= root->sectorsize)
2300 em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
2301 root->sectorsize, 0);
2302 else
2303 em = btrfs_get_extent_fiemap(inode, NULL, 0,
2304 start - root->sectorsize,
2305 root->sectorsize, 0);
2306 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002307 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002308 goto out;
2309 }
2310 last_end = em->start + em->len;
2311 if (em->block_start == EXTENT_MAP_DELALLOC)
2312 last_end = min_t(u64, last_end, inode->i_size);
2313 free_extent_map(em);
2314 }
2315
2316 while (1) {
2317 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2318 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002319 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002320 break;
2321 }
2322
2323 if (em->block_start == EXTENT_MAP_HOLE) {
2324 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2325 if (last_end <= orig_start) {
2326 free_extent_map(em);
2327 ret = -ENXIO;
2328 break;
2329 }
2330 }
2331
2332 if (origin == SEEK_HOLE) {
2333 *offset = start;
2334 free_extent_map(em);
2335 break;
2336 }
2337 } else {
2338 if (origin == SEEK_DATA) {
2339 if (em->block_start == EXTENT_MAP_DELALLOC) {
2340 if (start >= inode->i_size) {
2341 free_extent_map(em);
2342 ret = -ENXIO;
2343 break;
2344 }
2345 }
2346
Liu Bof9e4fb52013-01-07 10:10:12 +00002347 if (!test_bit(EXTENT_FLAG_PREALLOC,
2348 &em->flags)) {
2349 *offset = start;
2350 free_extent_map(em);
2351 break;
2352 }
Josef Bacikb2675152011-07-18 13:21:36 -04002353 }
2354 }
2355
2356 start = em->start + em->len;
2357 last_end = em->start + em->len;
2358
2359 if (em->block_start == EXTENT_MAP_DELALLOC)
2360 last_end = min_t(u64, last_end, inode->i_size);
2361
2362 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2363 free_extent_map(em);
2364 ret = -ENXIO;
2365 break;
2366 }
2367 free_extent_map(em);
2368 cond_resched();
2369 }
2370 if (!ret)
2371 *offset = min(*offset, inode->i_size);
2372out:
2373 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2374 &cached_state, GFP_NOFS);
2375 return ret;
2376}
2377
2378static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
2379{
2380 struct inode *inode = file->f_mapping->host;
2381 int ret;
2382
2383 mutex_lock(&inode->i_mutex);
2384 switch (origin) {
2385 case SEEK_END:
2386 case SEEK_CUR:
Andi Kleenef3d0fd2011-09-15 16:06:48 -07002387 offset = generic_file_llseek(file, offset, origin);
Josef Bacikb2675152011-07-18 13:21:36 -04002388 goto out;
2389 case SEEK_DATA:
2390 case SEEK_HOLE:
Jeff Liu48802c82011-09-18 10:34:02 -04002391 if (offset >= i_size_read(inode)) {
2392 mutex_unlock(&inode->i_mutex);
2393 return -ENXIO;
2394 }
2395
Josef Bacikb2675152011-07-18 13:21:36 -04002396 ret = find_desired_extent(inode, &offset, origin);
2397 if (ret) {
2398 mutex_unlock(&inode->i_mutex);
2399 return ret;
2400 }
2401 }
2402
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002403 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
Jeff Liu48802c82011-09-18 10:34:02 -04002404 offset = -EINVAL;
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002405 goto out;
2406 }
2407 if (offset > inode->i_sb->s_maxbytes) {
Jeff Liu48802c82011-09-18 10:34:02 -04002408 offset = -EINVAL;
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002409 goto out;
2410 }
Josef Bacikb2675152011-07-18 13:21:36 -04002411
2412 /* Special lock needed here? */
2413 if (offset != file->f_pos) {
2414 file->f_pos = offset;
2415 file->f_version = 0;
2416 }
2417out:
2418 mutex_unlock(&inode->i_mutex);
2419 return offset;
2420}
2421
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002422const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04002423 .llseek = btrfs_file_llseek,
Chris Mason39279cc2007-06-12 06:35:45 -04002424 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00002425 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002426 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05002427 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04002428 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002429 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04002430 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04002431 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002432 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002433 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002434 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002435#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002436 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002437#endif
2438};
Miao Xie9247f312012-11-26 09:24:43 +00002439
2440void btrfs_auto_defrag_exit(void)
2441{
2442 if (btrfs_inode_defrag_cachep)
2443 kmem_cache_destroy(btrfs_inode_defrag_cachep);
2444}
2445
2446int btrfs_auto_defrag_init(void)
2447{
2448 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
2449 sizeof(struct inode_defrag), 0,
2450 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2451 NULL);
2452 if (!btrfs_inode_defrag_cachep)
2453 return -ENOMEM;
2454
2455 return 0;
2456}