blob: d49d8eadf517b369aa54fdc8f6ae2bb9f1ba3972 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/fs.h>
20#include <linux/pagemap.h>
21#include <linux/highmem.h>
22#include <linux/time.h>
23#include <linux/init.h>
24#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040025#include <linux/backing-dev.h>
26#include <linux/mpage.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010027#include <linux/falloc.h>
Chris Mason39279cc2007-06-12 06:35:45 -040028#include <linux/swap.h>
29#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040030#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000032#include <linux/btrfs.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080033#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
40#include "locking.h"
Josef Bacik2aaa6652012-08-29 14:27:18 -040041#include "volumes.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070042#include "qgroup.h"
Anand Jainebb87652016-03-10 17:26:59 +080043#include "compression.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{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040098 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -040099 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
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400104 p = &fs_info->defrag_inodes.rb_node;
Chris Mason4cb53002011-05-24 15:35:30 -0400105 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);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400128 rb_insert_color(&defrag->rb_node, &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{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400134 struct btrfs_fs_info *fs_info = root->fs_info;
135
136 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
Miao Xie8ddc4732012-11-26 09:25:38 +0000137 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400138
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400139 if (btrfs_fs_closing(fs_info))
Miao Xie8ddc4732012-11-26 09:25:38 +0000140 return 0;
141
142 return 1;
Chris Mason4cb53002011-05-24 15:35:30 -0400143}
144
145/*
146 * insert a defrag record for this inode if auto defrag is
147 * enabled
148 */
149int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
150 struct inode *inode)
151{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400152 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -0400153 struct btrfs_root *root = BTRFS_I(inode)->root;
154 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400155 u64 transid;
Miao Xie8ddc4732012-11-26 09:25:38 +0000156 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400157
Miao Xie8ddc4732012-11-26 09:25:38 +0000158 if (!__need_auto_defrag(root))
Chris Mason4cb53002011-05-24 15:35:30 -0400159 return 0;
160
Josef Bacik72ac3c02012-05-23 14:13:11 -0400161 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
Chris Mason4cb53002011-05-24 15:35:30 -0400162 return 0;
163
164 if (trans)
165 transid = trans->transid;
166 else
167 transid = BTRFS_I(inode)->root->last_trans;
168
Miao Xie9247f312012-11-26 09:24:43 +0000169 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -0400170 if (!defrag)
171 return -ENOMEM;
172
David Sterbaa4689d22011-05-31 17:08:14 +0000173 defrag->ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400174 defrag->transid = transid;
175 defrag->root = root->root_key.objectid;
176
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400177 spin_lock(&fs_info->defrag_inodes_lock);
Miao Xie8ddc4732012-11-26 09:25:38 +0000178 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
179 /*
180 * If we set IN_DEFRAG flag and evict the inode from memory,
181 * and then re-read this inode, this new inode doesn't have
182 * IN_DEFRAG flag. At the case, we may find the existed defrag.
183 */
184 ret = __btrfs_add_inode_defrag(inode, defrag);
185 if (ret)
186 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
187 } else {
Miao Xie9247f312012-11-26 09:24:43 +0000188 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
Miao Xie8ddc4732012-11-26 09:25:38 +0000189 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400190 spin_unlock(&fs_info->defrag_inodes_lock);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000191 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400192}
193
194/*
Miao Xie8ddc4732012-11-26 09:25:38 +0000195 * Requeue the defrag object. If there is a defrag object that points to
196 * the same inode in the tree, we will merge them together (by
197 * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
Chris Mason4cb53002011-05-24 15:35:30 -0400198 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000199static void btrfs_requeue_inode_defrag(struct inode *inode,
200 struct inode_defrag *defrag)
Miao Xie8ddc4732012-11-26 09:25:38 +0000201{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8ddc4732012-11-26 09:25:38 +0000203 struct btrfs_root *root = BTRFS_I(inode)->root;
204 int ret;
205
206 if (!__need_auto_defrag(root))
207 goto out;
208
209 /*
210 * Here we don't check the IN_DEFRAG flag, because we need merge
211 * them together.
212 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400213 spin_lock(&fs_info->defrag_inodes_lock);
Miao Xie8ddc4732012-11-26 09:25:38 +0000214 ret = __btrfs_add_inode_defrag(inode, defrag);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400215 spin_unlock(&fs_info->defrag_inodes_lock);
Miao Xie8ddc4732012-11-26 09:25:38 +0000216 if (ret)
217 goto out;
218 return;
219out:
220 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
221}
222
223/*
Miao Xie26176e72012-11-26 09:26:20 +0000224 * pick the defragable inode that we want, if it doesn't exist, we will get
225 * the next one.
Chris Mason4cb53002011-05-24 15:35:30 -0400226 */
Miao Xie26176e72012-11-26 09:26:20 +0000227static struct inode_defrag *
228btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400229{
230 struct inode_defrag *entry = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800231 struct inode_defrag tmp;
Chris Mason4cb53002011-05-24 15:35:30 -0400232 struct rb_node *p;
233 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800234 int ret;
235
236 tmp.ino = ino;
237 tmp.root = root;
Chris Mason4cb53002011-05-24 15:35:30 -0400238
Miao Xie26176e72012-11-26 09:26:20 +0000239 spin_lock(&fs_info->defrag_inodes_lock);
240 p = fs_info->defrag_inodes.rb_node;
Chris Mason4cb53002011-05-24 15:35:30 -0400241 while (p) {
242 parent = p;
243 entry = rb_entry(parent, struct inode_defrag, rb_node);
244
Miao Xie762f2262012-05-24 18:58:27 +0800245 ret = __compare_inode_defrag(&tmp, entry);
246 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400247 p = parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800248 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400249 p = parent->rb_right;
250 else
Miao Xie26176e72012-11-26 09:26:20 +0000251 goto out;
Chris Mason4cb53002011-05-24 15:35:30 -0400252 }
253
Miao Xie26176e72012-11-26 09:26:20 +0000254 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
255 parent = rb_next(parent);
256 if (parent)
Chris Mason4cb53002011-05-24 15:35:30 -0400257 entry = rb_entry(parent, struct inode_defrag, rb_node);
Miao Xie26176e72012-11-26 09:26:20 +0000258 else
259 entry = NULL;
Chris Mason4cb53002011-05-24 15:35:30 -0400260 }
Miao Xie26176e72012-11-26 09:26:20 +0000261out:
262 if (entry)
263 rb_erase(parent, &fs_info->defrag_inodes);
264 spin_unlock(&fs_info->defrag_inodes_lock);
265 return entry;
266}
267
268void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
269{
270 struct inode_defrag *defrag;
271 struct rb_node *node;
272
273 spin_lock(&fs_info->defrag_inodes_lock);
274 node = rb_first(&fs_info->defrag_inodes);
275 while (node) {
276 rb_erase(node, &fs_info->defrag_inodes);
277 defrag = rb_entry(node, struct inode_defrag, rb_node);
278 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
279
David Sterba351810c2015-01-08 15:20:54 +0100280 cond_resched_lock(&fs_info->defrag_inodes_lock);
Miao Xie26176e72012-11-26 09:26:20 +0000281
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;
David Sterba962a2982014-06-04 18:41:45 +0200302 key.type = BTRFS_ROOT_ITEM_KEY;
Miao Xie26176e72012-11-26 09:26:20 +0000303 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;
David Sterba962a2982014-06-04 18:41:45 +0200314 key.type = BTRFS_INODE_ITEM_KEY;
Miao Xie26176e72012-11-26 09:26:20 +0000315 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 */
Zhao Leiee22f0c2016-01-06 18:47:31 +0800413static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
Chris Masona1b32a52008-09-05 16:09:51 -0400414 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -0400415 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -0400416{
Xin Zhong914ee292010-12-09 09:30:14 +0000417 size_t copied = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -0500418 size_t total_copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400419 int pg = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300420 int offset = pos & (PAGE_SIZE - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400421
Josef Bacik11c65dc2010-05-23 11:07:21 -0400422 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400423 size_t count = min_t(size_t,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300424 PAGE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400425 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +0000426 /*
427 * Copy data from userspace to the current page
Xin Zhong914ee292010-12-09 09:30:14 +0000428 */
Xin Zhong914ee292010-12-09 09:30:14 +0000429 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400430
Chris Mason39279cc2007-06-12 06:35:45 -0400431 /* Flush processor's dcache for this page */
432 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -0500433
434 /*
435 * if we get a partial write, we can end up with
436 * partially up to date pages. These add
437 * a lot of complexity, so make sure they don't
438 * happen by forcing this copy to be retried.
439 *
440 * The rest of the btrfs_file_write code will fall
441 * back to page at a time copies after we return 0.
442 */
443 if (!PageUptodate(page) && copied < count)
444 copied = 0;
445
Josef Bacik11c65dc2010-05-23 11:07:21 -0400446 iov_iter_advance(i, copied);
447 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +0000448 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400449
Al Virob30ac0f2014-04-03 14:29:04 -0400450 /* Return to btrfs_file_write_iter to fault page */
Josef Bacik9f570b82011-01-25 12:42:37 -0500451 if (unlikely(copied == 0))
Xin Zhong914ee292010-12-09 09:30:14 +0000452 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400453
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300454 if (copied < PAGE_SIZE - offset) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400455 offset += copied;
456 } else {
457 pg++;
458 offset = 0;
459 }
Chris Mason39279cc2007-06-12 06:35:45 -0400460 }
Xin Zhong914ee292010-12-09 09:30:14 +0000461 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400462}
463
Chris Masond352ac62008-09-29 15:18:18 -0400464/*
465 * unlocks pages after btrfs_file_write is done with them
466 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000467static void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400468{
469 size_t i;
470 for (i = 0; i < num_pages; i++) {
Chris Masond352ac62008-09-29 15:18:18 -0400471 /* page checked is some magic around finding pages that
472 * have been modified without going through btrfs_set_page_dirty
Mel Gorman2457aec2014-06-04 16:10:31 -0700473 * clear it here. There should be no need to mark the pages
474 * accessed as prepare_pages should have marked them accessed
475 * in prepare_pages via find_or_create_page()
Chris Masond352ac62008-09-29 15:18:18 -0400476 */
Chris Mason4a096752008-07-21 10:29:44 -0400477 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400478 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300479 put_page(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400480 }
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{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400496 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -0400497 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400498 int i;
Chris Masondb945352007-10-15 16:15:53 -0400499 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400500 u64 start_pos;
501 u64 end_of_last_block;
502 u64 end_pos = pos + write_bytes;
503 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400504
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400505 start_pos = pos & ~((u64) fs_info->sectorsize - 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400506 num_bytes = round_up(write_bytes + pos - start_pos,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400507 fs_info->sectorsize);
Chris Mason39279cc2007-06-12 06:35:45 -0400508
Chris Masondb945352007-10-15 16:15:53 -0400509 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000510 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800511 cached, 0);
Josef Bacikd0215f32011-01-25 14:57:24 -0500512 if (err)
513 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400514
Chris Masonc8b97812008-10-29 14:49:59 -0400515 for (i = 0; i < num_pages; i++) {
516 struct page *p = pages[i];
517 SetPageUptodate(p);
518 ClearPageChecked(p);
519 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400520 }
Josef Bacik9f570b82011-01-25 12:42:37 -0500521
522 /*
523 * we've only changed i_size in ram, and we haven't updated
524 * the disk i_size. There is no need to log the inode
525 * at this time.
526 */
527 if (end_pos > isize)
Chris Masona52d9a82007-08-27 16:49:44 -0400528 i_size_write(inode, end_pos);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400529 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400530}
531
Chris Masond352ac62008-09-29 15:18:18 -0400532/*
533 * this drops all the extents in the cache that intersect the range
534 * [start, end]. Existing extents are split as required.
535 */
Josef Bacik7014cdb2012-08-30 20:06:49 -0400536void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
537 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400538{
539 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400540 struct extent_map *split = NULL;
541 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400542 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500543 u64 len = end - start + 1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400544 u64 gen;
Chris Mason3b951512008-04-17 11:29:12 -0400545 int ret;
546 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400547 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400548 int compressed = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400549 bool modified;
Chris Masona52d9a82007-08-27 16:49:44 -0400550
Chris Masone6dcd2d2008-07-17 12:53:50 -0400551 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400552 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500553 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400554 testend = 0;
555 }
Chris Masond3977122009-01-05 21:25:51 -0500556 while (1) {
Josef Bacik7014cdb2012-08-30 20:06:49 -0400557 int no_splits = 0;
558
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400559 modified = false;
Chris Mason3b951512008-04-17 11:29:12 -0400560 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200561 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400562 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200563 split2 = alloc_extent_map();
Josef Bacik7014cdb2012-08-30 20:06:49 -0400564 if (!split || !split2)
565 no_splits = 1;
Chris Mason3b951512008-04-17 11:29:12 -0400566
Chris Mason890871b2009-09-02 16:24:52 -0400567 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500568 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500569 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400570 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400571 break;
Chris Masond1310b22008-01-24 16:13:08 -0500572 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400573 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400574 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400575 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000576 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400577 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400578 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400579 break;
580 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000581 start = em->start + em->len;
582 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400583 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400584 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400585 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400586 continue;
587 }
Chris Masonc8b97812008-10-29 14:49:59 -0400588 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400589 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo3b277592013-03-15 08:46:39 -0600590 clear_bit(EXTENT_FLAG_LOGGING, &flags);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400591 modified = !list_empty(&em->list);
Josef Bacik7014cdb2012-08-30 20:06:49 -0400592 if (no_splits)
593 goto next;
Chris Mason3b951512008-04-17 11:29:12 -0400594
Josef Bacikee20a982013-07-11 10:34:59 -0400595 if (em->start < start) {
Chris Mason3b951512008-04-17 11:29:12 -0400596 split->start = em->start;
597 split->len = start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400598
Josef Bacikee20a982013-07-11 10:34:59 -0400599 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
600 split->orig_start = em->orig_start;
601 split->block_start = em->block_start;
602
603 if (compressed)
604 split->block_len = em->block_len;
605 else
606 split->block_len = split->len;
607 split->orig_block_len = max(split->block_len,
608 em->orig_block_len);
609 split->ram_bytes = em->ram_bytes;
610 } else {
611 split->orig_start = split->start;
612 split->block_len = 0;
613 split->block_start = em->block_start;
614 split->orig_block_len = 0;
615 split->ram_bytes = split->len;
616 }
617
Josef Bacik5dc562c2012-08-17 13:14:17 -0400618 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400619 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400620 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800621 split->compress_type = em->compress_type;
Filipe Manana176840b2014-02-25 14:15:13 +0000622 replace_extent_mapping(em_tree, em, split, modified);
Chris Mason3b951512008-04-17 11:29:12 -0400623 free_extent_map(split);
624 split = split2;
625 split2 = NULL;
626 }
Josef Bacikee20a982013-07-11 10:34:59 -0400627 if (testend && em->start + em->len > start + len) {
Chris Mason3b951512008-04-17 11:29:12 -0400628 u64 diff = start + len - em->start;
629
630 split->start = start + len;
631 split->len = em->start + em->len - (start + len);
632 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400633 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800634 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400635 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400636
Josef Bacikee20a982013-07-11 10:34:59 -0400637 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
638 split->orig_block_len = max(em->block_len,
639 em->orig_block_len);
640
641 split->ram_bytes = em->ram_bytes;
642 if (compressed) {
643 split->block_len = em->block_len;
644 split->block_start = em->block_start;
645 split->orig_start = em->orig_start;
646 } else {
647 split->block_len = split->len;
648 split->block_start = em->block_start
649 + diff;
650 split->orig_start = em->orig_start;
651 }
Chris Masonc8b97812008-10-29 14:49:59 -0400652 } else {
Josef Bacikee20a982013-07-11 10:34:59 -0400653 split->ram_bytes = split->len;
654 split->orig_start = split->start;
655 split->block_len = 0;
656 split->block_start = em->block_start;
657 split->orig_block_len = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400658 }
Chris Mason3b951512008-04-17 11:29:12 -0400659
Filipe Manana176840b2014-02-25 14:15:13 +0000660 if (extent_map_in_tree(em)) {
661 replace_extent_mapping(em_tree, em, split,
662 modified);
663 } else {
664 ret = add_extent_mapping(em_tree, split,
665 modified);
666 ASSERT(ret == 0); /* Logic error */
667 }
Chris Mason3b951512008-04-17 11:29:12 -0400668 free_extent_map(split);
669 split = NULL;
670 }
Josef Bacik7014cdb2012-08-30 20:06:49 -0400671next:
Filipe Manana176840b2014-02-25 14:15:13 +0000672 if (extent_map_in_tree(em))
673 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400674 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500675
Chris Masona52d9a82007-08-27 16:49:44 -0400676 /* once for us */
677 free_extent_map(em);
678 /* once for the tree*/
679 free_extent_map(em);
680 }
Chris Mason3b951512008-04-17 11:29:12 -0400681 if (split)
682 free_extent_map(split);
683 if (split2)
684 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400685}
686
Chris Mason39279cc2007-06-12 06:35:45 -0400687/*
688 * this is very complex, but the basic idea is to drop all extents
689 * in the range start - end. hint_block is filled in with a block number
690 * that would be a good hint to the block allocator for this file.
691 *
692 * If an extent intersects the range but is not entirely inside the range
693 * it is either truncated or split. Anything entirely inside the range
694 * is deleted from the tree.
695 */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400696int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
697 struct btrfs_root *root, struct inode *inode,
698 struct btrfs_path *path, u64 start, u64 end,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000699 u64 *drop_end, int drop_cache,
700 int replace_extent,
701 u32 extent_item_size,
702 int *key_inserted)
Chris Mason39279cc2007-06-12 06:35:45 -0400703{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400704 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason00f5c792007-11-30 10:09:33 -0500705 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000706 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500707 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000708 struct btrfs_key new_key;
Li Zefan33345d012011-04-20 10:31:50 +0800709 u64 ino = btrfs_ino(inode);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000710 u64 search_start = start;
711 u64 disk_bytenr = 0;
712 u64 num_bytes = 0;
713 u64 extent_offset = 0;
714 u64 extent_end = 0;
Josef Bacik62fe51c2016-11-16 09:13:39 -0500715 u64 last_end = start;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000716 int del_nr = 0;
717 int del_slot = 0;
718 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400719 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500720 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400721 int modify_tree = -1;
Miao Xie27cdeb72014-04-02 19:51:05 +0800722 int update_refs;
Josef Bacikc3308f82012-09-14 14:51:22 -0400723 int found = 0;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000724 int leafs_visited = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400725
Chris Masona1ed8352009-09-11 12:27:37 -0400726 if (drop_cache)
727 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400728
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000729 if (start >= BTRFS_I(inode)->disk_i_size && !replace_extent)
Chris Masondc7fdde2012-04-27 14:31:29 -0400730 modify_tree = 0;
731
Miao Xie27cdeb72014-04-02 19:51:05 +0800732 update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400733 root == fs_info->tree_root);
Chris Masond3977122009-01-05 21:25:51 -0500734 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400735 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800736 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400737 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400738 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000739 break;
740 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
741 leaf = path->nodes[0];
742 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800743 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000744 key.type == BTRFS_EXTENT_DATA_KEY)
745 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400746 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400747 ret = 0;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000748 leafs_visited++;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000749next_slot:
750 leaf = path->nodes[0];
751 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
752 BUG_ON(del_nr > 0);
753 ret = btrfs_next_leaf(root, path);
754 if (ret < 0)
755 break;
756 if (ret > 0) {
757 ret = 0;
758 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400759 }
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000760 leafs_visited++;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000761 leaf = path->nodes[0];
762 recow = 1;
763 }
764
765 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Filipe Mananaaeafbf82015-11-06 13:33:33 +0000766
767 if (key.objectid > ino)
768 break;
769 if (WARN_ON_ONCE(key.objectid < ino) ||
770 key.type < BTRFS_EXTENT_DATA_KEY) {
771 ASSERT(del_nr == 0);
772 path->slots[0]++;
773 goto next_slot;
774 }
775 if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000776 break;
777
778 fi = btrfs_item_ptr(leaf, path->slots[0],
779 struct btrfs_file_extent_item);
780 extent_type = btrfs_file_extent_type(leaf, fi);
781
782 if (extent_type == BTRFS_FILE_EXTENT_REG ||
783 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
784 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
785 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
786 extent_offset = btrfs_file_extent_offset(leaf, fi);
787 extent_end = key.offset +
788 btrfs_file_extent_num_bytes(leaf, fi);
789 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
790 extent_end = key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -0800791 btrfs_file_extent_inline_len(leaf,
792 path->slots[0], fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400793 } else {
Filipe Mananaaeafbf82015-11-06 13:33:33 +0000794 /* can't happen */
795 BUG();
Chris Mason39279cc2007-06-12 06:35:45 -0400796 }
797
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100798 /*
799 * Don't skip extent items representing 0 byte lengths. They
800 * used to be created (bug) if while punching holes we hit
801 * -ENOSPC condition. So if we find one here, just ensure we
802 * delete it, otherwise we would insert a new file extent item
803 * with the same key (offset) as that 0 bytes length file
804 * extent item in the call to setup_items_for_insert() later
805 * in this function.
806 */
Josef Bacik62fe51c2016-11-16 09:13:39 -0500807 if (extent_end == key.offset && extent_end >= search_start) {
808 last_end = extent_end;
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100809 goto delete_extent_item;
Josef Bacik62fe51c2016-11-16 09:13:39 -0500810 }
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100811
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000812 if (extent_end <= search_start) {
813 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400814 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400815 }
816
Josef Bacikc3308f82012-09-14 14:51:22 -0400817 found = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000818 search_start = max(key.offset, start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400819 if (recow || !modify_tree) {
820 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200821 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000822 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400823 }
Chris Mason771ed682008-11-06 22:02:51 -0500824
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000825 /*
826 * | - range to drop - |
827 * | -------- extent -------- |
828 */
829 if (start > key.offset && end < extent_end) {
830 BUG_ON(del_nr > 0);
Liu Bo00fdf132014-03-10 18:56:07 +0800831 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200832 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800833 break;
834 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000835
836 memcpy(&new_key, &key, sizeof(new_key));
837 new_key.offset = start;
838 ret = btrfs_duplicate_item(trans, root, path,
839 &new_key);
840 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200841 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000842 continue;
843 }
844 if (ret < 0)
845 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400846
Chris Mason5f39d392007-10-15 16:14:19 -0400847 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000848 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
849 struct btrfs_file_extent_item);
850 btrfs_set_file_extent_num_bytes(leaf, fi,
851 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400852
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000853 fi = btrfs_item_ptr(leaf, path->slots[0],
854 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400855
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000856 extent_offset += start - key.offset;
857 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
858 btrfs_set_file_extent_num_bytes(leaf, fi,
859 extent_end - start);
860 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400861
Josef Bacik5dc562c2012-08-17 13:14:17 -0400862 if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000863 ret = btrfs_inc_extent_ref(trans, root,
864 disk_bytenr, num_bytes, 0,
865 root->root_key.objectid,
866 new_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100867 start - extent_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100868 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400869 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000870 key.offset = start;
871 }
872 /*
Josef Bacik62fe51c2016-11-16 09:13:39 -0500873 * From here on out we will have actually dropped something, so
874 * last_end can be updated.
875 */
876 last_end = extent_end;
877
878 /*
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000879 * | ---- range to drop ----- |
880 * | -------- extent -------- |
881 */
882 if (start <= key.offset && end < extent_end) {
Liu Bo00fdf132014-03-10 18:56:07 +0800883 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200884 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800885 break;
886 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000887
888 memcpy(&new_key, &key, sizeof(new_key));
889 new_key.offset = end;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400890 btrfs_set_item_key_safe(fs_info, path, &new_key);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000891
892 extent_offset += end - key.offset;
893 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
894 btrfs_set_file_extent_num_bytes(leaf, fi,
895 extent_end - end);
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, end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000899 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400900 }
901
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000902 search_start = extent_end;
903 /*
904 * | ---- range to drop ----- |
905 * | -------- extent -------- |
906 */
907 if (start > key.offset && end >= extent_end) {
908 BUG_ON(del_nr > 0);
Liu Bo00fdf132014-03-10 18:56:07 +0800909 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200910 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800911 break;
912 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000913
914 btrfs_set_file_extent_num_bytes(leaf, fi,
915 start - key.offset);
916 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400917 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000918 inode_sub_bytes(inode, extent_end - start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000919 if (end == extent_end)
920 break;
921
922 path->slots[0]++;
923 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400924 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000925
926 /*
927 * | ---- range to drop ----- |
928 * | ------ extent ------ |
929 */
930 if (start <= key.offset && end >= extent_end) {
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100931delete_extent_item:
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000932 if (del_nr == 0) {
933 del_slot = path->slots[0];
934 del_nr = 1;
935 } else {
936 BUG_ON(del_slot + del_nr != path->slots[0]);
937 del_nr++;
938 }
939
Josef Bacik5dc562c2012-08-17 13:14:17 -0400940 if (update_refs &&
941 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000942 inode_sub_bytes(inode,
943 extent_end - key.offset);
944 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400945 fs_info->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400946 } else if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000947 ret = btrfs_free_extent(trans, root,
948 disk_bytenr, num_bytes, 0,
949 root->root_key.objectid,
950 key.objectid, key.offset -
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100951 extent_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100952 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000953 inode_sub_bytes(inode,
954 extent_end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000955 }
956
957 if (end == extent_end)
958 break;
959
960 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
961 path->slots[0]++;
962 goto next_slot;
963 }
964
965 ret = btrfs_del_items(trans, root, path, del_slot,
966 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100967 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400968 btrfs_abort_transaction(trans, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400969 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000971
972 del_nr = 0;
973 del_slot = 0;
974
David Sterbab3b4aa72011-04-21 01:20:15 +0200975 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000976 continue;
977 }
978
979 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400980 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000981
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100982 if (!ret && del_nr > 0) {
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000983 /*
984 * Set path->slots[0] to first slot, so that after the delete
985 * if items are move off from our leaf to its immediate left or
986 * right neighbor leafs, we end up with a correct and adjusted
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000987 * path->slots[0] for our insertion (if replace_extent != 0).
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000988 */
989 path->slots[0] = del_slot;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000990 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400992 btrfs_abort_transaction(trans, ret);
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000993 }
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000994
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000995 leaf = path->nodes[0];
996 /*
997 * If btrfs_del_items() was called, it might have deleted a leaf, in
998 * which case it unlocked our path, so check path->locks[0] matches a
999 * write lock.
1000 */
1001 if (!ret && replace_extent && leafs_visited == 1 &&
1002 (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
1003 path->locks[0] == BTRFS_WRITE_LOCK) &&
1004 btrfs_leaf_free_space(root, leaf) >=
1005 sizeof(struct btrfs_item) + extent_item_size) {
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001006
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +00001007 key.objectid = ino;
1008 key.type = BTRFS_EXTENT_DATA_KEY;
1009 key.offset = start;
1010 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
1011 struct btrfs_key slot_key;
1012
1013 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
1014 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
1015 path->slots[0]++;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001016 }
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +00001017 setup_items_for_insert(root, path, &key,
1018 &extent_item_size,
1019 extent_item_size,
1020 sizeof(struct btrfs_item) +
1021 extent_item_size, 1);
1022 *key_inserted = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001023 }
1024
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001025 if (!replace_extent || !(*key_inserted))
1026 btrfs_release_path(path);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001027 if (drop_end)
Josef Bacik62fe51c2016-11-16 09:13:39 -05001028 *drop_end = found ? min(end, last_end) : end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001029 return ret;
1030}
1031
1032int btrfs_drop_extents(struct btrfs_trans_handle *trans,
1033 struct btrfs_root *root, struct inode *inode, u64 start,
Josef Bacik26714852012-08-29 12:24:27 -04001034 u64 end, int drop_cache)
Josef Bacik5dc562c2012-08-17 13:14:17 -04001035{
1036 struct btrfs_path *path;
1037 int ret;
1038
1039 path = btrfs_alloc_path();
1040 if (!path)
1041 return -ENOMEM;
Josef Bacik2aaa6652012-08-29 14:27:18 -04001042 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001043 drop_cache, 0, 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04001044 btrfs_free_path(path);
1045 return ret;
1046}
1047
Yan Zhengd899e052008-10-30 14:25:28 -04001048static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001049 u64 objectid, u64 bytenr, u64 orig_offset,
1050 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -04001051{
1052 struct btrfs_file_extent_item *fi;
1053 struct btrfs_key key;
1054 u64 extent_end;
1055
1056 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1057 return 0;
1058
1059 btrfs_item_key_to_cpu(leaf, &key, slot);
1060 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
1061 return 0;
1062
1063 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1064 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
1065 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001066 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -04001067 btrfs_file_extent_compression(leaf, fi) ||
1068 btrfs_file_extent_encryption(leaf, fi) ||
1069 btrfs_file_extent_other_encoding(leaf, fi))
1070 return 0;
1071
1072 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1073 if ((*start && *start != key.offset) || (*end && *end != extent_end))
1074 return 0;
1075
1076 *start = key.offset;
1077 *end = extent_end;
1078 return 1;
1079}
1080
1081/*
1082 * Mark extent in the range start - end as written.
1083 *
1084 * This changes extent type from 'pre-allocated' to 'regular'. If only
1085 * part of extent is marked as written, the extent will be split into
1086 * two or three.
1087 */
1088int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -04001089 struct inode *inode, u64 start, u64 end)
1090{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001092 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -04001093 struct extent_buffer *leaf;
1094 struct btrfs_path *path;
1095 struct btrfs_file_extent_item *fi;
1096 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001097 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -04001098 u64 bytenr;
1099 u64 num_bytes;
1100 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001101 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001102 u64 other_start;
1103 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001104 u64 split;
1105 int del_nr = 0;
1106 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001107 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -04001108 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08001109 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001110
Yan Zhengd899e052008-10-30 14:25:28 -04001111 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001112 if (!path)
1113 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001114again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001115 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001116 split = start;
Li Zefan33345d012011-04-20 10:31:50 +08001117 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -04001118 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001119 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -04001120
1121 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -04001122 if (ret < 0)
1123 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001124 if (ret > 0 && path->slots[0] > 0)
1125 path->slots[0]--;
1126
1127 leaf = path->nodes[0];
1128 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001129 if (key.objectid != ino ||
1130 key.type != BTRFS_EXTENT_DATA_KEY) {
1131 ret = -EINVAL;
1132 btrfs_abort_transaction(trans, ret);
1133 goto out;
1134 }
Yan Zhengd899e052008-10-30 14:25:28 -04001135 fi = btrfs_item_ptr(leaf, path->slots[0],
1136 struct btrfs_file_extent_item);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001137 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
1138 ret = -EINVAL;
1139 btrfs_abort_transaction(trans, ret);
1140 goto out;
1141 }
Yan Zhengd899e052008-10-30 14:25:28 -04001142 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001143 if (key.offset > start || extent_end < end) {
1144 ret = -EINVAL;
1145 btrfs_abort_transaction(trans, ret);
1146 goto out;
1147 }
Yan Zhengd899e052008-10-30 14:25:28 -04001148
1149 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1150 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001151 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001152 memcpy(&new_key, &key, sizeof(new_key));
1153
1154 if (start == key.offset && end < extent_end) {
1155 other_start = 0;
1156 other_end = start;
1157 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001158 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001159 &other_start, &other_end)) {
1160 new_key.offset = end;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 btrfs_set_item_key_safe(fs_info, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001162 fi = btrfs_item_ptr(leaf, path->slots[0],
1163 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001164 btrfs_set_file_extent_generation(leaf, fi,
1165 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001166 btrfs_set_file_extent_num_bytes(leaf, fi,
1167 extent_end - end);
1168 btrfs_set_file_extent_offset(leaf, fi,
1169 end - orig_offset);
1170 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1171 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001172 btrfs_set_file_extent_generation(leaf, fi,
1173 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001174 btrfs_set_file_extent_num_bytes(leaf, fi,
1175 end - other_start);
1176 btrfs_mark_buffer_dirty(leaf);
1177 goto out;
1178 }
1179 }
1180
1181 if (start > key.offset && end == extent_end) {
1182 other_start = end;
1183 other_end = 0;
1184 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001185 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001186 &other_start, &other_end)) {
1187 fi = btrfs_item_ptr(leaf, path->slots[0],
1188 struct btrfs_file_extent_item);
1189 btrfs_set_file_extent_num_bytes(leaf, fi,
1190 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -04001191 btrfs_set_file_extent_generation(leaf, fi,
1192 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001193 path->slots[0]++;
1194 new_key.offset = start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001195 btrfs_set_item_key_safe(fs_info, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001196
1197 fi = btrfs_item_ptr(leaf, path->slots[0],
1198 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001199 btrfs_set_file_extent_generation(leaf, fi,
1200 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001201 btrfs_set_file_extent_num_bytes(leaf, fi,
1202 other_end - start);
1203 btrfs_set_file_extent_offset(leaf, fi,
1204 start - orig_offset);
1205 btrfs_mark_buffer_dirty(leaf);
1206 goto out;
1207 }
1208 }
Yan Zhengd899e052008-10-30 14:25:28 -04001209
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001210 while (start > key.offset || end < extent_end) {
1211 if (key.offset == start)
1212 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001213
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001214 new_key.offset = split;
1215 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1216 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001217 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001218 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001219 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001220 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001221 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 goto out;
1223 }
Yan Zhengd899e052008-10-30 14:25:28 -04001224
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001225 leaf = path->nodes[0];
1226 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001227 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001228 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001229 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001230 split - key.offset);
1231
1232 fi = btrfs_item_ptr(leaf, path->slots[0],
1233 struct btrfs_file_extent_item);
1234
Josef Bacik224ecce2012-08-16 16:32:06 -04001235 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001236 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1237 btrfs_set_file_extent_num_bytes(leaf, fi,
1238 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001239 btrfs_mark_buffer_dirty(leaf);
1240
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001241 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1242 root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001243 ino, orig_offset);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001244 if (ret) {
1245 btrfs_abort_transaction(trans, ret);
1246 goto out;
1247 }
Yan Zhengd899e052008-10-30 14:25:28 -04001248
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001249 if (split == start) {
1250 key.offset = start;
1251 } else {
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001252 if (start != key.offset) {
1253 ret = -EINVAL;
1254 btrfs_abort_transaction(trans, ret);
1255 goto out;
1256 }
Yan Zhengd899e052008-10-30 14:25:28 -04001257 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001258 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001259 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001260 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001261 }
1262
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001263 other_start = end;
1264 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001265 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001266 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001267 &other_start, &other_end)) {
1268 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001269 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001270 goto again;
1271 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001272 extent_end = other_end;
1273 del_slot = path->slots[0] + 1;
1274 del_nr++;
1275 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1276 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001277 ino, orig_offset);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001278 if (ret) {
1279 btrfs_abort_transaction(trans, ret);
1280 goto out;
1281 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001282 }
1283 other_start = 0;
1284 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001285 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001286 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001287 &other_start, &other_end)) {
1288 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001289 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001290 goto again;
1291 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001292 key.offset = other_start;
1293 del_slot = path->slots[0];
1294 del_nr++;
1295 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1296 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01001297 ino, orig_offset);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001298 if (ret) {
1299 btrfs_abort_transaction(trans, ret);
1300 goto out;
1301 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001302 }
1303 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001304 fi = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001306 btrfs_set_file_extent_type(leaf, fi,
1307 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001308 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001309 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001310 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001311 fi = btrfs_item_ptr(leaf, del_slot - 1,
1312 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001313 btrfs_set_file_extent_type(leaf, fi,
1314 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001315 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001316 btrfs_set_file_extent_num_bytes(leaf, fi,
1317 extent_end - key.offset);
1318 btrfs_mark_buffer_dirty(leaf);
1319
1320 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001322 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto out;
1324 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001325 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001326out:
Yan Zhengd899e052008-10-30 14:25:28 -04001327 btrfs_free_path(path);
1328 return 0;
1329}
1330
Chris Mason39279cc2007-06-12 06:35:45 -04001331/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001332 * on error we return an unlocked page and the error value
1333 * on success we return a locked page and 0
1334 */
Chris Masonbb1591b42015-12-14 15:40:44 -08001335static int prepare_uptodate_page(struct inode *inode,
1336 struct page *page, u64 pos,
Josef Bacikb63164292011-09-30 15:23:54 -04001337 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001338{
1339 int ret = 0;
1340
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001341 if (((pos & (PAGE_SIZE - 1)) || force_uptodate) &&
Josef Bacikb63164292011-09-30 15:23:54 -04001342 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001343 ret = btrfs_readpage(NULL, page);
1344 if (ret)
1345 return ret;
1346 lock_page(page);
1347 if (!PageUptodate(page)) {
1348 unlock_page(page);
1349 return -EIO;
1350 }
Chris Masonbb1591b42015-12-14 15:40:44 -08001351 if (page->mapping != inode->i_mapping) {
1352 unlock_page(page);
1353 return -EAGAIN;
1354 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001355 }
1356 return 0;
1357}
1358
1359/*
Miao Xie376cc682013-12-10 19:25:04 +08001360 * this just gets pages into the page cache and locks them down.
Chris Mason39279cc2007-06-12 06:35:45 -04001361 */
Miao Xieb37392e2013-12-10 19:25:03 +08001362static noinline int prepare_pages(struct inode *inode, struct page **pages,
1363 size_t num_pages, loff_t pos,
1364 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001365{
1366 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001367 unsigned long index = pos >> PAGE_SHIFT;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001368 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Filipe David Borba Mananafc28b622013-12-13 19:39:34 +00001369 int err = 0;
Miao Xie376cc682013-12-10 19:25:04 +08001370 int faili;
Chris Mason8c2383c2007-06-18 09:57:58 -04001371
Chris Mason39279cc2007-06-12 06:35:45 -04001372 for (i = 0; i < num_pages; i++) {
Chris Masonbb1591b42015-12-14 15:40:44 -08001373again:
Josef Bacika94733d2011-07-11 10:47:06 -04001374 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001375 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001376 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001377 faili = i - 1;
1378 err = -ENOMEM;
1379 goto fail;
1380 }
1381
1382 if (i == 0)
Chris Masonbb1591b42015-12-14 15:40:44 -08001383 err = prepare_uptodate_page(inode, pages[i], pos,
Josef Bacikb63164292011-09-30 15:23:54 -04001384 force_uptodate);
Chris Masonbb1591b42015-12-14 15:40:44 -08001385 if (!err && i == num_pages - 1)
1386 err = prepare_uptodate_page(inode, pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001387 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001388 if (err) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001389 put_page(pages[i]);
Chris Masonbb1591b42015-12-14 15:40:44 -08001390 if (err == -EAGAIN) {
1391 err = 0;
1392 goto again;
1393 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001394 faili = i - 1;
1395 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001396 }
Chris Masonccd467d2007-06-28 15:57:36 -04001397 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001398 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001399
Chris Mason39279cc2007-06-12 06:35:45 -04001400 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001401fail:
1402 while (faili >= 0) {
1403 unlock_page(pages[faili]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001404 put_page(pages[faili]);
Chris Masonb1bf8622011-02-28 09:52:08 -05001405 faili--;
1406 }
1407 return err;
1408
Chris Mason39279cc2007-06-12 06:35:45 -04001409}
1410
Miao Xie376cc682013-12-10 19:25:04 +08001411/*
1412 * This function locks the extent and properly waits for data=ordered extents
1413 * to finish before allowing the pages to be modified if need.
1414 *
1415 * The return value:
1416 * 1 - the extent is locked
1417 * 0 - the extent is not locked, and everything is OK
1418 * -EAGAIN - need re-prepare the pages
1419 * the other < 0 number - Something wrong happens
1420 */
1421static noinline int
1422lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
1423 size_t num_pages, loff_t pos,
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301424 size_t write_bytes,
Miao Xie376cc682013-12-10 19:25:04 +08001425 u64 *lockstart, u64 *lockend,
1426 struct extent_state **cached_state)
1427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie376cc682013-12-10 19:25:04 +08001429 u64 start_pos;
1430 u64 last_pos;
1431 int i;
1432 int ret = 0;
1433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001434 start_pos = round_down(pos, fs_info->sectorsize);
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301435 last_pos = start_pos
Jeff Mahoneyda170662016-06-15 09:22:56 -04001436 + round_up(pos + write_bytes - start_pos,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001437 fs_info->sectorsize) - 1;
Miao Xie376cc682013-12-10 19:25:04 +08001438
1439 if (start_pos < inode->i_size) {
1440 struct btrfs_ordered_extent *ordered;
1441 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001442 start_pos, last_pos, cached_state);
Miao Xieb88935b2014-03-06 13:54:58 +08001443 ordered = btrfs_lookup_ordered_range(inode, start_pos,
1444 last_pos - start_pos + 1);
Miao Xie376cc682013-12-10 19:25:04 +08001445 if (ordered &&
1446 ordered->file_offset + ordered->len > start_pos &&
1447 ordered->file_offset <= last_pos) {
Miao Xie376cc682013-12-10 19:25:04 +08001448 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1449 start_pos, last_pos,
1450 cached_state, GFP_NOFS);
1451 for (i = 0; i < num_pages; i++) {
1452 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 put_page(pages[i]);
Miao Xie376cc682013-12-10 19:25:04 +08001454 }
Miao Xieb88935b2014-03-06 13:54:58 +08001455 btrfs_start_ordered_extent(inode, ordered, 1);
1456 btrfs_put_ordered_extent(ordered);
1457 return -EAGAIN;
Miao Xie376cc682013-12-10 19:25:04 +08001458 }
1459 if (ordered)
1460 btrfs_put_ordered_extent(ordered);
1461
1462 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
1463 last_pos, EXTENT_DIRTY | EXTENT_DELALLOC |
1464 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1465 0, 0, cached_state, GFP_NOFS);
1466 *lockstart = start_pos;
1467 *lockend = last_pos;
1468 ret = 1;
1469 }
1470
1471 for (i = 0; i < num_pages; i++) {
1472 if (clear_page_dirty_for_io(pages[i]))
1473 account_page_redirty(pages[i]);
1474 set_page_extent_mapped(pages[i]);
1475 WARN_ON(!PageLocked(pages[i]));
1476 }
1477
1478 return ret;
1479}
1480
Josef Bacik7ee9e442013-06-21 16:37:03 -04001481static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1482 size_t *write_bytes)
1483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001485 struct btrfs_root *root = BTRFS_I(inode)->root;
1486 struct btrfs_ordered_extent *ordered;
1487 u64 lockstart, lockend;
1488 u64 num_bytes;
1489 int ret;
1490
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001491 ret = btrfs_start_write_no_snapshoting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08001492 if (!ret)
1493 return -ENOSPC;
1494
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001495 lockstart = round_down(pos, fs_info->sectorsize);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001496 lockend = round_up(pos + *write_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 fs_info->sectorsize) - 1;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001498
1499 while (1) {
1500 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1501 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1502 lockend - lockstart + 1);
1503 if (!ordered) {
1504 break;
1505 }
1506 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1507 btrfs_start_ordered_extent(inode, ordered, 1);
1508 btrfs_put_ordered_extent(ordered);
1509 }
1510
Josef Bacik7ee9e442013-06-21 16:37:03 -04001511 num_bytes = lockend - lockstart + 1;
Josef Bacik00361582013-08-14 14:02:47 -04001512 ret = can_nocow_extent(inode, lockstart, &num_bytes, NULL, NULL, NULL);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001513 if (ret <= 0) {
1514 ret = 0;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001515 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001516 } else {
Miao Xiec9339562014-02-27 13:58:04 +08001517 *write_bytes = min_t(size_t, *write_bytes ,
1518 num_bytes - pos + lockstart);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001519 }
1520
1521 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
1522
1523 return ret;
1524}
1525
Josef Bacikd0215f32011-01-25 14:57:24 -05001526static noinline ssize_t __btrfs_buffered_write(struct file *file,
1527 struct iov_iter *i,
1528 loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001529{
Al Viro496ad9a2013-01-23 17:07:38 -05001530 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001532 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001533 struct page **pages = NULL;
Miao Xie376cc682013-12-10 19:25:04 +08001534 struct extent_state *cached_state = NULL;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001535 u64 release_bytes = 0;
Miao Xie376cc682013-12-10 19:25:04 +08001536 u64 lockstart;
1537 u64 lockend;
Josef Bacikd0215f32011-01-25 14:57:24 -05001538 size_t num_written = 0;
1539 int nrptrs;
Tsutomu Itohc9149232011-03-30 00:57:23 +00001540 int ret = 0;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001541 bool only_release_metadata = false;
Josef Bacikb63164292011-09-30 15:23:54 -04001542 bool force_page_uptodate = false;
Miao Xie376cc682013-12-10 19:25:04 +08001543 bool need_unlock;
Chris Masoncb843a62008-10-03 12:30:02 -04001544
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001545 nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE),
1546 PAGE_SIZE / (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001547 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1548 nrptrs = max(nrptrs, 8);
David Sterba31e818f2015-02-20 18:00:26 +01001549 pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001550 if (!pages)
1551 return -ENOMEM;
Chris Masonab93dbe2009-10-01 12:29:10 -04001552
Josef Bacikd0215f32011-01-25 14:57:24 -05001553 while (iov_iter_count(i) > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001554 size_t offset = pos & (PAGE_SIZE - 1);
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301555 size_t sector_offset;
Josef Bacikd0215f32011-01-25 14:57:24 -05001556 size_t write_bytes = min(iov_iter_count(i),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001557 nrptrs * (size_t)PAGE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001558 offset);
David Sterbaed6078f2014-06-05 01:59:57 +02001559 size_t num_pages = DIV_ROUND_UP(write_bytes + offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 PAGE_SIZE);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001561 size_t reserve_bytes;
Josef Bacikd0215f32011-01-25 14:57:24 -05001562 size_t dirty_pages;
1563 size_t copied;
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301564 size_t dirty_sectors;
1565 size_t num_sectors;
Chris Mason39279cc2007-06-12 06:35:45 -04001566
Chris Mason8c2383c2007-06-18 09:57:58 -04001567 WARN_ON(num_pages > nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001568
Xin Zhong914ee292010-12-09 09:30:14 +00001569 /*
1570 * Fault pages before locking them in prepare_pages
1571 * to avoid recursive lock
1572 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001573 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001574 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001575 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001576 }
1577
Jeff Mahoneyda170662016-06-15 09:22:56 -04001578 sector_offset = pos & (fs_info->sectorsize - 1);
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301579 reserve_bytes = round_up(write_bytes + sector_offset,
Jeff Mahoneyda170662016-06-15 09:22:56 -04001580 fs_info->sectorsize);
Qu Wenruod9d8b2a2015-09-08 17:22:43 +08001581
Josef Bacikc6887cd2016-03-25 13:26:00 -04001582 ret = btrfs_check_data_free_space(inode, pos, write_bytes);
1583 if (ret < 0) {
1584 if ((BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
1585 BTRFS_INODE_PREALLOC)) &&
1586 check_can_nocow(inode, pos, &write_bytes) > 0) {
1587 /*
1588 * For nodata cow case, no need to reserve
1589 * data space.
1590 */
1591 only_release_metadata = true;
1592 /*
1593 * our prealloc extent may be smaller than
1594 * write_bytes, so scale down.
1595 */
1596 num_pages = DIV_ROUND_UP(write_bytes + offset,
1597 PAGE_SIZE);
1598 reserve_bytes = round_up(write_bytes +
1599 sector_offset,
Jeff Mahoneyda170662016-06-15 09:22:56 -04001600 fs_info->sectorsize);
Josef Bacikc6887cd2016-03-25 13:26:00 -04001601 } else {
1602 break;
1603 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04001604 }
Zhao Lei4da2e262016-01-06 18:24:43 +08001605
Josef Bacik7ee9e442013-06-21 16:37:03 -04001606 ret = btrfs_delalloc_reserve_metadata(inode, reserve_bytes);
1607 if (ret) {
1608 if (!only_release_metadata)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08001609 btrfs_free_reserved_data_space(inode, pos,
1610 write_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001611 else
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001612 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001613 break;
1614 }
1615
1616 release_bytes = reserve_bytes;
Miao Xie376cc682013-12-10 19:25:04 +08001617 need_unlock = false;
1618again:
Josef Bacik4a640012011-01-25 15:10:08 -05001619 /*
1620 * This is going to setup the pages array with the number of
1621 * pages we want, so we don't really need to worry about the
1622 * contents of pages from loop to loop
1623 */
Miao Xieb37392e2013-12-10 19:25:03 +08001624 ret = prepare_pages(inode, pages, num_pages,
1625 pos, write_bytes,
Josef Bacikb63164292011-09-30 15:23:54 -04001626 force_page_uptodate);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001627 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001628 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001629
Miao Xie376cc682013-12-10 19:25:04 +08001630 ret = lock_and_cleanup_extent_if_need(inode, pages, num_pages,
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301631 pos, write_bytes, &lockstart,
1632 &lockend, &cached_state);
Miao Xie376cc682013-12-10 19:25:04 +08001633 if (ret < 0) {
1634 if (ret == -EAGAIN)
1635 goto again;
1636 break;
1637 } else if (ret > 0) {
1638 need_unlock = true;
1639 ret = 0;
1640 }
1641
Zhao Leiee22f0c2016-01-06 18:47:31 +08001642 copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001643
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001644 num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
Chris Mason56244ef2016-05-16 09:21:01 -07001645 dirty_sectors = round_up(copied + sector_offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001646 fs_info->sectorsize);
1647 dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
Chris Mason56244ef2016-05-16 09:21:01 -07001648
Chris Masonb1bf8622011-02-28 09:52:08 -05001649 /*
1650 * if we have trouble faulting in the pages, fall
1651 * back to one page at a time
1652 */
1653 if (copied < write_bytes)
1654 nrptrs = 1;
1655
Josef Bacikb63164292011-09-30 15:23:54 -04001656 if (copied == 0) {
1657 force_page_uptodate = true;
Chris Mason56244ef2016-05-16 09:21:01 -07001658 dirty_sectors = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001659 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001660 } else {
1661 force_page_uptodate = false;
David Sterbaed6078f2014-06-05 01:59:57 +02001662 dirty_pages = DIV_ROUND_UP(copied + offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001663 PAGE_SIZE);
Josef Bacikb63164292011-09-30 15:23:54 -04001664 }
Xin Zhong914ee292010-12-09 09:30:14 +00001665
Josef Bacikd0215f32011-01-25 14:57:24 -05001666 /*
1667 * If we had a short copy we need to release the excess delaloc
1668 * bytes we reserved. We need to increment outstanding_extents
Chris Mason56244ef2016-05-16 09:21:01 -07001669 * because btrfs_delalloc_release_space and
1670 * btrfs_delalloc_release_metadata will decrement it, but
Josef Bacikd0215f32011-01-25 14:57:24 -05001671 * we still have an outstanding extent for the chunk we actually
1672 * managed to copy.
1673 */
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301674 if (num_sectors > dirty_sectors) {
Chris Mason8b8b08c2016-07-19 05:52:36 -07001675 /* release everything except the sectors we dirtied */
1676 release_bytes -= dirty_sectors <<
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001677 fs_info->sb->s_blocksize_bits;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001678 if (copied > 0) {
1679 spin_lock(&BTRFS_I(inode)->lock);
1680 BTRFS_I(inode)->outstanding_extents++;
1681 spin_unlock(&BTRFS_I(inode)->lock);
1682 }
Qu Wenruo485290a2015-10-29 17:28:46 +08001683 if (only_release_metadata) {
Josef Bacik7ee9e442013-06-21 16:37:03 -04001684 btrfs_delalloc_release_metadata(inode,
1685 release_bytes);
Qu Wenruo485290a2015-10-29 17:28:46 +08001686 } else {
1687 u64 __pos;
1688
Jeff Mahoneyda170662016-06-15 09:22:56 -04001689 __pos = round_down(pos,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001690 fs_info->sectorsize) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001691 (dirty_pages << PAGE_SHIFT);
Qu Wenruo485290a2015-10-29 17:28:46 +08001692 btrfs_delalloc_release_space(inode, __pos,
Josef Bacik7ee9e442013-06-21 16:37:03 -04001693 release_bytes);
Qu Wenruo485290a2015-10-29 17:28:46 +08001694 }
Xin Zhong914ee292010-12-09 09:30:14 +00001695 }
1696
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301697 release_bytes = round_up(copied + sector_offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001698 fs_info->sectorsize);
Miao Xie376cc682013-12-10 19:25:04 +08001699
1700 if (copied > 0)
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001701 ret = btrfs_dirty_pages(root, inode, pages,
1702 dirty_pages, pos, copied,
1703 NULL);
Miao Xie376cc682013-12-10 19:25:04 +08001704 if (need_unlock)
1705 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1706 lockstart, lockend, &cached_state,
1707 GFP_NOFS);
Miao Xief1de9682014-01-09 10:06:10 +08001708 if (ret) {
1709 btrfs_drop_pages(pages, num_pages);
Miao Xie376cc682013-12-10 19:25:04 +08001710 break;
Miao Xief1de9682014-01-09 10:06:10 +08001711 }
Chris Mason39279cc2007-06-12 06:35:45 -04001712
Josef Bacik7ee9e442013-06-21 16:37:03 -04001713 release_bytes = 0;
Miao Xie8257b2d2014-03-06 13:38:19 +08001714 if (only_release_metadata)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001715 btrfs_end_write_no_snapshoting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08001716
Josef Bacik7ee9e442013-06-21 16:37:03 -04001717 if (only_release_metadata && copied > 0) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04001718 lockstart = round_down(pos,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 fs_info->sectorsize);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001720 lockend = round_up(pos + copied,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001721 fs_info->sectorsize) - 1;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001722
1723 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1724 lockend, EXTENT_NORESERVE, NULL,
1725 NULL, GFP_NOFS);
1726 only_release_metadata = false;
1727 }
1728
Miao Xief1de9682014-01-09 10:06:10 +08001729 btrfs_drop_pages(pages, num_pages);
1730
Josef Bacikd0215f32011-01-25 14:57:24 -05001731 cond_resched();
1732
Namjae Jeond0e1d662012-12-11 16:00:21 -08001733 balance_dirty_pages_ratelimited(inode->i_mapping);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 if (dirty_pages < (fs_info->nodesize >> PAGE_SHIFT) + 1)
Liu Bob53d3f52012-11-14 14:34:34 +00001735 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001736
Xin Zhong914ee292010-12-09 09:30:14 +00001737 pos += copied;
1738 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001739 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001740
Chris Mason8c2383c2007-06-18 09:57:58 -04001741 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001742
Josef Bacik7ee9e442013-06-21 16:37:03 -04001743 if (release_bytes) {
Miao Xie8257b2d2014-03-06 13:38:19 +08001744 if (only_release_metadata) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001745 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001746 btrfs_delalloc_release_metadata(inode, release_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001747 } else {
Chandan Rajendraa2af23b2016-04-04 02:53:06 +05301748 btrfs_delalloc_release_space(inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001749 round_down(pos, fs_info->sectorsize),
Chandan Rajendraa2af23b2016-04-04 02:53:06 +05301750 release_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001751 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04001752 }
1753
Josef Bacikd0215f32011-01-25 14:57:24 -05001754 return num_written ? num_written : ret;
1755}
1756
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001757static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
Josef Bacikd0215f32011-01-25 14:57:24 -05001758{
1759 struct file *file = iocb->ki_filp;
Filipe Manana728404d2014-10-10 09:43:11 +01001760 struct inode *inode = file_inode(file);
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001761 loff_t pos = iocb->ki_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001762 ssize_t written;
1763 ssize_t written_buffered;
1764 loff_t endbyte;
1765 int err;
1766
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001767 written = generic_file_direct_write(iocb, from);
Josef Bacikd0215f32011-01-25 14:57:24 -05001768
Al Viro0c949332014-03-22 06:51:37 -04001769 if (written < 0 || !iov_iter_count(from))
Josef Bacikd0215f32011-01-25 14:57:24 -05001770 return written;
1771
1772 pos += written;
Al Viro0ae5e4d2014-03-03 22:09:39 -05001773 written_buffered = __btrfs_buffered_write(file, from, pos);
Josef Bacikd0215f32011-01-25 14:57:24 -05001774 if (written_buffered < 0) {
1775 err = written_buffered;
1776 goto out;
1777 }
Filipe Manana075bdbd2014-10-09 21:18:55 +01001778 /*
1779 * Ensure all data is persisted. We want the next direct IO read to be
1780 * able to read what was just written.
1781 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001782 endbyte = pos + written_buffered - 1;
Filipe Manana728404d2014-10-10 09:43:11 +01001783 err = btrfs_fdatawrite_range(inode, pos, endbyte);
Filipe Manana075bdbd2014-10-09 21:18:55 +01001784 if (err)
1785 goto out;
Filipe Manana728404d2014-10-10 09:43:11 +01001786 err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte);
Josef Bacikd0215f32011-01-25 14:57:24 -05001787 if (err)
1788 goto out;
1789 written += written_buffered;
Al Viro867c4f92014-02-11 19:31:06 -05001790 iocb->ki_pos = pos + written_buffered;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001791 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT,
1792 endbyte >> PAGE_SHIFT);
Josef Bacikd0215f32011-01-25 14:57:24 -05001793out:
1794 return written ? written : err;
1795}
1796
Josef Bacik6c760c02012-11-09 10:53:21 -05001797static void update_time_for_write(struct inode *inode)
1798{
1799 struct timespec now;
1800
1801 if (IS_NOCMTIME(inode))
1802 return;
1803
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001804 now = current_time(inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05001805 if (!timespec_equal(&inode->i_mtime, &now))
1806 inode->i_mtime = now;
1807
1808 if (!timespec_equal(&inode->i_ctime, &now))
1809 inode->i_ctime = now;
1810
1811 if (IS_I_VERSION(inode))
1812 inode_inc_iversion(inode);
1813}
1814
Al Virob30ac0f2014-04-03 14:29:04 -04001815static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1816 struct iov_iter *from)
Josef Bacikd0215f32011-01-25 14:57:24 -05001817{
1818 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -05001819 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacikd0215f32011-01-25 14:57:24 -05001821 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001822 u64 start_pos;
Qu Wenruo3ac0d7b2014-03-27 02:51:58 +00001823 u64 end_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001824 ssize_t num_written = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001825 bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host);
Al Viro3309dd02015-04-09 12:55:47 -04001826 ssize_t err;
1827 loff_t pos;
1828 size_t count;
Chandan Rajendra27772b62016-01-21 15:56:03 +05301829 loff_t oldsize;
1830 int clean_page = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -05001831
Al Viro59551022016-01-22 15:40:57 -05001832 inode_lock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001833 err = generic_write_checks(iocb, from);
1834 if (err <= 0) {
Al Viro59551022016-01-22 15:40:57 -05001835 inode_unlock(inode);
Al Viro3309dd02015-04-09 12:55:47 -04001836 return err;
1837 }
Josef Bacikd0215f32011-01-25 14:57:24 -05001838
Christoph Hellwigde1414a2015-01-14 10:42:36 +01001839 current->backing_dev_info = inode_to_bdi(inode);
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001840 err = file_remove_privs(file);
Josef Bacikd0215f32011-01-25 14:57:24 -05001841 if (err) {
Al Viro59551022016-01-22 15:40:57 -05001842 inode_unlock(inode);
Josef Bacikd0215f32011-01-25 14:57:24 -05001843 goto out;
1844 }
1845
1846 /*
1847 * If BTRFS flips readonly due to some impossible error
1848 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1849 * although we have opened a file as writable, we have
1850 * to stop this write operation to ensure FS consistency.
1851 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001852 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
Al Viro59551022016-01-22 15:40:57 -05001853 inode_unlock(inode);
Josef Bacikd0215f32011-01-25 14:57:24 -05001854 err = -EROFS;
1855 goto out;
1856 }
1857
Josef Bacik6c760c02012-11-09 10:53:21 -05001858 /*
1859 * We reserve space for updating the inode when we reserve space for the
1860 * extent we are going to write, so we will enospc out there. We don't
1861 * need to start yet another transaction to update the inode as we will
1862 * update the inode when we finish writing whatever data we write.
1863 */
1864 update_time_for_write(inode);
Josef Bacikd0215f32011-01-25 14:57:24 -05001865
Al Viro3309dd02015-04-09 12:55:47 -04001866 pos = iocb->ki_pos;
1867 count = iov_iter_count(from);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001868 start_pos = round_down(pos, fs_info->sectorsize);
Chandan Rajendra27772b62016-01-21 15:56:03 +05301869 oldsize = i_size_read(inode);
1870 if (start_pos > oldsize) {
Qu Wenruo3ac0d7b2014-03-27 02:51:58 +00001871 /* Expand hole size to cover write data, preventing empty gap */
Jeff Mahoneyda170662016-06-15 09:22:56 -04001872 end_pos = round_up(pos + count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 fs_info->sectorsize);
Chandan Rajendra27772b62016-01-21 15:56:03 +05301874 err = btrfs_cont_expand(inode, oldsize, end_pos);
Miao Xie0c1a98c2011-09-11 10:52:24 -04001875 if (err) {
Al Viro59551022016-01-22 15:40:57 -05001876 inode_unlock(inode);
Miao Xie0c1a98c2011-09-11 10:52:24 -04001877 goto out;
1878 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001879 if (start_pos > round_up(oldsize, fs_info->sectorsize))
Chandan Rajendra27772b62016-01-21 15:56:03 +05301880 clean_page = 1;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001881 }
1882
Josef Bacikb812ce22012-11-16 13:56:32 -05001883 if (sync)
1884 atomic_inc(&BTRFS_I(inode)->sync_writers);
1885
Al Viro2ba48ce2015-04-09 13:52:01 -04001886 if (iocb->ki_flags & IOCB_DIRECT) {
Christoph Hellwig1af5bb42016-04-07 08:51:56 -07001887 num_written = __btrfs_direct_write(iocb, from);
Josef Bacikd0215f32011-01-25 14:57:24 -05001888 } else {
Al Virob30ac0f2014-04-03 14:29:04 -04001889 num_written = __btrfs_buffered_write(file, from, pos);
Josef Bacikd0215f32011-01-25 14:57:24 -05001890 if (num_written > 0)
Al Viro867c4f92014-02-11 19:31:06 -05001891 iocb->ki_pos = pos + num_written;
Chandan Rajendra27772b62016-01-21 15:56:03 +05301892 if (clean_page)
1893 pagecache_isize_extended(inode, oldsize,
1894 i_size_read(inode));
Josef Bacikd0215f32011-01-25 14:57:24 -05001895 }
1896
Al Viro59551022016-01-22 15:40:57 -05001897 inode_unlock(inode);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001898
Chris Mason5a3f23d2009-03-31 13:27:11 -04001899 /*
Josef Bacik6c760c02012-11-09 10:53:21 -05001900 * We also have to set last_sub_trans to the current log transid,
1901 * otherwise subsequent syncs to a file that's been synced in this
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001902 * transaction will appear to have already occurred.
Chris Mason5a3f23d2009-03-31 13:27:11 -04001903 */
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00001904 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik6c760c02012-11-09 10:53:21 -05001905 BTRFS_I(inode)->last_sub_trans = root->log_transid;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00001906 spin_unlock(&BTRFS_I(inode)->lock);
Christoph Hellwige2592212016-04-07 08:52:01 -07001907 if (num_written > 0)
1908 num_written = generic_write_sync(iocb, num_written);
Miao Xie0a3404d2013-01-28 12:34:55 +00001909
Josef Bacikb812ce22012-11-16 13:56:32 -05001910 if (sync)
1911 atomic_dec(&BTRFS_I(inode)->sync_writers);
Miao Xie0a3404d2013-01-28 12:34:55 +00001912out:
Chris Mason39279cc2007-06-12 06:35:45 -04001913 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001914 return num_written ? num_written : err;
1915}
1916
Chris Masond3977122009-01-05 21:25:51 -05001917int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001918{
Sage Weil6bf13c02008-06-10 10:07:39 -04001919 if (filp->private_data)
1920 btrfs_ioctl_trans_end(filp);
Chris Masonf6dc45c2014-08-20 07:15:33 -07001921 /*
1922 * ordered_data_close is set by settattr when we are about to truncate
1923 * a file from a non-zero size to a zero size. This tries to
1924 * flush down new bytes that may have been written if the
1925 * application were using truncate to replace a file in place.
1926 */
1927 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1928 &BTRFS_I(inode)->runtime_flags))
1929 filemap_flush(inode->i_mapping);
Mingminge1b81e62008-05-27 10:55:43 -04001930 return 0;
1931}
1932
Filipe Manana669249e2014-09-02 11:09:58 +01001933static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
1934{
1935 int ret;
1936
1937 atomic_inc(&BTRFS_I(inode)->sync_writers);
Filipe Manana728404d2014-10-10 09:43:11 +01001938 ret = btrfs_fdatawrite_range(inode, start, end);
Filipe Manana669249e2014-09-02 11:09:58 +01001939 atomic_dec(&BTRFS_I(inode)->sync_writers);
1940
1941 return ret;
1942}
1943
Chris Masond352ac62008-09-29 15:18:18 -04001944/*
1945 * fsync call for both files and directories. This logs the inode into
1946 * the tree log instead of forcing full commits whenever possible.
1947 *
1948 * It needs to call filemap_fdatawait so that all ordered extent updates are
1949 * in the metadata btree are up to date for copying to the log.
1950 *
1951 * It drops the inode mutex before doing the tree log commit. This is an
1952 * important optimization for directories because holding the mutex prevents
1953 * new operations on the dir while we write to disk.
1954 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001955int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001956{
Filipe Mananade17e792016-03-30 19:03:13 -04001957 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +00001958 struct inode *inode = d_inode(dentry);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001959 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04001960 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001961 struct btrfs_trans_handle *trans;
Miao Xie8b050d32014-02-20 18:08:58 +08001962 struct btrfs_log_ctx ctx;
1963 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001964 bool full_sync = 0;
David Sterba9dcbeed2015-11-09 11:44:45 +01001965 u64 len;
Chris Mason39279cc2007-06-12 06:35:45 -04001966
David Sterba9dcbeed2015-11-09 11:44:45 +01001967 /*
1968 * The range length can be represented by u64, we have to do the typecasts
1969 * to avoid signed overflow if it's [0, LLONG_MAX] eg. from fsync()
1970 */
1971 len = (u64)end - (u64)start + 1;
liubo1abe9b82011-03-24 11:18:59 +00001972 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04001973
Miao Xie90abccf2012-09-13 04:53:47 -06001974 /*
1975 * We write the dirty pages in the range and wait until they complete
1976 * out of the ->i_mutex. If so, we can flush the dirty pages by
Josef Bacik2ab28f32012-10-12 15:27:49 -04001977 * multi-task, and make the performance up. See
1978 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
Miao Xie90abccf2012-09-13 04:53:47 -06001979 */
Filipe Manana669249e2014-09-02 11:09:58 +01001980 ret = start_ordered_ops(inode, start, end);
Miao Xie90abccf2012-09-13 04:53:47 -06001981 if (ret)
1982 return ret;
1983
Al Viro59551022016-01-22 15:40:57 -05001984 inode_lock(inode);
Miao Xie2ecb7922012-09-06 04:04:27 -06001985 atomic_inc(&root->log_batch);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001986 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1987 &BTRFS_I(inode)->runtime_flags);
Filipe Manana669249e2014-09-02 11:09:58 +01001988 /*
1989 * We might have have had more pages made dirty after calling
1990 * start_ordered_ops and before acquiring the inode's i_mutex.
1991 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04001992 if (full_sync) {
Filipe Manana669249e2014-09-02 11:09:58 +01001993 /*
1994 * For a full sync, we need to make sure any ordered operations
1995 * start and finish before we start logging the inode, so that
1996 * all extents are persisted and the respective file extent
1997 * items are in the fs/subvol btree.
1998 */
Filipe Mananab659ef02015-03-31 14:16:52 +01001999 ret = btrfs_wait_ordered_range(inode, start, len);
Filipe Manana669249e2014-09-02 11:09:58 +01002000 } else {
2001 /*
2002 * Start any new ordered operations before starting to log the
2003 * inode. We will wait for them to finish in btrfs_sync_log().
2004 *
2005 * Right before acquiring the inode's mutex, we might have new
2006 * writes dirtying pages, which won't immediately start the
2007 * respective ordered operations - that is done through the
2008 * fill_delalloc callbacks invoked from the writepage and
2009 * writepages address space operations. So make sure we start
2010 * all ordered operations before starting to log our inode. Not
2011 * doing this means that while logging the inode, writeback
2012 * could start and invoke writepage/writepages, which would call
2013 * the fill_delalloc callbacks (cow_file_range,
2014 * submit_compressed_extents). These callbacks add first an
2015 * extent map to the modified list of extents and then create
2016 * the respective ordered operation, which means in
2017 * tree-log.c:btrfs_log_inode() we might capture all existing
2018 * ordered operations (with btrfs_get_logged_extents()) before
2019 * the fill_delalloc callback adds its ordered operation, and by
2020 * the time we visit the modified list of extent maps (with
2021 * btrfs_log_changed_extents()), we see and process the extent
2022 * map they created. We then use the extent map to construct a
2023 * file extent item for logging without waiting for the
2024 * respective ordered operation to finish - this file extent
2025 * item points to a disk location that might not have yet been
2026 * written to, containing random data - so after a crash a log
2027 * replay will make our inode have file extent items that point
2028 * to disk locations containing invalid data, as we returned
2029 * success to userspace without waiting for the respective
2030 * ordered operation to finish, because it wasn't captured by
2031 * btrfs_get_logged_extents().
2032 */
2033 ret = start_ordered_ops(inode, start, end);
2034 }
2035 if (ret) {
Al Viro59551022016-01-22 15:40:57 -05002036 inode_unlock(inode);
Filipe Manana669249e2014-09-02 11:09:58 +01002037 goto out;
Josef Bacik0ef8b722013-10-25 16:13:35 -04002038 }
Miao Xie2ecb7922012-09-06 04:04:27 -06002039 atomic_inc(&root->log_batch);
Chris Mason257c62e2009-10-13 13:21:08 -04002040
Chris Mason39279cc2007-06-12 06:35:45 -04002041 /*
Filipe Manana3a8b36f2015-03-01 20:36:00 +00002042 * If the last transaction that changed this file was before the current
2043 * transaction and we have the full sync flag set in our inode, we can
2044 * bail out now without any syncing.
2045 *
2046 * Note that we can't bail out if the full sync flag isn't set. This is
2047 * because when the full sync flag is set we start all ordered extents
2048 * and wait for them to fully complete - when they complete they update
2049 * the inode's last_trans field through:
2050 *
2051 * btrfs_finish_ordered_io() ->
2052 * btrfs_update_inode_fallback() ->
2053 * btrfs_update_inode() ->
2054 * btrfs_set_inode_last_trans()
2055 *
2056 * So we are sure that last_trans is up to date and can do this check to
2057 * bail out safely. For the fast path, when the full sync flag is not
2058 * set in our inode, we can not do it because we start only our ordered
2059 * extents and don't wait for them to complete (that is when
2060 * btrfs_finish_ordered_io runs), so here at this point their last_trans
2061 * value might be less than or equals to fs_info->last_trans_committed,
2062 * and setting a speculative last_trans for an inode when a buffered
2063 * write is made (such as fs_info->generation + 1 for example) would not
2064 * be reliable since after setting the value and before fsync is called
2065 * any number of transactions can start and commit (transaction kthread
2066 * commits the current transaction periodically), and a transaction
2067 * commit does not start nor waits for ordered extents to complete.
Chris Mason257c62e2009-10-13 13:21:08 -04002068 */
Josef Bacika4abeea2011-04-11 17:25:13 -04002069 smp_mb();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002070 if (btrfs_inode_in_log(inode, fs_info->generation) ||
Filipe Mananaaffc0ff2016-02-24 07:35:05 +00002071 (full_sync && BTRFS_I(inode)->last_trans <=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002072 fs_info->last_trans_committed) ||
Filipe Mananaaffc0ff2016-02-24 07:35:05 +00002073 (!btrfs_have_ordered_extents_in_range(inode, start, len) &&
2074 BTRFS_I(inode)->last_trans
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002075 <= fs_info->last_trans_committed)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04002076 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002077 * We've had everything committed since the last time we were
Josef Bacik5dc562c2012-08-17 13:14:17 -04002078 * modified so clear this flag in case it was set for whatever
2079 * reason, it's no longer relevant.
2080 */
2081 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2082 &BTRFS_I(inode)->runtime_flags);
Filipe Manana0596a902016-06-14 14:18:27 +01002083 /*
2084 * An ordered extent might have started before and completed
2085 * already with io errors, in which case the inode was not
2086 * updated and we end up here. So check the inode's mapping
2087 * flags for any errors that might have happened while doing
2088 * writeback of file data.
2089 */
Miklos Szeredif0312212016-09-16 12:44:21 +02002090 ret = filemap_check_errors(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05002091 inode_unlock(inode);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002092 goto out;
2093 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002094
2095 /*
Chris Masona52d9a82007-08-27 16:49:44 -04002096 * ok we haven't committed the transaction yet, lets do a commit
2097 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00002098 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04002099 btrfs_ioctl_trans_end(file);
2100
Josef Bacik5039edd2014-01-15 13:34:13 -05002101 /*
2102 * We use start here because we will need to wait on the IO to complete
2103 * in btrfs_sync_log, which could require joining a transaction (for
2104 * example checking cross references in the nocow path). If we use join
2105 * here we could get into a situation where we're waiting on IO to
2106 * happen that is blocked on a transaction trying to commit. With start
2107 * we inc the extwriter counter, so we wait for all extwriters to exit
2108 * before we start blocking join'ers. This comment is to keep somebody
2109 * from thinking they are super smart and changing this to
2110 * btrfs_join_transaction *cough*Josef*cough*.
2111 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002112 trans = btrfs_start_transaction(root, 0);
2113 if (IS_ERR(trans)) {
2114 ret = PTR_ERR(trans);
Al Viro59551022016-01-22 15:40:57 -05002115 inode_unlock(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002116 goto out;
2117 }
Josef Bacik5039edd2014-01-15 13:34:13 -05002118 trans->sync = true;
Chris Masone02119d2008-09-05 16:13:11 -04002119
Filipe Manana28a23592016-08-23 21:13:51 +01002120 btrfs_init_log_ctx(&ctx, inode);
Miao Xie8b050d32014-02-20 18:08:58 +08002121
Filipe Manana49dae1b2014-09-06 22:34:39 +01002122 ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
Josef Bacik02c24a82011-07-16 20:44:56 -04002123 if (ret < 0) {
Filipe David Borba Mananaa0634be2013-09-11 20:36:44 +01002124 /* Fallthrough and commit/free transaction. */
2125 ret = 1;
Josef Bacik02c24a82011-07-16 20:44:56 -04002126 }
Chris Mason49eb7e42008-09-11 15:53:12 -04002127
2128 /* we've logged all the items and now have a consistent
2129 * version of the file in the log. It is possible that
2130 * someone will come in and modify the file, but that's
2131 * fine because the log is consistent on disk, and we
2132 * have references to all of the file's extents
2133 *
2134 * It is possible that someone will come in and log the
2135 * file again, but that will end up using the synchronization
2136 * inside btrfs_sync_log to keep things safe.
2137 */
Al Viro59551022016-01-22 15:40:57 -05002138 inode_unlock(inode);
Chris Mason49eb7e42008-09-11 15:53:12 -04002139
Filipe Manana8407f552014-09-05 15:14:39 +01002140 /*
2141 * If any of the ordered extents had an error, just return it to user
2142 * space, so that the application knows some writes didn't succeed and
2143 * can take proper action (retry for e.g.). Blindly committing the
2144 * transaction in this case, would fool userspace that everything was
2145 * successful. And we also want to make sure our log doesn't contain
2146 * file extent items pointing to extents that weren't fully written to -
2147 * just like in the non fast fsync path, where we check for the ordered
2148 * operation's error flag before writing to the log tree and return -EIO
2149 * if any of them had this flag set (btrfs_wait_ordered_range) -
2150 * therefore we need to check for errors in the ordered operations,
2151 * which are indicated by ctx.io_err.
2152 */
2153 if (ctx.io_err) {
2154 btrfs_end_transaction(trans, root);
2155 ret = ctx.io_err;
2156 goto out;
2157 }
2158
Chris Mason257c62e2009-10-13 13:21:08 -04002159 if (ret != BTRFS_NO_LOG_SYNC) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04002160 if (!ret) {
Miao Xie8b050d32014-02-20 18:08:58 +08002161 ret = btrfs_sync_log(trans, root, &ctx);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002162 if (!ret) {
Chris Mason257c62e2009-10-13 13:21:08 -04002163 ret = btrfs_end_transaction(trans, root);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002164 goto out;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002165 }
Chris Mason257c62e2009-10-13 13:21:08 -04002166 }
Josef Bacik0ef8b722013-10-25 16:13:35 -04002167 if (!full_sync) {
David Sterba9dcbeed2015-11-09 11:44:45 +01002168 ret = btrfs_wait_ordered_range(inode, start, len);
Filipe Mananab05fd872014-05-29 23:31:39 +01002169 if (ret) {
2170 btrfs_end_transaction(trans, root);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002171 goto out;
Filipe Mananab05fd872014-05-29 23:31:39 +01002172 }
Josef Bacik0ef8b722013-10-25 16:13:35 -04002173 }
2174 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04002175 } else {
2176 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002177 }
Chris Mason39279cc2007-06-12 06:35:45 -04002178out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00002179 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002180}
2181
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002182static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04002183 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002184 .map_pages = filemap_map_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04002185 .page_mkwrite = btrfs_page_mkwrite,
2186};
2187
2188static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2189{
Miao Xie058a4572010-05-20 07:21:50 +00002190 struct address_space *mapping = filp->f_mapping;
2191
2192 if (!mapping->a_ops->readpage)
2193 return -ENOEXEC;
2194
Chris Mason9ebefb182007-06-15 13:50:00 -04002195 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00002196 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00002197
Chris Mason9ebefb182007-06-15 13:50:00 -04002198 return 0;
2199}
2200
Josef Bacik2aaa6652012-08-29 14:27:18 -04002201static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
2202 int slot, u64 start, u64 end)
2203{
2204 struct btrfs_file_extent_item *fi;
2205 struct btrfs_key key;
2206
2207 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2208 return 0;
2209
2210 btrfs_item_key_to_cpu(leaf, &key, slot);
2211 if (key.objectid != btrfs_ino(inode) ||
2212 key.type != BTRFS_EXTENT_DATA_KEY)
2213 return 0;
2214
2215 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2216
2217 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2218 return 0;
2219
2220 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2221 return 0;
2222
2223 if (key.offset == end)
2224 return 1;
2225 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2226 return 1;
2227 return 0;
2228}
2229
2230static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2231 struct btrfs_path *path, u64 offset, u64 end)
2232{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002233 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002234 struct btrfs_root *root = BTRFS_I(inode)->root;
2235 struct extent_buffer *leaf;
2236 struct btrfs_file_extent_item *fi;
2237 struct extent_map *hole_em;
2238 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2239 struct btrfs_key key;
2240 int ret;
2241
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002242 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04002243 goto out;
2244
Josef Bacik2aaa6652012-08-29 14:27:18 -04002245 key.objectid = btrfs_ino(inode);
2246 key.type = BTRFS_EXTENT_DATA_KEY;
2247 key.offset = offset;
2248
Josef Bacik2aaa6652012-08-29 14:27:18 -04002249 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacikf94480b2016-11-14 14:06:22 -05002250 if (ret <= 0) {
2251 /*
2252 * We should have dropped this offset, so if we find it then
2253 * something has gone horribly wrong.
2254 */
2255 if (ret == 0)
2256 ret = -EINVAL;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002257 return ret;
Josef Bacikf94480b2016-11-14 14:06:22 -05002258 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002259
2260 leaf = path->nodes[0];
2261 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
2262 u64 num_bytes;
2263
2264 path->slots[0]--;
2265 fi = btrfs_item_ptr(leaf, path->slots[0],
2266 struct btrfs_file_extent_item);
2267 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2268 end - offset;
2269 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2270 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2271 btrfs_set_file_extent_offset(leaf, fi, 0);
2272 btrfs_mark_buffer_dirty(leaf);
2273 goto out;
2274 }
2275
chandan1707e262014-07-01 12:04:28 +05302276 if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04002277 u64 num_bytes;
2278
Josef Bacik2aaa6652012-08-29 14:27:18 -04002279 key.offset = offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002280 btrfs_set_item_key_safe(fs_info, path, &key);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002281 fi = btrfs_item_ptr(leaf, path->slots[0],
2282 struct btrfs_file_extent_item);
2283 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2284 offset;
2285 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2286 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2287 btrfs_set_file_extent_offset(leaf, fi, 0);
2288 btrfs_mark_buffer_dirty(leaf);
2289 goto out;
2290 }
2291 btrfs_release_path(path);
2292
2293 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
2294 0, 0, end - offset, 0, end - offset,
2295 0, 0, 0);
2296 if (ret)
2297 return ret;
2298
2299out:
2300 btrfs_release_path(path);
2301
2302 hole_em = alloc_extent_map();
2303 if (!hole_em) {
2304 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2305 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2306 &BTRFS_I(inode)->runtime_flags);
2307 } else {
2308 hole_em->start = offset;
2309 hole_em->len = end - offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04002310 hole_em->ram_bytes = hole_em->len;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002311 hole_em->orig_start = offset;
2312
2313 hole_em->block_start = EXTENT_MAP_HOLE;
2314 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05002315 hole_em->orig_block_len = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002317 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2318 hole_em->generation = trans->transid;
2319
2320 do {
2321 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2322 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002323 ret = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002324 write_unlock(&em_tree->lock);
2325 } while (ret == -EEXIST);
2326 free_extent_map(hole_em);
2327 if (ret)
2328 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2329 &BTRFS_I(inode)->runtime_flags);
2330 }
2331
2332 return 0;
2333}
2334
Qu Wenruod7781542014-05-30 15:16:10 +08002335/*
2336 * Find a hole extent on given inode and change start/len to the end of hole
2337 * extent.(hole/vacuum extent whose em->start <= start &&
2338 * em->start + em->len > start)
2339 * When a hole extent is found, return 1 and modify start/len.
2340 */
2341static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
2342{
2343 struct extent_map *em;
2344 int ret = 0;
2345
2346 em = btrfs_get_extent(inode, NULL, 0, *start, *len, 0);
2347 if (IS_ERR_OR_NULL(em)) {
2348 if (!em)
2349 ret = -ENOMEM;
2350 else
2351 ret = PTR_ERR(em);
2352 return ret;
2353 }
2354
2355 /* Hole or vacuum extent(only exists in no-hole mode) */
2356 if (em->block_start == EXTENT_MAP_HOLE) {
2357 ret = 1;
2358 *len = em->start + em->len > *start + *len ?
2359 0 : *start + *len - em->start - em->len;
2360 *start = em->start + em->len;
2361 }
2362 free_extent_map(em);
2363 return ret;
2364}
2365
Josef Bacik2aaa6652012-08-29 14:27:18 -04002366static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002369 struct btrfs_root *root = BTRFS_I(inode)->root;
2370 struct extent_state *cached_state = NULL;
2371 struct btrfs_path *path;
2372 struct btrfs_block_rsv *rsv;
2373 struct btrfs_trans_handle *trans;
Qu Wenruod7781542014-05-30 15:16:10 +08002374 u64 lockstart;
2375 u64 lockend;
2376 u64 tail_start;
2377 u64 tail_len;
2378 u64 orig_start = offset;
2379 u64 cur_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002380 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002381 u64 drop_end;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002382 int ret = 0;
2383 int err = 0;
Alexandru Moise6e4d6fa2015-09-22 21:00:07 +00002384 unsigned int rsv_count;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302385 bool same_block;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002386 bool no_holes = btrfs_fs_incompat(fs_info, NO_HOLES);
Filipe Mananaa1a50f62014-04-26 01:35:31 +01002387 u64 ino_size;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302388 bool truncated_block = false;
Filipe Mananae8c1c762015-02-15 22:38:54 +00002389 bool updated_inode = false;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002390
Josef Bacik0ef8b722013-10-25 16:13:35 -04002391 ret = btrfs_wait_ordered_range(inode, offset, len);
2392 if (ret)
2393 return ret;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002394
Al Viro59551022016-01-22 15:40:57 -05002395 inode_lock(inode);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396 ino_size = round_up(inode->i_size, fs_info->sectorsize);
Qu Wenruod7781542014-05-30 15:16:10 +08002397 ret = find_first_non_hole(inode, &offset, &len);
2398 if (ret < 0)
2399 goto out_only_mutex;
2400 if (ret && !len) {
2401 /* Already in a large hole */
2402 ret = 0;
2403 goto out_only_mutex;
2404 }
2405
Jeff Mahoneyda170662016-06-15 09:22:56 -04002406 lockstart = round_up(offset, btrfs_inode_sectorsize(inode));
Qu Wenruod7781542014-05-30 15:16:10 +08002407 lockend = round_down(offset + len,
Jeff Mahoneyda170662016-06-15 09:22:56 -04002408 btrfs_inode_sectorsize(inode)) - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
2410 == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
Miao Xie7426cc02012-12-05 10:54:52 +00002411 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302412 * We needn't truncate any block which is beyond the end of the file
Miao Xie7426cc02012-12-05 10:54:52 +00002413 * because we are sure there is no data there.
2414 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04002415 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302416 * Only do this if we are in the same block and we aren't doing the
2417 * entire block.
Josef Bacik2aaa6652012-08-29 14:27:18 -04002418 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002419 if (same_block && len < fs_info->sectorsize) {
Filipe Mananae8c1c762015-02-15 22:38:54 +00002420 if (offset < ino_size) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302421 truncated_block = true;
2422 ret = btrfs_truncate_block(inode, offset, len, 0);
Filipe Mananae8c1c762015-02-15 22:38:54 +00002423 } else {
2424 ret = 0;
2425 }
Qu Wenruod7781542014-05-30 15:16:10 +08002426 goto out_only_mutex;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002427 }
2428
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302429 /* zero back part of the first block */
Filipe Manana12870f12014-02-15 15:55:58 +00002430 if (offset < ino_size) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302431 truncated_block = true;
2432 ret = btrfs_truncate_block(inode, offset, 0, 0);
Miao Xie7426cc02012-12-05 10:54:52 +00002433 if (ret) {
Al Viro59551022016-01-22 15:40:57 -05002434 inode_unlock(inode);
Miao Xie7426cc02012-12-05 10:54:52 +00002435 return ret;
2436 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002437 }
2438
Qu Wenruod7781542014-05-30 15:16:10 +08002439 /* Check the aligned pages after the first unaligned page,
2440 * if offset != orig_start, which means the first unaligned page
Nicholas D Steeves01327612016-05-19 21:18:45 -04002441 * including several following pages are already in holes,
Qu Wenruod7781542014-05-30 15:16:10 +08002442 * the extra check can be skipped */
2443 if (offset == orig_start) {
2444 /* after truncate page, check hole again */
2445 len = offset + len - lockstart;
2446 offset = lockstart;
2447 ret = find_first_non_hole(inode, &offset, &len);
2448 if (ret < 0)
2449 goto out_only_mutex;
2450 if (ret && !len) {
2451 ret = 0;
2452 goto out_only_mutex;
2453 }
2454 lockstart = offset;
2455 }
2456
2457 /* Check the tail unaligned part is in a hole */
2458 tail_start = lockend + 1;
2459 tail_len = offset + len - tail_start;
2460 if (tail_len) {
2461 ret = find_first_non_hole(inode, &tail_start, &tail_len);
2462 if (unlikely(ret < 0))
2463 goto out_only_mutex;
2464 if (!ret) {
2465 /* zero the front end of the last page */
2466 if (tail_start + tail_len < ino_size) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302467 truncated_block = true;
2468 ret = btrfs_truncate_block(inode,
2469 tail_start + tail_len,
2470 0, 1);
Qu Wenruod7781542014-05-30 15:16:10 +08002471 if (ret)
2472 goto out_only_mutex;
Qu Wenruo51f395a2014-08-08 13:06:20 +08002473 }
Miao Xie00612802012-12-05 10:54:12 +00002474 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002475 }
2476
2477 if (lockend < lockstart) {
Filipe Mananae8c1c762015-02-15 22:38:54 +00002478 ret = 0;
2479 goto out_only_mutex;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002480 }
2481
2482 while (1) {
2483 struct btrfs_ordered_extent *ordered;
2484
2485 truncate_pagecache_range(inode, lockstart, lockend);
2486
2487 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01002488 &cached_state);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002489 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
2490
2491 /*
2492 * We need to make sure we have no ordered extents in this range
2493 * and nobody raced in and read a page in this range, if we did
2494 * we need to try again.
2495 */
2496 if ((!ordered ||
Filipe David Borba Manana6126e3c2013-11-19 16:19:24 +00002497 (ordered->file_offset + ordered->len <= lockstart ||
Josef Bacik2aaa6652012-08-29 14:27:18 -04002498 ordered->file_offset > lockend)) &&
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07002499 !btrfs_page_exists_in_range(inode, lockstart, lockend)) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04002500 if (ordered)
2501 btrfs_put_ordered_extent(ordered);
2502 break;
2503 }
2504 if (ordered)
2505 btrfs_put_ordered_extent(ordered);
2506 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2507 lockend, &cached_state, GFP_NOFS);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002508 ret = btrfs_wait_ordered_range(inode, lockstart,
2509 lockend - lockstart + 1);
2510 if (ret) {
Al Viro59551022016-01-22 15:40:57 -05002511 inode_unlock(inode);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002512 return ret;
2513 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002514 }
2515
2516 path = btrfs_alloc_path();
2517 if (!path) {
2518 ret = -ENOMEM;
2519 goto out;
2520 }
2521
Miao Xie66d8f3d2012-09-06 04:02:28 -06002522 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002523 if (!rsv) {
2524 ret = -ENOMEM;
2525 goto out_free;
2526 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002527 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002528 rsv->failfast = 1;
2529
2530 /*
2531 * 1 - update the inode
2532 * 1 - removing the extents in the range
Josef Bacik16e75492013-10-22 12:18:51 -04002533 * 1 - adding the hole extent if no_holes isn't set
Josef Bacik2aaa6652012-08-29 14:27:18 -04002534 */
Josef Bacik16e75492013-10-22 12:18:51 -04002535 rsv_count = no_holes ? 2 : 3;
2536 trans = btrfs_start_transaction(root, rsv_count);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002537 if (IS_ERR(trans)) {
2538 err = PTR_ERR(trans);
2539 goto out_free;
2540 }
2541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002542 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04002543 min_size, 0);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002544 BUG_ON(ret);
2545 trans->block_rsv = rsv;
2546
Qu Wenruod7781542014-05-30 15:16:10 +08002547 cur_offset = lockstart;
2548 len = lockend - cur_offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002549 while (cur_offset < lockend) {
2550 ret = __btrfs_drop_extents(trans, root, inode, path,
2551 cur_offset, lockend + 1,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002552 &drop_end, 1, 0, 0, NULL);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002553 if (ret != -ENOSPC)
2554 break;
2555
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002556 trans->block_rsv = &fs_info->trans_block_rsv;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002557
Josef Bacik62fe51c2016-11-16 09:13:39 -05002558 if (cur_offset < drop_end && cur_offset < ino_size) {
Filipe Manana12870f12014-02-15 15:55:58 +00002559 ret = fill_holes(trans, inode, path, cur_offset,
2560 drop_end);
2561 if (ret) {
Josef Bacikf94480b2016-11-14 14:06:22 -05002562 /*
2563 * If we failed then we didn't insert our hole
2564 * entries for the area we dropped, so now the
2565 * fs is corrupted, so we must abort the
2566 * transaction.
2567 */
2568 btrfs_abort_transaction(trans, ret);
Filipe Manana12870f12014-02-15 15:55:58 +00002569 err = ret;
2570 break;
2571 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002572 }
2573
2574 cur_offset = drop_end;
2575
2576 ret = btrfs_update_inode(trans, root, inode);
2577 if (ret) {
2578 err = ret;
2579 break;
2580 }
2581
Josef Bacik2aaa6652012-08-29 14:27:18 -04002582 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002583 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002584
Josef Bacik16e75492013-10-22 12:18:51 -04002585 trans = btrfs_start_transaction(root, rsv_count);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002586 if (IS_ERR(trans)) {
2587 ret = PTR_ERR(trans);
2588 trans = NULL;
2589 break;
2590 }
2591
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002592 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04002593 rsv, min_size, 0);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002594 BUG_ON(ret); /* shouldn't happen */
2595 trans->block_rsv = rsv;
Qu Wenruod7781542014-05-30 15:16:10 +08002596
2597 ret = find_first_non_hole(inode, &cur_offset, &len);
2598 if (unlikely(ret < 0))
2599 break;
2600 if (ret && !len) {
2601 ret = 0;
2602 break;
2603 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002604 }
2605
2606 if (ret) {
2607 err = ret;
2608 goto out_trans;
2609 }
2610
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002611 trans->block_rsv = &fs_info->trans_block_rsv;
Filipe Mananafc19c5e2014-04-29 13:18:40 +01002612 /*
Filipe Manana2959a322015-11-02 12:32:44 +00002613 * If we are using the NO_HOLES feature we might have had already an
2614 * hole that overlaps a part of the region [lockstart, lockend] and
2615 * ends at (or beyond) lockend. Since we have no file extent items to
2616 * represent holes, drop_end can be less than lockend and so we must
2617 * make sure we have an extent map representing the existing hole (the
2618 * call to __btrfs_drop_extents() might have dropped the existing extent
2619 * map representing the existing hole), otherwise the fast fsync path
2620 * will not record the existence of the hole region
2621 * [existing_hole_start, lockend].
2622 */
2623 if (drop_end <= lockend)
2624 drop_end = lockend + 1;
2625 /*
Filipe Mananafc19c5e2014-04-29 13:18:40 +01002626 * Don't insert file hole extent item if it's for a range beyond eof
2627 * (because it's useless) or if it represents a 0 bytes range (when
2628 * cur_offset == drop_end).
2629 */
2630 if (cur_offset < ino_size && cur_offset < drop_end) {
Filipe Manana12870f12014-02-15 15:55:58 +00002631 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
2632 if (ret) {
Josef Bacikf94480b2016-11-14 14:06:22 -05002633 /* Same comment as above. */
2634 btrfs_abort_transaction(trans, ret);
Filipe Manana12870f12014-02-15 15:55:58 +00002635 err = ret;
2636 goto out_trans;
2637 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002638 }
2639
2640out_trans:
2641 if (!trans)
2642 goto out_free;
2643
Tsutomu Itohe1f57902012-11-08 04:47:33 +00002644 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07002645 inode->i_mtime = inode->i_ctime = current_time(inode);
Tsutomu Itohe1f57902012-11-08 04:47:33 +00002646
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002647 trans->block_rsv = &fs_info->trans_block_rsv;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002648 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananae8c1c762015-02-15 22:38:54 +00002649 updated_inode = true;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002650 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00002651 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002652out_free:
2653 btrfs_free_path(path);
2654 btrfs_free_block_rsv(root, rsv);
2655out:
2656 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2657 &cached_state, GFP_NOFS);
Qu Wenruod7781542014-05-30 15:16:10 +08002658out_only_mutex:
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302659 if (!updated_inode && truncated_block && !ret && !err) {
Filipe Mananae8c1c762015-02-15 22:38:54 +00002660 /*
2661 * If we only end up zeroing part of a page, we still need to
2662 * update the inode item, so that all the time fields are
2663 * updated as well as the necessary btrfs inode in memory fields
2664 * for detecting, at fsync time, if the inode isn't yet in the
2665 * log tree or it's there but not up to date.
2666 */
2667 trans = btrfs_start_transaction(root, 1);
2668 if (IS_ERR(trans)) {
2669 err = PTR_ERR(trans);
2670 } else {
2671 err = btrfs_update_inode(trans, root, inode);
2672 ret = btrfs_end_transaction(trans, root);
2673 }
2674 }
Al Viro59551022016-01-22 15:40:57 -05002675 inode_unlock(inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002676 if (ret && !err)
2677 err = ret;
2678 return err;
2679}
2680
Qu Wenruo14524a82015-09-08 17:22:44 +08002681/* Helper structure to record which range is already reserved */
2682struct falloc_range {
2683 struct list_head list;
2684 u64 start;
2685 u64 len;
2686};
2687
2688/*
2689 * Helper function to add falloc range
2690 *
2691 * Caller should have locked the larger range of extent containing
2692 * [start, len)
2693 */
2694static int add_falloc_range(struct list_head *head, u64 start, u64 len)
2695{
2696 struct falloc_range *prev = NULL;
2697 struct falloc_range *range = NULL;
2698
2699 if (list_empty(head))
2700 goto insert;
2701
2702 /*
2703 * As fallocate iterate by bytenr order, we only need to check
2704 * the last range.
2705 */
2706 prev = list_entry(head->prev, struct falloc_range, list);
2707 if (prev->start + prev->len == start) {
2708 prev->len += len;
2709 return 0;
2710 }
2711insert:
David Sterba32fc9322016-02-11 14:25:38 +01002712 range = kmalloc(sizeof(*range), GFP_KERNEL);
Qu Wenruo14524a82015-09-08 17:22:44 +08002713 if (!range)
2714 return -ENOMEM;
2715 range->start = start;
2716 range->len = len;
2717 list_add_tail(&range->list, head);
2718 return 0;
2719}
2720
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002721static long btrfs_fallocate(struct file *file, int mode,
2722 loff_t offset, loff_t len)
2723{
Al Viro496ad9a2013-01-23 17:07:38 -05002724 struct inode *inode = file_inode(file);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002725 struct extent_state *cached_state = NULL;
Qu Wenruo14524a82015-09-08 17:22:44 +08002726 struct falloc_range *range;
2727 struct falloc_range *tmp;
2728 struct list_head reserve_list;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002729 u64 cur_offset;
2730 u64 last_byte;
2731 u64 alloc_start;
2732 u64 alloc_end;
2733 u64 alloc_hint = 0;
2734 u64 locked_end;
Qu Wenruo14524a82015-09-08 17:22:44 +08002735 u64 actual_end = 0;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002736 struct extent_map *em;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002737 int blocksize = btrfs_inode_sectorsize(inode);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002738 int ret;
2739
Miao Xie797f4272012-11-28 10:28:07 +00002740 alloc_start = round_down(offset, blocksize);
2741 alloc_end = round_up(offset + len, blocksize);
Wang Xiaoguang18513092016-07-25 15:51:40 +08002742 cur_offset = alloc_start;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002743
Josef Bacik2aaa6652012-08-29 14:27:18 -04002744 /* Make sure we aren't being give some crap mode */
2745 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002746 return -EOPNOTSUPP;
2747
Josef Bacik2aaa6652012-08-29 14:27:18 -04002748 if (mode & FALLOC_FL_PUNCH_HOLE)
2749 return btrfs_punch_hole(inode, offset, len);
2750
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002751 /*
Qu Wenruo14524a82015-09-08 17:22:44 +08002752 * Only trigger disk allocation, don't trigger qgroup reserve
2753 *
2754 * For qgroup space, it will be checked later.
Chris Masond98456f2012-01-31 20:27:41 -05002755 */
Qu Wenruo14524a82015-09-08 17:22:44 +08002756 ret = btrfs_alloc_data_chunk_ondemand(inode, alloc_end - alloc_start);
2757 if (ret < 0)
Chris Masond98456f2012-01-31 20:27:41 -05002758 return ret;
2759
Al Viro59551022016-01-22 15:40:57 -05002760 inode_lock(inode);
Davide Italiano2a162ce2015-04-06 22:09:15 -07002761
2762 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
2763 ret = inode_newsize_ok(inode, offset + len);
2764 if (ret)
2765 goto out;
2766 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002767
Qu Wenruo14524a82015-09-08 17:22:44 +08002768 /*
2769 * TODO: Move these two operations after we have checked
2770 * accurate reserved space, or fallocate can still fail but
2771 * with page truncated or size expanded.
2772 *
2773 * But that's a minor problem and won't do much harm BTW.
2774 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002775 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002776 ret = btrfs_cont_expand(inode, i_size_read(inode),
2777 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002778 if (ret)
2779 goto out;
Qu Wenruo0f6925f2015-10-14 15:26:13 +08002780 } else if (offset + len > inode->i_size) {
Josef Bacika71754f2013-06-17 17:14:39 -04002781 /*
2782 * If we are fallocating from the end of the file onward we
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302783 * need to zero out the end of the block if i_size lands in the
2784 * middle of a block.
Josef Bacika71754f2013-06-17 17:14:39 -04002785 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302786 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04002787 if (ret)
2788 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002789 }
2790
Josef Bacika71754f2013-06-17 17:14:39 -04002791 /*
2792 * wait for ordered IO before we have any locks. We'll loop again
2793 * below with the locks held.
2794 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04002795 ret = btrfs_wait_ordered_range(inode, alloc_start,
2796 alloc_end - alloc_start);
2797 if (ret)
2798 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04002799
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002800 locked_end = alloc_end - 1;
2801 while (1) {
2802 struct btrfs_ordered_extent *ordered;
2803
2804 /* the extent lock is ordered inside the running
2805 * transaction
2806 */
2807 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
David Sterbaff13db42015-12-03 14:30:40 +01002808 locked_end, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002809 ordered = btrfs_lookup_first_ordered_extent(inode,
2810 alloc_end - 1);
2811 if (ordered &&
2812 ordered->file_offset + ordered->len > alloc_start &&
2813 ordered->file_offset < alloc_end) {
2814 btrfs_put_ordered_extent(ordered);
2815 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2816 alloc_start, locked_end,
David Sterba32fc9322016-02-11 14:25:38 +01002817 &cached_state, GFP_KERNEL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002818 /*
2819 * we can't wait on the range with the transaction
2820 * running or with the extent lock held
2821 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04002822 ret = btrfs_wait_ordered_range(inode, alloc_start,
2823 alloc_end - alloc_start);
2824 if (ret)
2825 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002826 } else {
2827 if (ordered)
2828 btrfs_put_ordered_extent(ordered);
2829 break;
2830 }
2831 }
2832
Qu Wenruo14524a82015-09-08 17:22:44 +08002833 /* First, check if we exceed the qgroup limit */
2834 INIT_LIST_HEAD(&reserve_list);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002835 while (1) {
2836 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2837 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002838 if (IS_ERR_OR_NULL(em)) {
2839 if (!em)
2840 ret = -ENOMEM;
2841 else
2842 ret = PTR_ERR(em);
2843 break;
2844 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002845 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002846 actual_end = min_t(u64, extent_map_end(em), offset + len);
Miao Xie797f4272012-11-28 10:28:07 +00002847 last_byte = ALIGN(last_byte, blocksize);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002848 if (em->block_start == EXTENT_MAP_HOLE ||
2849 (cur_offset >= inode->i_size &&
2850 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Qu Wenruo14524a82015-09-08 17:22:44 +08002851 ret = add_falloc_range(&reserve_list, cur_offset,
2852 last_byte - cur_offset);
2853 if (ret < 0) {
2854 free_extent_map(em);
2855 break;
Filipe Manana3d850dd2015-03-12 23:23:13 +00002856 }
Qu Wenruo14524a82015-09-08 17:22:44 +08002857 ret = btrfs_qgroup_reserve_data(inode, cur_offset,
2858 last_byte - cur_offset);
2859 if (ret < 0)
2860 break;
Wang Xiaoguang18513092016-07-25 15:51:40 +08002861 } else {
2862 /*
2863 * Do not need to reserve unwritten extent for this
2864 * range, free reserved data space first, otherwise
2865 * it'll result in false ENOSPC error.
2866 */
2867 btrfs_free_reserved_data_space(inode, cur_offset,
2868 last_byte - cur_offset);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002869 }
2870 free_extent_map(em);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002871 cur_offset = last_byte;
Qu Wenruo14524a82015-09-08 17:22:44 +08002872 if (cur_offset >= alloc_end)
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002873 break;
Qu Wenruo14524a82015-09-08 17:22:44 +08002874 }
2875
2876 /*
2877 * If ret is still 0, means we're OK to fallocate.
2878 * Or just cleanup the list and exit.
2879 */
2880 list_for_each_entry_safe(range, tmp, &reserve_list, list) {
2881 if (!ret)
2882 ret = btrfs_prealloc_file_range(inode, mode,
2883 range->start,
2884 range->len, 1 << inode->i_blkbits,
2885 offset + len, &alloc_hint);
Wang Xiaoguang18513092016-07-25 15:51:40 +08002886 else
2887 btrfs_free_reserved_data_space(inode, range->start,
2888 range->len);
Qu Wenruo14524a82015-09-08 17:22:44 +08002889 list_del(&range->list);
2890 kfree(range);
2891 }
2892 if (ret < 0)
2893 goto out_unlock;
2894
2895 if (actual_end > inode->i_size &&
2896 !(mode & FALLOC_FL_KEEP_SIZE)) {
2897 struct btrfs_trans_handle *trans;
2898 struct btrfs_root *root = BTRFS_I(inode)->root;
2899
2900 /*
2901 * We didn't need to allocate any more space, but we
2902 * still extended the size of the file so we need to
2903 * update i_size and the inode item.
2904 */
2905 trans = btrfs_start_transaction(root, 1);
2906 if (IS_ERR(trans)) {
2907 ret = PTR_ERR(trans);
2908 } else {
Deepa Dinamanic2050a42016-09-14 07:48:06 -07002909 inode->i_ctime = current_time(inode);
Qu Wenruo14524a82015-09-08 17:22:44 +08002910 i_size_write(inode, actual_end);
2911 btrfs_ordered_update_i_size(inode, actual_end, NULL);
2912 ret = btrfs_update_inode(trans, root, inode);
2913 if (ret)
2914 btrfs_end_transaction(trans, root);
2915 else
2916 ret = btrfs_end_transaction(trans, root);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002917 }
2918 }
Qu Wenruo14524a82015-09-08 17:22:44 +08002919out_unlock:
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002920 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
David Sterba32fc9322016-02-11 14:25:38 +01002921 &cached_state, GFP_KERNEL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002922out:
Al Viro59551022016-01-22 15:40:57 -05002923 inode_unlock(inode);
Chris Masond98456f2012-01-31 20:27:41 -05002924 /* Let go of our reservation. */
Wang Xiaoguang18513092016-07-25 15:51:40 +08002925 if (ret != 0)
2926 btrfs_free_reserved_data_space(inode, alloc_start,
2927 alloc_end - cur_offset);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002928 return ret;
2929}
2930
Andrew Morton965c8e52012-12-17 15:59:39 -08002931static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002932{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002934 struct extent_map *em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002935 struct extent_state *cached_state = NULL;
Liu Bo4d1a40c2014-09-16 17:49:30 +08002936 u64 lockstart;
2937 u64 lockend;
2938 u64 start;
2939 u64 len;
Josef Bacikb2675152011-07-18 13:21:36 -04002940 int ret = 0;
2941
Josef Bacikb2675152011-07-18 13:21:36 -04002942 if (inode->i_size == 0)
2943 return -ENXIO;
2944
Liu Bo4d1a40c2014-09-16 17:49:30 +08002945 /*
2946 * *offset can be negative, in this case we start finding DATA/HOLE from
2947 * the very start of the file.
2948 */
2949 start = max_t(loff_t, 0, *offset);
2950
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002951 lockstart = round_down(start, fs_info->sectorsize);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002952 lockend = round_up(i_size_read(inode),
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002953 fs_info->sectorsize);
Liu Bo4d1a40c2014-09-16 17:49:30 +08002954 if (lockend <= lockstart)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 lockend = lockstart + fs_info->sectorsize;
Liu Bo4d1a40c2014-09-16 17:49:30 +08002956 lockend--;
2957 len = lockend - lockstart + 1;
2958
David Sterbaff13db42015-12-03 14:30:40 +01002959 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002960 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002961
Josef Bacik7f4ca372013-10-18 11:44:46 -04002962 while (start < inode->i_size) {
Josef Bacikb2675152011-07-18 13:21:36 -04002963 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2964 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002965 ret = PTR_ERR(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002966 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002967 break;
2968 }
2969
Josef Bacik7f4ca372013-10-18 11:44:46 -04002970 if (whence == SEEK_HOLE &&
2971 (em->block_start == EXTENT_MAP_HOLE ||
2972 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2973 break;
2974 else if (whence == SEEK_DATA &&
2975 (em->block_start != EXTENT_MAP_HOLE &&
2976 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
2977 break;
Josef Bacikb2675152011-07-18 13:21:36 -04002978
2979 start = em->start + em->len;
Josef Bacikb2675152011-07-18 13:21:36 -04002980 free_extent_map(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04002981 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04002982 cond_resched();
2983 }
Josef Bacik7f4ca372013-10-18 11:44:46 -04002984 free_extent_map(em);
2985 if (!ret) {
2986 if (whence == SEEK_DATA && start >= inode->i_size)
2987 ret = -ENXIO;
2988 else
2989 *offset = min_t(loff_t, start, inode->i_size);
2990 }
Josef Bacikb2675152011-07-18 13:21:36 -04002991 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2992 &cached_state, GFP_NOFS);
2993 return ret;
2994}
2995
Andrew Morton965c8e52012-12-17 15:59:39 -08002996static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04002997{
2998 struct inode *inode = file->f_mapping->host;
2999 int ret;
3000
Al Viro59551022016-01-22 15:40:57 -05003001 inode_lock(inode);
Andrew Morton965c8e52012-12-17 15:59:39 -08003002 switch (whence) {
Josef Bacikb2675152011-07-18 13:21:36 -04003003 case SEEK_END:
3004 case SEEK_CUR:
Andrew Morton965c8e52012-12-17 15:59:39 -08003005 offset = generic_file_llseek(file, offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04003006 goto out;
3007 case SEEK_DATA:
3008 case SEEK_HOLE:
Jeff Liu48802c82011-09-18 10:34:02 -04003009 if (offset >= i_size_read(inode)) {
Al Viro59551022016-01-22 15:40:57 -05003010 inode_unlock(inode);
Jeff Liu48802c82011-09-18 10:34:02 -04003011 return -ENXIO;
3012 }
3013
Andrew Morton965c8e52012-12-17 15:59:39 -08003014 ret = find_desired_extent(inode, &offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04003015 if (ret) {
Al Viro59551022016-01-22 15:40:57 -05003016 inode_unlock(inode);
Josef Bacikb2675152011-07-18 13:21:36 -04003017 return ret;
3018 }
3019 }
3020
Jie Liu46a1c2c2013-06-25 12:02:13 +08003021 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Josef Bacikb2675152011-07-18 13:21:36 -04003022out:
Al Viro59551022016-01-22 15:40:57 -05003023 inode_unlock(inode);
Josef Bacikb2675152011-07-18 13:21:36 -04003024 return offset;
3025}
3026
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003027const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04003028 .llseek = btrfs_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -04003029 .read_iter = generic_file_read_iter,
Chris Masone9906a92007-12-14 12:56:58 -05003030 .splice_read = generic_file_splice_read,
Al Virob30ac0f2014-04-03 14:29:04 -04003031 .write_iter = btrfs_file_write_iter,
Chris Mason9ebefb182007-06-15 13:50:00 -04003032 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04003033 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04003034 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003035 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003036 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003037 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003038#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +00003039 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003040#endif
Zach Brown3db11b22015-11-10 16:53:32 -05003041 .copy_file_range = btrfs_copy_file_range,
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003042 .clone_file_range = btrfs_clone_file_range,
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003043 .dedupe_file_range = btrfs_dedupe_file_range,
Chris Mason39279cc2007-06-12 06:35:45 -04003044};
Miao Xie9247f312012-11-26 09:24:43 +00003045
3046void btrfs_auto_defrag_exit(void)
3047{
Kinglong Mee5598e902016-01-29 21:36:35 +08003048 kmem_cache_destroy(btrfs_inode_defrag_cachep);
Miao Xie9247f312012-11-26 09:24:43 +00003049}
3050
3051int btrfs_auto_defrag_init(void)
3052{
3053 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
3054 sizeof(struct inode_defrag), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03003055 SLAB_MEM_SPREAD,
Miao Xie9247f312012-11-26 09:24:43 +00003056 NULL);
3057 if (!btrfs_inode_defrag_cachep)
3058 return -ENOMEM;
3059
3060 return 0;
3061}
Filipe Manana728404d2014-10-10 09:43:11 +01003062
3063int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)
3064{
3065 int ret;
3066
3067 /*
3068 * So with compression we will find and lock a dirty page and clear the
3069 * first one as dirty, setup an async extent, and immediately return
3070 * with the entire range locked but with nobody actually marked with
3071 * writeback. So we can't just filemap_write_and_wait_range() and
3072 * expect it to work since it will just kick off a thread to do the
3073 * actual work. So we need to call filemap_fdatawrite_range _again_
3074 * since it will wait on the page lock, which won't be unlocked until
3075 * after the pages have been marked as writeback and so we're good to go
3076 * from there. We have to do this otherwise we'll miss the ordered
3077 * extents and that results in badness. Please Josef, do not think you
3078 * know better and pull this out at some point in the future, it is
3079 * right and you are wrong.
3080 */
3081 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
3082 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
3083 &BTRFS_I(inode)->runtime_flags))
3084 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
3085
3086 return ret;
3087}