blob: ad7c05909a49b387b72e5ce02dd8d6bbc65fb925 [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"
Josef Bacik2aaa6652012-08-29 14:27:18 -040042#include "volumes.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070043#include "qgroup.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);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530373 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
Xin Zhong914ee292010-12-09 09:30:14 +0000429 */
Xin Zhong914ee292010-12-09 09:30:14 +0000430 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400431
Chris Mason39279cc2007-06-12 06:35:45 -0400432 /* Flush processor's dcache for this page */
433 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -0500434
435 /*
436 * if we get a partial write, we can end up with
437 * partially up to date pages. These add
438 * a lot of complexity, so make sure they don't
439 * happen by forcing this copy to be retried.
440 *
441 * The rest of the btrfs_file_write code will fall
442 * back to page at a time copies after we return 0.
443 */
444 if (!PageUptodate(page) && copied < count)
445 copied = 0;
446
Josef Bacik11c65dc2010-05-23 11:07:21 -0400447 iov_iter_advance(i, copied);
448 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +0000449 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400450
Xin Zhong914ee292010-12-09 09:30:14 +0000451 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik9f570b82011-01-25 12:42:37 -0500452 if (unlikely(copied == 0))
Xin Zhong914ee292010-12-09 09:30:14 +0000453 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400454
455 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
456 offset += copied;
457 } else {
458 pg++;
459 offset = 0;
460 }
Chris Mason39279cc2007-06-12 06:35:45 -0400461 }
Xin Zhong914ee292010-12-09 09:30:14 +0000462 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400463}
464
Chris Masond352ac62008-09-29 15:18:18 -0400465/*
466 * unlocks pages after btrfs_file_write is done with them
467 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000468static void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400469{
470 size_t i;
471 for (i = 0; i < num_pages; i++) {
Chris Masond352ac62008-09-29 15:18:18 -0400472 /* page checked is some magic around finding pages that
473 * have been modified without going through btrfs_set_page_dirty
474 * clear it here
475 */
Chris Mason4a096752008-07-21 10:29:44 -0400476 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400477 unlock_page(pages[i]);
478 mark_page_accessed(pages[i]);
479 page_cache_release(pages[i]);
480 }
481}
482
Chris Masond352ac62008-09-29 15:18:18 -0400483/*
484 * after copy_from_user, pages need to be dirtied and we need to make
485 * sure holes are created between the current EOF and the start of
486 * any next extents (if required).
487 *
488 * this also makes the decision about creating an inline extent vs
489 * doing real data extents, marking pages dirty and delalloc as required.
490 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400491int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
Eric Sandeen48a3b632013-04-25 20:41:01 +0000492 struct page **pages, size_t num_pages,
493 loff_t pos, size_t write_bytes,
494 struct extent_state **cached)
Chris Mason39279cc2007-06-12 06:35:45 -0400495{
Chris Mason39279cc2007-06-12 06:35:45 -0400496 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400497 int i;
Chris Masondb945352007-10-15 16:15:53 -0400498 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400499 u64 start_pos;
500 u64 end_of_last_block;
501 u64 end_pos = pos + write_bytes;
502 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400503
Chris Mason5f39d392007-10-15 16:14:19 -0400504 start_pos = pos & ~((u64)root->sectorsize - 1);
Qu Wenruofda28322013-02-26 08:10:22 +0000505 num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize);
Chris Mason39279cc2007-06-12 06:35:45 -0400506
Chris Masondb945352007-10-15 16:15:53 -0400507 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000508 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400509 cached);
Josef Bacikd0215f32011-01-25 14:57:24 -0500510 if (err)
511 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400512
Chris Masonc8b97812008-10-29 14:49:59 -0400513 for (i = 0; i < num_pages; i++) {
514 struct page *p = pages[i];
515 SetPageUptodate(p);
516 ClearPageChecked(p);
517 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400518 }
Josef Bacik9f570b82011-01-25 12:42:37 -0500519
520 /*
521 * we've only changed i_size in ram, and we haven't updated
522 * the disk i_size. There is no need to log the inode
523 * at this time.
524 */
525 if (end_pos > isize)
Chris Masona52d9a82007-08-27 16:49:44 -0400526 i_size_write(inode, end_pos);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400527 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400528}
529
Chris Masond352ac62008-09-29 15:18:18 -0400530/*
531 * this drops all the extents in the cache that intersect the range
532 * [start, end]. Existing extents are split as required.
533 */
Josef Bacik7014cdb2012-08-30 20:06:49 -0400534void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
535 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400536{
537 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400538 struct extent_map *split = NULL;
539 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400540 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500541 u64 len = end - start + 1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400542 u64 gen;
Chris Mason3b951512008-04-17 11:29:12 -0400543 int ret;
544 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400545 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400546 int compressed = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400547 bool modified;
Chris Masona52d9a82007-08-27 16:49:44 -0400548
Chris Masone6dcd2d2008-07-17 12:53:50 -0400549 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400550 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500551 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400552 testend = 0;
553 }
Chris Masond3977122009-01-05 21:25:51 -0500554 while (1) {
Josef Bacik7014cdb2012-08-30 20:06:49 -0400555 int no_splits = 0;
556
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400557 modified = false;
Chris Mason3b951512008-04-17 11:29:12 -0400558 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200559 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400560 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200561 split2 = alloc_extent_map();
Josef Bacik7014cdb2012-08-30 20:06:49 -0400562 if (!split || !split2)
563 no_splits = 1;
Chris Mason3b951512008-04-17 11:29:12 -0400564
Chris Mason890871b2009-09-02 16:24:52 -0400565 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500566 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500567 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400568 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400569 break;
Chris Masond1310b22008-01-24 16:13:08 -0500570 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400571 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400572 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400573 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000574 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400575 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400576 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400577 break;
578 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000579 start = em->start + em->len;
580 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400581 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400582 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400583 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400584 continue;
585 }
Chris Masonc8b97812008-10-29 14:49:59 -0400586 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400587 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo3b277592013-03-15 08:46:39 -0600588 clear_bit(EXTENT_FLAG_LOGGING, &flags);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400589 modified = !list_empty(&em->list);
Josef Bacik7014cdb2012-08-30 20:06:49 -0400590 if (no_splits)
591 goto next;
Chris Mason3b951512008-04-17 11:29:12 -0400592
Josef Bacikee20a982013-07-11 10:34:59 -0400593 if (em->start < start) {
Chris Mason3b951512008-04-17 11:29:12 -0400594 split->start = em->start;
595 split->len = start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400596
Josef Bacikee20a982013-07-11 10:34:59 -0400597 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
598 split->orig_start = em->orig_start;
599 split->block_start = em->block_start;
600
601 if (compressed)
602 split->block_len = em->block_len;
603 else
604 split->block_len = split->len;
605 split->orig_block_len = max(split->block_len,
606 em->orig_block_len);
607 split->ram_bytes = em->ram_bytes;
608 } else {
609 split->orig_start = split->start;
610 split->block_len = 0;
611 split->block_start = em->block_start;
612 split->orig_block_len = 0;
613 split->ram_bytes = split->len;
614 }
615
Josef Bacik5dc562c2012-08-17 13:14:17 -0400616 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400617 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400618 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800619 split->compress_type = em->compress_type;
Filipe Manana176840b2014-02-25 14:15:13 +0000620 replace_extent_mapping(em_tree, em, split, modified);
Chris Mason3b951512008-04-17 11:29:12 -0400621 free_extent_map(split);
622 split = split2;
623 split2 = NULL;
624 }
Josef Bacikee20a982013-07-11 10:34:59 -0400625 if (testend && em->start + em->len > start + len) {
Chris Mason3b951512008-04-17 11:29:12 -0400626 u64 diff = start + len - em->start;
627
628 split->start = start + len;
629 split->len = em->start + em->len - (start + len);
630 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400631 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800632 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400633 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400634
Josef Bacikee20a982013-07-11 10:34:59 -0400635 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
636 split->orig_block_len = max(em->block_len,
637 em->orig_block_len);
638
639 split->ram_bytes = em->ram_bytes;
640 if (compressed) {
641 split->block_len = em->block_len;
642 split->block_start = em->block_start;
643 split->orig_start = em->orig_start;
644 } else {
645 split->block_len = split->len;
646 split->block_start = em->block_start
647 + diff;
648 split->orig_start = em->orig_start;
649 }
Chris Masonc8b97812008-10-29 14:49:59 -0400650 } else {
Josef Bacikee20a982013-07-11 10:34:59 -0400651 split->ram_bytes = split->len;
652 split->orig_start = split->start;
653 split->block_len = 0;
654 split->block_start = em->block_start;
655 split->orig_block_len = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400656 }
Chris Mason3b951512008-04-17 11:29:12 -0400657
Filipe Manana176840b2014-02-25 14:15:13 +0000658 if (extent_map_in_tree(em)) {
659 replace_extent_mapping(em_tree, em, split,
660 modified);
661 } else {
662 ret = add_extent_mapping(em_tree, split,
663 modified);
664 ASSERT(ret == 0); /* Logic error */
665 }
Chris Mason3b951512008-04-17 11:29:12 -0400666 free_extent_map(split);
667 split = NULL;
668 }
Josef Bacik7014cdb2012-08-30 20:06:49 -0400669next:
Filipe Manana176840b2014-02-25 14:15:13 +0000670 if (extent_map_in_tree(em))
671 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400672 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500673
Chris Masona52d9a82007-08-27 16:49:44 -0400674 /* once for us */
675 free_extent_map(em);
676 /* once for the tree*/
677 free_extent_map(em);
678 }
Chris Mason3b951512008-04-17 11:29:12 -0400679 if (split)
680 free_extent_map(split);
681 if (split2)
682 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400683}
684
Chris Mason39279cc2007-06-12 06:35:45 -0400685/*
686 * this is very complex, but the basic idea is to drop all extents
687 * in the range start - end. hint_block is filled in with a block number
688 * that would be a good hint to the block allocator for this file.
689 *
690 * If an extent intersects the range but is not entirely inside the range
691 * it is either truncated or split. Anything entirely inside the range
692 * is deleted from the tree.
693 */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400694int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
695 struct btrfs_root *root, struct inode *inode,
696 struct btrfs_path *path, u64 start, u64 end,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000697 u64 *drop_end, int drop_cache,
698 int replace_extent,
699 u32 extent_item_size,
700 int *key_inserted)
Chris Mason39279cc2007-06-12 06:35:45 -0400701{
Chris Mason00f5c792007-11-30 10:09:33 -0500702 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000703 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500704 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000705 struct btrfs_key new_key;
Li Zefan33345d012011-04-20 10:31:50 +0800706 u64 ino = btrfs_ino(inode);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000707 u64 search_start = start;
708 u64 disk_bytenr = 0;
709 u64 num_bytes = 0;
710 u64 extent_offset = 0;
711 u64 extent_end = 0;
712 int del_nr = 0;
713 int del_slot = 0;
714 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400715 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500716 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400717 int modify_tree = -1;
Miao Xie27cdeb72014-04-02 19:51:05 +0800718 int update_refs;
Josef Bacikc3308f82012-09-14 14:51:22 -0400719 int found = 0;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000720 int leafs_visited = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400721
Chris Masona1ed8352009-09-11 12:27:37 -0400722 if (drop_cache)
723 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400724
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000725 if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
Chris Masondc7fdde2012-04-27 14:31:29 -0400726 modify_tree = 0;
727
Miao Xie27cdeb72014-04-02 19:51:05 +0800728 update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
729 root == root->fs_info->tree_root);
Chris Masond3977122009-01-05 21:25:51 -0500730 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400731 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800732 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400733 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400734 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000735 break;
736 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
737 leaf = path->nodes[0];
738 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800739 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000740 key.type == BTRFS_EXTENT_DATA_KEY)
741 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400742 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400743 ret = 0;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000744 leafs_visited++;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000745next_slot:
746 leaf = path->nodes[0];
747 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
748 BUG_ON(del_nr > 0);
749 ret = btrfs_next_leaf(root, path);
750 if (ret < 0)
751 break;
752 if (ret > 0) {
753 ret = 0;
754 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400755 }
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000756 leafs_visited++;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000757 leaf = path->nodes[0];
758 recow = 1;
759 }
760
761 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800762 if (key.objectid > ino ||
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000763 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
764 break;
765
766 fi = btrfs_item_ptr(leaf, path->slots[0],
767 struct btrfs_file_extent_item);
768 extent_type = btrfs_file_extent_type(leaf, fi);
769
770 if (extent_type == BTRFS_FILE_EXTENT_REG ||
771 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
772 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
773 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
774 extent_offset = btrfs_file_extent_offset(leaf, fi);
775 extent_end = key.offset +
776 btrfs_file_extent_num_bytes(leaf, fi);
777 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
778 extent_end = key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -0800779 btrfs_file_extent_inline_len(leaf,
780 path->slots[0], fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400781 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000782 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400783 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400784 }
785
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100786 /*
787 * Don't skip extent items representing 0 byte lengths. They
788 * used to be created (bug) if while punching holes we hit
789 * -ENOSPC condition. So if we find one here, just ensure we
790 * delete it, otherwise we would insert a new file extent item
791 * with the same key (offset) as that 0 bytes length file
792 * extent item in the call to setup_items_for_insert() later
793 * in this function.
794 */
795 if (extent_end == key.offset && extent_end >= search_start)
796 goto delete_extent_item;
797
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000798 if (extent_end <= search_start) {
799 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400800 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400801 }
802
Josef Bacikc3308f82012-09-14 14:51:22 -0400803 found = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000804 search_start = max(key.offset, start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400805 if (recow || !modify_tree) {
806 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200807 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000808 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400809 }
Chris Mason771ed682008-11-06 22:02:51 -0500810
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000811 /*
812 * | - range to drop - |
813 * | -------- extent -------- |
814 */
815 if (start > key.offset && end < extent_end) {
816 BUG_ON(del_nr > 0);
Liu Bo00fdf132014-03-10 18:56:07 +0800817 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200818 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800819 break;
820 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000821
822 memcpy(&new_key, &key, sizeof(new_key));
823 new_key.offset = start;
824 ret = btrfs_duplicate_item(trans, root, path,
825 &new_key);
826 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200827 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000828 continue;
829 }
830 if (ret < 0)
831 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400832
Chris Mason5f39d392007-10-15 16:14:19 -0400833 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000834 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
835 struct btrfs_file_extent_item);
836 btrfs_set_file_extent_num_bytes(leaf, fi,
837 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400838
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000839 fi = btrfs_item_ptr(leaf, path->slots[0],
840 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400841
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000842 extent_offset += start - key.offset;
843 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
844 btrfs_set_file_extent_num_bytes(leaf, fi,
845 extent_end - start);
846 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400847
Josef Bacik5dc562c2012-08-17 13:14:17 -0400848 if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000849 ret = btrfs_inc_extent_ref(trans, root,
850 disk_bytenr, num_bytes, 0,
851 root->root_key.objectid,
852 new_key.objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -0700853 start - extent_offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100854 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400855 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000856 key.offset = start;
857 }
858 /*
859 * | ---- range to drop ----- |
860 * | -------- extent -------- |
861 */
862 if (start <= key.offset && end < extent_end) {
Liu Bo00fdf132014-03-10 18:56:07 +0800863 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200864 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800865 break;
866 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000867
868 memcpy(&new_key, &key, sizeof(new_key));
869 new_key.offset = end;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000870 btrfs_set_item_key_safe(root, path, &new_key);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000871
872 extent_offset += end - key.offset;
873 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
874 btrfs_set_file_extent_num_bytes(leaf, fi,
875 extent_end - end);
876 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400877 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000878 inode_sub_bytes(inode, end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000879 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400880 }
881
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000882 search_start = extent_end;
883 /*
884 * | ---- range to drop ----- |
885 * | -------- extent -------- |
886 */
887 if (start > key.offset && end >= extent_end) {
888 BUG_ON(del_nr > 0);
Liu Bo00fdf132014-03-10 18:56:07 +0800889 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200890 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800891 break;
892 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000893
894 btrfs_set_file_extent_num_bytes(leaf, fi,
895 start - key.offset);
896 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400897 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000898 inode_sub_bytes(inode, extent_end - start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000899 if (end == extent_end)
900 break;
901
902 path->slots[0]++;
903 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400904 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000905
906 /*
907 * | ---- range to drop ----- |
908 * | ------ extent ------ |
909 */
910 if (start <= key.offset && end >= extent_end) {
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100911delete_extent_item:
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000912 if (del_nr == 0) {
913 del_slot = path->slots[0];
914 del_nr = 1;
915 } else {
916 BUG_ON(del_slot + del_nr != path->slots[0]);
917 del_nr++;
918 }
919
Josef Bacik5dc562c2012-08-17 13:14:17 -0400920 if (update_refs &&
921 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000922 inode_sub_bytes(inode,
923 extent_end - key.offset);
924 extent_end = ALIGN(extent_end,
925 root->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400926 } else if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000927 ret = btrfs_free_extent(trans, root,
928 disk_bytenr, num_bytes, 0,
929 root->root_key.objectid,
930 key.objectid, key.offset -
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200931 extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100932 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000933 inode_sub_bytes(inode,
934 extent_end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000935 }
936
937 if (end == extent_end)
938 break;
939
940 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
941 path->slots[0]++;
942 goto next_slot;
943 }
944
945 ret = btrfs_del_items(trans, root, path, del_slot,
946 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100947 if (ret) {
948 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400949 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100950 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000951
952 del_nr = 0;
953 del_slot = 0;
954
David Sterbab3b4aa72011-04-21 01:20:15 +0200955 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000956 continue;
957 }
958
959 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400960 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000961
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962 if (!ret && del_nr > 0) {
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000963 /*
964 * Set path->slots[0] to first slot, so that after the delete
965 * if items are move off from our leaf to its immediate left or
966 * right neighbor leafs, we end up with a correct and adjusted
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000967 * path->slots[0] for our insertion (if replace_extent != 0).
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000968 */
969 path->slots[0] = del_slot;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000970 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 if (ret)
972 btrfs_abort_transaction(trans, root, ret);
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000973 }
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000974
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000975 leaf = path->nodes[0];
976 /*
977 * If btrfs_del_items() was called, it might have deleted a leaf, in
978 * which case it unlocked our path, so check path->locks[0] matches a
979 * write lock.
980 */
981 if (!ret && replace_extent && leafs_visited == 1 &&
982 (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
983 path->locks[0] == BTRFS_WRITE_LOCK) &&
984 btrfs_leaf_free_space(root, leaf) >=
985 sizeof(struct btrfs_item) + extent_item_size) {
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000986
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000987 key.objectid = ino;
988 key.type = BTRFS_EXTENT_DATA_KEY;
989 key.offset = start;
990 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
991 struct btrfs_key slot_key;
992
993 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
994 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
995 path->slots[0]++;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000996 }
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000997 setup_items_for_insert(root, path, &key,
998 &extent_item_size,
999 extent_item_size,
1000 sizeof(struct btrfs_item) +
1001 extent_item_size, 1);
1002 *key_inserted = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001003 }
1004
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001005 if (!replace_extent || !(*key_inserted))
1006 btrfs_release_path(path);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001007 if (drop_end)
Josef Bacikc3308f82012-09-14 14:51:22 -04001008 *drop_end = found ? min(end, extent_end) : end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001009 return ret;
1010}
1011
1012int btrfs_drop_extents(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root, struct inode *inode, u64 start,
Josef Bacik26714852012-08-29 12:24:27 -04001014 u64 end, int drop_cache)
Josef Bacik5dc562c2012-08-17 13:14:17 -04001015{
1016 struct btrfs_path *path;
1017 int ret;
1018
1019 path = btrfs_alloc_path();
1020 if (!path)
1021 return -ENOMEM;
Josef Bacik2aaa6652012-08-29 14:27:18 -04001022 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001023 drop_cache, 0, 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04001024 btrfs_free_path(path);
1025 return ret;
1026}
1027
Yan Zhengd899e052008-10-30 14:25:28 -04001028static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001029 u64 objectid, u64 bytenr, u64 orig_offset,
1030 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -04001031{
1032 struct btrfs_file_extent_item *fi;
1033 struct btrfs_key key;
1034 u64 extent_end;
1035
1036 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1037 return 0;
1038
1039 btrfs_item_key_to_cpu(leaf, &key, slot);
1040 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
1041 return 0;
1042
1043 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1044 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
1045 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001046 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -04001047 btrfs_file_extent_compression(leaf, fi) ||
1048 btrfs_file_extent_encryption(leaf, fi) ||
1049 btrfs_file_extent_other_encoding(leaf, fi))
1050 return 0;
1051
1052 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1053 if ((*start && *start != key.offset) || (*end && *end != extent_end))
1054 return 0;
1055
1056 *start = key.offset;
1057 *end = extent_end;
1058 return 1;
1059}
1060
1061/*
1062 * Mark extent in the range start - end as written.
1063 *
1064 * This changes extent type from 'pre-allocated' to 'regular'. If only
1065 * part of extent is marked as written, the extent will be split into
1066 * two or three.
1067 */
1068int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -04001069 struct inode *inode, u64 start, u64 end)
1070{
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001071 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -04001072 struct extent_buffer *leaf;
1073 struct btrfs_path *path;
1074 struct btrfs_file_extent_item *fi;
1075 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001076 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -04001077 u64 bytenr;
1078 u64 num_bytes;
1079 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001080 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001081 u64 other_start;
1082 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001083 u64 split;
1084 int del_nr = 0;
1085 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001086 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -04001087 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08001088 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001089
Yan Zhengd899e052008-10-30 14:25:28 -04001090 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001091 if (!path)
1092 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001093again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001094 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001095 split = start;
Li Zefan33345d012011-04-20 10:31:50 +08001096 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -04001097 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001098 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -04001099
1100 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -04001101 if (ret < 0)
1102 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001103 if (ret > 0 && path->slots[0] > 0)
1104 path->slots[0]--;
1105
1106 leaf = path->nodes[0];
1107 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001108 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
Yan Zhengd899e052008-10-30 14:25:28 -04001109 fi = btrfs_item_ptr(leaf, path->slots[0],
1110 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001111 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
1112 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -04001113 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1114 BUG_ON(key.offset > start || extent_end < end);
1115
1116 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1117 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001119 memcpy(&new_key, &key, sizeof(new_key));
1120
1121 if (start == key.offset && end < extent_end) {
1122 other_start = 0;
1123 other_end = start;
1124 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001125 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001126 &other_start, &other_end)) {
1127 new_key.offset = end;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001128 btrfs_set_item_key_safe(root, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001129 fi = btrfs_item_ptr(leaf, path->slots[0],
1130 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001131 btrfs_set_file_extent_generation(leaf, fi,
1132 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001133 btrfs_set_file_extent_num_bytes(leaf, fi,
1134 extent_end - end);
1135 btrfs_set_file_extent_offset(leaf, fi,
1136 end - orig_offset);
1137 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1138 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001139 btrfs_set_file_extent_generation(leaf, fi,
1140 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001141 btrfs_set_file_extent_num_bytes(leaf, fi,
1142 end - other_start);
1143 btrfs_mark_buffer_dirty(leaf);
1144 goto out;
1145 }
1146 }
1147
1148 if (start > key.offset && end == extent_end) {
1149 other_start = end;
1150 other_end = 0;
1151 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001152 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001153 &other_start, &other_end)) {
1154 fi = btrfs_item_ptr(leaf, path->slots[0],
1155 struct btrfs_file_extent_item);
1156 btrfs_set_file_extent_num_bytes(leaf, fi,
1157 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -04001158 btrfs_set_file_extent_generation(leaf, fi,
1159 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001160 path->slots[0]++;
1161 new_key.offset = start;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001162 btrfs_set_item_key_safe(root, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001163
1164 fi = btrfs_item_ptr(leaf, path->slots[0],
1165 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001166 btrfs_set_file_extent_generation(leaf, fi,
1167 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001168 btrfs_set_file_extent_num_bytes(leaf, fi,
1169 other_end - start);
1170 btrfs_set_file_extent_offset(leaf, fi,
1171 start - orig_offset);
1172 btrfs_mark_buffer_dirty(leaf);
1173 goto out;
1174 }
1175 }
Yan Zhengd899e052008-10-30 14:25:28 -04001176
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001177 while (start > key.offset || end < extent_end) {
1178 if (key.offset == start)
1179 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001180
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001181 new_key.offset = split;
1182 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1183 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001184 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001185 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001186 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 if (ret < 0) {
1188 btrfs_abort_transaction(trans, root, ret);
1189 goto out;
1190 }
Yan Zhengd899e052008-10-30 14:25:28 -04001191
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001192 leaf = path->nodes[0];
1193 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001194 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001195 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001196 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001197 split - key.offset);
1198
1199 fi = btrfs_item_ptr(leaf, path->slots[0],
1200 struct btrfs_file_extent_item);
1201
Josef Bacik224ecce2012-08-16 16:32:06 -04001202 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001203 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1204 btrfs_set_file_extent_num_bytes(leaf, fi,
1205 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001206 btrfs_mark_buffer_dirty(leaf);
1207
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001208 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1209 root->root_key.objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07001210 ino, orig_offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001211 BUG_ON(ret); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001212
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001213 if (split == start) {
1214 key.offset = start;
1215 } else {
1216 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -04001217 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001218 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001219 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001220 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001221 }
1222
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001223 other_start = end;
1224 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001225 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001226 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001227 &other_start, &other_end)) {
1228 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001229 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001230 goto again;
1231 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001232 extent_end = other_end;
1233 del_slot = path->slots[0] + 1;
1234 del_nr++;
1235 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1236 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001237 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001238 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001239 }
1240 other_start = 0;
1241 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001242 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001243 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001244 &other_start, &other_end)) {
1245 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001246 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001247 goto again;
1248 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001249 key.offset = other_start;
1250 del_slot = path->slots[0];
1251 del_nr++;
1252 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1253 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001254 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001255 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001256 }
1257 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001258 fi = btrfs_item_ptr(leaf, path->slots[0],
1259 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001260 btrfs_set_file_extent_type(leaf, fi,
1261 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001262 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001263 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001264 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001265 fi = btrfs_item_ptr(leaf, del_slot - 1,
1266 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001267 btrfs_set_file_extent_type(leaf, fi,
1268 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001269 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001270 btrfs_set_file_extent_num_bytes(leaf, fi,
1271 extent_end - key.offset);
1272 btrfs_mark_buffer_dirty(leaf);
1273
1274 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 if (ret < 0) {
1276 btrfs_abort_transaction(trans, root, ret);
1277 goto out;
1278 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001279 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001280out:
Yan Zhengd899e052008-10-30 14:25:28 -04001281 btrfs_free_path(path);
1282 return 0;
1283}
1284
Chris Mason39279cc2007-06-12 06:35:45 -04001285/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001286 * on error we return an unlocked page and the error value
1287 * on success we return a locked page and 0
1288 */
Josef Bacikb63164292011-09-30 15:23:54 -04001289static int prepare_uptodate_page(struct page *page, u64 pos,
1290 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001291{
1292 int ret = 0;
1293
Josef Bacikb63164292011-09-30 15:23:54 -04001294 if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1295 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001296 ret = btrfs_readpage(NULL, page);
1297 if (ret)
1298 return ret;
1299 lock_page(page);
1300 if (!PageUptodate(page)) {
1301 unlock_page(page);
1302 return -EIO;
1303 }
1304 }
1305 return 0;
1306}
1307
1308/*
Miao Xie376cc682013-12-10 19:25:04 +08001309 * this just gets pages into the page cache and locks them down.
Chris Mason39279cc2007-06-12 06:35:45 -04001310 */
Miao Xieb37392e2013-12-10 19:25:03 +08001311static noinline int prepare_pages(struct inode *inode, struct page **pages,
1312 size_t num_pages, loff_t pos,
1313 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001314{
1315 int i;
1316 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001317 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Filipe David Borba Mananafc28b622013-12-13 19:39:34 +00001318 int err = 0;
Miao Xie376cc682013-12-10 19:25:04 +08001319 int faili;
Chris Mason8c2383c2007-06-18 09:57:58 -04001320
Chris Mason39279cc2007-06-12 06:35:45 -04001321 for (i = 0; i < num_pages; i++) {
Josef Bacika94733d2011-07-11 10:47:06 -04001322 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001323 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001324 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001325 faili = i - 1;
1326 err = -ENOMEM;
1327 goto fail;
1328 }
1329
1330 if (i == 0)
Josef Bacikb63164292011-09-30 15:23:54 -04001331 err = prepare_uptodate_page(pages[i], pos,
1332 force_uptodate);
Chris Masonb1bf8622011-02-28 09:52:08 -05001333 if (i == num_pages - 1)
1334 err = prepare_uptodate_page(pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001335 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001336 if (err) {
1337 page_cache_release(pages[i]);
1338 faili = i - 1;
1339 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001340 }
Chris Masonccd467d2007-06-28 15:57:36 -04001341 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001342 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001343
Chris Mason39279cc2007-06-12 06:35:45 -04001344 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001345fail:
1346 while (faili >= 0) {
1347 unlock_page(pages[faili]);
1348 page_cache_release(pages[faili]);
1349 faili--;
1350 }
1351 return err;
1352
Chris Mason39279cc2007-06-12 06:35:45 -04001353}
1354
Miao Xie376cc682013-12-10 19:25:04 +08001355/*
1356 * This function locks the extent and properly waits for data=ordered extents
1357 * to finish before allowing the pages to be modified if need.
1358 *
1359 * The return value:
1360 * 1 - the extent is locked
1361 * 0 - the extent is not locked, and everything is OK
1362 * -EAGAIN - need re-prepare the pages
1363 * the other < 0 number - Something wrong happens
1364 */
1365static noinline int
1366lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
1367 size_t num_pages, loff_t pos,
1368 u64 *lockstart, u64 *lockend,
1369 struct extent_state **cached_state)
1370{
1371 u64 start_pos;
1372 u64 last_pos;
1373 int i;
1374 int ret = 0;
1375
1376 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
1377 last_pos = start_pos + ((u64)num_pages << PAGE_CACHE_SHIFT) - 1;
1378
1379 if (start_pos < inode->i_size) {
1380 struct btrfs_ordered_extent *ordered;
1381 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1382 start_pos, last_pos, 0, cached_state);
Miao Xieb88935b2014-03-06 13:54:58 +08001383 ordered = btrfs_lookup_ordered_range(inode, start_pos,
1384 last_pos - start_pos + 1);
Miao Xie376cc682013-12-10 19:25:04 +08001385 if (ordered &&
1386 ordered->file_offset + ordered->len > start_pos &&
1387 ordered->file_offset <= last_pos) {
Miao Xie376cc682013-12-10 19:25:04 +08001388 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1389 start_pos, last_pos,
1390 cached_state, GFP_NOFS);
1391 for (i = 0; i < num_pages; i++) {
1392 unlock_page(pages[i]);
1393 page_cache_release(pages[i]);
1394 }
Miao Xieb88935b2014-03-06 13:54:58 +08001395 btrfs_start_ordered_extent(inode, ordered, 1);
1396 btrfs_put_ordered_extent(ordered);
1397 return -EAGAIN;
Miao Xie376cc682013-12-10 19:25:04 +08001398 }
1399 if (ordered)
1400 btrfs_put_ordered_extent(ordered);
1401
1402 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
1403 last_pos, EXTENT_DIRTY | EXTENT_DELALLOC |
1404 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1405 0, 0, cached_state, GFP_NOFS);
1406 *lockstart = start_pos;
1407 *lockend = last_pos;
1408 ret = 1;
1409 }
1410
1411 for (i = 0; i < num_pages; i++) {
1412 if (clear_page_dirty_for_io(pages[i]))
1413 account_page_redirty(pages[i]);
1414 set_page_extent_mapped(pages[i]);
1415 WARN_ON(!PageLocked(pages[i]));
1416 }
1417
1418 return ret;
1419}
1420
Josef Bacik7ee9e442013-06-21 16:37:03 -04001421static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1422 size_t *write_bytes)
1423{
Josef Bacik7ee9e442013-06-21 16:37:03 -04001424 struct btrfs_root *root = BTRFS_I(inode)->root;
1425 struct btrfs_ordered_extent *ordered;
1426 u64 lockstart, lockend;
1427 u64 num_bytes;
1428 int ret;
1429
Miao Xie8257b2d2014-03-06 13:38:19 +08001430 ret = btrfs_start_nocow_write(root);
1431 if (!ret)
1432 return -ENOSPC;
1433
Josef Bacik7ee9e442013-06-21 16:37:03 -04001434 lockstart = round_down(pos, root->sectorsize);
Miao Xiec9339562014-02-27 13:58:04 +08001435 lockend = round_up(pos + *write_bytes, root->sectorsize) - 1;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001436
1437 while (1) {
1438 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1439 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1440 lockend - lockstart + 1);
1441 if (!ordered) {
1442 break;
1443 }
1444 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1445 btrfs_start_ordered_extent(inode, ordered, 1);
1446 btrfs_put_ordered_extent(ordered);
1447 }
1448
Josef Bacik7ee9e442013-06-21 16:37:03 -04001449 num_bytes = lockend - lockstart + 1;
Josef Bacik00361582013-08-14 14:02:47 -04001450 ret = can_nocow_extent(inode, lockstart, &num_bytes, NULL, NULL, NULL);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001451 if (ret <= 0) {
1452 ret = 0;
Miao Xie8257b2d2014-03-06 13:38:19 +08001453 btrfs_end_nocow_write(root);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001454 } else {
Miao Xiec9339562014-02-27 13:58:04 +08001455 *write_bytes = min_t(size_t, *write_bytes ,
1456 num_bytes - pos + lockstart);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001457 }
1458
1459 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1460
1461 return ret;
1462}
1463
Josef Bacikd0215f32011-01-25 14:57:24 -05001464static noinline ssize_t __btrfs_buffered_write(struct file *file,
1465 struct iov_iter *i,
1466 loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001467{
Al Viro496ad9a2013-01-23 17:07:38 -05001468 struct inode *inode = file_inode(file);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001469 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001470 struct page **pages = NULL;
Miao Xie376cc682013-12-10 19:25:04 +08001471 struct extent_state *cached_state = NULL;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001472 u64 release_bytes = 0;
Miao Xie376cc682013-12-10 19:25:04 +08001473 u64 lockstart;
1474 u64 lockend;
Chris Mason39279cc2007-06-12 06:35:45 -04001475 unsigned long first_index;
Josef Bacikd0215f32011-01-25 14:57:24 -05001476 size_t num_written = 0;
1477 int nrptrs;
Tsutomu Itohc9149232011-03-30 00:57:23 +00001478 int ret = 0;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001479 bool only_release_metadata = false;
Josef Bacikb63164292011-09-30 15:23:54 -04001480 bool force_page_uptodate = false;
Miao Xie376cc682013-12-10 19:25:04 +08001481 bool need_unlock;
Chris Masoncb843a62008-10-03 12:30:02 -04001482
Josef Bacikd0215f32011-01-25 14:57:24 -05001483 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
Josef Bacik11c65dc2010-05-23 11:07:21 -04001484 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1485 (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001486 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1487 nrptrs = max(nrptrs, 8);
Chris Mason8c2383c2007-06-18 09:57:58 -04001488 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001489 if (!pages)
1490 return -ENOMEM;
Chris Masonab93dbe2009-10-01 12:29:10 -04001491
Chris Mason39279cc2007-06-12 06:35:45 -04001492 first_index = pos >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001493
Josef Bacikd0215f32011-01-25 14:57:24 -05001494 while (iov_iter_count(i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001495 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacikd0215f32011-01-25 14:57:24 -05001496 size_t write_bytes = min(iov_iter_count(i),
Josef Bacik11c65dc2010-05-23 11:07:21 -04001497 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001498 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001499 size_t num_pages = (write_bytes + offset +
1500 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001501 size_t reserve_bytes;
Josef Bacikd0215f32011-01-25 14:57:24 -05001502 size_t dirty_pages;
1503 size_t copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001504
Chris Mason8c2383c2007-06-18 09:57:58 -04001505 WARN_ON(num_pages > nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001506
Xin Zhong914ee292010-12-09 09:30:14 +00001507 /*
1508 * Fault pages before locking them in prepare_pages
1509 * to avoid recursive lock
1510 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001511 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001512 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001513 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001514 }
1515
Josef Bacik7ee9e442013-06-21 16:37:03 -04001516 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1517 ret = btrfs_check_data_free_space(inode, reserve_bytes);
1518 if (ret == -ENOSPC &&
1519 (BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1520 BTRFS_INODE_PREALLOC))) {
1521 ret = check_can_nocow(inode, pos, &write_bytes);
1522 if (ret > 0) {
1523 only_release_metadata = true;
1524 /*
1525 * our prealloc extent may be smaller than
1526 * write_bytes, so scale down.
1527 */
1528 num_pages = (write_bytes + offset +
1529 PAGE_CACHE_SIZE - 1) >>
1530 PAGE_CACHE_SHIFT;
1531 reserve_bytes = num_pages << PAGE_CACHE_SHIFT;
1532 ret = 0;
1533 } else {
1534 ret = -ENOSPC;
1535 }
1536 }
1537
Chris Mason1832a6d2007-12-21 16:27:21 -05001538 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001539 break;
Chris Mason1832a6d2007-12-21 16:27:21 -05001540
Josef Bacik7ee9e442013-06-21 16:37:03 -04001541 ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes);
1542 if (ret) {
1543 if (!only_release_metadata)
1544 btrfs_free_reserved_data_space(inode,
1545 reserve_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001546 else
1547 btrfs_end_nocow_write(root);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001548 break;
1549 }
1550
1551 release_bytes = reserve_bytes;
Miao Xie376cc682013-12-10 19:25:04 +08001552 need_unlock = false;
1553again:
Josef Bacik4a640012011-01-25 15:10:08 -05001554 /*
1555 * This is going to setup the pages array with the number of
1556 * pages we want, so we don't really need to worry about the
1557 * contents of pages from loop to loop
1558 */
Miao Xieb37392e2013-12-10 19:25:03 +08001559 ret = prepare_pages(inode, pages, num_pages,
1560 pos, write_bytes,
Josef Bacikb63164292011-09-30 15:23:54 -04001561 force_page_uptodate);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001562 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001563 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001564
Miao Xie376cc682013-12-10 19:25:04 +08001565 ret = lock_and_cleanup_extent_if_need(inode, pages, num_pages,
1566 pos, &lockstart, &lockend,
1567 &cached_state);
1568 if (ret < 0) {
1569 if (ret == -EAGAIN)
1570 goto again;
1571 break;
1572 } else if (ret > 0) {
1573 need_unlock = true;
1574 ret = 0;
1575 }
1576
Xin Zhong914ee292010-12-09 09:30:14 +00001577 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -05001578 write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001579
1580 /*
1581 * if we have trouble faulting in the pages, fall
1582 * back to one page at a time
1583 */
1584 if (copied < write_bytes)
1585 nrptrs = 1;
1586
Josef Bacikb63164292011-09-30 15:23:54 -04001587 if (copied == 0) {
1588 force_page_uptodate = true;
Chris Masonb1bf8622011-02-28 09:52:08 -05001589 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001590 } else {
1591 force_page_uptodate = false;
Chris Masonb1bf8622011-02-28 09:52:08 -05001592 dirty_pages = (copied + offset +
1593 PAGE_CACHE_SIZE - 1) >>
1594 PAGE_CACHE_SHIFT;
Josef Bacikb63164292011-09-30 15:23:54 -04001595 }
Xin Zhong914ee292010-12-09 09:30:14 +00001596
Josef Bacikd0215f32011-01-25 14:57:24 -05001597 /*
1598 * If we had a short copy we need to release the excess delaloc
1599 * bytes we reserved. We need to increment outstanding_extents
1600 * because btrfs_delalloc_release_space will decrement it, but
1601 * we still have an outstanding extent for the chunk we actually
1602 * managed to copy.
1603 */
Xin Zhong914ee292010-12-09 09:30:14 +00001604 if (num_pages > dirty_pages) {
Josef Bacik7ee9e442013-06-21 16:37:03 -04001605 release_bytes = (num_pages - dirty_pages) <<
1606 PAGE_CACHE_SHIFT;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001607 if (copied > 0) {
1608 spin_lock(&BTRFS_I(inode)->lock);
1609 BTRFS_I(inode)->outstanding_extents++;
1610 spin_unlock(&BTRFS_I(inode)->lock);
1611 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04001612 if (only_release_metadata)
1613 btrfs_delalloc_release_metadata(inode,
1614 release_bytes);
1615 else
1616 btrfs_delalloc_release_space(inode,
1617 release_bytes);
Xin Zhong914ee292010-12-09 09:30:14 +00001618 }
1619
Josef Bacik7ee9e442013-06-21 16:37:03 -04001620 release_bytes = dirty_pages << PAGE_CACHE_SHIFT;
Miao Xie376cc682013-12-10 19:25:04 +08001621
1622 if (copied > 0)
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001623 ret = btrfs_dirty_pages(root, inode, pages,
1624 dirty_pages, pos, copied,
1625 NULL);
Miao Xie376cc682013-12-10 19:25:04 +08001626 if (need_unlock)
1627 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1628 lockstart, lockend, &cached_state,
1629 GFP_NOFS);
Miao Xief1de9682014-01-09 10:06:10 +08001630 if (ret) {
1631 btrfs_drop_pages(pages, num_pages);
Miao Xie376cc682013-12-10 19:25:04 +08001632 break;
Miao Xief1de9682014-01-09 10:06:10 +08001633 }
Chris Mason39279cc2007-06-12 06:35:45 -04001634
Josef Bacik7ee9e442013-06-21 16:37:03 -04001635 release_bytes = 0;
Miao Xie8257b2d2014-03-06 13:38:19 +08001636 if (only_release_metadata)
1637 btrfs_end_nocow_write(root);
1638
Josef Bacik7ee9e442013-06-21 16:37:03 -04001639 if (only_release_metadata && copied > 0) {
1640 u64 lockstart = round_down(pos, root->sectorsize);
1641 u64 lockend = lockstart +
1642 (dirty_pages << PAGE_CACHE_SHIFT) - 1;
1643
1644 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1645 lockend, EXTENT_NORESERVE, NULL,
1646 NULL, GFP_NOFS);
1647 only_release_metadata = false;
1648 }
1649
Miao Xief1de9682014-01-09 10:06:10 +08001650 btrfs_drop_pages(pages, num_pages);
1651
Josef Bacikd0215f32011-01-25 14:57:24 -05001652 cond_resched();
1653
Namjae Jeond0e1d662012-12-11 16:00:21 -08001654 balance_dirty_pages_ratelimited(inode->i_mapping);
Josef Bacikd0215f32011-01-25 14:57:24 -05001655 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
Liu Bob53d3f52012-11-14 14:34:34 +00001656 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001657
Xin Zhong914ee292010-12-09 09:30:14 +00001658 pos += copied;
1659 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001660 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001661
Chris Mason8c2383c2007-06-18 09:57:58 -04001662 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001663
Josef Bacik7ee9e442013-06-21 16:37:03 -04001664 if (release_bytes) {
Miao Xie8257b2d2014-03-06 13:38:19 +08001665 if (only_release_metadata) {
1666 btrfs_end_nocow_write(root);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001667 btrfs_delalloc_release_metadata(inode, release_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001668 } else {
Josef Bacik7ee9e442013-06-21 16:37:03 -04001669 btrfs_delalloc_release_space(inode, release_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001670 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04001671 }
1672
Josef Bacikd0215f32011-01-25 14:57:24 -05001673 return num_written ? num_written : ret;
1674}
1675
1676static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1677 const struct iovec *iov,
1678 unsigned long nr_segs, loff_t pos,
Al Viro867c4f92014-02-11 19:31:06 -05001679 size_t count, size_t ocount)
Josef Bacikd0215f32011-01-25 14:57:24 -05001680{
1681 struct file *file = iocb->ki_filp;
Josef Bacikd0215f32011-01-25 14:57:24 -05001682 struct iov_iter i;
1683 ssize_t written;
1684 ssize_t written_buffered;
1685 loff_t endbyte;
1686 int err;
1687
Al Viro5cb6c6c2014-02-11 20:58:20 -05001688 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
Josef Bacikd0215f32011-01-25 14:57:24 -05001689 count, ocount);
1690
Josef Bacikd0215f32011-01-25 14:57:24 -05001691 if (written < 0 || written == count)
1692 return written;
1693
1694 pos += written;
1695 count -= written;
1696 iov_iter_init(&i, iov, nr_segs, count, written);
1697 written_buffered = __btrfs_buffered_write(file, &i, pos);
1698 if (written_buffered < 0) {
1699 err = written_buffered;
1700 goto out;
1701 }
1702 endbyte = pos + written_buffered - 1;
1703 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1704 if (err)
1705 goto out;
1706 written += written_buffered;
Al Viro867c4f92014-02-11 19:31:06 -05001707 iocb->ki_pos = pos + written_buffered;
Josef Bacikd0215f32011-01-25 14:57:24 -05001708 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1709 endbyte >> PAGE_CACHE_SHIFT);
1710out:
1711 return written ? written : err;
1712}
1713
Josef Bacik6c760c02012-11-09 10:53:21 -05001714static void update_time_for_write(struct inode *inode)
1715{
1716 struct timespec now;
1717
1718 if (IS_NOCMTIME(inode))
1719 return;
1720
1721 now = current_fs_time(inode->i_sb);
1722 if (!timespec_equal(&inode->i_mtime, &now))
1723 inode->i_mtime = now;
1724
1725 if (!timespec_equal(&inode->i_ctime, &now))
1726 inode->i_ctime = now;
1727
1728 if (IS_I_VERSION(inode))
1729 inode_inc_iversion(inode);
1730}
1731
Josef Bacikd0215f32011-01-25 14:57:24 -05001732static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1733 const struct iovec *iov,
1734 unsigned long nr_segs, loff_t pos)
1735{
1736 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -05001737 struct inode *inode = file_inode(file);
Josef Bacikd0215f32011-01-25 14:57:24 -05001738 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001739 u64 start_pos;
Qu Wenruo3ac0d7b2014-03-27 02:51:58 +00001740 u64 end_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001741 ssize_t num_written = 0;
1742 ssize_t err = 0;
1743 size_t count, ocount;
Josef Bacikb812ce22012-11-16 13:56:32 -05001744 bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
Josef Bacikd0215f32011-01-25 14:57:24 -05001745
Josef Bacikd0215f32011-01-25 14:57:24 -05001746 mutex_lock(&inode->i_mutex);
1747
1748 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1749 if (err) {
1750 mutex_unlock(&inode->i_mutex);
1751 goto out;
1752 }
1753 count = ocount;
1754
1755 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1756 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1757 if (err) {
1758 mutex_unlock(&inode->i_mutex);
1759 goto out;
1760 }
1761
1762 if (count == 0) {
1763 mutex_unlock(&inode->i_mutex);
1764 goto out;
1765 }
1766
1767 err = file_remove_suid(file);
1768 if (err) {
1769 mutex_unlock(&inode->i_mutex);
1770 goto out;
1771 }
1772
1773 /*
1774 * If BTRFS flips readonly due to some impossible error
1775 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1776 * although we have opened a file as writable, we have
1777 * to stop this write operation to ensure FS consistency.
1778 */
Miao Xie87533c42013-01-29 10:14:48 +00001779 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
Josef Bacikd0215f32011-01-25 14:57:24 -05001780 mutex_unlock(&inode->i_mutex);
1781 err = -EROFS;
1782 goto out;
1783 }
1784
Josef Bacik6c760c02012-11-09 10:53:21 -05001785 /*
1786 * We reserve space for updating the inode when we reserve space for the
1787 * extent we are going to write, so we will enospc out there. We don't
1788 * need to start yet another transaction to update the inode as we will
1789 * update the inode when we finish writing whatever data we write.
1790 */
1791 update_time_for_write(inode);
Josef Bacikd0215f32011-01-25 14:57:24 -05001792
Miao Xie0c1a98c2011-09-11 10:52:24 -04001793 start_pos = round_down(pos, root->sectorsize);
1794 if (start_pos > i_size_read(inode)) {
Qu Wenruo3ac0d7b2014-03-27 02:51:58 +00001795 /* Expand hole size to cover write data, preventing empty gap */
Qu Wenruoc5f7d0b2014-04-15 10:41:00 +08001796 end_pos = round_up(pos + count, root->sectorsize);
Qu Wenruo3ac0d7b2014-03-27 02:51:58 +00001797 err = btrfs_cont_expand(inode, i_size_read(inode), end_pos);
Miao Xie0c1a98c2011-09-11 10:52:24 -04001798 if (err) {
1799 mutex_unlock(&inode->i_mutex);
1800 goto out;
1801 }
1802 }
1803
Josef Bacikb812ce22012-11-16 13:56:32 -05001804 if (sync)
1805 atomic_inc(&BTRFS_I(inode)->sync_writers);
1806
Josef Bacikd0215f32011-01-25 14:57:24 -05001807 if (unlikely(file->f_flags & O_DIRECT)) {
1808 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
Al Viro867c4f92014-02-11 19:31:06 -05001809 pos, count, ocount);
Josef Bacikd0215f32011-01-25 14:57:24 -05001810 } else {
1811 struct iov_iter i;
1812
1813 iov_iter_init(&i, iov, nr_segs, count, num_written);
1814
1815 num_written = __btrfs_buffered_write(file, &i, pos);
1816 if (num_written > 0)
Al Viro867c4f92014-02-11 19:31:06 -05001817 iocb->ki_pos = pos + num_written;
Josef Bacikd0215f32011-01-25 14:57:24 -05001818 }
1819
1820 mutex_unlock(&inode->i_mutex);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001821
Chris Mason5a3f23d2009-03-31 13:27:11 -04001822 /*
1823 * we want to make sure fsync finds this change
1824 * but we haven't joined a transaction running right now.
1825 *
1826 * Later on, someone is sure to update the inode and get the
1827 * real transid recorded.
1828 *
1829 * We set last_trans now to the fs_info generation + 1,
1830 * this will either be one more than the running transaction
1831 * or the generation used for the next transaction if there isn't
1832 * one running right now.
Josef Bacik6c760c02012-11-09 10:53:21 -05001833 *
1834 * We also have to set last_sub_trans to the current log transid,
1835 * otherwise subsequent syncs to a file that's been synced in this
1836 * transaction will appear to have already occured.
Chris Mason5a3f23d2009-03-31 13:27:11 -04001837 */
1838 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Josef Bacik6c760c02012-11-09 10:53:21 -05001839 BTRFS_I(inode)->last_sub_trans = root->log_transid;
Christoph Hellwig02afc272013-09-04 15:04:40 +02001840 if (num_written > 0) {
Josef Bacikd0215f32011-01-25 14:57:24 -05001841 err = generic_write_sync(file, pos, num_written);
Dan Carpenter45d4f852014-04-03 14:47:17 -07001842 if (err < 0)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001843 num_written = err;
1844 }
Miao Xie0a3404d2013-01-28 12:34:55 +00001845
Josef Bacikb812ce22012-11-16 13:56:32 -05001846 if (sync)
1847 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie0a3404d2013-01-28 12:34:55 +00001848out:
Chris Mason39279cc2007-06-12 06:35:45 -04001849 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001850 return num_written ? num_written : err;
1851}
1852
Chris Masond3977122009-01-05 21:25:51 -05001853int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001854{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001855 /*
1856 * ordered_data_close is set by settattr when we are about to truncate
1857 * a file from a non-zero size to a zero size. This tries to
1858 * flush down new bytes that may have been written if the
1859 * application were using truncate to replace a file in place.
1860 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04001861 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1862 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik569e0f32013-02-13 11:09:14 -05001863 struct btrfs_trans_handle *trans;
1864 struct btrfs_root *root = BTRFS_I(inode)->root;
1865
1866 /*
1867 * We need to block on a committing transaction to keep us from
1868 * throwing a ordered operation on to the list and causing
1869 * something like sync to deadlock trying to flush out this
1870 * inode.
1871 */
1872 trans = btrfs_start_transaction(root, 0);
1873 if (IS_ERR(trans))
1874 return PTR_ERR(trans);
1875 btrfs_add_ordered_operation(trans, BTRFS_I(inode)->root, inode);
1876 btrfs_end_transaction(trans, root);
Chris Mason5a3f23d2009-03-31 13:27:11 -04001877 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1878 filemap_flush(inode->i_mapping);
1879 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001880 if (filp->private_data)
1881 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001882 return 0;
1883}
1884
Chris Masond352ac62008-09-29 15:18:18 -04001885/*
1886 * fsync call for both files and directories. This logs the inode into
1887 * the tree log instead of forcing full commits whenever possible.
1888 *
1889 * It needs to call filemap_fdatawait so that all ordered extent updates are
1890 * in the metadata btree are up to date for copying to the log.
1891 *
1892 * It drops the inode mutex before doing the tree log commit. This is an
1893 * important optimization for directories because holding the mutex prevents
1894 * new operations on the dir while we write to disk.
1895 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001896int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001897{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001898 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001899 struct inode *inode = dentry->d_inode;
1900 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001901 struct btrfs_trans_handle *trans;
Miao Xie8b050d32014-02-20 18:08:58 +08001902 struct btrfs_log_ctx ctx;
1903 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001904 bool full_sync = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001905
liubo1abe9b82011-03-24 11:18:59 +00001906 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04001907
Miao Xie90abccf2012-09-13 04:53:47 -06001908 /*
1909 * We write the dirty pages in the range and wait until they complete
1910 * out of the ->i_mutex. If so, we can flush the dirty pages by
Josef Bacik2ab28f32012-10-12 15:27:49 -04001911 * multi-task, and make the performance up. See
1912 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
Miao Xie90abccf2012-09-13 04:53:47 -06001913 */
Josef Bacikb812ce22012-11-16 13:56:32 -05001914 atomic_inc(&BTRFS_I(inode)->sync_writers);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001915 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1916 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1917 &BTRFS_I(inode)->runtime_flags))
1918 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
Josef Bacikb812ce22012-11-16 13:56:32 -05001919 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie90abccf2012-09-13 04:53:47 -06001920 if (ret)
1921 return ret;
1922
Josef Bacik02c24a82011-07-16 20:44:56 -04001923 mutex_lock(&inode->i_mutex);
1924
Josef Bacik0885ef52012-04-23 15:09:39 -04001925 /*
Miao Xie90abccf2012-09-13 04:53:47 -06001926 * We flush the dirty pages again to avoid some dirty pages in the
1927 * range being left.
Josef Bacik0885ef52012-04-23 15:09:39 -04001928 */
Miao Xie2ecb7922012-09-06 04:04:27 -06001929 atomic_inc(&root->log_batch);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001930 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1931 &BTRFS_I(inode)->runtime_flags);
Josef Bacik0ef8b722013-10-25 16:13:35 -04001932 if (full_sync) {
1933 ret = btrfs_wait_ordered_range(inode, start, end - start + 1);
1934 if (ret) {
1935 mutex_unlock(&inode->i_mutex);
1936 goto out;
1937 }
1938 }
Miao Xie2ecb7922012-09-06 04:04:27 -06001939 atomic_inc(&root->log_batch);
Chris Mason257c62e2009-10-13 13:21:08 -04001940
Chris Mason39279cc2007-06-12 06:35:45 -04001941 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001942 * check the transaction that last modified this inode
1943 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001944 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001945 if (!BTRFS_I(inode)->last_trans) {
1946 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001947 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001948 }
Chris Masona2135012008-06-25 16:01:30 -04001949
Chris Mason257c62e2009-10-13 13:21:08 -04001950 /*
1951 * if the last transaction that changed this file was before
1952 * the current transaction, we can bail out now without any
1953 * syncing
1954 */
Josef Bacika4abeea2011-04-11 17:25:13 -04001955 smp_mb();
Josef Bacik22ee6985d2012-05-29 16:57:49 -04001956 if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1957 BTRFS_I(inode)->last_trans <=
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001958 root->fs_info->last_trans_committed) {
1959 BTRFS_I(inode)->last_trans = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001960
1961 /*
1962 * We'v had everything committed since the last time we were
1963 * modified so clear this flag in case it was set for whatever
1964 * reason, it's no longer relevant.
1965 */
1966 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1967 &BTRFS_I(inode)->runtime_flags);
Josef Bacik02c24a82011-07-16 20:44:56 -04001968 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001969 goto out;
1970 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001971
1972 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001973 * ok we haven't committed the transaction yet, lets do a commit
1974 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001975 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001976 btrfs_ioctl_trans_end(file);
1977
Josef Bacik5039edd2014-01-15 13:34:13 -05001978 /*
1979 * We use start here because we will need to wait on the IO to complete
1980 * in btrfs_sync_log, which could require joining a transaction (for
1981 * example checking cross references in the nocow path). If we use join
1982 * here we could get into a situation where we're waiting on IO to
1983 * happen that is blocked on a transaction trying to commit. With start
1984 * we inc the extwriter counter, so we wait for all extwriters to exit
1985 * before we start blocking join'ers. This comment is to keep somebody
1986 * from thinking they are super smart and changing this to
1987 * btrfs_join_transaction *cough*Josef*cough*.
1988 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001989 trans = btrfs_start_transaction(root, 0);
1990 if (IS_ERR(trans)) {
1991 ret = PTR_ERR(trans);
Josef Bacik02c24a82011-07-16 20:44:56 -04001992 mutex_unlock(&inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001993 goto out;
1994 }
Josef Bacik5039edd2014-01-15 13:34:13 -05001995 trans->sync = true;
Chris Masone02119d2008-09-05 16:13:11 -04001996
Miao Xie8b050d32014-02-20 18:08:58 +08001997 btrfs_init_log_ctx(&ctx);
1998
1999 ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
Josef Bacik02c24a82011-07-16 20:44:56 -04002000 if (ret < 0) {
Filipe David Borba Mananaa0634be2013-09-11 20:36:44 +01002001 /* Fallthrough and commit/free transaction. */
2002 ret = 1;
Josef Bacik02c24a82011-07-16 20:44:56 -04002003 }
Chris Mason49eb7e42008-09-11 15:53:12 -04002004
2005 /* we've logged all the items and now have a consistent
2006 * version of the file in the log. It is possible that
2007 * someone will come in and modify the file, but that's
2008 * fine because the log is consistent on disk, and we
2009 * have references to all of the file's extents
2010 *
2011 * It is possible that someone will come in and log the
2012 * file again, but that will end up using the synchronization
2013 * inside btrfs_sync_log to keep things safe.
2014 */
Josef Bacik02c24a82011-07-16 20:44:56 -04002015 mutex_unlock(&inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04002016
Chris Mason257c62e2009-10-13 13:21:08 -04002017 if (ret != BTRFS_NO_LOG_SYNC) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04002018 if (!ret) {
Miao Xie8b050d32014-02-20 18:08:58 +08002019 ret = btrfs_sync_log(trans, root, &ctx);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002020 if (!ret) {
Chris Mason257c62e2009-10-13 13:21:08 -04002021 ret = btrfs_end_transaction(trans, root);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002022 goto out;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002023 }
Chris Mason257c62e2009-10-13 13:21:08 -04002024 }
Josef Bacik0ef8b722013-10-25 16:13:35 -04002025 if (!full_sync) {
2026 ret = btrfs_wait_ordered_range(inode, start,
2027 end - start + 1);
Filipe Mananab05fd872014-05-29 23:31:39 +01002028 if (ret) {
2029 btrfs_end_transaction(trans, root);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002030 goto out;
Filipe Mananab05fd872014-05-29 23:31:39 +01002031 }
Josef Bacik0ef8b722013-10-25 16:13:35 -04002032 }
2033 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04002034 } else {
2035 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002036 }
Chris Mason39279cc2007-06-12 06:35:45 -04002037out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00002038 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002039}
2040
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002041static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04002042 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002043 .map_pages = filemap_map_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04002044 .page_mkwrite = btrfs_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002045 .remap_pages = generic_file_remap_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04002046};
2047
2048static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2049{
Miao Xie058a4572010-05-20 07:21:50 +00002050 struct address_space *mapping = filp->f_mapping;
2051
2052 if (!mapping->a_ops->readpage)
2053 return -ENOEXEC;
2054
Chris Mason9ebefb182007-06-15 13:50:00 -04002055 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00002056 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00002057
Chris Mason9ebefb182007-06-15 13:50:00 -04002058 return 0;
2059}
2060
Josef Bacik2aaa6652012-08-29 14:27:18 -04002061static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
2062 int slot, u64 start, u64 end)
2063{
2064 struct btrfs_file_extent_item *fi;
2065 struct btrfs_key key;
2066
2067 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2068 return 0;
2069
2070 btrfs_item_key_to_cpu(leaf, &key, slot);
2071 if (key.objectid != btrfs_ino(inode) ||
2072 key.type != BTRFS_EXTENT_DATA_KEY)
2073 return 0;
2074
2075 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2076
2077 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2078 return 0;
2079
2080 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2081 return 0;
2082
2083 if (key.offset == end)
2084 return 1;
2085 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2086 return 1;
2087 return 0;
2088}
2089
2090static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2091 struct btrfs_path *path, u64 offset, u64 end)
2092{
2093 struct btrfs_root *root = BTRFS_I(inode)->root;
2094 struct extent_buffer *leaf;
2095 struct btrfs_file_extent_item *fi;
2096 struct extent_map *hole_em;
2097 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2098 struct btrfs_key key;
2099 int ret;
2100
Josef Bacik16e75492013-10-22 12:18:51 -04002101 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
2102 goto out;
2103
Josef Bacik2aaa6652012-08-29 14:27:18 -04002104 key.objectid = btrfs_ino(inode);
2105 key.type = BTRFS_EXTENT_DATA_KEY;
2106 key.offset = offset;
2107
Josef Bacik2aaa6652012-08-29 14:27:18 -04002108 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2109 if (ret < 0)
2110 return ret;
2111 BUG_ON(!ret);
2112
2113 leaf = path->nodes[0];
2114 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
2115 u64 num_bytes;
2116
2117 path->slots[0]--;
2118 fi = btrfs_item_ptr(leaf, path->slots[0],
2119 struct btrfs_file_extent_item);
2120 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2121 end - offset;
2122 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2123 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2124 btrfs_set_file_extent_offset(leaf, fi, 0);
2125 btrfs_mark_buffer_dirty(leaf);
2126 goto out;
2127 }
2128
2129 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
2130 u64 num_bytes;
2131
2132 path->slots[0]++;
2133 key.offset = offset;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002134 btrfs_set_item_key_safe(root, path, &key);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002135 fi = btrfs_item_ptr(leaf, path->slots[0],
2136 struct btrfs_file_extent_item);
2137 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2138 offset;
2139 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2140 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2141 btrfs_set_file_extent_offset(leaf, fi, 0);
2142 btrfs_mark_buffer_dirty(leaf);
2143 goto out;
2144 }
2145 btrfs_release_path(path);
2146
2147 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
2148 0, 0, end - offset, 0, end - offset,
2149 0, 0, 0);
2150 if (ret)
2151 return ret;
2152
2153out:
2154 btrfs_release_path(path);
2155
2156 hole_em = alloc_extent_map();
2157 if (!hole_em) {
2158 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2159 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2160 &BTRFS_I(inode)->runtime_flags);
2161 } else {
2162 hole_em->start = offset;
2163 hole_em->len = end - offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04002164 hole_em->ram_bytes = hole_em->len;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002165 hole_em->orig_start = offset;
2166
2167 hole_em->block_start = EXTENT_MAP_HOLE;
2168 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05002169 hole_em->orig_block_len = 0;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002170 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
2171 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2172 hole_em->generation = trans->transid;
2173
2174 do {
2175 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2176 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002177 ret = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002178 write_unlock(&em_tree->lock);
2179 } while (ret == -EEXIST);
2180 free_extent_map(hole_em);
2181 if (ret)
2182 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2183 &BTRFS_I(inode)->runtime_flags);
2184 }
2185
2186 return 0;
2187}
2188
Qu Wenruod7781542014-05-30 15:16:10 +08002189/*
2190 * Find a hole extent on given inode and change start/len to the end of hole
2191 * extent.(hole/vacuum extent whose em->start <= start &&
2192 * em->start + em->len > start)
2193 * When a hole extent is found, return 1 and modify start/len.
2194 */
2195static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
2196{
2197 struct extent_map *em;
2198 int ret = 0;
2199
2200 em = btrfs_get_extent(inode, NULL, 0, *start, *len, 0);
2201 if (IS_ERR_OR_NULL(em)) {
2202 if (!em)
2203 ret = -ENOMEM;
2204 else
2205 ret = PTR_ERR(em);
2206 return ret;
2207 }
2208
2209 /* Hole or vacuum extent(only exists in no-hole mode) */
2210 if (em->block_start == EXTENT_MAP_HOLE) {
2211 ret = 1;
2212 *len = em->start + em->len > *start + *len ?
2213 0 : *start + *len - em->start - em->len;
2214 *start = em->start + em->len;
2215 }
2216 free_extent_map(em);
2217 return ret;
2218}
2219
Josef Bacik2aaa6652012-08-29 14:27:18 -04002220static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2221{
2222 struct btrfs_root *root = BTRFS_I(inode)->root;
2223 struct extent_state *cached_state = NULL;
2224 struct btrfs_path *path;
2225 struct btrfs_block_rsv *rsv;
2226 struct btrfs_trans_handle *trans;
Qu Wenruod7781542014-05-30 15:16:10 +08002227 u64 lockstart;
2228 u64 lockend;
2229 u64 tail_start;
2230 u64 tail_len;
2231 u64 orig_start = offset;
2232 u64 cur_offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002233 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
2234 u64 drop_end;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002235 int ret = 0;
2236 int err = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04002237 int rsv_count;
Qu Wenruod7781542014-05-30 15:16:10 +08002238 bool same_page;
Josef Bacik16e75492013-10-22 12:18:51 -04002239 bool no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
Filipe Mananaa1a50f62014-04-26 01:35:31 +01002240 u64 ino_size;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002241
Josef Bacik0ef8b722013-10-25 16:13:35 -04002242 ret = btrfs_wait_ordered_range(inode, offset, len);
2243 if (ret)
2244 return ret;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002245
2246 mutex_lock(&inode->i_mutex);
Filipe Mananaa1a50f62014-04-26 01:35:31 +01002247 ino_size = round_up(inode->i_size, PAGE_CACHE_SIZE);
Qu Wenruod7781542014-05-30 15:16:10 +08002248 ret = find_first_non_hole(inode, &offset, &len);
2249 if (ret < 0)
2250 goto out_only_mutex;
2251 if (ret && !len) {
2252 /* Already in a large hole */
2253 ret = 0;
2254 goto out_only_mutex;
2255 }
2256
2257 lockstart = round_up(offset , BTRFS_I(inode)->root->sectorsize);
2258 lockend = round_down(offset + len,
2259 BTRFS_I(inode)->root->sectorsize) - 1;
2260 same_page = ((offset >> PAGE_CACHE_SHIFT) ==
2261 ((offset + len - 1) >> PAGE_CACHE_SHIFT));
2262
Miao Xie7426cc02012-12-05 10:54:52 +00002263 /*
2264 * We needn't truncate any page which is beyond the end of the file
2265 * because we are sure there is no data there.
2266 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04002267 /*
2268 * Only do this if we are in the same page and we aren't doing the
2269 * entire page.
2270 */
2271 if (same_page && len < PAGE_CACHE_SIZE) {
Filipe Manana12870f12014-02-15 15:55:58 +00002272 if (offset < ino_size)
Miao Xie7426cc02012-12-05 10:54:52 +00002273 ret = btrfs_truncate_page(inode, offset, len, 0);
Qu Wenruod7781542014-05-30 15:16:10 +08002274 goto out_only_mutex;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002275 }
2276
2277 /* zero back part of the first page */
Filipe Manana12870f12014-02-15 15:55:58 +00002278 if (offset < ino_size) {
Miao Xie7426cc02012-12-05 10:54:52 +00002279 ret = btrfs_truncate_page(inode, offset, 0, 0);
2280 if (ret) {
2281 mutex_unlock(&inode->i_mutex);
2282 return ret;
2283 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002284 }
2285
Qu Wenruod7781542014-05-30 15:16:10 +08002286 /* Check the aligned pages after the first unaligned page,
2287 * if offset != orig_start, which means the first unaligned page
2288 * including serveral following pages are already in holes,
2289 * the extra check can be skipped */
2290 if (offset == orig_start) {
2291 /* after truncate page, check hole again */
2292 len = offset + len - lockstart;
2293 offset = lockstart;
2294 ret = find_first_non_hole(inode, &offset, &len);
2295 if (ret < 0)
2296 goto out_only_mutex;
2297 if (ret && !len) {
2298 ret = 0;
2299 goto out_only_mutex;
2300 }
2301 lockstart = offset;
2302 }
2303
2304 /* Check the tail unaligned part is in a hole */
2305 tail_start = lockend + 1;
2306 tail_len = offset + len - tail_start;
2307 if (tail_len) {
2308 ret = find_first_non_hole(inode, &tail_start, &tail_len);
2309 if (unlikely(ret < 0))
2310 goto out_only_mutex;
2311 if (!ret) {
2312 /* zero the front end of the last page */
2313 if (tail_start + tail_len < ino_size) {
2314 ret = btrfs_truncate_page(inode,
2315 tail_start + tail_len, 0, 1);
2316 if (ret)
2317 goto out_only_mutex;
2318 }
Miao Xie00612802012-12-05 10:54:12 +00002319 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002320 }
2321
2322 if (lockend < lockstart) {
2323 mutex_unlock(&inode->i_mutex);
2324 return 0;
2325 }
2326
2327 while (1) {
2328 struct btrfs_ordered_extent *ordered;
2329
2330 truncate_pagecache_range(inode, lockstart, lockend);
2331
2332 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2333 0, &cached_state);
2334 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
2335
2336 /*
2337 * We need to make sure we have no ordered extents in this range
2338 * and nobody raced in and read a page in this range, if we did
2339 * we need to try again.
2340 */
2341 if ((!ordered ||
Filipe David Borba Manana6126e3c2013-11-19 16:19:24 +00002342 (ordered->file_offset + ordered->len <= lockstart ||
Josef Bacik2aaa6652012-08-29 14:27:18 -04002343 ordered->file_offset > lockend)) &&
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07002344 !btrfs_page_exists_in_range(inode, lockstart, lockend)) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04002345 if (ordered)
2346 btrfs_put_ordered_extent(ordered);
2347 break;
2348 }
2349 if (ordered)
2350 btrfs_put_ordered_extent(ordered);
2351 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2352 lockend, &cached_state, GFP_NOFS);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002353 ret = btrfs_wait_ordered_range(inode, lockstart,
2354 lockend - lockstart + 1);
2355 if (ret) {
2356 mutex_unlock(&inode->i_mutex);
2357 return ret;
2358 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002359 }
2360
2361 path = btrfs_alloc_path();
2362 if (!path) {
2363 ret = -ENOMEM;
2364 goto out;
2365 }
2366
Miao Xie66d8f3d2012-09-06 04:02:28 -06002367 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002368 if (!rsv) {
2369 ret = -ENOMEM;
2370 goto out_free;
2371 }
2372 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
2373 rsv->failfast = 1;
2374
2375 /*
2376 * 1 - update the inode
2377 * 1 - removing the extents in the range
Josef Bacik16e75492013-10-22 12:18:51 -04002378 * 1 - adding the hole extent if no_holes isn't set
Josef Bacik2aaa6652012-08-29 14:27:18 -04002379 */
Josef Bacik16e75492013-10-22 12:18:51 -04002380 rsv_count = no_holes ? 2 : 3;
2381 trans = btrfs_start_transaction(root, rsv_count);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002382 if (IS_ERR(trans)) {
2383 err = PTR_ERR(trans);
2384 goto out_free;
2385 }
2386
2387 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
2388 min_size);
2389 BUG_ON(ret);
2390 trans->block_rsv = rsv;
2391
Qu Wenruod7781542014-05-30 15:16:10 +08002392 cur_offset = lockstart;
2393 len = lockend - cur_offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002394 while (cur_offset < lockend) {
2395 ret = __btrfs_drop_extents(trans, root, inode, path,
2396 cur_offset, lockend + 1,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002397 &drop_end, 1, 0, 0, NULL);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002398 if (ret != -ENOSPC)
2399 break;
2400
2401 trans->block_rsv = &root->fs_info->trans_block_rsv;
2402
Filipe Manana12870f12014-02-15 15:55:58 +00002403 if (cur_offset < ino_size) {
2404 ret = fill_holes(trans, inode, path, cur_offset,
2405 drop_end);
2406 if (ret) {
2407 err = ret;
2408 break;
2409 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002410 }
2411
2412 cur_offset = drop_end;
2413
2414 ret = btrfs_update_inode(trans, root, inode);
2415 if (ret) {
2416 err = ret;
2417 break;
2418 }
2419
Josef Bacik2aaa6652012-08-29 14:27:18 -04002420 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002421 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002422
Josef Bacik16e75492013-10-22 12:18:51 -04002423 trans = btrfs_start_transaction(root, rsv_count);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002424 if (IS_ERR(trans)) {
2425 ret = PTR_ERR(trans);
2426 trans = NULL;
2427 break;
2428 }
2429
2430 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
2431 rsv, min_size);
2432 BUG_ON(ret); /* shouldn't happen */
2433 trans->block_rsv = rsv;
Qu Wenruod7781542014-05-30 15:16:10 +08002434
2435 ret = find_first_non_hole(inode, &cur_offset, &len);
2436 if (unlikely(ret < 0))
2437 break;
2438 if (ret && !len) {
2439 ret = 0;
2440 break;
2441 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002442 }
2443
2444 if (ret) {
2445 err = ret;
2446 goto out_trans;
2447 }
2448
2449 trans->block_rsv = &root->fs_info->trans_block_rsv;
Filipe Mananafc19c5e2014-04-29 13:18:40 +01002450 /*
2451 * Don't insert file hole extent item if it's for a range beyond eof
2452 * (because it's useless) or if it represents a 0 bytes range (when
2453 * cur_offset == drop_end).
2454 */
2455 if (cur_offset < ino_size && cur_offset < drop_end) {
Filipe Manana12870f12014-02-15 15:55:58 +00002456 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2457 if (ret) {
2458 err = ret;
2459 goto out_trans;
2460 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002461 }
2462
2463out_trans:
2464 if (!trans)
2465 goto out_free;
2466
Tsutomu Itohe1f57902012-11-08 04:47:33 +00002467 inode_inc_iversion(inode);
2468 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2469
Josef Bacik2aaa6652012-08-29 14:27:18 -04002470 trans->block_rsv = &root->fs_info->trans_block_rsv;
2471 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002472 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002473 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002474out_free:
2475 btrfs_free_path(path);
2476 btrfs_free_block_rsv(root, rsv);
2477out:
2478 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2479 &cached_state, GFP_NOFS);
Qu Wenruod7781542014-05-30 15:16:10 +08002480out_only_mutex:
Josef Bacik2aaa6652012-08-29 14:27:18 -04002481 mutex_unlock(&inode->i_mutex);
2482 if (ret && !err)
2483 err = ret;
2484 return err;
2485}
2486
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002487static long btrfs_fallocate(struct file *file, int mode,
2488 loff_t offset, loff_t len)
2489{
Al Viro496ad9a2013-01-23 17:07:38 -05002490 struct inode *inode = file_inode(file);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002491 struct extent_state *cached_state = NULL;
Wang Shilong61130772013-03-19 10:57:14 +00002492 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002493 u64 cur_offset;
2494 u64 last_byte;
2495 u64 alloc_start;
2496 u64 alloc_end;
2497 u64 alloc_hint = 0;
2498 u64 locked_end;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002499 struct extent_map *em;
Miao Xie797f4272012-11-28 10:28:07 +00002500 int blocksize = BTRFS_I(inode)->root->sectorsize;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002501 int ret;
2502
Miao Xie797f4272012-11-28 10:28:07 +00002503 alloc_start = round_down(offset, blocksize);
2504 alloc_end = round_up(offset + len, blocksize);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002505
Josef Bacik2aaa6652012-08-29 14:27:18 -04002506 /* Make sure we aren't being give some crap mode */
2507 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002508 return -EOPNOTSUPP;
2509
Josef Bacik2aaa6652012-08-29 14:27:18 -04002510 if (mode & FALLOC_FL_PUNCH_HOLE)
2511 return btrfs_punch_hole(inode, offset, len);
2512
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002513 /*
Chris Masond98456f2012-01-31 20:27:41 -05002514 * Make sure we have enough space before we do the
2515 * allocation.
2516 */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002517 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Chris Masond98456f2012-01-31 20:27:41 -05002518 if (ret)
2519 return ret;
Wang Shilong61130772013-03-19 10:57:14 +00002520 if (root->fs_info->quota_enabled) {
2521 ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
2522 if (ret)
2523 goto out_reserve_fail;
2524 }
Chris Masond98456f2012-01-31 20:27:41 -05002525
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002526 mutex_lock(&inode->i_mutex);
2527 ret = inode_newsize_ok(inode, alloc_end);
2528 if (ret)
2529 goto out;
2530
2531 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002532 ret = btrfs_cont_expand(inode, i_size_read(inode),
2533 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002534 if (ret)
2535 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04002536 } else {
2537 /*
2538 * If we are fallocating from the end of the file onward we
2539 * need to zero out the end of the page if i_size lands in the
2540 * middle of a page.
2541 */
2542 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
2543 if (ret)
2544 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002545 }
2546
Josef Bacika71754f2013-06-17 17:14:39 -04002547 /*
2548 * wait for ordered IO before we have any locks. We'll loop again
2549 * below with the locks held.
2550 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04002551 ret = btrfs_wait_ordered_range(inode, alloc_start,
2552 alloc_end - alloc_start);
2553 if (ret)
2554 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04002555
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002556 locked_end = alloc_end - 1;
2557 while (1) {
2558 struct btrfs_ordered_extent *ordered;
2559
2560 /* the extent lock is ordered inside the running
2561 * transaction
2562 */
2563 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002564 locked_end, 0, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002565 ordered = btrfs_lookup_first_ordered_extent(inode,
2566 alloc_end - 1);
2567 if (ordered &&
2568 ordered->file_offset + ordered->len > alloc_start &&
2569 ordered->file_offset < alloc_end) {
2570 btrfs_put_ordered_extent(ordered);
2571 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2572 alloc_start, locked_end,
2573 &cached_state, GFP_NOFS);
2574 /*
2575 * we can't wait on the range with the transaction
2576 * running or with the extent lock held
2577 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04002578 ret = btrfs_wait_ordered_range(inode, alloc_start,
2579 alloc_end - alloc_start);
2580 if (ret)
2581 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002582 } else {
2583 if (ordered)
2584 btrfs_put_ordered_extent(ordered);
2585 break;
2586 }
2587 }
2588
2589 cur_offset = alloc_start;
2590 while (1) {
Josef Bacikf1e490a2011-08-18 10:36:39 -04002591 u64 actual_end;
2592
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002593 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2594 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002595 if (IS_ERR_OR_NULL(em)) {
2596 if (!em)
2597 ret = -ENOMEM;
2598 else
2599 ret = PTR_ERR(em);
2600 break;
2601 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002602 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002603 actual_end = min_t(u64, extent_map_end(em), offset + len);
Miao Xie797f4272012-11-28 10:28:07 +00002604 last_byte = ALIGN(last_byte, blocksize);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002605
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002606 if (em->block_start == EXTENT_MAP_HOLE ||
2607 (cur_offset >= inode->i_size &&
2608 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2609 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2610 last_byte - cur_offset,
2611 1 << inode->i_blkbits,
2612 offset + len,
2613 &alloc_hint);
Josef Bacik1b9c3322011-08-17 10:19:52 -04002614
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002615 if (ret < 0) {
2616 free_extent_map(em);
2617 break;
2618 }
Josef Bacikf1e490a2011-08-18 10:36:39 -04002619 } else if (actual_end > inode->i_size &&
2620 !(mode & FALLOC_FL_KEEP_SIZE)) {
2621 /*
2622 * We didn't need to allocate any more space, but we
2623 * still extended the size of the file so we need to
2624 * update i_size.
2625 */
2626 inode->i_ctime = CURRENT_TIME;
2627 i_size_write(inode, actual_end);
2628 btrfs_ordered_update_i_size(inode, actual_end, NULL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002629 }
2630 free_extent_map(em);
2631
2632 cur_offset = last_byte;
2633 if (cur_offset >= alloc_end) {
2634 ret = 0;
2635 break;
2636 }
2637 }
2638 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2639 &cached_state, GFP_NOFS);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002640out:
2641 mutex_unlock(&inode->i_mutex);
Wang Shilong61130772013-03-19 10:57:14 +00002642 if (root->fs_info->quota_enabled)
2643 btrfs_qgroup_free(root, alloc_end - alloc_start);
2644out_reserve_fail:
Chris Masond98456f2012-01-31 20:27:41 -05002645 /* Let go of our reservation. */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002646 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002647 return ret;
2648}
2649
Andrew Morton965c8e52012-12-17 15:59:39 -08002650static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002651{
2652 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik7f4ca372013-10-18 11:44:46 -04002653 struct extent_map *em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002654 struct extent_state *cached_state = NULL;
2655 u64 lockstart = *offset;
2656 u64 lockend = i_size_read(inode);
2657 u64 start = *offset;
Josef Bacikb2675152011-07-18 13:21:36 -04002658 u64 len = i_size_read(inode);
Josef Bacikb2675152011-07-18 13:21:36 -04002659 int ret = 0;
2660
2661 lockend = max_t(u64, root->sectorsize, lockend);
2662 if (lockend <= lockstart)
2663 lockend = lockstart + root->sectorsize;
2664
Liu Bo1214b532013-01-07 03:53:08 +00002665 lockend--;
Josef Bacikb2675152011-07-18 13:21:36 -04002666 len = lockend - lockstart + 1;
2667
2668 len = max_t(u64, len, root->sectorsize);
2669 if (inode->i_size == 0)
2670 return -ENXIO;
2671
2672 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002673 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002674
Josef Bacik7f4ca372013-10-18 11:44:46 -04002675 while (start < inode->i_size) {
Josef Bacikb2675152011-07-18 13:21:36 -04002676 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2677 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002678 ret = PTR_ERR(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002679 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002680 break;
2681 }
2682
Josef Bacik7f4ca372013-10-18 11:44:46 -04002683 if (whence == SEEK_HOLE &&
2684 (em->block_start == EXTENT_MAP_HOLE ||
2685 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2686 break;
2687 else if (whence == SEEK_DATA &&
2688 (em->block_start != EXTENT_MAP_HOLE &&
2689 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2690 break;
Josef Bacikb2675152011-07-18 13:21:36 -04002691
2692 start = em->start + em->len;
Josef Bacikb2675152011-07-18 13:21:36 -04002693 free_extent_map(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002694 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002695 cond_resched();
2696 }
Josef Bacik7f4ca372013-10-18 11:44:46 -04002697 free_extent_map(em);
2698 if (!ret) {
2699 if (whence == SEEK_DATA && start >= inode->i_size)
2700 ret = -ENXIO;
2701 else
2702 *offset = min_t(loff_t, start, inode->i_size);
2703 }
Josef Bacikb2675152011-07-18 13:21:36 -04002704 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2705 &cached_state, GFP_NOFS);
2706 return ret;
2707}
2708
Andrew Morton965c8e52012-12-17 15:59:39 -08002709static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002710{
2711 struct inode *inode = file->f_mapping->host;
2712 int ret;
2713
2714 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -08002715 switch (whence) {
Josef Bacikb2675152011-07-18 13:21:36 -04002716 case SEEK_END:
2717 case SEEK_CUR:
Andrew Morton965c8e52012-12-17 15:59:39 -08002718 offset = generic_file_llseek(file, offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04002719 goto out;
2720 case SEEK_DATA:
2721 case SEEK_HOLE:
Jeff Liu48802c82011-09-18 10:34:02 -04002722 if (offset >= i_size_read(inode)) {
2723 mutex_unlock(&inode->i_mutex);
2724 return -ENXIO;
2725 }
2726
Andrew Morton965c8e52012-12-17 15:59:39 -08002727 ret = find_desired_extent(inode, &offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04002728 if (ret) {
2729 mutex_unlock(&inode->i_mutex);
2730 return ret;
2731 }
2732 }
2733
Jie Liu46a1c2c72013-06-25 12:02:13 +08002734 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Josef Bacikb2675152011-07-18 13:21:36 -04002735out:
2736 mutex_unlock(&inode->i_mutex);
2737 return offset;
2738}
2739
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002740const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04002741 .llseek = btrfs_file_llseek,
Chris Mason39279cc2007-06-12 06:35:45 -04002742 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00002743 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002744 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05002745 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04002746 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002747 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04002748 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04002749 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002750 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002751 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002752 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002753#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002754 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002755#endif
2756};
Miao Xie9247f312012-11-26 09:24:43 +00002757
2758void btrfs_auto_defrag_exit(void)
2759{
2760 if (btrfs_inode_defrag_cachep)
2761 kmem_cache_destroy(btrfs_inode_defrag_cachep);
2762}
2763
2764int btrfs_auto_defrag_init(void)
2765{
2766 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
2767 sizeof(struct inode_defrag), 0,
2768 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2769 NULL);
2770 if (!btrfs_inode_defrag_cachep)
2771 return -ENOMEM;
2772
2773 return 0;
2774}