blob: 8accf94ef220515965e634d7ce759cae7a4f4eb9 [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 Hellwig02afc27f2013-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 Bacik22ee6982012-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);
2028 if (ret)
2029 goto out;
2030 }
2031 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04002032 } else {
2033 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002034 }
Chris Mason39279cc2007-06-12 06:35:45 -04002035out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00002036 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002037}
2038
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002039static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04002040 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002041 .map_pages = filemap_map_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04002042 .page_mkwrite = btrfs_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002043 .remap_pages = generic_file_remap_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04002044};
2045
2046static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2047{
Miao Xie058a4572010-05-20 07:21:50 +00002048 struct address_space *mapping = filp->f_mapping;
2049
2050 if (!mapping->a_ops->readpage)
2051 return -ENOEXEC;
2052
Chris Mason9ebefb182007-06-15 13:50:00 -04002053 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00002054 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00002055
Chris Mason9ebefb182007-06-15 13:50:00 -04002056 return 0;
2057}
2058
Josef Bacik2aaa6652012-08-29 14:27:18 -04002059static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
2060 int slot, u64 start, u64 end)
2061{
2062 struct btrfs_file_extent_item *fi;
2063 struct btrfs_key key;
2064
2065 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2066 return 0;
2067
2068 btrfs_item_key_to_cpu(leaf, &key, slot);
2069 if (key.objectid != btrfs_ino(inode) ||
2070 key.type != BTRFS_EXTENT_DATA_KEY)
2071 return 0;
2072
2073 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2074
2075 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2076 return 0;
2077
2078 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2079 return 0;
2080
2081 if (key.offset == end)
2082 return 1;
2083 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2084 return 1;
2085 return 0;
2086}
2087
2088static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2089 struct btrfs_path *path, u64 offset, u64 end)
2090{
2091 struct btrfs_root *root = BTRFS_I(inode)->root;
2092 struct extent_buffer *leaf;
2093 struct btrfs_file_extent_item *fi;
2094 struct extent_map *hole_em;
2095 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2096 struct btrfs_key key;
2097 int ret;
2098
Josef Bacik16e75492013-10-22 12:18:51 -04002099 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
2100 goto out;
2101
Josef Bacik2aaa6652012-08-29 14:27:18 -04002102 key.objectid = btrfs_ino(inode);
2103 key.type = BTRFS_EXTENT_DATA_KEY;
2104 key.offset = offset;
2105
Josef Bacik2aaa6652012-08-29 14:27:18 -04002106 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2107 if (ret < 0)
2108 return ret;
2109 BUG_ON(!ret);
2110
2111 leaf = path->nodes[0];
2112 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
2113 u64 num_bytes;
2114
2115 path->slots[0]--;
2116 fi = btrfs_item_ptr(leaf, path->slots[0],
2117 struct btrfs_file_extent_item);
2118 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2119 end - offset;
2120 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2121 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2122 btrfs_set_file_extent_offset(leaf, fi, 0);
2123 btrfs_mark_buffer_dirty(leaf);
2124 goto out;
2125 }
2126
2127 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
2128 u64 num_bytes;
2129
2130 path->slots[0]++;
2131 key.offset = offset;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002132 btrfs_set_item_key_safe(root, path, &key);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002133 fi = btrfs_item_ptr(leaf, path->slots[0],
2134 struct btrfs_file_extent_item);
2135 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2136 offset;
2137 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2138 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2139 btrfs_set_file_extent_offset(leaf, fi, 0);
2140 btrfs_mark_buffer_dirty(leaf);
2141 goto out;
2142 }
2143 btrfs_release_path(path);
2144
2145 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
2146 0, 0, end - offset, 0, end - offset,
2147 0, 0, 0);
2148 if (ret)
2149 return ret;
2150
2151out:
2152 btrfs_release_path(path);
2153
2154 hole_em = alloc_extent_map();
2155 if (!hole_em) {
2156 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2157 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2158 &BTRFS_I(inode)->runtime_flags);
2159 } else {
2160 hole_em->start = offset;
2161 hole_em->len = end - offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04002162 hole_em->ram_bytes = hole_em->len;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002163 hole_em->orig_start = offset;
2164
2165 hole_em->block_start = EXTENT_MAP_HOLE;
2166 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05002167 hole_em->orig_block_len = 0;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002168 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
2169 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2170 hole_em->generation = trans->transid;
2171
2172 do {
2173 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2174 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002175 ret = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002176 write_unlock(&em_tree->lock);
2177 } while (ret == -EEXIST);
2178 free_extent_map(hole_em);
2179 if (ret)
2180 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2181 &BTRFS_I(inode)->runtime_flags);
2182 }
2183
2184 return 0;
2185}
2186
2187static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2188{
2189 struct btrfs_root *root = BTRFS_I(inode)->root;
2190 struct extent_state *cached_state = NULL;
2191 struct btrfs_path *path;
2192 struct btrfs_block_rsv *rsv;
2193 struct btrfs_trans_handle *trans;
Miao Xie00612802012-12-05 10:54:12 +00002194 u64 lockstart = round_up(offset, BTRFS_I(inode)->root->sectorsize);
2195 u64 lockend = round_down(offset + len,
2196 BTRFS_I(inode)->root->sectorsize) - 1;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002197 u64 cur_offset = lockstart;
2198 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
2199 u64 drop_end;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002200 int ret = 0;
2201 int err = 0;
Josef Bacik16e75492013-10-22 12:18:51 -04002202 int rsv_count;
Miao Xie6347b3c2012-12-05 10:53:45 +00002203 bool same_page = ((offset >> PAGE_CACHE_SHIFT) ==
2204 ((offset + len - 1) >> PAGE_CACHE_SHIFT));
Josef Bacik16e75492013-10-22 12:18:51 -04002205 bool no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
Filipe Mananaa1a50f62014-04-26 01:35:31 +01002206 u64 ino_size;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002207
Josef Bacik0ef8b722013-10-25 16:13:35 -04002208 ret = btrfs_wait_ordered_range(inode, offset, len);
2209 if (ret)
2210 return ret;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002211
2212 mutex_lock(&inode->i_mutex);
Filipe Mananaa1a50f62014-04-26 01:35:31 +01002213 ino_size = round_up(inode->i_size, PAGE_CACHE_SIZE);
Miao Xie7426cc02012-12-05 10:54:52 +00002214 /*
2215 * We needn't truncate any page which is beyond the end of the file
2216 * because we are sure there is no data there.
2217 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04002218 /*
2219 * Only do this if we are in the same page and we aren't doing the
2220 * entire page.
2221 */
2222 if (same_page && len < PAGE_CACHE_SIZE) {
Filipe Manana12870f12014-02-15 15:55:58 +00002223 if (offset < ino_size)
Miao Xie7426cc02012-12-05 10:54:52 +00002224 ret = btrfs_truncate_page(inode, offset, len, 0);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002225 mutex_unlock(&inode->i_mutex);
2226 return ret;
2227 }
2228
2229 /* zero back part of the first page */
Filipe Manana12870f12014-02-15 15:55:58 +00002230 if (offset < ino_size) {
Miao Xie7426cc02012-12-05 10:54:52 +00002231 ret = btrfs_truncate_page(inode, offset, 0, 0);
2232 if (ret) {
2233 mutex_unlock(&inode->i_mutex);
2234 return ret;
2235 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002236 }
2237
2238 /* zero the front end of the last page */
Filipe Manana12870f12014-02-15 15:55:58 +00002239 if (offset + len < ino_size) {
Miao Xie00612802012-12-05 10:54:12 +00002240 ret = btrfs_truncate_page(inode, offset + len, 0, 1);
2241 if (ret) {
2242 mutex_unlock(&inode->i_mutex);
2243 return ret;
2244 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002245 }
2246
2247 if (lockend < lockstart) {
2248 mutex_unlock(&inode->i_mutex);
2249 return 0;
2250 }
2251
2252 while (1) {
2253 struct btrfs_ordered_extent *ordered;
2254
2255 truncate_pagecache_range(inode, lockstart, lockend);
2256
2257 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2258 0, &cached_state);
2259 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
2260
2261 /*
2262 * We need to make sure we have no ordered extents in this range
2263 * and nobody raced in and read a page in this range, if we did
2264 * we need to try again.
2265 */
2266 if ((!ordered ||
Filipe David Borba Manana6126e3c2013-11-19 16:19:24 +00002267 (ordered->file_offset + ordered->len <= lockstart ||
Josef Bacik2aaa6652012-08-29 14:27:18 -04002268 ordered->file_offset > lockend)) &&
2269 !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
2270 lockend, EXTENT_UPTODATE, 0,
2271 cached_state)) {
2272 if (ordered)
2273 btrfs_put_ordered_extent(ordered);
2274 break;
2275 }
2276 if (ordered)
2277 btrfs_put_ordered_extent(ordered);
2278 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2279 lockend, &cached_state, GFP_NOFS);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002280 ret = btrfs_wait_ordered_range(inode, lockstart,
2281 lockend - lockstart + 1);
2282 if (ret) {
2283 mutex_unlock(&inode->i_mutex);
2284 return ret;
2285 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002286 }
2287
2288 path = btrfs_alloc_path();
2289 if (!path) {
2290 ret = -ENOMEM;
2291 goto out;
2292 }
2293
Miao Xie66d8f3d2012-09-06 04:02:28 -06002294 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002295 if (!rsv) {
2296 ret = -ENOMEM;
2297 goto out_free;
2298 }
2299 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
2300 rsv->failfast = 1;
2301
2302 /*
2303 * 1 - update the inode
2304 * 1 - removing the extents in the range
Josef Bacik16e75492013-10-22 12:18:51 -04002305 * 1 - adding the hole extent if no_holes isn't set
Josef Bacik2aaa6652012-08-29 14:27:18 -04002306 */
Josef Bacik16e75492013-10-22 12:18:51 -04002307 rsv_count = no_holes ? 2 : 3;
2308 trans = btrfs_start_transaction(root, rsv_count);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002309 if (IS_ERR(trans)) {
2310 err = PTR_ERR(trans);
2311 goto out_free;
2312 }
2313
2314 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
2315 min_size);
2316 BUG_ON(ret);
2317 trans->block_rsv = rsv;
2318
2319 while (cur_offset < lockend) {
2320 ret = __btrfs_drop_extents(trans, root, inode, path,
2321 cur_offset, lockend + 1,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002322 &drop_end, 1, 0, 0, NULL);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002323 if (ret != -ENOSPC)
2324 break;
2325
2326 trans->block_rsv = &root->fs_info->trans_block_rsv;
2327
Filipe Manana12870f12014-02-15 15:55:58 +00002328 if (cur_offset < ino_size) {
2329 ret = fill_holes(trans, inode, path, cur_offset,
2330 drop_end);
2331 if (ret) {
2332 err = ret;
2333 break;
2334 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002335 }
2336
2337 cur_offset = drop_end;
2338
2339 ret = btrfs_update_inode(trans, root, inode);
2340 if (ret) {
2341 err = ret;
2342 break;
2343 }
2344
Josef Bacik2aaa6652012-08-29 14:27:18 -04002345 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002346 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002347
Josef Bacik16e75492013-10-22 12:18:51 -04002348 trans = btrfs_start_transaction(root, rsv_count);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002349 if (IS_ERR(trans)) {
2350 ret = PTR_ERR(trans);
2351 trans = NULL;
2352 break;
2353 }
2354
2355 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
2356 rsv, min_size);
2357 BUG_ON(ret); /* shouldn't happen */
2358 trans->block_rsv = rsv;
2359 }
2360
2361 if (ret) {
2362 err = ret;
2363 goto out_trans;
2364 }
2365
2366 trans->block_rsv = &root->fs_info->trans_block_rsv;
Filipe Mananafc19c5e2014-04-29 13:18:40 +01002367 /*
2368 * Don't insert file hole extent item if it's for a range beyond eof
2369 * (because it's useless) or if it represents a 0 bytes range (when
2370 * cur_offset == drop_end).
2371 */
2372 if (cur_offset < ino_size && cur_offset < drop_end) {
Filipe Manana12870f12014-02-15 15:55:58 +00002373 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2374 if (ret) {
2375 err = ret;
2376 goto out_trans;
2377 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002378 }
2379
2380out_trans:
2381 if (!trans)
2382 goto out_free;
2383
Tsutomu Itohe1f57902012-11-08 04:47:33 +00002384 inode_inc_iversion(inode);
2385 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2386
Josef Bacik2aaa6652012-08-29 14:27:18 -04002387 trans->block_rsv = &root->fs_info->trans_block_rsv;
2388 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002389 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002390 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002391out_free:
2392 btrfs_free_path(path);
2393 btrfs_free_block_rsv(root, rsv);
2394out:
2395 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2396 &cached_state, GFP_NOFS);
2397 mutex_unlock(&inode->i_mutex);
2398 if (ret && !err)
2399 err = ret;
2400 return err;
2401}
2402
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002403static long btrfs_fallocate(struct file *file, int mode,
2404 loff_t offset, loff_t len)
2405{
Al Viro496ad9a2013-01-23 17:07:38 -05002406 struct inode *inode = file_inode(file);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002407 struct extent_state *cached_state = NULL;
Wang Shilong61130772013-03-19 10:57:14 +00002408 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002409 u64 cur_offset;
2410 u64 last_byte;
2411 u64 alloc_start;
2412 u64 alloc_end;
2413 u64 alloc_hint = 0;
2414 u64 locked_end;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002415 struct extent_map *em;
Miao Xie797f4272012-11-28 10:28:07 +00002416 int blocksize = BTRFS_I(inode)->root->sectorsize;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002417 int ret;
2418
Miao Xie797f4272012-11-28 10:28:07 +00002419 alloc_start = round_down(offset, blocksize);
2420 alloc_end = round_up(offset + len, blocksize);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002421
Josef Bacik2aaa6652012-08-29 14:27:18 -04002422 /* Make sure we aren't being give some crap mode */
2423 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002424 return -EOPNOTSUPP;
2425
Josef Bacik2aaa6652012-08-29 14:27:18 -04002426 if (mode & FALLOC_FL_PUNCH_HOLE)
2427 return btrfs_punch_hole(inode, offset, len);
2428
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002429 /*
Chris Masond98456f2012-01-31 20:27:41 -05002430 * Make sure we have enough space before we do the
2431 * allocation.
2432 */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002433 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Chris Masond98456f2012-01-31 20:27:41 -05002434 if (ret)
2435 return ret;
Wang Shilong61130772013-03-19 10:57:14 +00002436 if (root->fs_info->quota_enabled) {
2437 ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
2438 if (ret)
2439 goto out_reserve_fail;
2440 }
Chris Masond98456f2012-01-31 20:27:41 -05002441
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002442 mutex_lock(&inode->i_mutex);
2443 ret = inode_newsize_ok(inode, alloc_end);
2444 if (ret)
2445 goto out;
2446
2447 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002448 ret = btrfs_cont_expand(inode, i_size_read(inode),
2449 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002450 if (ret)
2451 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04002452 } else {
2453 /*
2454 * If we are fallocating from the end of the file onward we
2455 * need to zero out the end of the page if i_size lands in the
2456 * middle of a page.
2457 */
2458 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
2459 if (ret)
2460 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002461 }
2462
Josef Bacika71754f2013-06-17 17:14:39 -04002463 /*
2464 * wait for ordered IO before we have any locks. We'll loop again
2465 * below with the locks held.
2466 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04002467 ret = btrfs_wait_ordered_range(inode, alloc_start,
2468 alloc_end - alloc_start);
2469 if (ret)
2470 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04002471
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002472 locked_end = alloc_end - 1;
2473 while (1) {
2474 struct btrfs_ordered_extent *ordered;
2475
2476 /* the extent lock is ordered inside the running
2477 * transaction
2478 */
2479 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002480 locked_end, 0, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002481 ordered = btrfs_lookup_first_ordered_extent(inode,
2482 alloc_end - 1);
2483 if (ordered &&
2484 ordered->file_offset + ordered->len > alloc_start &&
2485 ordered->file_offset < alloc_end) {
2486 btrfs_put_ordered_extent(ordered);
2487 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2488 alloc_start, locked_end,
2489 &cached_state, GFP_NOFS);
2490 /*
2491 * we can't wait on the range with the transaction
2492 * running or with the extent lock held
2493 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04002494 ret = btrfs_wait_ordered_range(inode, alloc_start,
2495 alloc_end - alloc_start);
2496 if (ret)
2497 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002498 } else {
2499 if (ordered)
2500 btrfs_put_ordered_extent(ordered);
2501 break;
2502 }
2503 }
2504
2505 cur_offset = alloc_start;
2506 while (1) {
Josef Bacikf1e490a2011-08-18 10:36:39 -04002507 u64 actual_end;
2508
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002509 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2510 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002511 if (IS_ERR_OR_NULL(em)) {
2512 if (!em)
2513 ret = -ENOMEM;
2514 else
2515 ret = PTR_ERR(em);
2516 break;
2517 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002518 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002519 actual_end = min_t(u64, extent_map_end(em), offset + len);
Miao Xie797f4272012-11-28 10:28:07 +00002520 last_byte = ALIGN(last_byte, blocksize);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002521
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002522 if (em->block_start == EXTENT_MAP_HOLE ||
2523 (cur_offset >= inode->i_size &&
2524 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2525 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2526 last_byte - cur_offset,
2527 1 << inode->i_blkbits,
2528 offset + len,
2529 &alloc_hint);
Josef Bacik1b9c3322011-08-17 10:19:52 -04002530
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002531 if (ret < 0) {
2532 free_extent_map(em);
2533 break;
2534 }
Josef Bacikf1e490a2011-08-18 10:36:39 -04002535 } else if (actual_end > inode->i_size &&
2536 !(mode & FALLOC_FL_KEEP_SIZE)) {
2537 /*
2538 * We didn't need to allocate any more space, but we
2539 * still extended the size of the file so we need to
2540 * update i_size.
2541 */
2542 inode->i_ctime = CURRENT_TIME;
2543 i_size_write(inode, actual_end);
2544 btrfs_ordered_update_i_size(inode, actual_end, NULL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002545 }
2546 free_extent_map(em);
2547
2548 cur_offset = last_byte;
2549 if (cur_offset >= alloc_end) {
2550 ret = 0;
2551 break;
2552 }
2553 }
2554 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2555 &cached_state, GFP_NOFS);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002556out:
2557 mutex_unlock(&inode->i_mutex);
Wang Shilong61130772013-03-19 10:57:14 +00002558 if (root->fs_info->quota_enabled)
2559 btrfs_qgroup_free(root, alloc_end - alloc_start);
2560out_reserve_fail:
Chris Masond98456f2012-01-31 20:27:41 -05002561 /* Let go of our reservation. */
Miao Xie0ff6fab2012-11-28 10:28:54 +00002562 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002563 return ret;
2564}
2565
Andrew Morton965c8e52012-12-17 15:59:39 -08002566static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002567{
2568 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik7f4ca372013-10-18 11:44:46 -04002569 struct extent_map *em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002570 struct extent_state *cached_state = NULL;
2571 u64 lockstart = *offset;
2572 u64 lockend = i_size_read(inode);
2573 u64 start = *offset;
Josef Bacikb2675152011-07-18 13:21:36 -04002574 u64 len = i_size_read(inode);
Josef Bacikb2675152011-07-18 13:21:36 -04002575 int ret = 0;
2576
2577 lockend = max_t(u64, root->sectorsize, lockend);
2578 if (lockend <= lockstart)
2579 lockend = lockstart + root->sectorsize;
2580
Liu Bo1214b532013-01-07 03:53:08 +00002581 lockend--;
Josef Bacikb2675152011-07-18 13:21:36 -04002582 len = lockend - lockstart + 1;
2583
2584 len = max_t(u64, len, root->sectorsize);
2585 if (inode->i_size == 0)
2586 return -ENXIO;
2587
2588 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002589 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002590
Josef Bacik7f4ca372013-10-18 11:44:46 -04002591 while (start < inode->i_size) {
Josef Bacikb2675152011-07-18 13:21:36 -04002592 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2593 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002594 ret = PTR_ERR(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002595 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002596 break;
2597 }
2598
Josef Bacik7f4ca372013-10-18 11:44:46 -04002599 if (whence == SEEK_HOLE &&
2600 (em->block_start == EXTENT_MAP_HOLE ||
2601 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2602 break;
2603 else if (whence == SEEK_DATA &&
2604 (em->block_start != EXTENT_MAP_HOLE &&
2605 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2606 break;
Josef Bacikb2675152011-07-18 13:21:36 -04002607
2608 start = em->start + em->len;
Josef Bacikb2675152011-07-18 13:21:36 -04002609 free_extent_map(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002610 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002611 cond_resched();
2612 }
Josef Bacik7f4ca372013-10-18 11:44:46 -04002613 free_extent_map(em);
2614 if (!ret) {
2615 if (whence == SEEK_DATA && start >= inode->i_size)
2616 ret = -ENXIO;
2617 else
2618 *offset = min_t(loff_t, start, inode->i_size);
2619 }
Josef Bacikb2675152011-07-18 13:21:36 -04002620 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2621 &cached_state, GFP_NOFS);
2622 return ret;
2623}
2624
Andrew Morton965c8e52012-12-17 15:59:39 -08002625static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002626{
2627 struct inode *inode = file->f_mapping->host;
2628 int ret;
2629
2630 mutex_lock(&inode->i_mutex);
Andrew Morton965c8e52012-12-17 15:59:39 -08002631 switch (whence) {
Josef Bacikb2675152011-07-18 13:21:36 -04002632 case SEEK_END:
2633 case SEEK_CUR:
Andrew Morton965c8e52012-12-17 15:59:39 -08002634 offset = generic_file_llseek(file, offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04002635 goto out;
2636 case SEEK_DATA:
2637 case SEEK_HOLE:
Jeff Liu48802c8a2011-09-18 10:34:02 -04002638 if (offset >= i_size_read(inode)) {
2639 mutex_unlock(&inode->i_mutex);
2640 return -ENXIO;
2641 }
2642
Andrew Morton965c8e52012-12-17 15:59:39 -08002643 ret = find_desired_extent(inode, &offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04002644 if (ret) {
2645 mutex_unlock(&inode->i_mutex);
2646 return ret;
2647 }
2648 }
2649
Jie Liu46a1c2c2013-06-25 12:02:13 +08002650 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Josef Bacikb2675152011-07-18 13:21:36 -04002651out:
2652 mutex_unlock(&inode->i_mutex);
2653 return offset;
2654}
2655
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002656const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04002657 .llseek = btrfs_file_llseek,
Chris Mason39279cc2007-06-12 06:35:45 -04002658 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00002659 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002660 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05002661 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04002662 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002663 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04002664 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04002665 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002666 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002667 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002668 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002669#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002670 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002671#endif
2672};
Miao Xie9247f312012-11-26 09:24:43 +00002673
2674void btrfs_auto_defrag_exit(void)
2675{
2676 if (btrfs_inode_defrag_cachep)
2677 kmem_cache_destroy(btrfs_inode_defrag_cachep);
2678}
2679
2680int btrfs_auto_defrag_init(void)
2681{
2682 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
2683 sizeof(struct inode_defrag), 0,
2684 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2685 NULL);
2686 if (!btrfs_inode_defrag_cachep)
2687 return -ENOMEM;
2688
2689 return 0;
2690}