blob: a005fe2c072ad0751254adba0fa4e04db10cc996 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070027#include <linux/aio.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010028#include <linux/falloc.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000034#include <linux/btrfs.h>
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "print-tree.h"
Chris Masone02119d2008-09-05 16:13:11 -040040#include "tree-log.h"
41#include "locking.h"
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -040042#include "compat.h"
Josef Bacik2aaa6652012-08-29 14:27:18 -040043#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044
Miao Xie9247f312012-11-26 09:24:43 +000045static struct kmem_cache *btrfs_inode_defrag_cachep;
Chris Mason4cb53002011-05-24 15:35:30 -040046/*
47 * when auto defrag is enabled we
48 * queue up these defrag structs to remember which
49 * inodes need defragging passes
50 */
51struct inode_defrag {
52 struct rb_node rb_node;
53 /* objectid */
54 u64 ino;
55 /*
56 * transid where the defrag was added, we search for
57 * extents newer than this
58 */
59 u64 transid;
60
61 /* root objectid */
62 u64 root;
63
64 /* last offset we were able to defrag */
65 u64 last_offset;
66
67 /* if we've wrapped around back to zero once already */
68 int cycled;
69};
70
Miao Xie762f2262012-05-24 18:58:27 +080071static int __compare_inode_defrag(struct inode_defrag *defrag1,
72 struct inode_defrag *defrag2)
73{
74 if (defrag1->root > defrag2->root)
75 return 1;
76 else if (defrag1->root < defrag2->root)
77 return -1;
78 else if (defrag1->ino > defrag2->ino)
79 return 1;
80 else if (defrag1->ino < defrag2->ino)
81 return -1;
82 else
83 return 0;
84}
85
Chris Mason4cb53002011-05-24 15:35:30 -040086/* pop a record for an inode into the defrag tree. The lock
87 * must be held already
88 *
89 * If you're inserting a record for an older transid than an
90 * existing record, the transid already in the tree is lowered
91 *
92 * If an existing record is found the defrag item you
93 * pass in is freed
94 */
Miao Xie8ddc4732012-11-26 09:25:38 +000095static int __btrfs_add_inode_defrag(struct inode *inode,
Chris Mason4cb53002011-05-24 15:35:30 -040096 struct inode_defrag *defrag)
97{
98 struct btrfs_root *root = BTRFS_I(inode)->root;
99 struct inode_defrag *entry;
100 struct rb_node **p;
101 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800102 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400103
104 p = &root->fs_info->defrag_inodes.rb_node;
105 while (*p) {
106 parent = *p;
107 entry = rb_entry(parent, struct inode_defrag, rb_node);
108
Miao Xie762f2262012-05-24 18:58:27 +0800109 ret = __compare_inode_defrag(defrag, entry);
110 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400111 p = &parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800112 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400113 p = &parent->rb_right;
114 else {
115 /* if we're reinserting an entry for
116 * an old defrag run, make sure to
117 * lower the transid of our existing record
118 */
119 if (defrag->transid < entry->transid)
120 entry->transid = defrag->transid;
121 if (defrag->last_offset > entry->last_offset)
122 entry->last_offset = defrag->last_offset;
Miao Xie8ddc4732012-11-26 09:25:38 +0000123 return -EEXIST;
Chris Mason4cb53002011-05-24 15:35:30 -0400124 }
125 }
Josef Bacik72ac3c02012-05-23 14:13:11 -0400126 set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
Chris Mason4cb53002011-05-24 15:35:30 -0400127 rb_link_node(&defrag->rb_node, parent, p);
128 rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
Miao Xie8ddc4732012-11-26 09:25:38 +0000129 return 0;
130}
Chris Mason4cb53002011-05-24 15:35:30 -0400131
Miao Xie8ddc4732012-11-26 09:25:38 +0000132static inline int __need_auto_defrag(struct btrfs_root *root)
133{
134 if (!btrfs_test_opt(root, AUTO_DEFRAG))
135 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400136
Miao Xie8ddc4732012-11-26 09:25:38 +0000137 if (btrfs_fs_closing(root->fs_info))
138 return 0;
139
140 return 1;
Chris Mason4cb53002011-05-24 15:35:30 -0400141}
142
143/*
144 * insert a defrag record for this inode if auto defrag is
145 * enabled
146 */
147int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
148 struct inode *inode)
149{
150 struct btrfs_root *root = BTRFS_I(inode)->root;
151 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400152 u64 transid;
Miao Xie8ddc4732012-11-26 09:25:38 +0000153 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400154
Miao Xie8ddc4732012-11-26 09:25:38 +0000155 if (!__need_auto_defrag(root))
Chris Mason4cb53002011-05-24 15:35:30 -0400156 return 0;
157
Josef Bacik72ac3c02012-05-23 14:13:11 -0400158 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
Chris Mason4cb53002011-05-24 15:35:30 -0400159 return 0;
160
161 if (trans)
162 transid = trans->transid;
163 else
164 transid = BTRFS_I(inode)->root->last_trans;
165
Miao Xie9247f312012-11-26 09:24:43 +0000166 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -0400167 if (!defrag)
168 return -ENOMEM;
169
David Sterbaa4689d22011-05-31 17:08:14 +0000170 defrag->ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400171 defrag->transid = transid;
172 defrag->root = root->root_key.objectid;
173
174 spin_lock(&root->fs_info->defrag_inodes_lock);
Miao Xie8ddc4732012-11-26 09:25:38 +0000175 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
176 /*
177 * If we set IN_DEFRAG flag and evict the inode from memory,
178 * and then re-read this inode, this new inode doesn't have
179 * IN_DEFRAG flag. At the case, we may find the existed defrag.
180 */
181 ret = __btrfs_add_inode_defrag(inode, defrag);
182 if (ret)
183 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
184 } else {
Miao Xie9247f312012-11-26 09:24:43 +0000185 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
Miao Xie8ddc4732012-11-26 09:25:38 +0000186 }
Chris Mason4cb53002011-05-24 15:35:30 -0400187 spin_unlock(&root->fs_info->defrag_inodes_lock);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000188 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400189}
190
191/*
Miao Xie8ddc4732012-11-26 09:25:38 +0000192 * Requeue the defrag object. If there is a defrag object that points to
193 * the same inode in the tree, we will merge them together (by
194 * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
Chris Mason4cb53002011-05-24 15:35:30 -0400195 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000196static void btrfs_requeue_inode_defrag(struct inode *inode,
197 struct inode_defrag *defrag)
Miao Xie8ddc4732012-11-26 09:25:38 +0000198{
199 struct btrfs_root *root = BTRFS_I(inode)->root;
200 int ret;
201
202 if (!__need_auto_defrag(root))
203 goto out;
204
205 /*
206 * Here we don't check the IN_DEFRAG flag, because we need merge
207 * them together.
208 */
209 spin_lock(&root->fs_info->defrag_inodes_lock);
210 ret = __btrfs_add_inode_defrag(inode, defrag);
211 spin_unlock(&root->fs_info->defrag_inodes_lock);
212 if (ret)
213 goto out;
214 return;
215out:
216 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
217}
218
219/*
Miao Xie26176e72012-11-26 09:26:20 +0000220 * pick the defragable inode that we want, if it doesn't exist, we will get
221 * the next one.
Chris Mason4cb53002011-05-24 15:35:30 -0400222 */
Miao Xie26176e72012-11-26 09:26:20 +0000223static struct inode_defrag *
224btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400225{
226 struct inode_defrag *entry = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800227 struct inode_defrag tmp;
Chris Mason4cb53002011-05-24 15:35:30 -0400228 struct rb_node *p;
229 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800230 int ret;
231
232 tmp.ino = ino;
233 tmp.root = root;
Chris Mason4cb53002011-05-24 15:35:30 -0400234
Miao Xie26176e72012-11-26 09:26:20 +0000235 spin_lock(&fs_info->defrag_inodes_lock);
236 p = fs_info->defrag_inodes.rb_node;
Chris Mason4cb53002011-05-24 15:35:30 -0400237 while (p) {
238 parent = p;
239 entry = rb_entry(parent, struct inode_defrag, rb_node);
240
Miao Xie762f2262012-05-24 18:58:27 +0800241 ret = __compare_inode_defrag(&tmp, entry);
242 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400243 p = parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800244 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400245 p = parent->rb_right;
246 else
Miao Xie26176e72012-11-26 09:26:20 +0000247 goto out;
Chris Mason4cb53002011-05-24 15:35:30 -0400248 }
249
Miao Xie26176e72012-11-26 09:26:20 +0000250 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
251 parent = rb_next(parent);
252 if (parent)
Chris Mason4cb53002011-05-24 15:35:30 -0400253 entry = rb_entry(parent, struct inode_defrag, rb_node);
Miao Xie26176e72012-11-26 09:26:20 +0000254 else
255 entry = NULL;
Chris Mason4cb53002011-05-24 15:35:30 -0400256 }
Miao Xie26176e72012-11-26 09:26:20 +0000257out:
258 if (entry)
259 rb_erase(parent, &fs_info->defrag_inodes);
260 spin_unlock(&fs_info->defrag_inodes_lock);
261 return entry;
262}
263
264void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
265{
266 struct inode_defrag *defrag;
267 struct rb_node *node;
268
269 spin_lock(&fs_info->defrag_inodes_lock);
270 node = rb_first(&fs_info->defrag_inodes);
271 while (node) {
272 rb_erase(node, &fs_info->defrag_inodes);
273 defrag = rb_entry(node, struct inode_defrag, rb_node);
274 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
275
276 if (need_resched()) {
277 spin_unlock(&fs_info->defrag_inodes_lock);
278 cond_resched();
279 spin_lock(&fs_info->defrag_inodes_lock);
280 }
281
282 node = rb_first(&fs_info->defrag_inodes);
283 }
284 spin_unlock(&fs_info->defrag_inodes_lock);
285}
286
287#define BTRFS_DEFRAG_BATCH 1024
288
289static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
290 struct inode_defrag *defrag)
291{
292 struct btrfs_root *inode_root;
293 struct inode *inode;
294 struct btrfs_key key;
295 struct btrfs_ioctl_defrag_range_args range;
296 int num_defrag;
Liu Bo6f1c3602013-01-29 03:22:10 +0000297 int index;
298 int ret;
Miao Xie26176e72012-11-26 09:26:20 +0000299
300 /* get the inode */
301 key.objectid = defrag->root;
302 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
303 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000304
305 index = srcu_read_lock(&fs_info->subvol_srcu);
306
Miao Xie26176e72012-11-26 09:26:20 +0000307 inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
308 if (IS_ERR(inode_root)) {
Liu Bo6f1c3602013-01-29 03:22:10 +0000309 ret = PTR_ERR(inode_root);
310 goto cleanup;
311 }
Miao Xie26176e72012-11-26 09:26:20 +0000312
313 key.objectid = defrag->ino;
314 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
315 key.offset = 0;
316 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
317 if (IS_ERR(inode)) {
Liu Bo6f1c3602013-01-29 03:22:10 +0000318 ret = PTR_ERR(inode);
319 goto cleanup;
Miao Xie26176e72012-11-26 09:26:20 +0000320 }
Liu Bo6f1c3602013-01-29 03:22:10 +0000321 srcu_read_unlock(&fs_info->subvol_srcu, index);
Miao Xie26176e72012-11-26 09:26:20 +0000322
323 /* do a chunk of defrag */
324 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
325 memset(&range, 0, sizeof(range));
326 range.len = (u64)-1;
327 range.start = defrag->last_offset;
Miao Xieb66f00d2012-11-26 09:27:29 +0000328
329 sb_start_write(fs_info->sb);
Miao Xie26176e72012-11-26 09:26:20 +0000330 num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
331 BTRFS_DEFRAG_BATCH);
Miao Xieb66f00d2012-11-26 09:27:29 +0000332 sb_end_write(fs_info->sb);
Miao Xie26176e72012-11-26 09:26:20 +0000333 /*
334 * if we filled the whole defrag batch, there
335 * must be more work to do. Queue this defrag
336 * again
337 */
338 if (num_defrag == BTRFS_DEFRAG_BATCH) {
339 defrag->last_offset = range.start;
340 btrfs_requeue_inode_defrag(inode, defrag);
341 } else if (defrag->last_offset && !defrag->cycled) {
342 /*
343 * we didn't fill our defrag batch, but
344 * we didn't start at zero. Make sure we loop
345 * around to the start of the file.
346 */
347 defrag->last_offset = 0;
348 defrag->cycled = 1;
349 btrfs_requeue_inode_defrag(inode, defrag);
350 } else {
351 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
352 }
353
354 iput(inode);
355 return 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000356cleanup:
357 srcu_read_unlock(&fs_info->subvol_srcu, index);
358 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
359 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400360}
361
362/*
363 * run through the list of inodes in the FS that need
364 * defragging
365 */
366int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
367{
368 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400369 u64 first_ino = 0;
Miao Xie762f2262012-05-24 18:58:27 +0800370 u64 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400371
372 atomic_inc(&fs_info->defrag_running);
Chris Mason4cb53002011-05-24 15:35:30 -0400373 while(1) {
Miao Xiedc81cdc2013-02-20 23:32:52 -0700374 /* Pause the auto defragger. */
375 if (test_bit(BTRFS_FS_STATE_REMOUNTING,
376 &fs_info->fs_state))
377 break;
378
Miao Xie26176e72012-11-26 09:26:20 +0000379 if (!__need_auto_defrag(fs_info->tree_root))
380 break;
Chris Mason4cb53002011-05-24 15:35:30 -0400381
382 /* find an inode to defrag */
Miao Xie26176e72012-11-26 09:26:20 +0000383 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
384 first_ino);
Chris Mason4cb53002011-05-24 15:35:30 -0400385 if (!defrag) {
Miao Xie26176e72012-11-26 09:26:20 +0000386 if (root_objectid || first_ino) {
Miao Xie762f2262012-05-24 18:58:27 +0800387 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400388 first_ino = 0;
389 continue;
390 } else {
391 break;
392 }
393 }
394
Chris Mason4cb53002011-05-24 15:35:30 -0400395 first_ino = defrag->ino + 1;
Miao Xie762f2262012-05-24 18:58:27 +0800396 root_objectid = defrag->root;
Chris Mason4cb53002011-05-24 15:35:30 -0400397
Miao Xie26176e72012-11-26 09:26:20 +0000398 __btrfs_run_defrag_inode(fs_info, defrag);
Chris Mason4cb53002011-05-24 15:35:30 -0400399 }
Chris Mason4cb53002011-05-24 15:35:30 -0400400 atomic_dec(&fs_info->defrag_running);
401
402 /*
403 * during unmount, we use the transaction_wait queue to
404 * wait for the defragger to stop
405 */
406 wake_up(&fs_info->transaction_wait);
407 return 0;
408}
Chris Mason39279cc2007-06-12 06:35:45 -0400409
Chris Masond352ac62008-09-29 15:18:18 -0400410/* simple helper to fault in pages and copy. This should go away
411 * and be replaced with calls into generic code.
412 */
Chris Masond3977122009-01-05 21:25:51 -0500413static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -0500414 size_t write_bytes,
Chris Masona1b32a52008-09-05 16:09:51 -0400415 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -0400416 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -0400417{
Xin Zhong914ee292010-12-09 09:30:14 +0000418 size_t copied = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -0500419 size_t total_copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400420 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400421 int offset = pos & (PAGE_CACHE_SIZE - 1);
422
Josef Bacik11c65dc2010-05-23 11:07:21 -0400423 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400424 size_t count = min_t(size_t,
425 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400426 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +0000427 /*
428 * Copy data from userspace to the current page
429 *
430 * Disable pagefault to avoid recursive lock since
431 * the pages are already locked
432 */
433 pagefault_disable();
434 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
435 pagefault_enable();
Josef Bacik11c65dc2010-05-23 11:07:21 -0400436
Chris Mason39279cc2007-06-12 06:35:45 -0400437 /* Flush processor's dcache for this page */
438 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -0500439
440 /*
441 * if we get a partial write, we can end up with
442 * partially up to date pages. These add
443 * a lot of complexity, so make sure they don't
444 * happen by forcing this copy to be retried.
445 *
446 * The rest of the btrfs_file_write code will fall
447 * back to page at a time copies after we return 0.
448 */
449 if (!PageUptodate(page) && copied < count)
450 copied = 0;
451
Josef Bacik11c65dc2010-05-23 11:07:21 -0400452 iov_iter_advance(i, copied);
453 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +0000454 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400455
Xin Zhong914ee292010-12-09 09:30:14 +0000456 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik9f570b82011-01-25 12:42:37 -0500457 if (unlikely(copied == 0))
Xin Zhong914ee292010-12-09 09:30:14 +0000458 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400459
460 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
461 offset += copied;
462 } else {
463 pg++;
464 offset = 0;
465 }
Chris Mason39279cc2007-06-12 06:35:45 -0400466 }
Xin Zhong914ee292010-12-09 09:30:14 +0000467 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400468}
469
Chris Masond352ac62008-09-29 15:18:18 -0400470/*
471 * unlocks pages after btrfs_file_write is done with them
472 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000473static void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400474{
475 size_t i;
476 for (i = 0; i < num_pages; i++) {
Chris Masond352ac62008-09-29 15:18:18 -0400477 /* page checked is some magic around finding pages that
478 * have been modified without going through btrfs_set_page_dirty
479 * clear it here
480 */
Chris Mason4a096752008-07-21 10:29:44 -0400481 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400482 unlock_page(pages[i]);
483 mark_page_accessed(pages[i]);
484 page_cache_release(pages[i]);
485 }
486}
487
Chris Masond352ac62008-09-29 15:18:18 -0400488/*
489 * after copy_from_user, pages need to be dirtied and we need to make
490 * sure holes are created between the current EOF and the start of
491 * any next extents (if required).
492 *
493 * this also makes the decision about creating an inline extent vs
494 * doing real data extents, marking pages dirty and delalloc as required.
495 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400496int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
Eric Sandeen48a3b632013-04-25 20:41:01 +0000497 struct page **pages, size_t num_pages,
498 loff_t pos, size_t write_bytes,
499 struct extent_state **cached)
Chris Mason39279cc2007-06-12 06:35:45 -0400500{
Chris Mason39279cc2007-06-12 06:35:45 -0400501 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400502 int i;
Chris Masondb945352007-10-15 16:15:53 -0400503 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400504 u64 start_pos;
505 u64 end_of_last_block;
506 u64 end_pos = pos + write_bytes;
507 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400508
Chris Mason5f39d392007-10-15 16:14:19 -0400509 start_pos = pos & ~((u64)root->sectorsize - 1);
Qu Wenruofda28322013-02-26 08:10:22 +0000510 num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize);
Chris Mason39279cc2007-06-12 06:35:45 -0400511
Chris Masondb945352007-10-15 16:15:53 -0400512 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000513 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400514 cached);
Josef Bacikd0215f32011-01-25 14:57:24 -0500515 if (err)
516 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400517
Chris Masonc8b97812008-10-29 14:49:59 -0400518 for (i = 0; i < num_pages; i++) {
519 struct page *p = pages[i];
520 SetPageUptodate(p);
521 ClearPageChecked(p);
522 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400523 }
Josef Bacik9f570b82011-01-25 12:42:37 -0500524
525 /*
526 * we've only changed i_size in ram, and we haven't updated
527 * the disk i_size. There is no need to log the inode
528 * at this time.
529 */
530 if (end_pos > isize)
Chris Masona52d9a82007-08-27 16:49:44 -0400531 i_size_write(inode, end_pos);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400532 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400533}
534
Chris Masond352ac62008-09-29 15:18:18 -0400535/*
536 * this drops all the extents in the cache that intersect the range
537 * [start, end]. Existing extents are split as required.
538 */
Josef Bacik7014cdb2012-08-30 20:06:49 -0400539void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
540 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400541{
542 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400543 struct extent_map *split = NULL;
544 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400545 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500546 u64 len = end - start + 1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400547 u64 gen;
Chris Mason3b951512008-04-17 11:29:12 -0400548 int ret;
549 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400550 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400551 int compressed = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400552 bool modified;
Chris Masona52d9a82007-08-27 16:49:44 -0400553
Chris Masone6dcd2d2008-07-17 12:53:50 -0400554 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400555 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500556 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400557 testend = 0;
558 }
Chris Masond3977122009-01-05 21:25:51 -0500559 while (1) {
Josef Bacik7014cdb2012-08-30 20:06:49 -0400560 int no_splits = 0;
561
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400562 modified = false;
Chris Mason3b951512008-04-17 11:29:12 -0400563 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200564 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400565 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200566 split2 = alloc_extent_map();
Josef Bacik7014cdb2012-08-30 20:06:49 -0400567 if (!split || !split2)
568 no_splits = 1;
Chris Mason3b951512008-04-17 11:29:12 -0400569
Chris Mason890871b2009-09-02 16:24:52 -0400570 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500571 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500572 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400573 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400574 break;
Chris Masond1310b22008-01-24 16:13:08 -0500575 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400576 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400577 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400578 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000579 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400580 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400581 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400582 break;
583 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000584 start = em->start + em->len;
585 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400586 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400587 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400588 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400589 continue;
590 }
Chris Masonc8b97812008-10-29 14:49:59 -0400591 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400592 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo3b277592013-03-15 08:46:39 -0600593 clear_bit(EXTENT_FLAG_LOGGING, &flags);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400594 modified = !list_empty(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -0400595 remove_extent_mapping(em_tree, em);
Josef Bacik7014cdb2012-08-30 20:06:49 -0400596 if (no_splits)
597 goto next;
Chris Mason3b951512008-04-17 11:29:12 -0400598
599 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
600 em->start < start) {
601 split->start = em->start;
602 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500603 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400604 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400605
606 if (compressed)
607 split->block_len = em->block_len;
608 else
609 split->block_len = split->len;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400610 split->ram_bytes = em->ram_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -0500611 split->orig_block_len = max(split->block_len,
612 em->orig_block_len);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400613 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400614 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400615 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800616 split->compress_type = em->compress_type;
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400617 ret = add_extent_mapping(em_tree, split, modified);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100618 BUG_ON(ret); /* Logic error */
Chris Mason3b951512008-04-17 11:29:12 -0400619 free_extent_map(split);
620 split = split2;
621 split2 = NULL;
622 }
623 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
624 testend && em->start + em->len > start + len) {
625 u64 diff = start + len - em->start;
626
627 split->start = start + len;
628 split->len = em->start + em->len - (start + len);
629 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400630 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800631 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400632 split->generation = gen;
Josef Bacikb4939682012-12-03 10:31:19 -0500633 split->orig_block_len = max(em->block_len,
634 em->orig_block_len);
Josef Bacikcc95bef2013-04-04 14:31:27 -0400635 split->ram_bytes = em->ram_bytes;
Chris Mason3b951512008-04-17 11:29:12 -0400636
Chris Masonc8b97812008-10-29 14:49:59 -0400637 if (compressed) {
638 split->block_len = em->block_len;
639 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500640 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400641 } else {
642 split->block_len = split->len;
643 split->block_start = em->block_start + diff;
Josef Bacik70c8a912012-10-11 16:54:30 -0400644 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400645 }
Chris Mason3b951512008-04-17 11:29:12 -0400646
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400647 ret = add_extent_mapping(em_tree, split, modified);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100648 BUG_ON(ret); /* Logic error */
Chris Mason3b951512008-04-17 11:29:12 -0400649 free_extent_map(split);
650 split = NULL;
651 }
Josef Bacik7014cdb2012-08-30 20:06:49 -0400652next:
Chris Mason890871b2009-09-02 16:24:52 -0400653 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654
Chris Masona52d9a82007-08-27 16:49:44 -0400655 /* once for us */
656 free_extent_map(em);
657 /* once for the tree*/
658 free_extent_map(em);
659 }
Chris Mason3b951512008-04-17 11:29:12 -0400660 if (split)
661 free_extent_map(split);
662 if (split2)
663 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400664}
665
Chris Mason39279cc2007-06-12 06:35:45 -0400666/*
667 * this is very complex, but the basic idea is to drop all extents
668 * in the range start - end. hint_block is filled in with a block number
669 * that would be a good hint to the block allocator for this file.
670 *
671 * If an extent intersects the range but is not entirely inside the range
672 * it is either truncated or split. Anything entirely inside the range
673 * is deleted from the tree.
674 */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400675int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
676 struct btrfs_root *root, struct inode *inode,
677 struct btrfs_path *path, u64 start, u64 end,
Josef Bacik2aaa6652012-08-29 14:27:18 -0400678 u64 *drop_end, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400679{
Chris Mason00f5c792007-11-30 10:09:33 -0500680 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000681 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500682 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000683 struct btrfs_key new_key;
Li Zefan33345d012011-04-20 10:31:50 +0800684 u64 ino = btrfs_ino(inode);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000685 u64 search_start = start;
686 u64 disk_bytenr = 0;
687 u64 num_bytes = 0;
688 u64 extent_offset = 0;
689 u64 extent_end = 0;
690 int del_nr = 0;
691 int del_slot = 0;
692 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400693 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500694 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400695 int modify_tree = -1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400696 int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
Josef Bacikc3308f82012-09-14 14:51:22 -0400697 int found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400698
Chris Masona1ed8352009-09-11 12:27:37 -0400699 if (drop_cache)
700 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400701
Chris Masondc7fdde2012-04-27 14:31:29 -0400702 if (start >= BTRFS_I(inode)->disk_i_size)
703 modify_tree = 0;
704
Chris Masond3977122009-01-05 21:25:51 -0500705 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400706 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800707 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400708 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400709 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000710 break;
711 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
712 leaf = path->nodes[0];
713 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800714 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000715 key.type == BTRFS_EXTENT_DATA_KEY)
716 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400717 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400718 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000719next_slot:
720 leaf = path->nodes[0];
721 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
722 BUG_ON(del_nr > 0);
723 ret = btrfs_next_leaf(root, path);
724 if (ret < 0)
725 break;
726 if (ret > 0) {
727 ret = 0;
728 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400729 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000730 leaf = path->nodes[0];
731 recow = 1;
732 }
733
734 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800735 if (key.objectid > ino ||
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000736 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
737 break;
738
739 fi = btrfs_item_ptr(leaf, path->slots[0],
740 struct btrfs_file_extent_item);
741 extent_type = btrfs_file_extent_type(leaf, fi);
742
743 if (extent_type == BTRFS_FILE_EXTENT_REG ||
744 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
745 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
746 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
747 extent_offset = btrfs_file_extent_offset(leaf, fi);
748 extent_end = key.offset +
749 btrfs_file_extent_num_bytes(leaf, fi);
750 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
751 extent_end = key.offset +
752 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400753 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000754 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400755 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400756 }
757
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000758 if (extent_end <= search_start) {
759 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400760 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400761 }
762
Josef Bacikc3308f82012-09-14 14:51:22 -0400763 found = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000764 search_start = max(key.offset, start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400765 if (recow || !modify_tree) {
766 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200767 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000768 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400769 }
Chris Mason771ed682008-11-06 22:02:51 -0500770
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000771 /*
772 * | - range to drop - |
773 * | -------- extent -------- |
774 */
775 if (start > key.offset && end < extent_end) {
776 BUG_ON(del_nr > 0);
777 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
778
779 memcpy(&new_key, &key, sizeof(new_key));
780 new_key.offset = start;
781 ret = btrfs_duplicate_item(trans, root, path,
782 &new_key);
783 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200784 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000785 continue;
786 }
787 if (ret < 0)
788 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400789
Chris Mason5f39d392007-10-15 16:14:19 -0400790 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000791 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
792 struct btrfs_file_extent_item);
793 btrfs_set_file_extent_num_bytes(leaf, fi,
794 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400795
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000796 fi = btrfs_item_ptr(leaf, path->slots[0],
797 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400798
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000799 extent_offset += start - key.offset;
800 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
801 btrfs_set_file_extent_num_bytes(leaf, fi,
802 extent_end - start);
803 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400804
Josef Bacik5dc562c2012-08-17 13:14:17 -0400805 if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000806 ret = btrfs_inc_extent_ref(trans, root,
807 disk_bytenr, num_bytes, 0,
808 root->root_key.objectid,
809 new_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200810 start - extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100811 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400812 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000813 key.offset = start;
814 }
815 /*
816 * | ---- range to drop ----- |
817 * | -------- extent -------- |
818 */
819 if (start <= key.offset && end < extent_end) {
820 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
821
822 memcpy(&new_key, &key, sizeof(new_key));
823 new_key.offset = end;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000824 btrfs_set_item_key_safe(root, path, &new_key);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000825
826 extent_offset += end - key.offset;
827 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
828 btrfs_set_file_extent_num_bytes(leaf, fi,
829 extent_end - end);
830 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400831 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000832 inode_sub_bytes(inode, end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000833 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400834 }
835
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000836 search_start = extent_end;
837 /*
838 * | ---- range to drop ----- |
839 * | -------- extent -------- |
840 */
841 if (start > key.offset && end >= extent_end) {
842 BUG_ON(del_nr > 0);
843 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
844
845 btrfs_set_file_extent_num_bytes(leaf, fi,
846 start - key.offset);
847 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400848 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000849 inode_sub_bytes(inode, extent_end - start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000850 if (end == extent_end)
851 break;
852
853 path->slots[0]++;
854 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400855 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000856
857 /*
858 * | ---- range to drop ----- |
859 * | ------ extent ------ |
860 */
861 if (start <= key.offset && end >= extent_end) {
862 if (del_nr == 0) {
863 del_slot = path->slots[0];
864 del_nr = 1;
865 } else {
866 BUG_ON(del_slot + del_nr != path->slots[0]);
867 del_nr++;
868 }
869
Josef Bacik5dc562c2012-08-17 13:14:17 -0400870 if (update_refs &&
871 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000872 inode_sub_bytes(inode,
873 extent_end - key.offset);
874 extent_end = ALIGN(extent_end,
875 root->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400876 } else if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000877 ret = btrfs_free_extent(trans, root,
878 disk_bytenr, num_bytes, 0,
879 root->root_key.objectid,
880 key.objectid, key.offset -
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200881 extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100882 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000883 inode_sub_bytes(inode,
884 extent_end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000885 }
886
887 if (end == extent_end)
888 break;
889
890 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
891 path->slots[0]++;
892 goto next_slot;
893 }
894
895 ret = btrfs_del_items(trans, root, path, del_slot,
896 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100897 if (ret) {
898 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400899 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000901
902 del_nr = 0;
903 del_slot = 0;
904
David Sterbab3b4aa72011-04-21 01:20:15 +0200905 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000906 continue;
907 }
908
909 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400910 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000911
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100912 if (!ret && del_nr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000913 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100914 if (ret)
915 btrfs_abort_transaction(trans, root, ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000916 }
917
Josef Bacik2aaa6652012-08-29 14:27:18 -0400918 if (drop_end)
Josef Bacikc3308f82012-09-14 14:51:22 -0400919 *drop_end = found ? min(end, extent_end) : end;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400920 btrfs_release_path(path);
921 return ret;
922}
923
924int btrfs_drop_extents(struct btrfs_trans_handle *trans,
925 struct btrfs_root *root, struct inode *inode, u64 start,
Josef Bacik26714852012-08-29 12:24:27 -0400926 u64 end, int drop_cache)
Josef Bacik5dc562c2012-08-17 13:14:17 -0400927{
928 struct btrfs_path *path;
929 int ret;
930
931 path = btrfs_alloc_path();
932 if (!path)
933 return -ENOMEM;
Josef Bacik2aaa6652012-08-29 14:27:18 -0400934 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
Josef Bacik26714852012-08-29 12:24:27 -0400935 drop_cache);
Chris Mason39279cc2007-06-12 06:35:45 -0400936 btrfs_free_path(path);
937 return ret;
938}
939
Yan Zhengd899e052008-10-30 14:25:28 -0400940static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000941 u64 objectid, u64 bytenr, u64 orig_offset,
942 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400943{
944 struct btrfs_file_extent_item *fi;
945 struct btrfs_key key;
946 u64 extent_end;
947
948 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
949 return 0;
950
951 btrfs_item_key_to_cpu(leaf, &key, slot);
952 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
953 return 0;
954
955 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
956 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
957 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000958 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400959 btrfs_file_extent_compression(leaf, fi) ||
960 btrfs_file_extent_encryption(leaf, fi) ||
961 btrfs_file_extent_other_encoding(leaf, fi))
962 return 0;
963
964 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
965 if ((*start && *start != key.offset) || (*end && *end != extent_end))
966 return 0;
967
968 *start = key.offset;
969 *end = extent_end;
970 return 1;
971}
972
973/*
974 * Mark extent in the range start - end as written.
975 *
976 * This changes extent type from 'pre-allocated' to 'regular'. If only
977 * part of extent is marked as written, the extent will be split into
978 * two or three.
979 */
980int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400981 struct inode *inode, u64 start, u64 end)
982{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000983 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400984 struct extent_buffer *leaf;
985 struct btrfs_path *path;
986 struct btrfs_file_extent_item *fi;
987 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000988 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400989 u64 bytenr;
990 u64 num_bytes;
991 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400993 u64 other_start;
994 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000995 u64 split;
996 int del_nr = 0;
997 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000998 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400999 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08001000 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001001
Yan Zhengd899e052008-10-30 14:25:28 -04001002 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001003 if (!path)
1004 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001005again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001006 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001007 split = start;
Li Zefan33345d012011-04-20 10:31:50 +08001008 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -04001009 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001010 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -04001011
1012 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -04001013 if (ret < 0)
1014 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001015 if (ret > 0 && path->slots[0] > 0)
1016 path->slots[0]--;
1017
1018 leaf = path->nodes[0];
1019 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001020 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
Yan Zhengd899e052008-10-30 14:25:28 -04001021 fi = btrfs_item_ptr(leaf, path->slots[0],
1022 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001023 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
1024 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -04001025 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1026 BUG_ON(key.offset > start || extent_end < end);
1027
1028 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1029 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001031 memcpy(&new_key, &key, sizeof(new_key));
1032
1033 if (start == key.offset && end < extent_end) {
1034 other_start = 0;
1035 other_end = start;
1036 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001037 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001038 &other_start, &other_end)) {
1039 new_key.offset = end;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001040 btrfs_set_item_key_safe(root, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001041 fi = btrfs_item_ptr(leaf, path->slots[0],
1042 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001043 btrfs_set_file_extent_generation(leaf, fi,
1044 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001045 btrfs_set_file_extent_num_bytes(leaf, fi,
1046 extent_end - end);
1047 btrfs_set_file_extent_offset(leaf, fi,
1048 end - orig_offset);
1049 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1050 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001051 btrfs_set_file_extent_generation(leaf, fi,
1052 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001053 btrfs_set_file_extent_num_bytes(leaf, fi,
1054 end - other_start);
1055 btrfs_mark_buffer_dirty(leaf);
1056 goto out;
1057 }
1058 }
1059
1060 if (start > key.offset && end == extent_end) {
1061 other_start = end;
1062 other_end = 0;
1063 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001064 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001065 &other_start, &other_end)) {
1066 fi = btrfs_item_ptr(leaf, path->slots[0],
1067 struct btrfs_file_extent_item);
1068 btrfs_set_file_extent_num_bytes(leaf, fi,
1069 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -04001070 btrfs_set_file_extent_generation(leaf, fi,
1071 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001072 path->slots[0]++;
1073 new_key.offset = start;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001074 btrfs_set_item_key_safe(root, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001075
1076 fi = btrfs_item_ptr(leaf, path->slots[0],
1077 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001078 btrfs_set_file_extent_generation(leaf, fi,
1079 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001080 btrfs_set_file_extent_num_bytes(leaf, fi,
1081 other_end - start);
1082 btrfs_set_file_extent_offset(leaf, fi,
1083 start - orig_offset);
1084 btrfs_mark_buffer_dirty(leaf);
1085 goto out;
1086 }
1087 }
Yan Zhengd899e052008-10-30 14:25:28 -04001088
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001089 while (start > key.offset || end < extent_end) {
1090 if (key.offset == start)
1091 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001092
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001093 new_key.offset = split;
1094 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1095 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001096 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001097 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001098 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 if (ret < 0) {
1100 btrfs_abort_transaction(trans, root, ret);
1101 goto out;
1102 }
Yan Zhengd899e052008-10-30 14:25:28 -04001103
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001104 leaf = path->nodes[0];
1105 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001106 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001107 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001108 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001109 split - key.offset);
1110
1111 fi = btrfs_item_ptr(leaf, path->slots[0],
1112 struct btrfs_file_extent_item);
1113
Josef Bacik224ecce2012-08-16 16:32:06 -04001114 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001115 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1116 btrfs_set_file_extent_num_bytes(leaf, fi,
1117 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001118 btrfs_mark_buffer_dirty(leaf);
1119
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001120 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1121 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001122 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001123 BUG_ON(ret); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001124
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001125 if (split == start) {
1126 key.offset = start;
1127 } else {
1128 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -04001129 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001130 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001131 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001132 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001133 }
1134
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001135 other_start = end;
1136 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001137 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001138 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001139 &other_start, &other_end)) {
1140 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001141 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001142 goto again;
1143 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001144 extent_end = other_end;
1145 del_slot = path->slots[0] + 1;
1146 del_nr++;
1147 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1148 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001149 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001150 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001151 }
1152 other_start = 0;
1153 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001154 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001155 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001156 &other_start, &other_end)) {
1157 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001158 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001159 goto again;
1160 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001161 key.offset = other_start;
1162 del_slot = path->slots[0];
1163 del_nr++;
1164 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1165 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001166 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001167 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001168 }
1169 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001170 fi = btrfs_item_ptr(leaf, path->slots[0],
1171 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001172 btrfs_set_file_extent_type(leaf, fi,
1173 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001174 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001175 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001176 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001177 fi = btrfs_item_ptr(leaf, del_slot - 1,
1178 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001179 btrfs_set_file_extent_type(leaf, fi,
1180 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001181 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001182 btrfs_set_file_extent_num_bytes(leaf, fi,
1183 extent_end - key.offset);
1184 btrfs_mark_buffer_dirty(leaf);
1185
1186 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 if (ret < 0) {
1188 btrfs_abort_transaction(trans, root, ret);
1189 goto out;
1190 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001191 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001192out:
Yan Zhengd899e052008-10-30 14:25:28 -04001193 btrfs_free_path(path);
1194 return 0;
1195}
1196
Chris Mason39279cc2007-06-12 06:35:45 -04001197/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001198 * on error we return an unlocked page and the error value
1199 * on success we return a locked page and 0
1200 */
Josef Bacikb63164292011-09-30 15:23:54 -04001201static int prepare_uptodate_page(struct page *page, u64 pos,
1202 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001203{
1204 int ret = 0;
1205
Josef Bacikb63164292011-09-30 15:23:54 -04001206 if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1207 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001208 ret = btrfs_readpage(NULL, page);
1209 if (ret)
1210 return ret;
1211 lock_page(page);
1212 if (!PageUptodate(page)) {
1213 unlock_page(page);
1214 return -EIO;
1215 }
1216 }
1217 return 0;
1218}
1219
1220/*
Chris Masond352ac62008-09-29 15:18:18 -04001221 * this gets pages into the page cache and locks them down, it also properly
1222 * waits for data=ordered extents to finish before allowing the pages to be
1223 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -04001224 */
Chris Masond3977122009-01-05 21:25:51 -05001225static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -05001226 struct page **pages, size_t num_pages,
1227 loff_t pos, unsigned long first_index,
Josef Bacikb63164292011-09-30 15:23:54 -04001228 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001229{
Josef Bacik2ac55d42010-02-03 19:33:23 +00001230 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001231 int i;
1232 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Al Viro496ad9a2013-01-23 17:07:38 -05001233 struct inode *inode = file_inode(file);
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001234 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason39279cc2007-06-12 06:35:45 -04001235 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001236 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -04001237 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001238 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -04001239
Chris Mason5f39d392007-10-15 16:14:19 -04001240 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001241 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001242
Chris Masone6dcd2d2008-07-17 12:53:50 -04001243again:
Chris Mason39279cc2007-06-12 06:35:45 -04001244 for (i = 0; i < num_pages; i++) {
Josef Bacika94733d2011-07-11 10:47:06 -04001245 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001246 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001247 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001248 faili = i - 1;
1249 err = -ENOMEM;
1250 goto fail;
1251 }
1252
1253 if (i == 0)
Josef Bacikb63164292011-09-30 15:23:54 -04001254 err = prepare_uptodate_page(pages[i], pos,
1255 force_uptodate);
Chris Masonb1bf8622011-02-28 09:52:08 -05001256 if (i == num_pages - 1)
1257 err = prepare_uptodate_page(pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001258 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001259 if (err) {
1260 page_cache_release(pages[i]);
1261 faili = i - 1;
1262 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001263 }
Chris Masonccd467d2007-06-28 15:57:36 -04001264 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001265 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001266 err = 0;
Chris Mason07627042008-02-19 11:29:24 -05001267 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04001268 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001269 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001270 start_pos, last_pos - 1, 0, &cached_state);
Chris Masond3977122009-01-05 21:25:51 -05001271 ordered = btrfs_lookup_first_ordered_extent(inode,
1272 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001273 if (ordered &&
1274 ordered->file_offset + ordered->len > start_pos &&
1275 ordered->file_offset < last_pos) {
1276 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001277 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1278 start_pos, last_pos - 1,
1279 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001280 for (i = 0; i < num_pages; i++) {
1281 unlock_page(pages[i]);
1282 page_cache_release(pages[i]);
1283 }
1284 btrfs_wait_ordered_range(inode, start_pos,
1285 last_pos - start_pos);
1286 goto again;
1287 }
1288 if (ordered)
1289 btrfs_put_ordered_extent(ordered);
1290
Josef Bacik2ac55d42010-02-03 19:33:23 +00001291 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -04001292 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001293 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1294 0, 0, &cached_state, GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001295 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1296 start_pos, last_pos - 1, &cached_state,
1297 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -05001298 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001299 for (i = 0; i < num_pages; i++) {
Wu Fengguang32c7f202011-08-08 15:19:47 -06001300 if (clear_page_dirty_for_io(pages[i]))
1301 account_page_redirty(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001302 set_page_extent_mapped(pages[i]);
1303 WARN_ON(!PageLocked(pages[i]));
1304 }
Chris Mason39279cc2007-06-12 06:35:45 -04001305 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001306fail:
1307 while (faili >= 0) {
1308 unlock_page(pages[faili]);
1309 page_cache_release(pages[faili]);
1310 faili--;
1311 }
1312 return err;
1313
Chris Mason39279cc2007-06-12 06:35:45 -04001314}
1315
Josef Bacik7ee9e442013-06-21 16:37:03 -04001316static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1317 size_t *write_bytes)
1318{
1319 struct btrfs_trans_handle *trans;
1320 struct btrfs_root *root = BTRFS_I(inode)->root;
1321 struct btrfs_ordered_extent *ordered;
1322 u64 lockstart, lockend;
1323 u64 num_bytes;
1324 int ret;
1325
1326 lockstart = round_down(pos, root->sectorsize);
1327 lockend = lockstart + round_up(*write_bytes, root->sectorsize) - 1;
1328
1329 while (1) {
1330 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1331 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1332 lockend - lockstart + 1);
1333 if (!ordered) {
1334 break;
1335 }
1336 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1337 btrfs_start_ordered_extent(inode, ordered, 1);
1338 btrfs_put_ordered_extent(ordered);
1339 }
1340
1341 trans = btrfs_join_transaction(root);
1342 if (IS_ERR(trans)) {
1343 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1344 return PTR_ERR(trans);
1345 }
1346
1347 num_bytes = lockend - lockstart + 1;
1348 ret = can_nocow_extent(trans, inode, lockstart, &num_bytes, NULL, NULL,
1349 NULL);
1350 btrfs_end_transaction(trans, root);
1351 if (ret <= 0) {
1352 ret = 0;
1353 } else {
1354 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1355 EXTENT_DIRTY | EXTENT_DELALLOC |
1356 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1357 NULL, GFP_NOFS);
1358 *write_bytes = min_t(size_t, *write_bytes, num_bytes);
1359 }
1360
1361 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1362
1363 return ret;
1364}
1365
Josef Bacikd0215f32011-01-25 14:57:24 -05001366static noinline ssize_t __btrfs_buffered_write(struct file *file,
1367 struct iov_iter *i,
1368 loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001369{
Al Viro496ad9a2013-01-23 17:07:38 -05001370 struct inode *inode = file_inode(file);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001371 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001372 struct page **pages = NULL;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001373 u64 release_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001374 unsigned long first_index;
Josef Bacikd0215f32011-01-25 14:57:24 -05001375 size_t num_written = 0;
1376 int nrptrs;
Tsutomu Itohc9149232011-03-30 00:57:23 +00001377 int ret = 0;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001378 bool only_release_metadata = false;
Josef Bacikb63164292011-09-30 15:23:54 -04001379 bool force_page_uptodate = false;
Chris Masoncb843a62008-10-03 12:30:02 -04001380
Josef Bacikd0215f32011-01-25 14:57:24 -05001381 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
Josef Bacik11c65dc2010-05-23 11:07:21 -04001382 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1383 (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001384 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1385 nrptrs = max(nrptrs, 8);
Chris Mason8c2383c2007-06-18 09:57:58 -04001386 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001387 if (!pages)
1388 return -ENOMEM;
Chris Masonab93dbe2009-10-01 12:29:10 -04001389
Chris Mason39279cc2007-06-12 06:35:45 -04001390 first_index = pos >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001391
Josef Bacikd0215f32011-01-25 14:57:24 -05001392 while (iov_iter_count(i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001393 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacikd0215f32011-01-25 14:57:24 -05001394 size_t write_bytes = min(iov_iter_count(i),
Josef Bacik11c65dc2010-05-23 11:07:21 -04001395 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001396 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001397 size_t num_pages = (write_bytes + offset +
1398 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001399 size_t reserve_bytes;
Josef Bacikd0215f32011-01-25 14:57:24 -05001400 size_t dirty_pages;
1401 size_t copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001402
Chris Mason8c2383c2007-06-18 09:57:58 -04001403 WARN_ON(num_pages > nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001404
Xin Zhong914ee292010-12-09 09:30:14 +00001405 /*
1406 * Fault pages before locking them in prepare_pages
1407 * to avoid recursive lock
1408 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001409 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001410 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001411 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001412 }
1413
Josef Bacik7ee9e442013-06-21 16:37:03 -04001414 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1415 ret = btrfs_check_data_free_space(inode, reserve_bytes);
1416 if (ret == -ENOSPC &&
1417 (BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1418 BTRFS_INODE_PREALLOC))) {
1419 ret = check_can_nocow(inode, pos, &write_bytes);
1420 if (ret > 0) {
1421 only_release_metadata = true;
1422 /*
1423 * our prealloc extent may be smaller than
1424 * write_bytes, so scale down.
1425 */
1426 num_pages = (write_bytes + offset +
1427 PAGE_CACHE_SIZE - 1) >>
1428 PAGE_CACHE_SHIFT;
1429 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1430 ret = 0;
1431 } else {
1432 ret = -ENOSPC;
1433 }
1434 }
1435
Chris Mason1832a6d2007-12-21 16:27:21 -05001436 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001437 break;
Chris Mason1832a6d2007-12-21 16:27:21 -05001438
Josef Bacik7ee9e442013-06-21 16:37:03 -04001439 ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes);
1440 if (ret) {
1441 if (!only_release_metadata)
1442 btrfs_free_reserved_data_space(inode,
1443 reserve_bytes);
1444 break;
1445 }
1446
1447 release_bytes = reserve_bytes;
1448
Josef Bacik4a640012011-01-25 15:10:08 -05001449 /*
1450 * This is going to setup the pages array with the number of
1451 * pages we want, so we don't really need to worry about the
1452 * contents of pages from loop to loop
1453 */
Chris Mason39279cc2007-06-12 06:35:45 -04001454 ret = prepare_pages(root, file, pages, num_pages,
Josef Bacikb63164292011-09-30 15:23:54 -04001455 pos, first_index, write_bytes,
1456 force_page_uptodate);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001457 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001458 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001459
Xin Zhong914ee292010-12-09 09:30:14 +00001460 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -05001461 write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001462
1463 /*
1464 * if we have trouble faulting in the pages, fall
1465 * back to one page at a time
1466 */
1467 if (copied < write_bytes)
1468 nrptrs = 1;
1469
Josef Bacikb63164292011-09-30 15:23:54 -04001470 if (copied == 0) {
1471 force_page_uptodate = true;
Chris Masonb1bf8622011-02-28 09:52:08 -05001472 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001473 } else {
1474 force_page_uptodate = false;
Chris Masonb1bf8622011-02-28 09:52:08 -05001475 dirty_pages = (copied + offset +
1476 PAGE_CACHE_SIZE - 1) >>
1477 PAGE_CACHE_SHIFT;
Josef Bacikb63164292011-09-30 15:23:54 -04001478 }
Xin Zhong914ee292010-12-09 09:30:14 +00001479
Josef Bacikd0215f32011-01-25 14:57:24 -05001480 /*
1481 * If we had a short copy we need to release the excess delaloc
1482 * bytes we reserved. We need to increment outstanding_extents
1483 * because btrfs_delalloc_release_space will decrement it, but
1484 * we still have an outstanding extent for the chunk we actually
1485 * managed to copy.
1486 */
Xin Zhong914ee292010-12-09 09:30:14 +00001487 if (num_pages > dirty_pages) {
Josef Bacik7ee9e442013-06-21 16:37:03 -04001488 release_bytes = (num_pages - dirty_pages) <<
1489 PAGE_CACHE_SHIFT;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001490 if (copied > 0) {
1491 spin_lock(&BTRFS_I(inode)->lock);
1492 BTRFS_I(inode)->outstanding_extents++;
1493 spin_unlock(&BTRFS_I(inode)->lock);
1494 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04001495 if (only_release_metadata)
1496 btrfs_delalloc_release_metadata(inode,
1497 release_bytes);
1498 else
1499 btrfs_delalloc_release_space(inode,
1500 release_bytes);
Xin Zhong914ee292010-12-09 09:30:14 +00001501 }
1502
Josef Bacik7ee9e442013-06-21 16:37:03 -04001503 release_bytes = dirty_pages << PAGE_CACHE_SHIFT;
Xin Zhong914ee292010-12-09 09:30:14 +00001504 if (copied > 0) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001505 ret = btrfs_dirty_pages(root, inode, pages,
1506 dirty_pages, pos, copied,
1507 NULL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001508 if (ret) {
Josef Bacikd0215f32011-01-25 14:57:24 -05001509 btrfs_drop_pages(pages, num_pages);
1510 break;
1511 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001512 }
Chris Mason39279cc2007-06-12 06:35:45 -04001513
Josef Bacik7ee9e442013-06-21 16:37:03 -04001514 release_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001515 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001516
Josef Bacik7ee9e442013-06-21 16:37:03 -04001517 if (only_release_metadata && copied > 0) {
1518 u64 lockstart = round_down(pos, root->sectorsize);
1519 u64 lockend = lockstart +
1520 (dirty_pages << PAGE_CACHE_SHIFT) - 1;
1521
1522 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1523 lockend, EXTENT_NORESERVE, NULL,
1524 NULL, GFP_NOFS);
1525 only_release_metadata = false;
1526 }
1527
Josef Bacikd0215f32011-01-25 14:57:24 -05001528 cond_resched();
1529
Namjae Jeond0e1d662012-12-11 16:00:21 -08001530 balance_dirty_pages_ratelimited(inode->i_mapping);
Josef Bacikd0215f32011-01-25 14:57:24 -05001531 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
Liu Bob53d3f52012-11-14 14:34:34 +00001532 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001533
Xin Zhong914ee292010-12-09 09:30:14 +00001534 pos += copied;
1535 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001536 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001537
Chris Mason8c2383c2007-06-18 09:57:58 -04001538 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001539
Josef Bacik7ee9e442013-06-21 16:37:03 -04001540 if (release_bytes) {
1541 if (only_release_metadata)
1542 btrfs_delalloc_release_metadata(inode, release_bytes);
1543 else
1544 btrfs_delalloc_release_space(inode, release_bytes);
1545 }
1546
Josef Bacikd0215f32011-01-25 14:57:24 -05001547 return num_written ? num_written : ret;
1548}
1549
1550static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1551 const struct iovec *iov,
1552 unsigned long nr_segs, loff_t pos,
1553 loff_t *ppos, size_t count, size_t ocount)
1554{
1555 struct file *file = iocb->ki_filp;
Josef Bacikd0215f32011-01-25 14:57:24 -05001556 struct iov_iter i;
1557 ssize_t written;
1558 ssize_t written_buffered;
1559 loff_t endbyte;
1560 int err;
1561
1562 written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
1563 count, ocount);
1564
Josef Bacikd0215f32011-01-25 14:57:24 -05001565 if (written < 0 || written == count)
1566 return written;
1567
1568 pos += written;
1569 count -= written;
1570 iov_iter_init(&i, iov, nr_segs, count, written);
1571 written_buffered = __btrfs_buffered_write(file, &i, pos);
1572 if (written_buffered < 0) {
1573 err = written_buffered;
1574 goto out;
1575 }
1576 endbyte = pos + written_buffered - 1;
1577 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1578 if (err)
1579 goto out;
1580 written += written_buffered;
1581 *ppos = pos + written_buffered;
1582 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1583 endbyte >> PAGE_CACHE_SHIFT);
1584out:
1585 return written ? written : err;
1586}
1587
Josef Bacik6c760c02012-11-09 10:53:21 -05001588static void update_time_for_write(struct inode *inode)
1589{
1590 struct timespec now;
1591
1592 if (IS_NOCMTIME(inode))
1593 return;
1594
1595 now = current_fs_time(inode->i_sb);
1596 if (!timespec_equal(&inode->i_mtime, &now))
1597 inode->i_mtime = now;
1598
1599 if (!timespec_equal(&inode->i_ctime, &now))
1600 inode->i_ctime = now;
1601
1602 if (IS_I_VERSION(inode))
1603 inode_inc_iversion(inode);
1604}
1605
Josef Bacikd0215f32011-01-25 14:57:24 -05001606static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1607 const struct iovec *iov,
1608 unsigned long nr_segs, loff_t pos)
1609{
1610 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -05001611 struct inode *inode = file_inode(file);
Josef Bacikd0215f32011-01-25 14:57:24 -05001612 struct btrfs_root *root = BTRFS_I(inode)->root;
1613 loff_t *ppos = &iocb->ki_pos;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001614 u64 start_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001615 ssize_t num_written = 0;
1616 ssize_t err = 0;
1617 size_t count, ocount;
Josef Bacikb812ce22012-11-16 13:56:32 -05001618 bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
Josef Bacikd0215f32011-01-25 14:57:24 -05001619
Josef Bacikd0215f32011-01-25 14:57:24 -05001620 mutex_lock(&inode->i_mutex);
1621
1622 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1623 if (err) {
1624 mutex_unlock(&inode->i_mutex);
1625 goto out;
1626 }
1627 count = ocount;
1628
1629 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1630 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1631 if (err) {
1632 mutex_unlock(&inode->i_mutex);
1633 goto out;
1634 }
1635
1636 if (count == 0) {
1637 mutex_unlock(&inode->i_mutex);
1638 goto out;
1639 }
1640
1641 err = file_remove_suid(file);
1642 if (err) {
1643 mutex_unlock(&inode->i_mutex);
1644 goto out;
1645 }
1646
1647 /*
1648 * If BTRFS flips readonly due to some impossible error
1649 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1650 * although we have opened a file as writable, we have
1651 * to stop this write operation to ensure FS consistency.
1652 */
Miao Xie87533c42013-01-29 10:14:48 +00001653 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
Josef Bacikd0215f32011-01-25 14:57:24 -05001654 mutex_unlock(&inode->i_mutex);
1655 err = -EROFS;
1656 goto out;
1657 }
1658
Josef Bacik6c760c02012-11-09 10:53:21 -05001659 /*
1660 * We reserve space for updating the inode when we reserve space for the
1661 * extent we are going to write, so we will enospc out there. We don't
1662 * need to start yet another transaction to update the inode as we will
1663 * update the inode when we finish writing whatever data we write.
1664 */
1665 update_time_for_write(inode);
Josef Bacikd0215f32011-01-25 14:57:24 -05001666
Miao Xie0c1a98c2011-09-11 10:52:24 -04001667 start_pos = round_down(pos, root->sectorsize);
1668 if (start_pos > i_size_read(inode)) {
1669 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1670 if (err) {
1671 mutex_unlock(&inode->i_mutex);
1672 goto out;
1673 }
1674 }
1675
Josef Bacikb812ce22012-11-16 13:56:32 -05001676 if (sync)
1677 atomic_inc(&BTRFS_I(inode)->sync_writers);
1678
Josef Bacikd0215f32011-01-25 14:57:24 -05001679 if (unlikely(file->f_flags & O_DIRECT)) {
1680 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1681 pos, ppos, count, ocount);
1682 } else {
1683 struct iov_iter i;
1684
1685 iov_iter_init(&i, iov, nr_segs, count, num_written);
1686
1687 num_written = __btrfs_buffered_write(file, &i, pos);
1688 if (num_written > 0)
1689 *ppos = pos + num_written;
1690 }
1691
1692 mutex_unlock(&inode->i_mutex);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001693
Chris Mason5a3f23d2009-03-31 13:27:11 -04001694 /*
1695 * we want to make sure fsync finds this change
1696 * but we haven't joined a transaction running right now.
1697 *
1698 * Later on, someone is sure to update the inode and get the
1699 * real transid recorded.
1700 *
1701 * We set last_trans now to the fs_info generation + 1,
1702 * this will either be one more than the running transaction
1703 * or the generation used for the next transaction if there isn't
1704 * one running right now.
Josef Bacik6c760c02012-11-09 10:53:21 -05001705 *
1706 * We also have to set last_sub_trans to the current log transid,
1707 * otherwise subsequent syncs to a file that's been synced in this
1708 * transaction will appear to have already occured.
Chris Mason5a3f23d2009-03-31 13:27:11 -04001709 */
1710 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Josef Bacik6c760c02012-11-09 10:53:21 -05001711 BTRFS_I(inode)->last_sub_trans = root->log_transid;
Josef Bacikd0215f32011-01-25 14:57:24 -05001712 if (num_written > 0 || num_written == -EIOCBQUEUED) {
1713 err = generic_write_sync(file, pos, num_written);
1714 if (err < 0 && num_written > 0)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001715 num_written = err;
1716 }
Miao Xie0a3404d2013-01-28 12:34:55 +00001717
Josef Bacikb812ce22012-11-16 13:56:32 -05001718 if (sync)
1719 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie0a3404d2013-01-28 12:34:55 +00001720out:
Chris Mason39279cc2007-06-12 06:35:45 -04001721 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001722 return num_written ? num_written : err;
1723}
1724
Chris Masond3977122009-01-05 21:25:51 -05001725int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001726{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001727 /*
1728 * ordered_data_close is set by settattr when we are about to truncate
1729 * a file from a non-zero size to a zero size. This tries to
1730 * flush down new bytes that may have been written if the
1731 * application were using truncate to replace a file in place.
1732 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04001733 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1734 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik569e0f32013-02-13 11:09:14 -05001735 struct btrfs_trans_handle *trans;
1736 struct btrfs_root *root = BTRFS_I(inode)->root;
1737
1738 /*
1739 * We need to block on a committing transaction to keep us from
1740 * throwing a ordered operation on to the list and causing
1741 * something like sync to deadlock trying to flush out this
1742 * inode.
1743 */
1744 trans = btrfs_start_transaction(root, 0);
1745 if (IS_ERR(trans))
1746 return PTR_ERR(trans);
1747 btrfs_add_ordered_operation(trans, BTRFS_I(inode)->root, inode);
1748 btrfs_end_transaction(trans, root);
Chris Mason5a3f23d2009-03-31 13:27:11 -04001749 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1750 filemap_flush(inode->i_mapping);
1751 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001752 if (filp->private_data)
1753 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001754 return 0;
1755}
1756
Chris Masond352ac62008-09-29 15:18:18 -04001757/*
1758 * fsync call for both files and directories. This logs the inode into
1759 * the tree log instead of forcing full commits whenever possible.
1760 *
1761 * It needs to call filemap_fdatawait so that all ordered extent updates are
1762 * in the metadata btree are up to date for copying to the log.
1763 *
1764 * It drops the inode mutex before doing the tree log commit. This is an
1765 * important optimization for directories because holding the mutex prevents
1766 * new operations on the dir while we write to disk.
1767 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001768int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001769{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001770 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001771 struct inode *inode = dentry->d_inode;
1772 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001773 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001774 struct btrfs_trans_handle *trans;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001775 bool full_sync = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001776
liubo1abe9b82011-03-24 11:18:59 +00001777 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04001778
Miao Xie90abccf2012-09-13 04:53:47 -06001779 /*
1780 * We write the dirty pages in the range and wait until they complete
1781 * out of the ->i_mutex. If so, we can flush the dirty pages by
Josef Bacik2ab28f32012-10-12 15:27:49 -04001782 * multi-task, and make the performance up. See
1783 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
Miao Xie90abccf2012-09-13 04:53:47 -06001784 */
Josef Bacikb812ce22012-11-16 13:56:32 -05001785 atomic_inc(&BTRFS_I(inode)->sync_writers);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001786 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1787 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1788 &BTRFS_I(inode)->runtime_flags))
1789 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
Josef Bacikb812ce22012-11-16 13:56:32 -05001790 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie90abccf2012-09-13 04:53:47 -06001791 if (ret)
1792 return ret;
1793
Josef Bacik02c24a82011-07-16 20:44:56 -04001794 mutex_lock(&inode->i_mutex);
1795
Josef Bacik0885ef52012-04-23 15:09:39 -04001796 /*
Miao Xie90abccf2012-09-13 04:53:47 -06001797 * We flush the dirty pages again to avoid some dirty pages in the
1798 * range being left.
Josef Bacik0885ef52012-04-23 15:09:39 -04001799 */
Miao Xie2ecb7922012-09-06 04:04:27 -06001800 atomic_inc(&root->log_batch);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001801 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1802 &BTRFS_I(inode)->runtime_flags);
1803 if (full_sync)
1804 btrfs_wait_ordered_range(inode, start, end - start + 1);
Miao Xie2ecb7922012-09-06 04:04:27 -06001805 atomic_inc(&root->log_batch);
Chris Mason257c62e2009-10-13 13:21:08 -04001806
Chris Mason39279cc2007-06-12 06:35:45 -04001807 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001808 * check the transaction that last modified this inode
1809 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001810 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001811 if (!BTRFS_I(inode)->last_trans) {
1812 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001813 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001814 }
Chris Masona2135012008-06-25 16:01:30 -04001815
Chris Mason257c62e2009-10-13 13:21:08 -04001816 /*
1817 * if the last transaction that changed this file was before
1818 * the current transaction, we can bail out now without any
1819 * syncing
1820 */
Josef Bacika4abeea2011-04-11 17:25:13 -04001821 smp_mb();
Josef Bacik22ee6982012-05-29 16:57:49 -04001822 if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1823 BTRFS_I(inode)->last_trans <=
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001824 root->fs_info->last_trans_committed) {
1825 BTRFS_I(inode)->last_trans = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001826
1827 /*
1828 * We'v had everything committed since the last time we were
1829 * modified so clear this flag in case it was set for whatever
1830 * reason, it's no longer relevant.
1831 */
1832 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1833 &BTRFS_I(inode)->runtime_flags);
Josef Bacik02c24a82011-07-16 20:44:56 -04001834 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001835 goto out;
1836 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001837
1838 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001839 * ok we haven't committed the transaction yet, lets do a commit
1840 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001841 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001842 btrfs_ioctl_trans_end(file);
1843
Yan, Zhenga22285a2010-05-16 10:48:46 -04001844 trans = btrfs_start_transaction(root, 0);
1845 if (IS_ERR(trans)) {
1846 ret = PTR_ERR(trans);
Josef Bacik02c24a82011-07-16 20:44:56 -04001847 mutex_unlock(&inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001848 goto out;
1849 }
Chris Masone02119d2008-09-05 16:13:11 -04001850
Chris Mason2cfbd502009-02-20 10:55:10 -05001851 ret = btrfs_log_dentry_safe(trans, root, dentry);
Josef Bacik02c24a82011-07-16 20:44:56 -04001852 if (ret < 0) {
1853 mutex_unlock(&inode->i_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001854 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001855 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001856
1857 /* we've logged all the items and now have a consistent
1858 * version of the file in the log. It is possible that
1859 * someone will come in and modify the file, but that's
1860 * fine because the log is consistent on disk, and we
1861 * have references to all of the file's extents
1862 *
1863 * It is possible that someone will come in and log the
1864 * file again, but that will end up using the synchronization
1865 * inside btrfs_sync_log to keep things safe.
1866 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001867 mutex_unlock(&inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001868
Chris Mason257c62e2009-10-13 13:21:08 -04001869 if (ret != BTRFS_NO_LOG_SYNC) {
1870 if (ret > 0) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04001871 /*
1872 * If we didn't already wait for ordered extents we need
1873 * to do that now.
1874 */
1875 if (!full_sync)
1876 btrfs_wait_ordered_range(inode, start,
1877 end - start + 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04001878 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001879 } else {
1880 ret = btrfs_sync_log(trans, root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001881 if (ret == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04001882 ret = btrfs_end_transaction(trans, root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001883 } else {
1884 if (!full_sync)
1885 btrfs_wait_ordered_range(inode, start,
1886 end -
1887 start + 1);
Chris Mason257c62e2009-10-13 13:21:08 -04001888 ret = btrfs_commit_transaction(trans, root);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001889 }
Chris Mason257c62e2009-10-13 13:21:08 -04001890 }
1891 } else {
1892 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001893 }
Chris Mason39279cc2007-06-12 06:35:45 -04001894out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001895 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001896}
1897
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001898static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001899 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001900 .page_mkwrite = btrfs_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001901 .remap_pages = generic_file_remap_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04001902};
1903
1904static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1905{
Miao Xie058a4572010-05-20 07:21:50 +00001906 struct address_space *mapping = filp->f_mapping;
1907
1908 if (!mapping->a_ops->readpage)
1909 return -ENOEXEC;
1910
Chris Mason9ebefb182007-06-15 13:50:00 -04001911 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001912 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00001913
Chris Mason9ebefb182007-06-15 13:50:00 -04001914 return 0;
1915}
1916
Josef Bacik2aaa6652012-08-29 14:27:18 -04001917static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
1918 int slot, u64 start, u64 end)
1919{
1920 struct btrfs_file_extent_item *fi;
1921 struct btrfs_key key;
1922
1923 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1924 return 0;
1925
1926 btrfs_item_key_to_cpu(leaf, &key, slot);
1927 if (key.objectid != btrfs_ino(inode) ||
1928 key.type != BTRFS_EXTENT_DATA_KEY)
1929 return 0;
1930
1931 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1932
1933 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1934 return 0;
1935
1936 if (btrfs_file_extent_disk_bytenr(leaf, fi))
1937 return 0;
1938
1939 if (key.offset == end)
1940 return 1;
1941 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1942 return 1;
1943 return 0;
1944}
1945
1946static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
1947 struct btrfs_path *path, u64 offset, u64 end)
1948{
1949 struct btrfs_root *root = BTRFS_I(inode)->root;
1950 struct extent_buffer *leaf;
1951 struct btrfs_file_extent_item *fi;
1952 struct extent_map *hole_em;
1953 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1954 struct btrfs_key key;
1955 int ret;
1956
1957 key.objectid = btrfs_ino(inode);
1958 key.type = BTRFS_EXTENT_DATA_KEY;
1959 key.offset = offset;
1960
1961
1962 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1963 if (ret < 0)
1964 return ret;
1965 BUG_ON(!ret);
1966
1967 leaf = path->nodes[0];
1968 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
1969 u64 num_bytes;
1970
1971 path->slots[0]--;
1972 fi = btrfs_item_ptr(leaf, path->slots[0],
1973 struct btrfs_file_extent_item);
1974 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
1975 end - offset;
1976 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1977 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1978 btrfs_set_file_extent_offset(leaf, fi, 0);
1979 btrfs_mark_buffer_dirty(leaf);
1980 goto out;
1981 }
1982
1983 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
1984 u64 num_bytes;
1985
1986 path->slots[0]++;
1987 key.offset = offset;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001988 btrfs_set_item_key_safe(root, path, &key);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001989 fi = btrfs_item_ptr(leaf, path->slots[0],
1990 struct btrfs_file_extent_item);
1991 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
1992 offset;
1993 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1994 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1995 btrfs_set_file_extent_offset(leaf, fi, 0);
1996 btrfs_mark_buffer_dirty(leaf);
1997 goto out;
1998 }
1999 btrfs_release_path(path);
2000
2001 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
2002 0, 0, end - offset, 0, end - offset,
2003 0, 0, 0);
2004 if (ret)
2005 return ret;
2006
2007out:
2008 btrfs_release_path(path);
2009
2010 hole_em = alloc_extent_map();
2011 if (!hole_em) {
2012 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2013 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2014 &BTRFS_I(inode)->runtime_flags);
2015 } else {
2016 hole_em->start = offset;
2017 hole_em->len = end - offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04002018 hole_em->ram_bytes = hole_em->len;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002019 hole_em->orig_start = offset;
2020
2021 hole_em->block_start = EXTENT_MAP_HOLE;
2022 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05002023 hole_em->orig_block_len = 0;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002024 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
2025 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2026 hole_em->generation = trans->transid;
2027
2028 do {
2029 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2030 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002031 ret = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002032 write_unlock(&em_tree->lock);
2033 } while (ret == -EEXIST);
2034 free_extent_map(hole_em);
2035 if (ret)
2036 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2037 &BTRFS_I(inode)->runtime_flags);
2038 }
2039
2040 return 0;
2041}
2042
2043static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2044{
2045 struct btrfs_root *root = BTRFS_I(inode)->root;
2046 struct extent_state *cached_state = NULL;
2047 struct btrfs_path *path;
2048 struct btrfs_block_rsv *rsv;
2049 struct btrfs_trans_handle *trans;
Miao Xie00612802012-12-05 10:54:12 +00002050 u64 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize);
2051 u64 lockend = round_down(offset + len,
2052 BTRFS_I(inode)->root->sectorsize) - 1;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002053 u64 cur_offset = lockstart;
2054 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
2055 u64 drop_end;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002056 int ret = 0;
2057 int err = 0;
Miao Xie6347b3c2012-12-05 10:53:45 +00002058 bool same_page = ((offset >> PAGE_CACHE_SHIFT) ==
2059 ((offset + len - 1) >> PAGE_CACHE_SHIFT));
Josef Bacik2aaa6652012-08-29 14:27:18 -04002060
2061 btrfs_wait_ordered_range(inode, offset, len);
2062
2063 mutex_lock(&inode->i_mutex);
Miao Xie7426cc02012-12-05 10:54:52 +00002064 /*
2065 * We needn't truncate any page which is beyond the end of the file
2066 * because we are sure there is no data there.
2067 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04002068 /*
2069 * Only do this if we are in the same page and we aren't doing the
2070 * entire page.
2071 */
2072 if (same_page && len < PAGE_CACHE_SIZE) {
Miao Xie7426cc02012-12-05 10:54:52 +00002073 if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE))
2074 ret = btrfs_truncate_page(inode, offset, len, 0);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002075 mutex_unlock(&inode->i_mutex);
2076 return ret;
2077 }
2078
2079 /* zero back part of the first page */
Miao Xie7426cc02012-12-05 10:54:52 +00002080 if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
2081 ret = btrfs_truncate_page(inode, offset, 0, 0);
2082 if (ret) {
2083 mutex_unlock(&inode->i_mutex);
2084 return ret;
2085 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002086 }
2087
2088 /* zero the front end of the last page */
Miao Xie00612802012-12-05 10:54:12 +00002089 if (offset + len < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
2090 ret = btrfs_truncate_page(inode, offset + len, 0, 1);
2091 if (ret) {
2092 mutex_unlock(&inode->i_mutex);
2093 return ret;
2094 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002095 }
2096
2097 if (lockend < lockstart) {
2098 mutex_unlock(&inode->i_mutex);
2099 return 0;
2100 }
2101
2102 while (1) {
2103 struct btrfs_ordered_extent *ordered;
2104
2105 truncate_pagecache_range(inode, lockstart, lockend);
2106
2107 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2108 0, &cached_state);
2109 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
2110
2111 /*
2112 * We need to make sure we have no ordered extents in this range
2113 * and nobody raced in and read a page in this range, if we did
2114 * we need to try again.
2115 */
2116 if ((!ordered ||
2117 (ordered->file_offset + ordered->len < lockstart ||
2118 ordered->file_offset > lockend)) &&
2119 !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
2120 lockend, EXTENT_UPTODATE, 0,
2121 cached_state)) {
2122 if (ordered)
2123 btrfs_put_ordered_extent(ordered);
2124 break;
2125 }
2126 if (ordered)
2127 btrfs_put_ordered_extent(ordered);
2128 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2129 lockend, &cached_state, GFP_NOFS);
2130 btrfs_wait_ordered_range(inode, lockstart,
2131 lockend - lockstart + 1);
2132 }
2133
2134 path = btrfs_alloc_path();
2135 if (!path) {
2136 ret = -ENOMEM;
2137 goto out;
2138 }
2139
Miao Xie66d8f3d2012-09-06 04:02:28 -06002140 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002141 if (!rsv) {
2142 ret = -ENOMEM;
2143 goto out_free;
2144 }
2145 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
2146 rsv->failfast = 1;
2147
2148 /*
2149 * 1 - update the inode
2150 * 1 - removing the extents in the range
2151 * 1 - adding the hole extent
2152 */
2153 trans = btrfs_start_transaction(root, 3);
2154 if (IS_ERR(trans)) {
2155 err = PTR_ERR(trans);
2156 goto out_free;
2157 }
2158
2159 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
2160 min_size);
2161 BUG_ON(ret);
2162 trans->block_rsv = rsv;
2163
2164 while (cur_offset < lockend) {
2165 ret = __btrfs_drop_extents(trans, root, inode, path,
2166 cur_offset, lockend + 1,
2167 &drop_end, 1);
2168 if (ret != -ENOSPC)
2169 break;
2170
2171 trans->block_rsv = &root->fs_info->trans_block_rsv;
2172
2173 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2174 if (ret) {
2175 err = ret;
2176 break;
2177 }
2178
2179 cur_offset = drop_end;
2180
2181 ret = btrfs_update_inode(trans, root, inode);
2182 if (ret) {
2183 err = ret;
2184 break;
2185 }
2186
Josef Bacik2aaa6652012-08-29 14:27:18 -04002187 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002188 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002189
2190 trans = btrfs_start_transaction(root, 3);
2191 if (IS_ERR(trans)) {
2192 ret = PTR_ERR(trans);
2193 trans = NULL;
2194 break;
2195 }
2196
2197 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
2198 rsv, min_size);
2199 BUG_ON(ret); /* shouldn't happen */
2200 trans->block_rsv = rsv;
2201 }
2202
2203 if (ret) {
2204 err = ret;
2205 goto out_trans;
2206 }
2207
2208 trans->block_rsv = &root->fs_info->trans_block_rsv;
2209 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2210 if (ret) {
2211 err = ret;
2212 goto out_trans;
2213 }
2214
2215out_trans:
2216 if (!trans)
2217 goto out_free;
2218
Tsutomu Itohe1f57902012-11-08 04:47:33 +00002219 inode_inc_iversion(inode);
2220 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2221
Josef Bacik2aaa6652012-08-29 14:27:18 -04002222 trans->block_rsv = &root->fs_info->trans_block_rsv;
2223 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002224 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002225 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002226out_free:
2227 btrfs_free_path(path);
2228 btrfs_free_block_rsv(root, rsv);
2229out:
2230 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2231 &cached_state, GFP_NOFS);
2232 mutex_unlock(&inode->i_mutex);
2233 if (ret && !err)
2234 err = ret;
2235 return err;
2236}
2237
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002238static long btrfs_fallocate(struct file *file, int mode,
2239 loff_t offset, loff_t len)
2240{
Al Viro496ad9a2013-01-23 17:07:38 -05002241 struct inode *inode = file_inode(file);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002242 struct extent_state *cached_state = NULL;
Wang Shilong61130772013-03-19 10:57:14 +00002243 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002244 u64 cur_offset;
2245 u64 last_byte;
2246 u64 alloc_start;
2247 u64 alloc_end;
2248 u64 alloc_hint = 0;
2249 u64 locked_end;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002250 struct extent_map *em;
Miao Xie797f4272012-11-28 10:28:07 +00002251 int blocksize = BTRFS_I(inode)->root->sectorsize;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002252 int ret;
2253
Miao Xie797f4272012-11-28 10:28:07 +00002254 alloc_start = round_down(offset, blocksize);
2255 alloc_end = round_up(offset + len, blocksize);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002256
Josef Bacik2aaa6652012-08-29 14:27:18 -04002257 /* Make sure we aren't being give some crap mode */
2258 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002259 return -EOPNOTSUPP;
2260
Josef Bacik2aaa6652012-08-29 14:27:18 -04002261 if (mode & FALLOC_FL_PUNCH_HOLE)
2262 return btrfs_punch_hole(inode, offset, len);
2263
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002264 /*
Chris Masond98456f2012-01-31 20:27:41 -05002265 * Make sure we have enough space before we do the
2266 * allocation.
2267 */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002268 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Chris Masond98456f2012-01-31 20:27:41 -05002269 if (ret)
2270 return ret;
Wang Shilong61130772013-03-19 10:57:14 +00002271 if (root->fs_info->quota_enabled) {
2272 ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
2273 if (ret)
2274 goto out_reserve_fail;
2275 }
Chris Masond98456f2012-01-31 20:27:41 -05002276
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002277 mutex_lock(&inode->i_mutex);
2278 ret = inode_newsize_ok(inode, alloc_end);
2279 if (ret)
2280 goto out;
2281
2282 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002283 ret = btrfs_cont_expand(inode, i_size_read(inode),
2284 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002285 if (ret)
2286 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04002287 } else {
2288 /*
2289 * If we are fallocating from the end of the file onward we
2290 * need to zero out the end of the page if i_size lands in the
2291 * middle of a page.
2292 */
2293 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
2294 if (ret)
2295 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002296 }
2297
Josef Bacika71754f2013-06-17 17:14:39 -04002298 /*
2299 * wait for ordered IO before we have any locks. We'll loop again
2300 * below with the locks held.
2301 */
2302 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2303
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002304 locked_end = alloc_end - 1;
2305 while (1) {
2306 struct btrfs_ordered_extent *ordered;
2307
2308 /* the extent lock is ordered inside the running
2309 * transaction
2310 */
2311 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002312 locked_end, 0, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002313 ordered = btrfs_lookup_first_ordered_extent(inode,
2314 alloc_end - 1);
2315 if (ordered &&
2316 ordered->file_offset + ordered->len > alloc_start &&
2317 ordered->file_offset < alloc_end) {
2318 btrfs_put_ordered_extent(ordered);
2319 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2320 alloc_start, locked_end,
2321 &cached_state, GFP_NOFS);
2322 /*
2323 * we can't wait on the range with the transaction
2324 * running or with the extent lock held
2325 */
2326 btrfs_wait_ordered_range(inode, alloc_start,
2327 alloc_end - alloc_start);
2328 } else {
2329 if (ordered)
2330 btrfs_put_ordered_extent(ordered);
2331 break;
2332 }
2333 }
2334
2335 cur_offset = alloc_start;
2336 while (1) {
Josef Bacikf1e490a2011-08-18 10:36:39 -04002337 u64 actual_end;
2338
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002339 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2340 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002341 if (IS_ERR_OR_NULL(em)) {
2342 if (!em)
2343 ret = -ENOMEM;
2344 else
2345 ret = PTR_ERR(em);
2346 break;
2347 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002348 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002349 actual_end = min_t(u64, extent_map_end(em), offset + len);
Miao Xie797f4272012-11-28 10:28:07 +00002350 last_byte = ALIGN(last_byte, blocksize);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002351
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002352 if (em->block_start == EXTENT_MAP_HOLE ||
2353 (cur_offset >= inode->i_size &&
2354 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2355 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2356 last_byte - cur_offset,
2357 1 << inode->i_blkbits,
2358 offset + len,
2359 &alloc_hint);
Josef Bacik1b9c3322011-08-17 10:19:52 -04002360
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002361 if (ret < 0) {
2362 free_extent_map(em);
2363 break;
2364 }
Josef Bacikf1e490a2011-08-18 10:36:39 -04002365 } else if (actual_end > inode->i_size &&
2366 !(mode & FALLOC_FL_KEEP_SIZE)) {
2367 /*
2368 * We didn't need to allocate any more space, but we
2369 * still extended the size of the file so we need to
2370 * update i_size.
2371 */
2372 inode->i_ctime = CURRENT_TIME;
2373 i_size_write(inode, actual_end);
2374 btrfs_ordered_update_i_size(inode, actual_end, NULL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002375 }
2376 free_extent_map(em);
2377
2378 cur_offset = last_byte;
2379 if (cur_offset >= alloc_end) {
2380 ret = 0;
2381 break;
2382 }
2383 }
2384 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2385 &cached_state, GFP_NOFS);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002386out:
2387 mutex_unlock(&inode->i_mutex);
Wang Shilong61130772013-03-19 10:57:14 +00002388 if (root->fs_info->quota_enabled)
2389 btrfs_qgroup_free(root, alloc_end - alloc_start);
2390out_reserve_fail:
Chris Masond98456f2012-01-31 20:27:41 -05002391 /* Let go of our reservation. */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002392 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002393 return ret;
2394}
2395
Andrew Morton965c8e52012-12-17 15:59:39 -08002396static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002397{
2398 struct btrfs_root *root = BTRFS_I(inode)->root;
2399 struct extent_map *em;
2400 struct extent_state *cached_state = NULL;
2401 u64 lockstart = *offset;
2402 u64 lockend = i_size_read(inode);
2403 u64 start = *offset;
2404 u64 orig_start = *offset;
2405 u64 len = i_size_read(inode);
2406 u64 last_end = 0;
2407 int ret = 0;
2408
2409 lockend = max_t(u64, root->sectorsize, lockend);
2410 if (lockend <= lockstart)
2411 lockend = lockstart + root->sectorsize;
2412
Liu Bo1214b532013-01-07 03:53:08 +00002413 lockend--;
Josef Bacikb2675152011-07-18 13:21:36 -04002414 len = lockend - lockstart + 1;
2415
2416 len = max_t(u64, len, root->sectorsize);
2417 if (inode->i_size == 0)
2418 return -ENXIO;
2419
2420 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002421 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002422
2423 /*
2424 * Delalloc is such a pain. If we have a hole and we have pending
2425 * delalloc for a portion of the hole we will get back a hole that
2426 * exists for the entire range since it hasn't been actually written
2427 * yet. So to take care of this case we need to look for an extent just
2428 * before the position we want in case there is outstanding delalloc
2429 * going on here.
2430 */
Andrew Morton965c8e52012-12-17 15:59:39 -08002431 if (whence == SEEK_HOLE && start != 0) {
Josef Bacikb2675152011-07-18 13:21:36 -04002432 if (start <= root->sectorsize)
2433 em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
2434 root->sectorsize, 0);
2435 else
2436 em = btrfs_get_extent_fiemap(inode, NULL, 0,
2437 start - root->sectorsize,
2438 root->sectorsize, 0);
2439 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002440 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002441 goto out;
2442 }
2443 last_end = em->start + em->len;
2444 if (em->block_start == EXTENT_MAP_DELALLOC)
2445 last_end = min_t(u64, last_end, inode->i_size);
2446 free_extent_map(em);
2447 }
2448
2449 while (1) {
2450 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2451 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002452 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002453 break;
2454 }
2455
2456 if (em->block_start == EXTENT_MAP_HOLE) {
2457 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2458 if (last_end <= orig_start) {
2459 free_extent_map(em);
2460 ret = -ENXIO;
2461 break;
2462 }
2463 }
2464
Andrew Morton965c8e52012-12-17 15:59:39 -08002465 if (whence == SEEK_HOLE) {
Josef Bacikb2675152011-07-18 13:21:36 -04002466 *offset = start;
2467 free_extent_map(em);
2468 break;
2469 }
2470 } else {
Andrew Morton965c8e52012-12-17 15:59:39 -08002471 if (whence == SEEK_DATA) {
Josef Bacikb2675152011-07-18 13:21:36 -04002472 if (em->block_start == EXTENT_MAP_DELALLOC) {
2473 if (start >= inode->i_size) {
2474 free_extent_map(em);
2475 ret = -ENXIO;
2476 break;
2477 }
2478 }
2479
Liu Bof9e4fb52013-01-07 10:10:12 +00002480 if (!test_bit(EXTENT_FLAG_PREALLOC,
2481 &em->flags)) {
2482 *offset = start;
2483 free_extent_map(em);
2484 break;
2485 }
Josef Bacikb2675152011-07-18 13:21:36 -04002486 }
2487 }
2488
2489 start = em->start + em->len;
2490 last_end = em->start + em->len;
2491
2492 if (em->block_start == EXTENT_MAP_DELALLOC)
2493 last_end = min_t(u64, last_end, inode->i_size);
2494
2495 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2496 free_extent_map(em);
2497 ret = -ENXIO;
2498 break;
2499 }
2500 free_extent_map(em);
2501 cond_resched();
2502 }
2503 if (!ret)
2504 *offset = min(*offset, inode->i_size);
2505out:
2506 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2507 &cached_state, GFP_NOFS);
2508 return ret;
2509}
2510
Andrew Morton965c8e52012-12-17 15:59:39 -08002511static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002512{
2513 struct inode *inode = file->f_mapping->host;
2514 int ret;
2515
2516 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -08002517 switch (whence) {
Josef Bacikb2675152011-07-18 13:21:36 -04002518 case SEEK_END:
2519 case SEEK_CUR:
Andrew Morton965c8e52012-12-17 15:59:39 -08002520 offset = generic_file_llseek(file, offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04002521 goto out;
2522 case SEEK_DATA:
2523 case SEEK_HOLE:
Jeff Liu48802c82011-09-18 10:34:02 -04002524 if (offset >= i_size_read(inode)) {
2525 mutex_unlock(&inode->i_mutex);
2526 return -ENXIO;
2527 }
2528
Andrew Morton965c8e52012-12-17 15:59:39 -08002529 ret = find_desired_extent(inode, &offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04002530 if (ret) {
2531 mutex_unlock(&inode->i_mutex);
2532 return ret;
2533 }
2534 }
2535
Jie Liu46a1c2c2013-06-25 12:02:13 +08002536 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Josef Bacikb2675152011-07-18 13:21:36 -04002537out:
2538 mutex_unlock(&inode->i_mutex);
2539 return offset;
2540}
2541
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002542const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04002543 .llseek = btrfs_file_llseek,
Chris Mason39279cc2007-06-12 06:35:45 -04002544 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00002545 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002546 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05002547 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04002548 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002549 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04002550 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04002551 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002552 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002553 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002554 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002555#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002556 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002557#endif
2558};
Miao Xie9247f312012-11-26 09:24:43 +00002559
2560void btrfs_auto_defrag_exit(void)
2561{
2562 if (btrfs_inode_defrag_cachep)
2563 kmem_cache_destroy(btrfs_inode_defrag_cachep);
2564}
2565
2566int btrfs_auto_defrag_init(void)
2567{
2568 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
2569 sizeof(struct inode_defrag), 0,
2570 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2571 NULL);
2572 if (!btrfs_inode_defrag_cachep)
2573 return -ENOMEM;
2574
2575 return 0;
2576}