blob: 380054c94e4b6a1cb2502f06998acfc015806db3 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason39279cc2007-06-12 06:35:45 -04006#include <linux/fs.h>
7#include <linux/pagemap.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/time.h>
9#include <linux/init.h>
10#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040011#include <linux/backing-dev.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010012#include <linux/falloc.h>
Chris Mason39279cc2007-06-12 06:35:45 -040013#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040014#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000016#include <linux/btrfs.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080017#include <linux/uio.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050018#include <linux/iversion.h>
Boris Burkov14605402021-06-30 13:01:49 -070019#include <linux/fsverity.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include "ctree.h"
21#include "disk-io.h"
22#include "transaction.h"
23#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040024#include "print-tree.h"
Chris Masone02119d2008-09-05 16:13:11 -040025#include "tree-log.h"
26#include "locking.h"
Josef Bacik2aaa6652012-08-29 14:27:18 -040027#include "volumes.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070028#include "qgroup.h"
Anand Jainebb87652016-03-10 17:26:59 +080029#include "compression.h"
Josef Bacik86736342019-06-19 15:12:00 -040030#include "delalloc-space.h"
Filipe Manana6a177382020-02-28 13:04:17 +000031#include "reflink.h"
Qu Wenruof02a85d2021-05-31 16:50:41 +080032#include "subpage.h"
Chris Mason39279cc2007-06-12 06:35:45 -040033
Miao Xie9247f312012-11-26 09:24:43 +000034static struct kmem_cache *btrfs_inode_defrag_cachep;
Chris Mason4cb53002011-05-24 15:35:30 -040035/*
36 * when auto defrag is enabled we
37 * queue up these defrag structs to remember which
38 * inodes need defragging passes
39 */
40struct inode_defrag {
41 struct rb_node rb_node;
42 /* objectid */
43 u64 ino;
44 /*
45 * transid where the defrag was added, we search for
46 * extents newer than this
47 */
48 u64 transid;
49
50 /* root objectid */
51 u64 root;
Qu Wenruo558732d2022-02-13 15:42:33 +080052
53 /*
54 * The extent size threshold for autodefrag.
55 *
56 * This value is different for compressed/non-compressed extents,
57 * thus needs to be passed from higher layer.
58 * (aka, inode_should_defrag())
59 */
60 u32 extent_thresh;
Chris Mason4cb53002011-05-24 15:35:30 -040061};
62
Miao Xie762f2262012-05-24 18:58:27 +080063static int __compare_inode_defrag(struct inode_defrag *defrag1,
64 struct inode_defrag *defrag2)
65{
66 if (defrag1->root > defrag2->root)
67 return 1;
68 else if (defrag1->root < defrag2->root)
69 return -1;
70 else if (defrag1->ino > defrag2->ino)
71 return 1;
72 else if (defrag1->ino < defrag2->ino)
73 return -1;
74 else
75 return 0;
76}
77
Chris Mason4cb53002011-05-24 15:35:30 -040078/* pop a record for an inode into the defrag tree. The lock
79 * must be held already
80 *
81 * If you're inserting a record for an older transid than an
82 * existing record, the transid already in the tree is lowered
83 *
84 * If an existing record is found the defrag item you
85 * pass in is freed
86 */
Nikolay Borisov6158e1c2017-02-20 13:50:43 +020087static int __btrfs_add_inode_defrag(struct btrfs_inode *inode,
Chris Mason4cb53002011-05-24 15:35:30 -040088 struct inode_defrag *defrag)
89{
David Sterba3ffbd682018-06-29 10:56:42 +020090 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason4cb53002011-05-24 15:35:30 -040091 struct inode_defrag *entry;
92 struct rb_node **p;
93 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +080094 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -040095
Jeff Mahoney0b246af2016-06-22 18:54:23 -040096 p = &fs_info->defrag_inodes.rb_node;
Chris Mason4cb53002011-05-24 15:35:30 -040097 while (*p) {
98 parent = *p;
99 entry = rb_entry(parent, struct inode_defrag, rb_node);
100
Miao Xie762f2262012-05-24 18:58:27 +0800101 ret = __compare_inode_defrag(defrag, entry);
102 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400103 p = &parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800104 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400105 p = &parent->rb_right;
106 else {
107 /* if we're reinserting an entry for
108 * an old defrag run, make sure to
109 * lower the transid of our existing record
110 */
111 if (defrag->transid < entry->transid)
112 entry->transid = defrag->transid;
Qu Wenruo558732d2022-02-13 15:42:33 +0800113 entry->extent_thresh = min(defrag->extent_thresh,
114 entry->extent_thresh);
Miao Xie8ddc4732012-11-26 09:25:38 +0000115 return -EEXIST;
Chris Mason4cb53002011-05-24 15:35:30 -0400116 }
117 }
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200118 set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags);
Chris Mason4cb53002011-05-24 15:35:30 -0400119 rb_link_node(&defrag->rb_node, parent, p);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400120 rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes);
Miao Xie8ddc4732012-11-26 09:25:38 +0000121 return 0;
122}
Chris Mason4cb53002011-05-24 15:35:30 -0400123
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400124static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
Miao Xie8ddc4732012-11-26 09:25:38 +0000125{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400126 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
Miao Xie8ddc4732012-11-26 09:25:38 +0000127 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400128
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400129 if (btrfs_fs_closing(fs_info))
Miao Xie8ddc4732012-11-26 09:25:38 +0000130 return 0;
131
132 return 1;
Chris Mason4cb53002011-05-24 15:35:30 -0400133}
134
135/*
136 * insert a defrag record for this inode if auto defrag is
137 * enabled
138 */
139int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
Qu Wenruo558732d2022-02-13 15:42:33 +0800140 struct btrfs_inode *inode, u32 extent_thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400141{
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200142 struct btrfs_root *root = inode->root;
David Sterba3ffbd682018-06-29 10:56:42 +0200143 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason4cb53002011-05-24 15:35:30 -0400144 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400145 u64 transid;
Miao Xie8ddc4732012-11-26 09:25:38 +0000146 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400147
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400148 if (!__need_auto_defrag(fs_info))
Chris Mason4cb53002011-05-24 15:35:30 -0400149 return 0;
150
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200151 if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags))
Chris Mason4cb53002011-05-24 15:35:30 -0400152 return 0;
153
154 if (trans)
155 transid = trans->transid;
156 else
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200157 transid = inode->root->last_trans;
Chris Mason4cb53002011-05-24 15:35:30 -0400158
Miao Xie9247f312012-11-26 09:24:43 +0000159 defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -0400160 if (!defrag)
161 return -ENOMEM;
162
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200163 defrag->ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400164 defrag->transid = transid;
165 defrag->root = root->root_key.objectid;
Qu Wenruo558732d2022-02-13 15:42:33 +0800166 defrag->extent_thresh = extent_thresh;
Chris Mason4cb53002011-05-24 15:35:30 -0400167
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400168 spin_lock(&fs_info->defrag_inodes_lock);
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200169 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) {
Miao Xie8ddc4732012-11-26 09:25:38 +0000170 /*
171 * If we set IN_DEFRAG flag and evict the inode from memory,
172 * and then re-read this inode, this new inode doesn't have
173 * IN_DEFRAG flag. At the case, we may find the existed defrag.
174 */
175 ret = __btrfs_add_inode_defrag(inode, defrag);
176 if (ret)
177 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
178 } else {
Miao Xie9247f312012-11-26 09:24:43 +0000179 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
Miao Xie8ddc4732012-11-26 09:25:38 +0000180 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400181 spin_unlock(&fs_info->defrag_inodes_lock);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000182 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400183}
184
185/*
Miao Xie26176e72012-11-26 09:26:20 +0000186 * pick the defragable inode that we want, if it doesn't exist, we will get
187 * the next one.
Chris Mason4cb53002011-05-24 15:35:30 -0400188 */
Miao Xie26176e72012-11-26 09:26:20 +0000189static struct inode_defrag *
190btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400191{
192 struct inode_defrag *entry = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800193 struct inode_defrag tmp;
Chris Mason4cb53002011-05-24 15:35:30 -0400194 struct rb_node *p;
195 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800196 int ret;
197
198 tmp.ino = ino;
199 tmp.root = root;
Chris Mason4cb53002011-05-24 15:35:30 -0400200
Miao Xie26176e72012-11-26 09:26:20 +0000201 spin_lock(&fs_info->defrag_inodes_lock);
202 p = fs_info->defrag_inodes.rb_node;
Chris Mason4cb53002011-05-24 15:35:30 -0400203 while (p) {
204 parent = p;
205 entry = rb_entry(parent, struct inode_defrag, rb_node);
206
Miao Xie762f2262012-05-24 18:58:27 +0800207 ret = __compare_inode_defrag(&tmp, entry);
208 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400209 p = parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800210 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400211 p = parent->rb_right;
212 else
Miao Xie26176e72012-11-26 09:26:20 +0000213 goto out;
Chris Mason4cb53002011-05-24 15:35:30 -0400214 }
215
Miao Xie26176e72012-11-26 09:26:20 +0000216 if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
217 parent = rb_next(parent);
218 if (parent)
Chris Mason4cb53002011-05-24 15:35:30 -0400219 entry = rb_entry(parent, struct inode_defrag, rb_node);
Miao Xie26176e72012-11-26 09:26:20 +0000220 else
221 entry = NULL;
Chris Mason4cb53002011-05-24 15:35:30 -0400222 }
Miao Xie26176e72012-11-26 09:26:20 +0000223out:
224 if (entry)
225 rb_erase(parent, &fs_info->defrag_inodes);
226 spin_unlock(&fs_info->defrag_inodes_lock);
227 return entry;
228}
229
230void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
231{
232 struct inode_defrag *defrag;
233 struct rb_node *node;
234
235 spin_lock(&fs_info->defrag_inodes_lock);
236 node = rb_first(&fs_info->defrag_inodes);
237 while (node) {
238 rb_erase(node, &fs_info->defrag_inodes);
239 defrag = rb_entry(node, struct inode_defrag, rb_node);
240 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
241
David Sterba351810c2015-01-08 15:20:54 +0100242 cond_resched_lock(&fs_info->defrag_inodes_lock);
Miao Xie26176e72012-11-26 09:26:20 +0000243
244 node = rb_first(&fs_info->defrag_inodes);
245 }
246 spin_unlock(&fs_info->defrag_inodes_lock);
247}
248
249#define BTRFS_DEFRAG_BATCH 1024
250
251static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
252 struct inode_defrag *defrag)
253{
254 struct btrfs_root *inode_root;
255 struct inode *inode;
Miao Xie26176e72012-11-26 09:26:20 +0000256 struct btrfs_ioctl_defrag_range_args range;
Qu Wenruo26fbac22022-02-22 18:20:59 +0100257 int ret = 0;
258 u64 cur = 0;
259
260again:
261 if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
262 goto cleanup;
263 if (!__need_auto_defrag(fs_info))
264 goto cleanup;
Miao Xie26176e72012-11-26 09:26:20 +0000265
266 /* get the inode */
David Sterba56e93572020-05-15 19:35:55 +0200267 inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
Miao Xie26176e72012-11-26 09:26:20 +0000268 if (IS_ERR(inode_root)) {
Liu Bo6f1c3602013-01-29 03:22:10 +0000269 ret = PTR_ERR(inode_root);
270 goto cleanup;
271 }
Miao Xie26176e72012-11-26 09:26:20 +0000272
David Sterba0202e832020-05-15 19:35:59 +0200273 inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
Josef Bacik00246522020-01-24 09:33:01 -0500274 btrfs_put_root(inode_root);
Miao Xie26176e72012-11-26 09:26:20 +0000275 if (IS_ERR(inode)) {
Liu Bo6f1c3602013-01-29 03:22:10 +0000276 ret = PTR_ERR(inode);
277 goto cleanup;
Miao Xie26176e72012-11-26 09:26:20 +0000278 }
279
Qu Wenruo26fbac22022-02-22 18:20:59 +0100280 if (cur >= i_size_read(inode)) {
281 iput(inode);
282 goto cleanup;
283 }
284
Miao Xie26176e72012-11-26 09:26:20 +0000285 /* do a chunk of defrag */
286 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
287 memset(&range, 0, sizeof(range));
288 range.len = (u64)-1;
Qu Wenruo26fbac22022-02-22 18:20:59 +0100289 range.start = cur;
Qu Wenruo558732d2022-02-13 15:42:33 +0800290 range.extent_thresh = defrag->extent_thresh;
Miao Xieb66f00d2012-11-26 09:27:29 +0000291
292 sb_start_write(fs_info->sb);
Qu Wenruo26fbac22022-02-22 18:20:59 +0100293 ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
Miao Xie26176e72012-11-26 09:26:20 +0000294 BTRFS_DEFRAG_BATCH);
Miao Xieb66f00d2012-11-26 09:27:29 +0000295 sb_end_write(fs_info->sb);
Miao Xie26176e72012-11-26 09:26:20 +0000296 iput(inode);
Qu Wenruo26fbac22022-02-22 18:20:59 +0100297
298 if (ret < 0)
299 goto cleanup;
300
301 cur = max(cur + fs_info->sectorsize, range.start);
302 goto again;
303
Liu Bo6f1c3602013-01-29 03:22:10 +0000304cleanup:
Liu Bo6f1c3602013-01-29 03:22:10 +0000305 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
306 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400307}
308
309/*
310 * run through the list of inodes in the FS that need
311 * defragging
312 */
313int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
314{
315 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400316 u64 first_ino = 0;
Miao Xie762f2262012-05-24 18:58:27 +0800317 u64 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400318
319 atomic_inc(&fs_info->defrag_running);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530320 while (1) {
Miao Xiedc81cdc2013-02-20 23:32:52 -0700321 /* Pause the auto defragger. */
322 if (test_bit(BTRFS_FS_STATE_REMOUNTING,
323 &fs_info->fs_state))
324 break;
325
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400326 if (!__need_auto_defrag(fs_info))
Miao Xie26176e72012-11-26 09:26:20 +0000327 break;
Chris Mason4cb53002011-05-24 15:35:30 -0400328
329 /* find an inode to defrag */
Miao Xie26176e72012-11-26 09:26:20 +0000330 defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
331 first_ino);
Chris Mason4cb53002011-05-24 15:35:30 -0400332 if (!defrag) {
Miao Xie26176e72012-11-26 09:26:20 +0000333 if (root_objectid || first_ino) {
Miao Xie762f2262012-05-24 18:58:27 +0800334 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400335 first_ino = 0;
336 continue;
337 } else {
338 break;
339 }
340 }
341
Chris Mason4cb53002011-05-24 15:35:30 -0400342 first_ino = defrag->ino + 1;
Miao Xie762f2262012-05-24 18:58:27 +0800343 root_objectid = defrag->root;
Chris Mason4cb53002011-05-24 15:35:30 -0400344
Miao Xie26176e72012-11-26 09:26:20 +0000345 __btrfs_run_defrag_inode(fs_info, defrag);
Chris Mason4cb53002011-05-24 15:35:30 -0400346 }
Chris Mason4cb53002011-05-24 15:35:30 -0400347 atomic_dec(&fs_info->defrag_running);
348
349 /*
350 * during unmount, we use the transaction_wait queue to
351 * wait for the defragger to stop
352 */
353 wake_up(&fs_info->transaction_wait);
354 return 0;
355}
Chris Mason39279cc2007-06-12 06:35:45 -0400356
Chris Masond352ac62008-09-29 15:18:18 -0400357/* simple helper to fault in pages and copy. This should go away
358 * and be replaced with calls into generic code.
359 */
Zhao Leiee22f0c2016-01-06 18:47:31 +0800360static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
Chris Masona1b32a52008-09-05 16:09:51 -0400361 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -0400362 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -0400363{
Xin Zhong914ee292010-12-09 09:30:14 +0000364 size_t copied = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -0500365 size_t total_copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400366 int pg = 0;
Johannes Thumshirn70730172018-12-05 15:23:03 +0100367 int offset = offset_in_page(pos);
Chris Mason39279cc2007-06-12 06:35:45 -0400368
Josef Bacik11c65dc2010-05-23 11:07:21 -0400369 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400370 size_t count = min_t(size_t,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300371 PAGE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400372 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +0000373 /*
374 * Copy data from userspace to the current page
Xin Zhong914ee292010-12-09 09:30:14 +0000375 */
Al Virof0b65f32021-04-30 10:26:41 -0400376 copied = copy_page_from_iter_atomic(page, offset, count, i);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400377
Chris Mason39279cc2007-06-12 06:35:45 -0400378 /* Flush processor's dcache for this page */
379 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -0500380
381 /*
382 * if we get a partial write, we can end up with
383 * partially up to date pages. These add
384 * a lot of complexity, so make sure they don't
385 * happen by forcing this copy to be retried.
386 *
387 * The rest of the btrfs_file_write code will fall
388 * back to page at a time copies after we return 0.
389 */
Al Virof0b65f32021-04-30 10:26:41 -0400390 if (unlikely(copied < count)) {
391 if (!PageUptodate(page)) {
392 iov_iter_revert(i, copied);
393 copied = 0;
394 }
395 if (!copied)
396 break;
397 }
Chris Mason31339ac2011-03-07 11:10:24 -0500398
Josef Bacik11c65dc2010-05-23 11:07:21 -0400399 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +0000400 total_copied += copied;
Al Virof0b65f32021-04-30 10:26:41 -0400401 offset += copied;
402 if (offset == PAGE_SIZE) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400403 pg++;
404 offset = 0;
405 }
Chris Mason39279cc2007-06-12 06:35:45 -0400406 }
Xin Zhong914ee292010-12-09 09:30:14 +0000407 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400408}
409
Chris Masond352ac62008-09-29 15:18:18 -0400410/*
411 * unlocks pages after btrfs_file_write is done with them
412 */
Qu Wenruoe4f94342021-09-27 15:21:49 +0800413static void btrfs_drop_pages(struct btrfs_fs_info *fs_info,
414 struct page **pages, size_t num_pages,
415 u64 pos, u64 copied)
Chris Mason39279cc2007-06-12 06:35:45 -0400416{
417 size_t i;
Qu Wenruoe4f94342021-09-27 15:21:49 +0800418 u64 block_start = round_down(pos, fs_info->sectorsize);
419 u64 block_len = round_up(pos + copied, fs_info->sectorsize) - block_start;
420
421 ASSERT(block_len <= U32_MAX);
Chris Mason39279cc2007-06-12 06:35:45 -0400422 for (i = 0; i < num_pages; i++) {
Chris Masond352ac62008-09-29 15:18:18 -0400423 /* page checked is some magic around finding pages that
424 * have been modified without going through btrfs_set_page_dirty
Mel Gorman2457aec2014-06-04 16:10:31 -0700425 * clear it here. There should be no need to mark the pages
426 * accessed as prepare_pages should have marked them accessed
427 * in prepare_pages via find_or_create_page()
Chris Masond352ac62008-09-29 15:18:18 -0400428 */
Qu Wenruoe4f94342021-09-27 15:21:49 +0800429 btrfs_page_clamp_clear_checked(fs_info, pages[i], block_start,
430 block_len);
Chris Mason39279cc2007-06-12 06:35:45 -0400431 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300432 put_page(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400433 }
434}
435
Chris Masond352ac62008-09-29 15:18:18 -0400436/*
Qu Wenruoc0fab482021-01-06 09:01:42 +0800437 * After btrfs_copy_from_user(), update the following things for delalloc:
438 * - Mark newly dirtied pages as DELALLOC in the io tree.
439 * Used to advise which range is to be written back.
440 * - Mark modified pages as Uptodate/Dirty and not needing COW fixup
441 * - Update inode size for past EOF write
Chris Masond352ac62008-09-29 15:18:18 -0400442 */
Nikolay Borisov088545f2020-06-03 08:55:36 +0300443int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400444 size_t num_pages, loff_t pos, size_t write_bytes,
Goldwyn Rodriguesaa8c1a42020-10-14 09:55:45 -0500445 struct extent_state **cached, bool noreserve)
Chris Mason39279cc2007-06-12 06:35:45 -0400446{
Nikolay Borisov088545f2020-06-03 08:55:36 +0300447 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400448 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400449 int i;
Chris Masondb945352007-10-15 16:15:53 -0400450 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400451 u64 start_pos;
452 u64 end_of_last_block;
453 u64 end_pos = pos + write_bytes;
Nikolay Borisov088545f2020-06-03 08:55:36 +0300454 loff_t isize = i_size_read(&inode->vfs_inode);
Filipe Mananae3b8a482017-11-04 00:16:59 +0000455 unsigned int extra_bits = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400456
Goldwyn Rodriguesaa8c1a42020-10-14 09:55:45 -0500457 if (write_bytes == 0)
458 return 0;
459
460 if (noreserve)
461 extra_bits |= EXTENT_NORESERVE;
462
Goldwyn Rodrigues13f0dd82020-10-14 09:55:44 -0500463 start_pos = round_down(pos, fs_info->sectorsize);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400464 num_bytes = round_up(write_bytes + pos - start_pos,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400465 fs_info->sectorsize);
Qu Wenruof02a85d2021-05-31 16:50:41 +0800466 ASSERT(num_bytes <= U32_MAX);
Chris Mason39279cc2007-06-12 06:35:45 -0400467
Chris Masondb945352007-10-15 16:15:53 -0400468 end_of_last_block = start_pos + num_bytes - 1;
Filipe Mananae3b8a482017-11-04 00:16:59 +0000469
Chris Mason7703bdd2018-06-20 07:56:11 -0700470 /*
471 * The pages may have already been dirty, clear out old accounting so
472 * we can set things up properly
473 */
Nikolay Borisov088545f2020-06-03 08:55:36 +0300474 clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block,
Omar Sandovale1821632019-08-15 14:04:04 -0700475 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
476 0, 0, cached);
Chris Mason7703bdd2018-06-20 07:56:11 -0700477
Nikolay Borisov088545f2020-06-03 08:55:36 +0300478 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300479 extra_bits, cached);
Josef Bacikd0215f32011-01-25 14:57:24 -0500480 if (err)
481 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400482
Chris Masonc8b97812008-10-29 14:49:59 -0400483 for (i = 0; i < num_pages; i++) {
484 struct page *p = pages[i];
Qu Wenruof02a85d2021-05-31 16:50:41 +0800485
486 btrfs_page_clamp_set_uptodate(fs_info, p, start_pos, num_bytes);
Qu Wenruoe4f94342021-09-27 15:21:49 +0800487 btrfs_page_clamp_clear_checked(fs_info, p, start_pos, num_bytes);
Qu Wenruof02a85d2021-05-31 16:50:41 +0800488 btrfs_page_clamp_set_dirty(fs_info, p, start_pos, num_bytes);
Chris Masona52d9a82007-08-27 16:49:44 -0400489 }
Josef Bacik9f570b82011-01-25 12:42:37 -0500490
491 /*
492 * we've only changed i_size in ram, and we haven't updated
493 * the disk i_size. There is no need to log the inode
494 * at this time.
495 */
496 if (end_pos > isize)
Nikolay Borisov088545f2020-06-03 08:55:36 +0300497 i_size_write(&inode->vfs_inode, end_pos);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400498 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400499}
500
Chris Masond352ac62008-09-29 15:18:18 -0400501/*
502 * this drops all the extents in the cache that intersect the range
503 * [start, end]. Existing extents are split as required.
504 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200505void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
Josef Bacik7014cdb2012-08-30 20:06:49 -0400506 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400507{
508 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400509 struct extent_map *split = NULL;
510 struct extent_map *split2 = NULL;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200511 struct extent_map_tree *em_tree = &inode->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500512 u64 len = end - start + 1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400513 u64 gen;
Chris Mason3b951512008-04-17 11:29:12 -0400514 int ret;
515 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400516 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400517 int compressed = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400518 bool modified;
Chris Masona52d9a82007-08-27 16:49:44 -0400519
Chris Masone6dcd2d2008-07-17 12:53:50 -0400520 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400521 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500522 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400523 testend = 0;
524 }
Chris Masond3977122009-01-05 21:25:51 -0500525 while (1) {
Josef Bacik7014cdb2012-08-30 20:06:49 -0400526 int no_splits = 0;
527
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400528 modified = false;
Chris Mason3b951512008-04-17 11:29:12 -0400529 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200530 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400531 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200532 split2 = alloc_extent_map();
Josef Bacik7014cdb2012-08-30 20:06:49 -0400533 if (!split || !split2)
534 no_splits = 1;
Chris Mason3b951512008-04-17 11:29:12 -0400535
Chris Mason890871b2009-09-02 16:24:52 -0400536 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500537 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500538 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400539 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400540 break;
Chris Masond1310b22008-01-24 16:13:08 -0500541 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400542 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400543 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400544 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000545 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400546 free_extent_map(em);
Chris Masona1ed835e12009-09-11 12:27:37 -0400547 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400548 break;
549 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000550 start = em->start + em->len;
551 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400552 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400553 free_extent_map(em);
Chris Masona1ed835e12009-09-11 12:27:37 -0400554 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400555 continue;
556 }
Chris Masonc8b97812008-10-29 14:49:59 -0400557 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400558 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo3b277592013-03-15 08:46:39 -0600559 clear_bit(EXTENT_FLAG_LOGGING, &flags);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400560 modified = !list_empty(&em->list);
Josef Bacik7014cdb2012-08-30 20:06:49 -0400561 if (no_splits)
562 goto next;
Chris Mason3b951512008-04-17 11:29:12 -0400563
Josef Bacikee20a982013-07-11 10:34:59 -0400564 if (em->start < start) {
Chris Mason3b951512008-04-17 11:29:12 -0400565 split->start = em->start;
566 split->len = start - em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400567
Josef Bacikee20a982013-07-11 10:34:59 -0400568 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
569 split->orig_start = em->orig_start;
570 split->block_start = em->block_start;
571
572 if (compressed)
573 split->block_len = em->block_len;
574 else
575 split->block_len = split->len;
576 split->orig_block_len = max(split->block_len,
577 em->orig_block_len);
578 split->ram_bytes = em->ram_bytes;
579 } else {
580 split->orig_start = split->start;
581 split->block_len = 0;
582 split->block_start = em->block_start;
583 split->orig_block_len = 0;
584 split->ram_bytes = split->len;
585 }
586
Josef Bacik5dc562c2012-08-17 13:14:17 -0400587 split->generation = gen;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400588 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800589 split->compress_type = em->compress_type;
Filipe Manana176840b2014-02-25 14:15:13 +0000590 replace_extent_mapping(em_tree, em, split, modified);
Chris Mason3b951512008-04-17 11:29:12 -0400591 free_extent_map(split);
592 split = split2;
593 split2 = NULL;
594 }
Josef Bacikee20a982013-07-11 10:34:59 -0400595 if (testend && em->start + em->len > start + len) {
Chris Mason3b951512008-04-17 11:29:12 -0400596 u64 diff = start + len - em->start;
597
598 split->start = start + len;
599 split->len = em->start + em->len - (start + len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400600 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800601 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400602 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400603
Josef Bacikee20a982013-07-11 10:34:59 -0400604 if (em->block_start < EXTENT_MAP_LAST_BYTE) {
605 split->orig_block_len = max(em->block_len,
606 em->orig_block_len);
607
608 split->ram_bytes = em->ram_bytes;
609 if (compressed) {
610 split->block_len = em->block_len;
611 split->block_start = em->block_start;
612 split->orig_start = em->orig_start;
613 } else {
614 split->block_len = split->len;
615 split->block_start = em->block_start
616 + diff;
617 split->orig_start = em->orig_start;
618 }
Chris Masonc8b97812008-10-29 14:49:59 -0400619 } else {
Josef Bacikee20a982013-07-11 10:34:59 -0400620 split->ram_bytes = split->len;
621 split->orig_start = split->start;
622 split->block_len = 0;
623 split->block_start = em->block_start;
624 split->orig_block_len = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400625 }
Chris Mason3b951512008-04-17 11:29:12 -0400626
Filipe Manana176840b2014-02-25 14:15:13 +0000627 if (extent_map_in_tree(em)) {
628 replace_extent_mapping(em_tree, em, split,
629 modified);
630 } else {
631 ret = add_extent_mapping(em_tree, split,
632 modified);
633 ASSERT(ret == 0); /* Logic error */
634 }
Chris Mason3b951512008-04-17 11:29:12 -0400635 free_extent_map(split);
636 split = NULL;
637 }
Josef Bacik7014cdb2012-08-30 20:06:49 -0400638next:
Filipe Manana176840b2014-02-25 14:15:13 +0000639 if (extent_map_in_tree(em))
640 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400641 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500642
Chris Masona52d9a82007-08-27 16:49:44 -0400643 /* once for us */
644 free_extent_map(em);
645 /* once for the tree*/
646 free_extent_map(em);
647 }
Chris Mason3b951512008-04-17 11:29:12 -0400648 if (split)
649 free_extent_map(split);
650 if (split2)
651 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400652}
653
Chris Mason39279cc2007-06-12 06:35:45 -0400654/*
655 * this is very complex, but the basic idea is to drop all extents
656 * in the range start - end. hint_block is filled in with a block number
657 * that would be a good hint to the block allocator for this file.
658 *
659 * If an extent intersects the range but is not entirely inside the range
660 * it is either truncated or split. Anything entirely inside the range
661 * is deleted from the tree.
Filipe Manana2766ff62020-11-04 11:07:34 +0000662 *
663 * Note: the VFS' inode number of bytes is not updated, it's up to the caller
664 * to deal with that. We set the field 'bytes_found' of the arguments structure
665 * with the number of allocated bytes found in the target range, so that the
666 * caller can update the inode's number of bytes in an atomic way when
667 * replacing extents in a range to avoid races with stat(2).
Chris Mason39279cc2007-06-12 06:35:45 -0400668 */
Filipe Manana5893dfb2020-11-04 11:07:32 +0000669int btrfs_drop_extents(struct btrfs_trans_handle *trans,
670 struct btrfs_root *root, struct btrfs_inode *inode,
671 struct btrfs_drop_extents_args *args)
Chris Mason39279cc2007-06-12 06:35:45 -0400672{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400673 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason00f5c792007-11-30 10:09:33 -0500674 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000675 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +0800676 struct btrfs_ref ref = { 0 };
Chris Mason00f5c792007-11-30 10:09:33 -0500677 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000678 struct btrfs_key new_key;
Nikolay Borisov906c4482020-06-03 08:55:08 +0300679 u64 ino = btrfs_ino(inode);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000680 u64 search_start = args->start;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000681 u64 disk_bytenr = 0;
682 u64 num_bytes = 0;
683 u64 extent_offset = 0;
684 u64 extent_end = 0;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000685 u64 last_end = args->start;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000686 int del_nr = 0;
687 int del_slot = 0;
688 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400689 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500690 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400691 int modify_tree = -1;
Miao Xie27cdeb72014-04-02 19:51:05 +0800692 int update_refs;
Josef Bacikc3308f82012-09-14 14:51:22 -0400693 int found = 0;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000694 struct btrfs_path *path = args->path;
Chris Mason39279cc2007-06-12 06:35:45 -0400695
Filipe Manana2766ff62020-11-04 11:07:34 +0000696 args->bytes_found = 0;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000697 args->extent_inserted = false;
Chris Masona52d9a82007-08-27 16:49:44 -0400698
Filipe Manana5893dfb2020-11-04 11:07:32 +0000699 /* Must always have a path if ->replace_extent is true */
700 ASSERT(!(args->replace_extent && !args->path));
701
702 if (!path) {
703 path = btrfs_alloc_path();
704 if (!path) {
705 ret = -ENOMEM;
706 goto out;
707 }
708 }
709
710 if (args->drop_cache)
711 btrfs_drop_extent_cache(inode, args->start, args->end - 1, 0);
712
713 if (args->start >= inode->disk_i_size && !args->replace_extent)
Chris Masondc7fdde2012-04-27 14:31:29 -0400714 modify_tree = 0;
715
Josef Bacikd1752092021-10-01 13:57:18 -0400716 update_refs = (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID);
Chris Masond3977122009-01-05 21:25:51 -0500717 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400718 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800719 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400720 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400721 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000722 break;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000723 if (ret > 0 && path->slots[0] > 0 && search_start == args->start) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000724 leaf = path->nodes[0];
725 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800726 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000727 key.type == BTRFS_EXTENT_DATA_KEY)
728 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400729 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400730 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000731next_slot:
732 leaf = path->nodes[0];
733 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
734 BUG_ON(del_nr > 0);
735 ret = btrfs_next_leaf(root, path);
736 if (ret < 0)
737 break;
738 if (ret > 0) {
739 ret = 0;
740 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400741 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000742 leaf = path->nodes[0];
743 recow = 1;
744 }
745
746 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Filipe Mananaaeafbf82015-11-06 13:33:33 +0000747
748 if (key.objectid > ino)
749 break;
750 if (WARN_ON_ONCE(key.objectid < ino) ||
751 key.type < BTRFS_EXTENT_DATA_KEY) {
752 ASSERT(del_nr == 0);
753 path->slots[0]++;
754 goto next_slot;
755 }
Filipe Manana5893dfb2020-11-04 11:07:32 +0000756 if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= args->end)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000757 break;
758
759 fi = btrfs_item_ptr(leaf, path->slots[0],
760 struct btrfs_file_extent_item);
761 extent_type = btrfs_file_extent_type(leaf, fi);
762
763 if (extent_type == BTRFS_FILE_EXTENT_REG ||
764 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
765 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
766 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
767 extent_offset = btrfs_file_extent_offset(leaf, fi);
768 extent_end = key.offset +
769 btrfs_file_extent_num_bytes(leaf, fi);
770 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
771 extent_end = key.offset +
Qu Wenruoe41ca582018-06-06 15:41:49 +0800772 btrfs_file_extent_ram_bytes(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400773 } else {
Filipe Mananaaeafbf82015-11-06 13:33:33 +0000774 /* can't happen */
775 BUG();
Chris Mason39279cc2007-06-12 06:35:45 -0400776 }
777
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100778 /*
779 * Don't skip extent items representing 0 byte lengths. They
780 * used to be created (bug) if while punching holes we hit
781 * -ENOSPC condition. So if we find one here, just ensure we
782 * delete it, otherwise we would insert a new file extent item
783 * with the same key (offset) as that 0 bytes length file
784 * extent item in the call to setup_items_for_insert() later
785 * in this function.
786 */
Josef Bacik62fe51c12016-11-16 09:13:39 -0500787 if (extent_end == key.offset && extent_end >= search_start) {
788 last_end = extent_end;
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100789 goto delete_extent_item;
Josef Bacik62fe51c12016-11-16 09:13:39 -0500790 }
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100791
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000792 if (extent_end <= search_start) {
793 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400794 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400795 }
796
Josef Bacikc3308f82012-09-14 14:51:22 -0400797 found = 1;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000798 search_start = max(key.offset, args->start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400799 if (recow || !modify_tree) {
800 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200801 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000802 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400803 }
Chris Mason771ed682008-11-06 22:02:51 -0500804
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000805 /*
806 * | - range to drop - |
807 * | -------- extent -------- |
808 */
Filipe Manana5893dfb2020-11-04 11:07:32 +0000809 if (args->start > key.offset && args->end < extent_end) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000810 BUG_ON(del_nr > 0);
Liu Bo00fdf132014-03-10 18:56:07 +0800811 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200812 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800813 break;
814 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000815
816 memcpy(&new_key, &key, sizeof(new_key));
Filipe Manana5893dfb2020-11-04 11:07:32 +0000817 new_key.offset = args->start;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000818 ret = btrfs_duplicate_item(trans, root, path,
819 &new_key);
820 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200821 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000822 continue;
823 }
824 if (ret < 0)
825 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400826
Chris Mason5f39d392007-10-15 16:14:19 -0400827 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000828 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
829 struct btrfs_file_extent_item);
830 btrfs_set_file_extent_num_bytes(leaf, fi,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000831 args->start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400832
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000833 fi = btrfs_item_ptr(leaf, path->slots[0],
834 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400835
Filipe Manana5893dfb2020-11-04 11:07:32 +0000836 extent_offset += args->start - key.offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000837 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
838 btrfs_set_file_extent_num_bytes(leaf, fi,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000839 extent_end - args->start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000840 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400841
Josef Bacik5dc562c2012-08-17 13:14:17 -0400842 if (update_refs && disk_bytenr > 0) {
Qu Wenruo82fa1132019-04-04 14:45:35 +0800843 btrfs_init_generic_ref(&ref,
844 BTRFS_ADD_DELAYED_REF,
845 disk_bytenr, num_bytes, 0);
846 btrfs_init_data_ref(&ref,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000847 root->root_key.objectid,
848 new_key.objectid,
Nikolay Borisovf42c5da2021-10-12 11:21:35 +0300849 args->start - extent_offset,
850 0, false);
Qu Wenruo82fa1132019-04-04 14:45:35 +0800851 ret = btrfs_inc_extent_ref(trans, &ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100852 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400853 }
Filipe Manana5893dfb2020-11-04 11:07:32 +0000854 key.offset = args->start;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000855 }
856 /*
Josef Bacik62fe51c12016-11-16 09:13:39 -0500857 * From here on out we will have actually dropped something, so
858 * last_end can be updated.
859 */
860 last_end = extent_end;
861
862 /*
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000863 * | ---- range to drop ----- |
864 * | -------- extent -------- |
865 */
Filipe Manana5893dfb2020-11-04 11:07:32 +0000866 if (args->start <= key.offset && args->end < extent_end) {
Liu Bo00fdf132014-03-10 18:56:07 +0800867 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200868 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800869 break;
870 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000871
872 memcpy(&new_key, &key, sizeof(new_key));
Filipe Manana5893dfb2020-11-04 11:07:32 +0000873 new_key.offset = args->end;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400874 btrfs_set_item_key_safe(fs_info, path, &new_key);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000875
Filipe Manana5893dfb2020-11-04 11:07:32 +0000876 extent_offset += args->end - key.offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000877 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
878 btrfs_set_file_extent_num_bytes(leaf, fi,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000879 extent_end - args->end);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000880 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400881 if (update_refs && disk_bytenr > 0)
Filipe Manana2766ff62020-11-04 11:07:34 +0000882 args->bytes_found += args->end - key.offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000883 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400884 }
885
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000886 search_start = extent_end;
887 /*
888 * | ---- range to drop ----- |
889 * | -------- extent -------- |
890 */
Filipe Manana5893dfb2020-11-04 11:07:32 +0000891 if (args->start > key.offset && args->end >= extent_end) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000892 BUG_ON(del_nr > 0);
Liu Bo00fdf132014-03-10 18:56:07 +0800893 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
David Sterba3f9e3df2014-04-15 18:50:17 +0200894 ret = -EOPNOTSUPP;
Liu Bo00fdf132014-03-10 18:56:07 +0800895 break;
896 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000897
898 btrfs_set_file_extent_num_bytes(leaf, fi,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000899 args->start - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000900 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400901 if (update_refs && disk_bytenr > 0)
Filipe Manana2766ff62020-11-04 11:07:34 +0000902 args->bytes_found += extent_end - args->start;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000903 if (args->end == extent_end)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000904 break;
905
906 path->slots[0]++;
907 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400908 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000909
910 /*
911 * | ---- range to drop ----- |
912 * | ------ extent ------ |
913 */
Filipe Manana5893dfb2020-11-04 11:07:32 +0000914 if (args->start <= key.offset && args->end >= extent_end) {
Filipe Mananafc19c5e2014-04-29 13:18:40 +0100915delete_extent_item:
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000916 if (del_nr == 0) {
917 del_slot = path->slots[0];
918 del_nr = 1;
919 } else {
920 BUG_ON(del_slot + del_nr != path->slots[0]);
921 del_nr++;
922 }
923
Josef Bacik5dc562c2012-08-17 13:14:17 -0400924 if (update_refs &&
925 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Filipe Manana2766ff62020-11-04 11:07:34 +0000926 args->bytes_found += extent_end - key.offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000927 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400928 fs_info->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400929 } else if (update_refs && disk_bytenr > 0) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +0800930 btrfs_init_generic_ref(&ref,
931 BTRFS_DROP_DELAYED_REF,
932 disk_bytenr, num_bytes, 0);
933 btrfs_init_data_ref(&ref,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000934 root->root_key.objectid,
Qu Wenruoffd4bb22019-04-04 14:45:36 +0800935 key.objectid,
Nikolay Borisovf42c5da2021-10-12 11:21:35 +0300936 key.offset - extent_offset, 0,
937 false);
Qu Wenruoffd4bb22019-04-04 14:45:36 +0800938 ret = btrfs_free_extent(trans, &ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100939 BUG_ON(ret); /* -ENOMEM */
Filipe Manana2766ff62020-11-04 11:07:34 +0000940 args->bytes_found += extent_end - key.offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000941 }
942
Filipe Manana5893dfb2020-11-04 11:07:32 +0000943 if (args->end == extent_end)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000944 break;
945
946 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
947 path->slots[0]++;
948 goto next_slot;
949 }
950
951 ret = btrfs_del_items(trans, root, path, del_slot,
952 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100953 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400954 btrfs_abort_transaction(trans, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400955 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100956 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000957
958 del_nr = 0;
959 del_slot = 0;
960
David Sterbab3b4aa72011-04-21 01:20:15 +0200961 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000962 continue;
963 }
964
Arnd Bergmann290342f2019-03-25 14:02:25 +0100965 BUG();
Chris Mason39279cc2007-06-12 06:35:45 -0400966 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000967
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100968 if (!ret && del_nr > 0) {
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000969 /*
970 * Set path->slots[0] to first slot, so that after the delete
971 * if items are move off from our leaf to its immediate left or
972 * right neighbor leafs, we end up with a correct and adjusted
Filipe Manana5893dfb2020-11-04 11:07:32 +0000973 * path->slots[0] for our insertion (if args->replace_extent).
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000974 */
975 path->slots[0] = del_slot;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000976 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400978 btrfs_abort_transaction(trans, ret);
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000979 }
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000980
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000981 leaf = path->nodes[0];
982 /*
983 * If btrfs_del_items() was called, it might have deleted a leaf, in
984 * which case it unlocked our path, so check path->locks[0] matches a
985 * write lock.
986 */
Filipe Manana7ecb4c32022-02-03 14:55:48 +0000987 if (!ret && args->replace_extent &&
Josef Bacikac5887c2020-08-20 11:46:10 -0400988 path->locks[0] == BTRFS_WRITE_LOCK &&
David Sterbae902baa2019-03-20 14:36:46 +0100989 btrfs_leaf_free_space(leaf) >=
Filipe Manana5893dfb2020-11-04 11:07:32 +0000990 sizeof(struct btrfs_item) + args->extent_item_size) {
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000991
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000992 key.objectid = ino;
993 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana5893dfb2020-11-04 11:07:32 +0000994 key.offset = args->start;
Filipe David Borba Mananad5f37522014-02-09 23:45:12 +0000995 if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
996 struct btrfs_key slot_key;
997
998 btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
999 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
1000 path->slots[0]++;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001001 }
Filipe Mananaf0641652021-09-24 12:28:14 +01001002 btrfs_setup_item_for_insert(root, path, &key, args->extent_item_size);
Filipe Manana5893dfb2020-11-04 11:07:32 +00001003 args->extent_inserted = true;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001004 }
1005
Filipe Manana5893dfb2020-11-04 11:07:32 +00001006 if (!args->path)
1007 btrfs_free_path(path);
1008 else if (!args->extent_inserted)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00001009 btrfs_release_path(path);
Filipe Manana5893dfb2020-11-04 11:07:32 +00001010out:
1011 args->drop_end = found ? min(args->end, last_end) : args->end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001012
Chris Mason39279cc2007-06-12 06:35:45 -04001013 return ret;
1014}
1015
Yan Zhengd899e052008-10-30 14:25:28 -04001016static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001017 u64 objectid, u64 bytenr, u64 orig_offset,
1018 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -04001019{
1020 struct btrfs_file_extent_item *fi;
1021 struct btrfs_key key;
1022 u64 extent_end;
1023
1024 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1025 return 0;
1026
1027 btrfs_item_key_to_cpu(leaf, &key, slot);
1028 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
1029 return 0;
1030
1031 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1032 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
1033 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001034 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -04001035 btrfs_file_extent_compression(leaf, fi) ||
1036 btrfs_file_extent_encryption(leaf, fi) ||
1037 btrfs_file_extent_other_encoding(leaf, fi))
1038 return 0;
1039
1040 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
1041 if ((*start && *start != key.offset) || (*end && *end != extent_end))
1042 return 0;
1043
1044 *start = key.offset;
1045 *end = extent_end;
1046 return 1;
1047}
1048
1049/*
1050 * Mark extent in the range start - end as written.
1051 *
1052 * This changes extent type from 'pre-allocated' to 'regular'. If only
1053 * part of extent is marked as written, the extent will be split into
1054 * two or three.
1055 */
1056int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02001057 struct btrfs_inode *inode, u64 start, u64 end)
Yan Zhengd899e052008-10-30 14:25:28 -04001058{
David Sterba3ffbd682018-06-29 10:56:42 +02001059 struct btrfs_fs_info *fs_info = trans->fs_info;
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02001060 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04001061 struct extent_buffer *leaf;
1062 struct btrfs_path *path;
1063 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08001064 struct btrfs_ref ref = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04001065 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001066 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -04001067 u64 bytenr;
1068 u64 num_bytes;
1069 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001071 u64 other_start;
1072 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001073 u64 split;
1074 int del_nr = 0;
1075 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001076 int recow;
Ritesh Harjanie7b2ec32021-05-30 20:24:05 +05301077 int ret = 0;
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02001078 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001079
Yan Zhengd899e052008-10-30 14:25:28 -04001080 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001081 if (!path)
1082 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001083again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001084 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001085 split = start;
Li Zefan33345d012011-04-20 10:31:50 +08001086 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -04001087 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001088 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -04001089
1090 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -04001091 if (ret < 0)
1092 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001093 if (ret > 0 && path->slots[0] > 0)
1094 path->slots[0]--;
1095
1096 leaf = path->nodes[0];
1097 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001098 if (key.objectid != ino ||
1099 key.type != BTRFS_EXTENT_DATA_KEY) {
1100 ret = -EINVAL;
1101 btrfs_abort_transaction(trans, ret);
1102 goto out;
1103 }
Yan Zhengd899e052008-10-30 14:25:28 -04001104 fi = btrfs_item_ptr(leaf, path->slots[0],
1105 struct btrfs_file_extent_item);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001106 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
1107 ret = -EINVAL;
1108 btrfs_abort_transaction(trans, ret);
1109 goto out;
1110 }
Yan Zhengd899e052008-10-30 14:25:28 -04001111 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001112 if (key.offset > start || extent_end < end) {
1113 ret = -EINVAL;
1114 btrfs_abort_transaction(trans, ret);
1115 goto out;
1116 }
Yan Zhengd899e052008-10-30 14:25:28 -04001117
1118 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1119 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001121 memcpy(&new_key, &key, sizeof(new_key));
1122
1123 if (start == key.offset && end < extent_end) {
1124 other_start = 0;
1125 other_end = start;
1126 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001127 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001128 &other_start, &other_end)) {
1129 new_key.offset = end;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001130 btrfs_set_item_key_safe(fs_info, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001131 fi = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001133 btrfs_set_file_extent_generation(leaf, fi,
1134 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001135 btrfs_set_file_extent_num_bytes(leaf, fi,
1136 extent_end - end);
1137 btrfs_set_file_extent_offset(leaf, fi,
1138 end - orig_offset);
1139 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
1140 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001141 btrfs_set_file_extent_generation(leaf, fi,
1142 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001143 btrfs_set_file_extent_num_bytes(leaf, fi,
1144 end - other_start);
1145 btrfs_mark_buffer_dirty(leaf);
1146 goto out;
1147 }
1148 }
1149
1150 if (start > key.offset && end == extent_end) {
1151 other_start = end;
1152 other_end = 0;
1153 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001154 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001155 &other_start, &other_end)) {
1156 fi = btrfs_item_ptr(leaf, path->slots[0],
1157 struct btrfs_file_extent_item);
1158 btrfs_set_file_extent_num_bytes(leaf, fi,
1159 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -04001160 btrfs_set_file_extent_generation(leaf, fi,
1161 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001162 path->slots[0]++;
1163 new_key.offset = start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001164 btrfs_set_item_key_safe(fs_info, path, &new_key);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001165
1166 fi = btrfs_item_ptr(leaf, path->slots[0],
1167 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001168 btrfs_set_file_extent_generation(leaf, fi,
1169 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001170 btrfs_set_file_extent_num_bytes(leaf, fi,
1171 other_end - start);
1172 btrfs_set_file_extent_offset(leaf, fi,
1173 start - orig_offset);
1174 btrfs_mark_buffer_dirty(leaf);
1175 goto out;
1176 }
1177 }
Yan Zhengd899e052008-10-30 14:25:28 -04001178
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001179 while (start > key.offset || end < extent_end) {
1180 if (key.offset == start)
1181 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001182
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001183 new_key.offset = split;
1184 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1185 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001186 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001187 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001188 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001189 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001190 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191 goto out;
1192 }
Yan Zhengd899e052008-10-30 14:25:28 -04001193
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001194 leaf = path->nodes[0];
1195 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001196 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001197 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001198 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001199 split - key.offset);
1200
1201 fi = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_file_extent_item);
1203
Josef Bacik224ecce2012-08-16 16:32:06 -04001204 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001205 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1206 btrfs_set_file_extent_num_bytes(leaf, fi,
1207 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001208 btrfs_mark_buffer_dirty(leaf);
1209
Qu Wenruo82fa1132019-04-04 14:45:35 +08001210 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,
1211 num_bytes, 0);
1212 btrfs_init_data_ref(&ref, root->root_key.objectid, ino,
Nikolay Borisovf42c5da2021-10-12 11:21:35 +03001213 orig_offset, 0, false);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001214 ret = btrfs_inc_extent_ref(trans, &ref);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001215 if (ret) {
1216 btrfs_abort_transaction(trans, ret);
1217 goto out;
1218 }
Yan Zhengd899e052008-10-30 14:25:28 -04001219
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001220 if (split == start) {
1221 key.offset = start;
1222 } else {
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001223 if (start != key.offset) {
1224 ret = -EINVAL;
1225 btrfs_abort_transaction(trans, ret);
1226 goto out;
1227 }
Yan Zhengd899e052008-10-30 14:25:28 -04001228 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001229 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001230 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001231 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001232 }
1233
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001234 other_start = end;
1235 other_end = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08001236 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1237 num_bytes, 0);
Nikolay Borisovf42c5da2021-10-12 11:21:35 +03001238 btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset,
1239 0, false);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001240 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001241 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001242 &other_start, &other_end)) {
1243 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001244 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001245 goto again;
1246 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001247 extent_end = other_end;
1248 del_slot = path->slots[0] + 1;
1249 del_nr++;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08001250 ret = btrfs_free_extent(trans, &ref);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001251 if (ret) {
1252 btrfs_abort_transaction(trans, ret);
1253 goto out;
1254 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001255 }
1256 other_start = 0;
1257 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001258 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001259 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001260 &other_start, &other_end)) {
1261 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001262 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001263 goto again;
1264 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001265 key.offset = other_start;
1266 del_slot = path->slots[0];
1267 del_nr++;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08001268 ret = btrfs_free_extent(trans, &ref);
Josef Bacik9c8e63d2016-09-02 15:40:06 -04001269 if (ret) {
1270 btrfs_abort_transaction(trans, ret);
1271 goto out;
1272 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001273 }
1274 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001275 fi = btrfs_item_ptr(leaf, path->slots[0],
1276 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001277 btrfs_set_file_extent_type(leaf, fi,
1278 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001279 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001280 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001281 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001282 fi = btrfs_item_ptr(leaf, del_slot - 1,
1283 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001284 btrfs_set_file_extent_type(leaf, fi,
1285 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001286 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001287 btrfs_set_file_extent_num_bytes(leaf, fi,
1288 extent_end - key.offset);
1289 btrfs_mark_buffer_dirty(leaf);
1290
1291 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001292 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001293 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294 goto out;
1295 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001296 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001297out:
Yan Zhengd899e052008-10-30 14:25:28 -04001298 btrfs_free_path(path);
Ritesh Harjanie7b2ec32021-05-30 20:24:05 +05301299 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001300}
1301
Chris Mason39279cc2007-06-12 06:35:45 -04001302/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001303 * on error we return an unlocked page and the error value
1304 * on success we return a locked page and 0
1305 */
Chris Masonbb1591b42015-12-14 15:40:44 -08001306static int prepare_uptodate_page(struct inode *inode,
1307 struct page *page, u64 pos,
Josef Bacikb63164292011-09-30 15:23:54 -04001308 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001309{
1310 int ret = 0;
1311
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001312 if (((pos & (PAGE_SIZE - 1)) || force_uptodate) &&
Josef Bacikb63164292011-09-30 15:23:54 -04001313 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001314 ret = btrfs_readpage(NULL, page);
1315 if (ret)
1316 return ret;
1317 lock_page(page);
1318 if (!PageUptodate(page)) {
1319 unlock_page(page);
1320 return -EIO;
1321 }
Qu Wenruoe0467862021-07-26 14:35:02 +08001322
1323 /*
1324 * Since btrfs_readpage() will unlock the page before it
Qu Wenruo7c11d0a2021-07-26 14:35:03 +08001325 * returns, there is a window where btrfs_releasepage() can be
1326 * called to release the page. Here we check both inode
1327 * mapping and PagePrivate() to make sure the page was not
1328 * released.
Qu Wenruoe0467862021-07-26 14:35:02 +08001329 *
1330 * The private flag check is essential for subpage as we need
1331 * to store extra bitmap using page->private.
1332 */
1333 if (page->mapping != inode->i_mapping || !PagePrivate(page)) {
Chris Masonbb1591b42015-12-14 15:40:44 -08001334 unlock_page(page);
1335 return -EAGAIN;
1336 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001337 }
1338 return 0;
1339}
1340
1341/*
Miao Xie376cc682013-12-10 19:25:04 +08001342 * this just gets pages into the page cache and locks them down.
Chris Mason39279cc2007-06-12 06:35:45 -04001343 */
Miao Xieb37392e2013-12-10 19:25:03 +08001344static noinline int prepare_pages(struct inode *inode, struct page **pages,
1345 size_t num_pages, loff_t pos,
1346 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001347{
1348 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001349 unsigned long index = pos >> PAGE_SHIFT;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001350 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Filipe David Borba Mananafc28b622013-12-13 19:39:34 +00001351 int err = 0;
Miao Xie376cc682013-12-10 19:25:04 +08001352 int faili;
Chris Mason8c2383c2007-06-18 09:57:58 -04001353
Chris Mason39279cc2007-06-12 06:35:45 -04001354 for (i = 0; i < num_pages; i++) {
Chris Masonbb1591b42015-12-14 15:40:44 -08001355again:
Josef Bacika94733d2011-07-11 10:47:06 -04001356 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001357 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001358 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001359 faili = i - 1;
1360 err = -ENOMEM;
1361 goto fail;
1362 }
1363
Qu Wenruo32443de2021-01-26 16:34:00 +08001364 err = set_page_extent_mapped(pages[i]);
1365 if (err < 0) {
1366 faili = i;
1367 goto fail;
1368 }
1369
Chris Masonb1bf8622011-02-28 09:52:08 -05001370 if (i == 0)
Chris Masonbb1591b42015-12-14 15:40:44 -08001371 err = prepare_uptodate_page(inode, pages[i], pos,
Josef Bacikb63164292011-09-30 15:23:54 -04001372 force_uptodate);
Chris Masonbb1591b42015-12-14 15:40:44 -08001373 if (!err && i == num_pages - 1)
1374 err = prepare_uptodate_page(inode, pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001375 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001376 if (err) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001377 put_page(pages[i]);
Chris Masonbb1591b42015-12-14 15:40:44 -08001378 if (err == -EAGAIN) {
1379 err = 0;
1380 goto again;
1381 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001382 faili = i - 1;
1383 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001384 }
Chris Masonccd467d2007-06-28 15:57:36 -04001385 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001386 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001387
Chris Mason39279cc2007-06-12 06:35:45 -04001388 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001389fail:
1390 while (faili >= 0) {
1391 unlock_page(pages[faili]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001392 put_page(pages[faili]);
Chris Masonb1bf8622011-02-28 09:52:08 -05001393 faili--;
1394 }
1395 return err;
1396
Chris Mason39279cc2007-06-12 06:35:45 -04001397}
1398
Miao Xie376cc682013-12-10 19:25:04 +08001399/*
1400 * This function locks the extent and properly waits for data=ordered extents
1401 * to finish before allowing the pages to be modified if need.
1402 *
1403 * The return value:
1404 * 1 - the extent is locked
1405 * 0 - the extent is not locked, and everything is OK
1406 * -EAGAIN - need re-prepare the pages
1407 * the other < 0 number - Something wrong happens
1408 */
1409static noinline int
Nikolay Borisov2cff578c2017-02-20 13:50:51 +02001410lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages,
Miao Xie376cc682013-12-10 19:25:04 +08001411 size_t num_pages, loff_t pos,
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301412 size_t write_bytes,
Miao Xie376cc682013-12-10 19:25:04 +08001413 u64 *lockstart, u64 *lockend,
1414 struct extent_state **cached_state)
1415{
David Sterba3ffbd682018-06-29 10:56:42 +02001416 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Miao Xie376cc682013-12-10 19:25:04 +08001417 u64 start_pos;
1418 u64 last_pos;
1419 int i;
1420 int ret = 0;
1421
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001422 start_pos = round_down(pos, fs_info->sectorsize);
Qu Wenruoe21139c2020-08-13 14:33:52 +08001423 last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1;
Miao Xie376cc682013-12-10 19:25:04 +08001424
Filipe Mananae3b8a482017-11-04 00:16:59 +00001425 if (start_pos < inode->vfs_inode.i_size) {
Miao Xie376cc682013-12-10 19:25:04 +08001426 struct btrfs_ordered_extent *ordered;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001427
Nikolay Borisov2cff578c2017-02-20 13:50:51 +02001428 lock_extent_bits(&inode->io_tree, start_pos, last_pos,
1429 cached_state);
Miao Xieb88935b2014-03-06 13:54:58 +08001430 ordered = btrfs_lookup_ordered_range(inode, start_pos,
1431 last_pos - start_pos + 1);
Miao Xie376cc682013-12-10 19:25:04 +08001432 if (ordered &&
Omar Sandovalbffe6332019-12-02 17:34:19 -08001433 ordered->file_offset + ordered->num_bytes > start_pos &&
Miao Xie376cc682013-12-10 19:25:04 +08001434 ordered->file_offset <= last_pos) {
Nikolay Borisov2cff578c2017-02-20 13:50:51 +02001435 unlock_extent_cached(&inode->io_tree, start_pos,
David Sterbae43bbe52017-12-12 21:43:52 +01001436 last_pos, cached_state);
Miao Xie376cc682013-12-10 19:25:04 +08001437 for (i = 0; i < num_pages; i++) {
1438 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001439 put_page(pages[i]);
Miao Xie376cc682013-12-10 19:25:04 +08001440 }
Nikolay Borisovc0a43602020-09-18 12:15:53 +03001441 btrfs_start_ordered_extent(ordered, 1);
Miao Xieb88935b2014-03-06 13:54:58 +08001442 btrfs_put_ordered_extent(ordered);
1443 return -EAGAIN;
Miao Xie376cc682013-12-10 19:25:04 +08001444 }
1445 if (ordered)
1446 btrfs_put_ordered_extent(ordered);
Chris Mason7703bdd2018-06-20 07:56:11 -07001447
Miao Xie376cc682013-12-10 19:25:04 +08001448 *lockstart = start_pos;
1449 *lockend = last_pos;
1450 ret = 1;
1451 }
1452
Chris Mason7703bdd2018-06-20 07:56:11 -07001453 /*
Qu Wenruo32443de2021-01-26 16:34:00 +08001454 * We should be called after prepare_pages() which should have locked
1455 * all pages in the range.
Chris Mason7703bdd2018-06-20 07:56:11 -07001456 */
Qu Wenruo32443de2021-01-26 16:34:00 +08001457 for (i = 0; i < num_pages; i++)
Miao Xie376cc682013-12-10 19:25:04 +08001458 WARN_ON(!PageLocked(pages[i]));
Miao Xie376cc682013-12-10 19:25:04 +08001459
1460 return ret;
1461}
1462
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001463static int check_can_nocow(struct btrfs_inode *inode, loff_t pos,
1464 size_t *write_bytes, bool nowait)
Josef Bacik7ee9e442013-06-21 16:37:03 -04001465{
David Sterba3ffbd682018-06-29 10:56:42 +02001466 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov85b7ab62017-02-20 13:50:50 +02001467 struct btrfs_root *root = inode->root;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001468 u64 lockstart, lockend;
1469 u64 num_bytes;
1470 int ret;
1471
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001472 if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
1473 return 0;
1474
Filipe Manana5dbb75e2020-06-15 18:49:39 +01001475 if (!nowait && !btrfs_drew_try_write_lock(&root->snapshot_lock))
Nikolay Borisov5f791ec2019-05-07 10:23:46 +03001476 return -EAGAIN;
Miao Xie8257b2d2014-03-06 13:38:19 +08001477
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001478 lockstart = round_down(pos, fs_info->sectorsize);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001479 lockend = round_up(pos + *write_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001480 fs_info->sectorsize) - 1;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001481 num_bytes = lockend - lockstart + 1;
Filipe Manana5dbb75e2020-06-15 18:49:39 +01001482
1483 if (nowait) {
1484 struct btrfs_ordered_extent *ordered;
1485
1486 if (!try_lock_extent(&inode->io_tree, lockstart, lockend))
1487 return -EAGAIN;
1488
1489 ordered = btrfs_lookup_ordered_range(inode, lockstart,
1490 num_bytes);
1491 if (ordered) {
1492 btrfs_put_ordered_extent(ordered);
1493 ret = -EAGAIN;
1494 goto out_unlock;
1495 }
1496 } else {
1497 btrfs_lock_and_flush_ordered_range(inode, lockstart,
1498 lockend, NULL);
1499 }
1500
Nikolay Borisov85b7ab62017-02-20 13:50:50 +02001501 ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes,
Boris Burkova84d5d42020-08-18 11:00:05 -07001502 NULL, NULL, NULL, false);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001503 if (ret <= 0) {
1504 ret = 0;
Filipe Manana5dbb75e2020-06-15 18:49:39 +01001505 if (!nowait)
1506 btrfs_drew_write_unlock(&root->snapshot_lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001507 } else {
Miao Xiec9339562014-02-27 13:58:04 +08001508 *write_bytes = min_t(size_t, *write_bytes ,
1509 num_bytes - pos + lockstart);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001510 }
Filipe Manana5dbb75e2020-06-15 18:49:39 +01001511out_unlock:
Nikolay Borisov85b7ab62017-02-20 13:50:50 +02001512 unlock_extent(&inode->io_tree, lockstart, lockend);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001513
1514 return ret;
1515}
1516
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001517static int check_nocow_nolock(struct btrfs_inode *inode, loff_t pos,
1518 size_t *write_bytes)
1519{
1520 return check_can_nocow(inode, pos, write_bytes, true);
1521}
1522
1523/*
1524 * Check if we can do nocow write into the range [@pos, @pos + @write_bytes)
1525 *
1526 * @pos: File offset
1527 * @write_bytes: The length to write, will be updated to the nocow writeable
1528 * range
1529 *
1530 * This function will flush ordered extents in the range to ensure proper
1531 * nocow checks.
1532 *
1533 * Return:
1534 * >0 and update @write_bytes if we can do nocow write
1535 * 0 if we can't do nocow write
1536 * -EAGAIN if we can't get the needed lock or there are ordered extents
1537 * for * (nowait == true) case
1538 * <0 if other error happened
1539 *
1540 * NOTE: Callers need to release the lock by btrfs_check_nocow_unlock().
1541 */
1542int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
1543 size_t *write_bytes)
1544{
1545 return check_can_nocow(inode, pos, write_bytes, false);
1546}
1547
1548void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
1549{
1550 btrfs_drew_write_unlock(&inode->root->snapshot_lock);
1551}
1552
Goldwyn Rodriguesb8d8e1f2020-09-24 11:39:15 -05001553static void update_time_for_write(struct inode *inode)
1554{
1555 struct timespec64 now;
1556
1557 if (IS_NOCMTIME(inode))
1558 return;
1559
1560 now = current_time(inode);
1561 if (!timespec64_equal(&inode->i_mtime, &now))
1562 inode->i_mtime = now;
1563
1564 if (!timespec64_equal(&inode->i_ctime, &now))
1565 inode->i_ctime = now;
1566
1567 if (IS_I_VERSION(inode))
1568 inode_inc_iversion(inode);
1569}
1570
1571static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from,
1572 size_t count)
1573{
1574 struct file *file = iocb->ki_filp;
1575 struct inode *inode = file_inode(file);
1576 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1577 loff_t pos = iocb->ki_pos;
1578 int ret;
1579 loff_t oldsize;
1580 loff_t start_pos;
1581
1582 if (iocb->ki_flags & IOCB_NOWAIT) {
1583 size_t nocow_bytes = count;
1584
1585 /* We will allocate space in case nodatacow is not set, so bail */
1586 if (check_nocow_nolock(BTRFS_I(inode), pos, &nocow_bytes) <= 0)
1587 return -EAGAIN;
1588 /*
1589 * There are holes in the range or parts of the range that must
1590 * be COWed (shared extents, RO block groups, etc), so just bail
1591 * out.
1592 */
1593 if (nocow_bytes < count)
1594 return -EAGAIN;
1595 }
1596
1597 current->backing_dev_info = inode_to_bdi(inode);
1598 ret = file_remove_privs(file);
1599 if (ret)
1600 return ret;
1601
1602 /*
1603 * We reserve space for updating the inode when we reserve space for the
1604 * extent we are going to write, so we will enospc out there. We don't
1605 * need to start yet another transaction to update the inode as we will
1606 * update the inode when we finish writing whatever data we write.
1607 */
1608 update_time_for_write(inode);
1609
1610 start_pos = round_down(pos, fs_info->sectorsize);
1611 oldsize = i_size_read(inode);
1612 if (start_pos > oldsize) {
1613 /* Expand hole size to cover write data, preventing empty gap */
1614 loff_t end_pos = round_up(pos + count, fs_info->sectorsize);
1615
Nikolay Borisovb06359a32020-11-02 16:49:04 +02001616 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, end_pos);
Goldwyn Rodriguesb8d8e1f2020-09-24 11:39:15 -05001617 if (ret) {
1618 current->backing_dev_info = NULL;
1619 return ret;
1620 }
1621 }
1622
1623 return 0;
1624}
1625
Goldwyn Rodriguese4af4002018-06-17 12:39:47 -05001626static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
1627 struct iov_iter *i)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001628{
Goldwyn Rodriguese4af4002018-06-17 12:39:47 -05001629 struct file *file = iocb->ki_filp;
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001630 loff_t pos;
Al Viro496ad9a2013-01-23 17:07:38 -05001631 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001632 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001633 struct page **pages = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001634 struct extent_changeset *data_reserved = NULL;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001635 u64 release_bytes = 0;
Miao Xie376cc682013-12-10 19:25:04 +08001636 u64 lockstart;
1637 u64 lockend;
Josef Bacikd0215f32011-01-25 14:57:24 -05001638 size_t num_written = 0;
1639 int nrptrs;
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001640 ssize_t ret;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001641 bool only_release_metadata = false;
Josef Bacikb63164292011-09-30 15:23:54 -04001642 bool force_page_uptodate = false;
Goldwyn Rodrigues5e8b9ef2020-09-24 11:39:13 -05001643 loff_t old_isize = i_size_read(inode);
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001644 unsigned int ilock_flags = 0;
Chris Masoncb843a62008-10-03 12:30:02 -04001645
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001646 if (iocb->ki_flags & IOCB_NOWAIT)
1647 ilock_flags |= BTRFS_ILOCK_TRY;
1648
1649 ret = btrfs_inode_lock(inode, ilock_flags);
1650 if (ret < 0)
1651 return ret;
1652
1653 ret = generic_write_checks(iocb, i);
1654 if (ret <= 0)
1655 goto out;
1656
1657 ret = btrfs_write_check(iocb, i, ret);
1658 if (ret < 0)
1659 goto out;
1660
1661 pos = iocb->ki_pos;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001662 nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE),
1663 PAGE_SIZE / (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001664 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1665 nrptrs = max(nrptrs, 8);
David Sterba31e818f2015-02-20 18:00:26 +01001666 pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL);
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001667 if (!pages) {
1668 ret = -ENOMEM;
1669 goto out;
1670 }
Chris Masonab93dbe2009-10-01 12:29:10 -04001671
Josef Bacikd0215f32011-01-25 14:57:24 -05001672 while (iov_iter_count(i) > 0) {
Filipe Mananac67d9702019-09-30 10:20:25 +01001673 struct extent_state *cached_state = NULL;
Johannes Thumshirn70730172018-12-05 15:23:03 +01001674 size_t offset = offset_in_page(pos);
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301675 size_t sector_offset;
Josef Bacikd0215f32011-01-25 14:57:24 -05001676 size_t write_bytes = min(iov_iter_count(i),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001677 nrptrs * (size_t)PAGE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001678 offset);
Goldwyn Rodrigueseefa45f52020-09-25 15:36:38 -05001679 size_t num_pages;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001680 size_t reserve_bytes;
Josef Bacikd0215f32011-01-25 14:57:24 -05001681 size_t dirty_pages;
1682 size_t copied;
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301683 size_t dirty_sectors;
1684 size_t num_sectors;
Goldwyn Rodrigues79f015f2017-10-16 05:43:21 -05001685 int extents_locked;
Chris Mason39279cc2007-06-12 06:35:45 -04001686
Xin Zhong914ee292010-12-09 09:30:14 +00001687 /*
1688 * Fault pages before locking them in prepare_pages
1689 * to avoid recursive lock
1690 */
Andreas Gruenbachera6294592021-08-02 14:54:16 +02001691 if (unlikely(fault_in_iov_iter_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001692 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001693 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001694 }
1695
Filipe Mananaa0e248b2019-10-11 16:41:20 +01001696 only_release_metadata = false;
Jeff Mahoneyda170662016-06-15 09:22:56 -04001697 sector_offset = pos & (fs_info->sectorsize - 1);
Qu Wenruod9d8b2a2015-09-08 17:22:43 +08001698
Qu Wenruo364ecf32017-02-27 15:10:38 +08001699 extent_changeset_release(data_reserved);
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03001700 ret = btrfs_check_data_free_space(BTRFS_I(inode),
1701 &data_reserved, pos,
Qu Wenruo364ecf32017-02-27 15:10:38 +08001702 write_bytes);
Josef Bacikc6887cd2016-03-25 13:26:00 -04001703 if (ret < 0) {
Goldwyn Rodrigueseefa45f52020-09-25 15:36:38 -05001704 /*
1705 * If we don't have to COW at the offset, reserve
1706 * metadata only. write_bytes may get smaller than
1707 * requested here.
1708 */
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001709 if (btrfs_check_nocow_lock(BTRFS_I(inode), pos,
Goldwyn Rodrigueseefa45f52020-09-25 15:36:38 -05001710 &write_bytes) > 0)
Josef Bacikc6887cd2016-03-25 13:26:00 -04001711 only_release_metadata = true;
Goldwyn Rodrigueseefa45f52020-09-25 15:36:38 -05001712 else
Josef Bacikc6887cd2016-03-25 13:26:00 -04001713 break;
Josef Bacik7ee9e442013-06-21 16:37:03 -04001714 }
Zhao Lei4da2e262016-01-06 18:24:43 +08001715
Goldwyn Rodrigueseefa45f52020-09-25 15:36:38 -05001716 num_pages = DIV_ROUND_UP(write_bytes + offset, PAGE_SIZE);
1717 WARN_ON(num_pages > nrptrs);
1718 reserve_bytes = round_up(write_bytes + sector_offset,
1719 fs_info->sectorsize);
Josef Bacik8b62f87b2017-10-19 14:15:55 -04001720 WARN_ON(reserve_bytes == 0);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02001721 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
Omar Sandoval28c9b1e2019-11-18 22:45:55 -08001722 reserve_bytes,
1723 reserve_bytes);
Josef Bacik7ee9e442013-06-21 16:37:03 -04001724 if (ret) {
1725 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03001726 btrfs_free_reserved_data_space(BTRFS_I(inode),
Qu Wenruobc42bda2017-02-27 15:10:39 +08001727 data_reserved, pos,
1728 write_bytes);
Miao Xie8257b2d2014-03-06 13:38:19 +08001729 else
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001730 btrfs_check_nocow_unlock(BTRFS_I(inode));
Josef Bacik7ee9e442013-06-21 16:37:03 -04001731 break;
1732 }
1733
1734 release_bytes = reserve_bytes;
Miao Xie376cc682013-12-10 19:25:04 +08001735again:
Josef Bacik4a640012011-01-25 15:10:08 -05001736 /*
1737 * This is going to setup the pages array with the number of
1738 * pages we want, so we don't really need to worry about the
1739 * contents of pages from loop to loop
1740 */
Miao Xieb37392e2013-12-10 19:25:03 +08001741 ret = prepare_pages(inode, pages, num_pages,
1742 pos, write_bytes,
Josef Bacikb63164292011-09-30 15:23:54 -04001743 force_page_uptodate);
Josef Bacik8b62f87b2017-10-19 14:15:55 -04001744 if (ret) {
1745 btrfs_delalloc_release_extents(BTRFS_I(inode),
Qu Wenruo8702ba92019-10-14 14:34:51 +08001746 reserve_bytes);
Josef Bacikd0215f32011-01-25 14:57:24 -05001747 break;
Josef Bacik8b62f87b2017-10-19 14:15:55 -04001748 }
Chris Mason39279cc2007-06-12 06:35:45 -04001749
Goldwyn Rodrigues79f015f2017-10-16 05:43:21 -05001750 extents_locked = lock_and_cleanup_extent_if_need(
1751 BTRFS_I(inode), pages,
Nikolay Borisov2cff578c2017-02-20 13:50:51 +02001752 num_pages, pos, write_bytes, &lockstart,
1753 &lockend, &cached_state);
Goldwyn Rodrigues79f015f2017-10-16 05:43:21 -05001754 if (extents_locked < 0) {
1755 if (extents_locked == -EAGAIN)
Miao Xie376cc682013-12-10 19:25:04 +08001756 goto again;
Josef Bacik8b62f87b2017-10-19 14:15:55 -04001757 btrfs_delalloc_release_extents(BTRFS_I(inode),
Qu Wenruo8702ba92019-10-14 14:34:51 +08001758 reserve_bytes);
Goldwyn Rodrigues79f015f2017-10-16 05:43:21 -05001759 ret = extents_locked;
Miao Xie376cc682013-12-10 19:25:04 +08001760 break;
Miao Xie376cc682013-12-10 19:25:04 +08001761 }
1762
Zhao Leiee22f0c2016-01-06 18:47:31 +08001763 copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001764
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001765 num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
Chris Mason56244ef2016-05-16 09:21:01 -07001766 dirty_sectors = round_up(copied + sector_offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001767 fs_info->sectorsize);
1768 dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
Chris Mason56244ef2016-05-16 09:21:01 -07001769
Chris Masonb1bf8622011-02-28 09:52:08 -05001770 /*
1771 * if we have trouble faulting in the pages, fall
1772 * back to one page at a time
1773 */
1774 if (copied < write_bytes)
1775 nrptrs = 1;
1776
Josef Bacikb63164292011-09-30 15:23:54 -04001777 if (copied == 0) {
1778 force_page_uptodate = true;
Chris Mason56244ef2016-05-16 09:21:01 -07001779 dirty_sectors = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001780 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001781 } else {
1782 force_page_uptodate = false;
David Sterbaed6078f2014-06-05 01:59:57 +02001783 dirty_pages = DIV_ROUND_UP(copied + offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001784 PAGE_SIZE);
Josef Bacikb63164292011-09-30 15:23:54 -04001785 }
Xin Zhong914ee292010-12-09 09:30:14 +00001786
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301787 if (num_sectors > dirty_sectors) {
Chris Mason8b8b08c2016-07-19 05:52:36 -07001788 /* release everything except the sectors we dirtied */
David Sterba265fdfa2020-07-01 21:19:09 +02001789 release_bytes -= dirty_sectors << fs_info->sectorsize_bits;
Qu Wenruo485290a2015-10-29 17:28:46 +08001790 if (only_release_metadata) {
Nikolay Borisov691fa052017-02-20 13:50:42 +02001791 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08001792 release_bytes, true);
Qu Wenruo485290a2015-10-29 17:28:46 +08001793 } else {
1794 u64 __pos;
1795
Jeff Mahoneyda170662016-06-15 09:22:56 -04001796 __pos = round_down(pos,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001797 fs_info->sectorsize) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001798 (dirty_pages << PAGE_SHIFT);
Nikolay Borisov86d52922020-06-03 08:55:40 +03001799 btrfs_delalloc_release_space(BTRFS_I(inode),
Qu Wenruobc42bda2017-02-27 15:10:39 +08001800 data_reserved, __pos,
Qu Wenruo43b18592017-12-12 15:34:32 +08001801 release_bytes, true);
Qu Wenruo485290a2015-10-29 17:28:46 +08001802 }
Xin Zhong914ee292010-12-09 09:30:14 +00001803 }
1804
Chandan Rajendra2e78c922016-01-21 15:55:53 +05301805 release_bytes = round_up(copied + sector_offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001806 fs_info->sectorsize);
Miao Xie376cc682013-12-10 19:25:04 +08001807
Goldwyn Rodriguesaa8c1a42020-10-14 09:55:45 -05001808 ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
1809 dirty_pages, pos, copied,
1810 &cached_state, only_release_metadata);
Filipe Mananac67d9702019-09-30 10:20:25 +01001811
1812 /*
1813 * If we have not locked the extent range, because the range's
1814 * start offset is >= i_size, we might still have a non-NULL
1815 * cached extent state, acquired while marking the extent range
1816 * as delalloc through btrfs_dirty_pages(). Therefore free any
1817 * possible cached extent state to avoid a memory leak.
1818 */
Goldwyn Rodrigues79f015f2017-10-16 05:43:21 -05001819 if (extents_locked)
Miao Xie376cc682013-12-10 19:25:04 +08001820 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001821 lockstart, lockend, &cached_state);
Filipe Mananac67d9702019-09-30 10:20:25 +01001822 else
1823 free_extent_state(cached_state);
1824
Qu Wenruo8702ba92019-10-14 14:34:51 +08001825 btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
Miao Xief1de9682014-01-09 10:06:10 +08001826 if (ret) {
Qu Wenruoe4f94342021-09-27 15:21:49 +08001827 btrfs_drop_pages(fs_info, pages, num_pages, pos, copied);
Miao Xie376cc682013-12-10 19:25:04 +08001828 break;
Miao Xief1de9682014-01-09 10:06:10 +08001829 }
Chris Mason39279cc2007-06-12 06:35:45 -04001830
Josef Bacik7ee9e442013-06-21 16:37:03 -04001831 release_bytes = 0;
Miao Xie8257b2d2014-03-06 13:38:19 +08001832 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001833 btrfs_check_nocow_unlock(BTRFS_I(inode));
Miao Xie8257b2d2014-03-06 13:38:19 +08001834
Qu Wenruoe4f94342021-09-27 15:21:49 +08001835 btrfs_drop_pages(fs_info, pages, num_pages, pos, copied);
Miao Xief1de9682014-01-09 10:06:10 +08001836
Josef Bacikd0215f32011-01-25 14:57:24 -05001837 cond_resched();
1838
Namjae Jeond0e1d662012-12-11 16:00:21 -08001839 balance_dirty_pages_ratelimited(inode->i_mapping);
Chris Mason39279cc2007-06-12 06:35:45 -04001840
Xin Zhong914ee292010-12-09 09:30:14 +00001841 pos += copied;
1842 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001843 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001844
Chris Mason8c2383c2007-06-18 09:57:58 -04001845 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001846
Josef Bacik7ee9e442013-06-21 16:37:03 -04001847 if (release_bytes) {
Miao Xie8257b2d2014-03-06 13:38:19 +08001848 if (only_release_metadata) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08001849 btrfs_check_nocow_unlock(BTRFS_I(inode));
Nikolay Borisov691fa052017-02-20 13:50:42 +02001850 btrfs_delalloc_release_metadata(BTRFS_I(inode),
Qu Wenruo43b18592017-12-12 15:34:32 +08001851 release_bytes, true);
Miao Xie8257b2d2014-03-06 13:38:19 +08001852 } else {
Nikolay Borisov86d52922020-06-03 08:55:40 +03001853 btrfs_delalloc_release_space(BTRFS_I(inode),
1854 data_reserved,
Qu Wenruobc42bda2017-02-27 15:10:39 +08001855 round_down(pos, fs_info->sectorsize),
Qu Wenruo43b18592017-12-12 15:34:32 +08001856 release_bytes, true);
Miao Xie8257b2d2014-03-06 13:38:19 +08001857 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04001858 }
1859
Qu Wenruo364ecf32017-02-27 15:10:38 +08001860 extent_changeset_free(data_reserved);
Goldwyn Rodrigues5e8b9ef2020-09-24 11:39:13 -05001861 if (num_written > 0) {
1862 pagecache_isize_extended(inode, old_isize, iocb->ki_pos);
1863 iocb->ki_pos += num_written;
1864 }
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001865out:
1866 btrfs_inode_unlock(inode, ilock_flags);
Josef Bacikd0215f32011-01-25 14:57:24 -05001867 return num_written ? num_written : ret;
1868}
1869
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05001870static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
1871 const struct iov_iter *iter, loff_t offset)
1872{
1873 const u32 blocksize_mask = fs_info->sectorsize - 1;
1874
1875 if (offset & blocksize_mask)
1876 return -EINVAL;
1877
1878 if (iov_iter_alignment(iter) & blocksize_mask)
1879 return -EINVAL;
1880
1881 return 0;
1882}
1883
1884static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
Josef Bacikd0215f32011-01-25 14:57:24 -05001885{
Filipe Manana51bd9562021-10-25 17:27:47 +01001886 const bool is_sync_write = (iocb->ki_flags & IOCB_DSYNC);
Josef Bacikd0215f32011-01-25 14:57:24 -05001887 struct file *file = iocb->ki_filp;
Filipe Manana728404d2014-10-10 09:43:11 +01001888 struct inode *inode = file_inode(file);
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05001889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001890 loff_t pos;
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05001891 ssize_t written = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -05001892 ssize_t written_buffered;
Filipe Manana51bd9562021-10-25 17:27:47 +01001893 size_t prev_left = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -05001894 loff_t endbyte;
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001895 ssize_t err;
1896 unsigned int ilock_flags = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -05001897
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001898 if (iocb->ki_flags & IOCB_NOWAIT)
1899 ilock_flags |= BTRFS_ILOCK_TRY;
1900
Goldwyn Rodriguese9adabb2020-09-24 11:39:18 -05001901 /* If the write DIO is within EOF, use a shared lock */
1902 if (iocb->ki_pos + iov_iter_count(from) <= i_size_read(inode))
1903 ilock_flags |= BTRFS_ILOCK_SHARED;
1904
1905relock:
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001906 err = btrfs_inode_lock(inode, ilock_flags);
1907 if (err < 0)
1908 return err;
1909
1910 err = generic_write_checks(iocb, from);
1911 if (err <= 0) {
1912 btrfs_inode_unlock(inode, ilock_flags);
1913 return err;
1914 }
1915
1916 err = btrfs_write_check(iocb, from, err);
1917 if (err < 0) {
1918 btrfs_inode_unlock(inode, ilock_flags);
1919 goto out;
1920 }
1921
1922 pos = iocb->ki_pos;
Goldwyn Rodriguese9adabb2020-09-24 11:39:18 -05001923 /*
1924 * Re-check since file size may have changed just before taking the
1925 * lock or pos may have changed because of O_APPEND in generic_write_check()
1926 */
1927 if ((ilock_flags & BTRFS_ILOCK_SHARED) &&
1928 pos + iov_iter_count(from) > i_size_read(inode)) {
1929 btrfs_inode_unlock(inode, ilock_flags);
1930 ilock_flags &= ~BTRFS_ILOCK_SHARED;
1931 goto relock;
1932 }
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001933
1934 if (check_direct_IO(fs_info, from, pos)) {
1935 btrfs_inode_unlock(inode, ilock_flags);
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05001936 goto buffered;
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05001937 }
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05001938
Filipe Manana51bd9562021-10-25 17:27:47 +01001939 /*
1940 * We remove IOCB_DSYNC so that we don't deadlock when iomap_dio_rw()
1941 * calls generic_write_sync() (through iomap_dio_complete()), because
1942 * that results in calling fsync (btrfs_sync_file()) which will try to
1943 * lock the inode in exclusive/write mode.
1944 */
1945 if (is_sync_write)
1946 iocb->ki_flags &= ~IOCB_DSYNC;
1947
1948 /*
1949 * The iov_iter can be mapped to the same file range we are writing to.
1950 * If that's the case, then we will deadlock in the iomap code, because
1951 * it first calls our callback btrfs_dio_iomap_begin(), which will create
1952 * an ordered extent, and after that it will fault in the pages that the
1953 * iov_iter refers to. During the fault in we end up in the readahead
1954 * pages code (starting at btrfs_readahead()), which will lock the range,
1955 * find that ordered extent and then wait for it to complete (at
1956 * btrfs_lock_and_flush_ordered_range()), resulting in a deadlock since
1957 * obviously the ordered extent can never complete as we didn't submit
1958 * yet the respective bio(s). This always happens when the buffer is
1959 * memory mapped to the same file range, since the iomap DIO code always
1960 * invalidates pages in the target file range (after starting and waiting
1961 * for any writeback).
1962 *
1963 * So here we disable page faults in the iov_iter and then retry if we
1964 * got -EFAULT, faulting in the pages before the retry.
1965 */
1966again:
1967 from->nofault = true;
1968 err = iomap_dio_rw(iocb, from, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
1969 IOMAP_DIO_PARTIAL, written);
1970 from->nofault = false;
1971
1972 /* No increment (+=) because iomap returns a cumulative value. */
1973 if (err > 0)
1974 written = err;
1975
1976 if (iov_iter_count(from) > 0 && (err == -EFAULT || err > 0)) {
1977 const size_t left = iov_iter_count(from);
1978 /*
1979 * We have more data left to write. Try to fault in as many as
1980 * possible of the remainder pages and retry. We do this without
1981 * releasing and locking again the inode, to prevent races with
1982 * truncate.
1983 *
1984 * Also, in case the iov refers to pages in the file range of the
1985 * file we want to write to (due to a mmap), we could enter an
1986 * infinite loop if we retry after faulting the pages in, since
1987 * iomap will invalidate any pages in the range early on, before
1988 * it tries to fault in the pages of the iov. So we keep track of
1989 * how much was left of iov in the previous EFAULT and fallback
1990 * to buffered IO in case we haven't made any progress.
1991 */
1992 if (left == prev_left) {
1993 err = -ENOTBLK;
1994 } else {
1995 fault_in_iov_iter_readable(from, left);
1996 prev_left = left;
1997 goto again;
1998 }
1999 }
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05002000
Goldwyn Rodriguese9adabb2020-09-24 11:39:18 -05002001 btrfs_inode_unlock(inode, ilock_flags);
Josef Bacikd0215f32011-01-25 14:57:24 -05002002
Filipe Manana51bd9562021-10-25 17:27:47 +01002003 /*
2004 * Add back IOCB_DSYNC. Our caller, btrfs_file_write_iter(), will do
2005 * the fsync (call generic_write_sync()).
2006 */
2007 if (is_sync_write)
2008 iocb->ki_flags |= IOCB_DSYNC;
Goldwyn Rodriguesa42fa642020-09-24 11:39:20 -05002009
Filipe Manana51bd9562021-10-25 17:27:47 +01002010 /* If 'err' is -ENOTBLK then it means we must fallback to buffered IO. */
2011 if ((err < 0 && err != -ENOTBLK) || !iov_iter_count(from))
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05002012 goto out;
Josef Bacikd0215f32011-01-25 14:57:24 -05002013
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05002014buffered:
Goldwyn Rodriguese4af4002018-06-17 12:39:47 -05002015 pos = iocb->ki_pos;
2016 written_buffered = btrfs_buffered_write(iocb, from);
Josef Bacikd0215f32011-01-25 14:57:24 -05002017 if (written_buffered < 0) {
2018 err = written_buffered;
2019 goto out;
2020 }
Filipe Manana075bdbd2014-10-09 21:18:55 +01002021 /*
2022 * Ensure all data is persisted. We want the next direct IO read to be
2023 * able to read what was just written.
2024 */
Josef Bacikd0215f32011-01-25 14:57:24 -05002025 endbyte = pos + written_buffered - 1;
Filipe Manana728404d2014-10-10 09:43:11 +01002026 err = btrfs_fdatawrite_range(inode, pos, endbyte);
Filipe Manana075bdbd2014-10-09 21:18:55 +01002027 if (err)
2028 goto out;
Filipe Manana728404d2014-10-10 09:43:11 +01002029 err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte);
Josef Bacikd0215f32011-01-25 14:57:24 -05002030 if (err)
2031 goto out;
2032 written += written_buffered;
Al Viro867c4f92014-02-11 19:31:06 -05002033 iocb->ki_pos = pos + written_buffered;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002034 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT,
2035 endbyte >> PAGE_SHIFT);
Josef Bacikd0215f32011-01-25 14:57:24 -05002036out:
Filipe Manana51bd9562021-10-25 17:27:47 +01002037 return err < 0 ? err : written;
Josef Bacikd0215f32011-01-25 14:57:24 -05002038}
2039
Omar Sandoval7c0c7262019-08-13 16:00:02 -07002040static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from,
2041 const struct btrfs_ioctl_encoded_io_args *encoded)
2042{
2043 struct file *file = iocb->ki_filp;
2044 struct inode *inode = file_inode(file);
2045 loff_t count;
2046 ssize_t ret;
2047
2048 btrfs_inode_lock(inode, 0);
2049 count = encoded->len;
2050 ret = generic_write_checks_count(iocb, &count);
2051 if (ret == 0 && count != encoded->len) {
2052 /*
2053 * The write got truncated by generic_write_checks_count(). We
2054 * can't do a partial encoded write.
2055 */
2056 ret = -EFBIG;
2057 }
2058 if (ret || encoded->len == 0)
2059 goto out;
2060
2061 ret = btrfs_write_check(iocb, from, encoded->len);
2062 if (ret < 0)
2063 goto out;
2064
2065 ret = btrfs_do_encoded_write(iocb, from, encoded);
2066out:
2067 btrfs_inode_unlock(inode, 0);
2068 return ret;
2069}
2070
2071ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from,
2072 const struct btrfs_ioctl_encoded_io_args *encoded)
Josef Bacikd0215f32011-01-25 14:57:24 -05002073{
2074 struct file *file = iocb->ki_filp;
Nikolay Borisov14971652020-12-10 10:38:32 +02002075 struct btrfs_inode *inode = BTRFS_I(file_inode(file));
Omar Sandoval7c0c7262019-08-13 16:00:02 -07002076 ssize_t num_written, num_sync;
Omar Sandovalf50cb7a2019-08-15 14:04:03 -07002077 const bool sync = iocb->ki_flags & IOCB_DSYNC;
Josef Bacikd0215f32011-01-25 14:57:24 -05002078
Goldwyn Rodriguesc86537a2020-09-24 11:39:14 -05002079 /*
2080 * If the fs flips readonly due to some impossible error, although we
2081 * have opened a file as writable, we have to stop this write operation
2082 * to ensure consistency.
2083 */
Josef Bacik84961532021-10-05 16:35:25 -04002084 if (BTRFS_FS_ERROR(inode->root->fs_info))
Goldwyn Rodriguesc86537a2020-09-24 11:39:14 -05002085 return -EROFS;
2086
Omar Sandoval7c0c7262019-08-13 16:00:02 -07002087 if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
Christoph Hellwig91f99432017-08-29 16:13:20 +02002088 return -EOPNOTSUPP;
2089
Josef Bacikb812ce22012-11-16 13:56:32 -05002090 if (sync)
Nikolay Borisov14971652020-12-10 10:38:32 +02002091 atomic_inc(&inode->sync_writers);
Josef Bacikb812ce22012-11-16 13:56:32 -05002092
Omar Sandoval7c0c7262019-08-13 16:00:02 -07002093 if (encoded) {
2094 num_written = btrfs_encoded_write(iocb, from, encoded);
2095 num_sync = encoded->len;
2096 } else if (iocb->ki_flags & IOCB_DIRECT) {
2097 num_written = num_sync = btrfs_direct_write(iocb, from);
2098 } else {
2099 num_written = num_sync = btrfs_buffered_write(iocb, from);
2100 }
Josef Bacikd0215f32011-01-25 14:57:24 -05002101
Filipe Mananabc0939f2021-02-23 12:08:48 +00002102 btrfs_set_inode_last_sub_trans(inode);
2103
Omar Sandoval7c0c7262019-08-13 16:00:02 -07002104 if (num_sync > 0) {
2105 num_sync = generic_write_sync(iocb, num_sync);
2106 if (num_sync < 0)
2107 num_written = num_sync;
2108 }
Miao Xie0a3404d2013-01-28 12:34:55 +00002109
Josef Bacikb812ce22012-11-16 13:56:32 -05002110 if (sync)
Nikolay Borisov14971652020-12-10 10:38:32 +02002111 atomic_dec(&inode->sync_writers);
Goldwyn Rodriguesb8d8e1f2020-09-24 11:39:15 -05002112
Chris Mason39279cc2007-06-12 06:35:45 -04002113 current->backing_dev_info = NULL;
Goldwyn Rodriguesc3523702020-09-24 11:39:17 -05002114 return num_written;
Chris Mason39279cc2007-06-12 06:35:45 -04002115}
2116
Omar Sandoval7c0c7262019-08-13 16:00:02 -07002117static ssize_t btrfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2118{
2119 return btrfs_do_write_iter(iocb, from, NULL);
2120}
2121
Chris Masond3977122009-01-05 21:25:51 -05002122int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04002123{
Josef Bacik23b5ec72017-07-24 15:14:25 -04002124 struct btrfs_file_private *private = filp->private_data;
2125
Josef Bacik23b5ec72017-07-24 15:14:25 -04002126 if (private && private->filldir_buf)
2127 kfree(private->filldir_buf);
2128 kfree(private);
2129 filp->private_data = NULL;
2130
Chris Masonf6dc45c2014-08-20 07:15:33 -07002131 /*
Nikolay Borisov1fd40332020-10-01 09:40:39 +03002132 * Set by setattr when we are about to truncate a file from a non-zero
2133 * size to a zero size. This tries to flush down new bytes that may
2134 * have been written if the application were using truncate to replace
2135 * a file in place.
Chris Masonf6dc45c2014-08-20 07:15:33 -07002136 */
Nikolay Borisov1fd40332020-10-01 09:40:39 +03002137 if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Chris Masonf6dc45c2014-08-20 07:15:33 -07002138 &BTRFS_I(inode)->runtime_flags))
2139 filemap_flush(inode->i_mapping);
Mingminge1b81e62008-05-27 10:55:43 -04002140 return 0;
2141}
2142
Filipe Manana669249e2014-09-02 11:09:58 +01002143static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
2144{
2145 int ret;
Liu Bo343e4fc2017-11-15 16:10:28 -07002146 struct blk_plug plug;
Filipe Manana669249e2014-09-02 11:09:58 +01002147
Liu Bo343e4fc2017-11-15 16:10:28 -07002148 /*
2149 * This is only called in fsync, which would do synchronous writes, so
2150 * a plug can merge adjacent IOs as much as possible. Esp. in case of
2151 * multiple disks using raid profile, a large IO can be split to
2152 * several segments of stripe length (currently 64K).
2153 */
2154 blk_start_plug(&plug);
Filipe Manana669249e2014-09-02 11:09:58 +01002155 atomic_inc(&BTRFS_I(inode)->sync_writers);
Filipe Manana728404d2014-10-10 09:43:11 +01002156 ret = btrfs_fdatawrite_range(inode, start, end);
Filipe Manana669249e2014-09-02 11:09:58 +01002157 atomic_dec(&BTRFS_I(inode)->sync_writers);
Liu Bo343e4fc2017-11-15 16:10:28 -07002158 blk_finish_plug(&plug);
Filipe Manana669249e2014-09-02 11:09:58 +01002159
2160 return ret;
2161}
2162
Filipe Manana626e9f42021-04-27 11:27:20 +01002163static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
2164{
2165 struct btrfs_inode *inode = BTRFS_I(ctx->inode);
2166 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2167
2168 if (btrfs_inode_in_log(inode, fs_info->generation) &&
2169 list_empty(&ctx->ordered_extents))
2170 return true;
2171
2172 /*
2173 * If we are doing a fast fsync we can not bail out if the inode's
2174 * last_trans is <= then the last committed transaction, because we only
2175 * update the last_trans of the inode during ordered extent completion,
2176 * and for a fast fsync we don't wait for that, we only wait for the
2177 * writeback to complete.
2178 */
2179 if (inode->last_trans <= fs_info->last_trans_committed &&
2180 (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
2181 list_empty(&ctx->ordered_extents)))
2182 return true;
2183
2184 return false;
2185}
2186
Chris Masond352ac62008-09-29 15:18:18 -04002187/*
2188 * fsync call for both files and directories. This logs the inode into
2189 * the tree log instead of forcing full commits whenever possible.
2190 *
2191 * It needs to call filemap_fdatawait so that all ordered extent updates are
2192 * in the metadata btree are up to date for copying to the log.
2193 *
2194 * It drops the inode mutex before doing the tree log commit. This is an
2195 * important optimization for directories because holding the mutex prevents
2196 * new operations on the dir while we write to disk.
2197 */
Josef Bacik02c24a82011-07-16 20:44:56 -04002198int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04002199{
Filipe Mananade17e792016-03-30 19:03:13 -04002200 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +00002201 struct inode *inode = d_inode(dentry);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04002203 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002204 struct btrfs_trans_handle *trans;
Miao Xie8b050d32014-02-20 18:08:58 +08002205 struct btrfs_log_ctx ctx;
Jeff Layton333427a2017-07-06 07:02:31 -04002206 int ret = 0, err;
Filipe Manana48778172020-08-11 12:43:58 +01002207 u64 len;
2208 bool full_sync;
Chris Mason39279cc2007-06-12 06:35:45 -04002209
liubo1abe9b82011-03-24 11:18:59 +00002210 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04002211
Liu Boebb70442017-11-21 14:35:40 -07002212 btrfs_init_log_ctx(&ctx, inode);
2213
Miao Xie90abccf2012-09-13 04:53:47 -06002214 /*
Filipe Manana48778172020-08-11 12:43:58 +01002215 * Always set the range to a full range, otherwise we can get into
2216 * several problems, from missing file extent items to represent holes
2217 * when not using the NO_HOLES feature, to log tree corruption due to
2218 * races between hole detection during logging and completion of ordered
2219 * extents outside the range, to missing checksums due to ordered extents
2220 * for which we flushed only a subset of their pages.
Filipe Manana95418ed2020-03-09 12:41:05 +00002221 */
Filipe Manana48778172020-08-11 12:43:58 +01002222 start = 0;
2223 end = LLONG_MAX;
2224 len = (u64)LLONG_MAX + 1;
Filipe Manana95418ed2020-03-09 12:41:05 +00002225
2226 /*
Miao Xie90abccf2012-09-13 04:53:47 -06002227 * We write the dirty pages in the range and wait until they complete
2228 * out of the ->i_mutex. If so, we can flush the dirty pages by
Josef Bacik2ab28f32012-10-12 15:27:49 -04002229 * multi-task, and make the performance up. See
2230 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
Miao Xie90abccf2012-09-13 04:53:47 -06002231 */
Filipe Manana669249e2014-09-02 11:09:58 +01002232 ret = start_ordered_ops(inode, start, end);
Miao Xie90abccf2012-09-13 04:53:47 -06002233 if (ret)
Jeff Layton333427a2017-07-06 07:02:31 -04002234 goto out;
Miao Xie90abccf2012-09-13 04:53:47 -06002235
Filipe Manana885f46d2021-02-23 12:08:47 +00002236 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
Josef Bacikc4951442018-10-12 15:32:32 -04002237
Miao Xie2ecb7922012-09-06 04:04:27 -06002238 atomic_inc(&root->log_batch);
Josef Bacikb5e6c3e2018-05-23 11:58:33 -04002239
Filipe Manana669249e2014-09-02 11:09:58 +01002240 /*
Filipe Manana48778172020-08-11 12:43:58 +01002241 * Always check for the full sync flag while holding the inode's lock,
2242 * to avoid races with other tasks. The flag must be either set all the
2243 * time during logging or always off all the time while logging.
Filipe Manana7af59742020-04-07 11:37:44 +01002244 */
Filipe Manana48778172020-08-11 12:43:58 +01002245 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2246 &BTRFS_I(inode)->runtime_flags);
Filipe Manana7af59742020-04-07 11:37:44 +01002247
2248 /*
Filipe Manana885f46d2021-02-23 12:08:47 +00002249 * Before we acquired the inode's lock and the mmap lock, someone may
2250 * have dirtied more pages in the target range. We need to make sure
2251 * that writeback for any such pages does not start while we are logging
2252 * the inode, because if it does, any of the following might happen when
2253 * we are not doing a full inode sync:
Filipe Mananaaab15e82018-11-12 10:23:58 +00002254 *
2255 * 1) We log an extent after its writeback finishes but before its
2256 * checksums are added to the csum tree, leading to -EIO errors
2257 * when attempting to read the extent after a log replay.
2258 *
2259 * 2) We can end up logging an extent before its writeback finishes.
2260 * Therefore after the log replay we will have a file extent item
2261 * pointing to an unwritten extent (and no data checksums as well).
2262 *
2263 * So trigger writeback for any eventual new dirty pages and then we
2264 * wait for all ordered extents to complete below.
2265 */
2266 ret = start_ordered_ops(inode, start, end);
2267 if (ret) {
Filipe Manana885f46d2021-02-23 12:08:47 +00002268 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Filipe Mananaaab15e82018-11-12 10:23:58 +00002269 goto out;
2270 }
2271
2272 /*
Josef Bacikb5e6c3e2018-05-23 11:58:33 -04002273 * We have to do this here to avoid the priority inversion of waiting on
Andrea Gelmini52042d82018-11-28 12:05:13 +01002274 * IO of a lower priority task while holding a transaction open.
Filipe Mananaba0b0842019-10-16 16:28:52 +01002275 *
Filipe Manana48778172020-08-11 12:43:58 +01002276 * For a full fsync we wait for the ordered extents to complete while
2277 * for a fast fsync we wait just for writeback to complete, and then
2278 * attach the ordered extents to the transaction so that a transaction
2279 * commit waits for their completion, to avoid data loss if we fsync,
2280 * the current transaction commits before the ordered extents complete
2281 * and a power failure happens right after that.
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002282 *
2283 * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the
2284 * logical address recorded in the ordered extent may change. We need
2285 * to wait for the IO to stabilize the logical address.
Filipe Manana669249e2014-09-02 11:09:58 +01002286 */
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09002287 if (full_sync || btrfs_is_zoned(fs_info)) {
Filipe Manana48778172020-08-11 12:43:58 +01002288 ret = btrfs_wait_ordered_range(inode, start, len);
2289 } else {
2290 /*
2291 * Get our ordered extents as soon as possible to avoid doing
2292 * checksum lookups in the csum tree, and use instead the
2293 * checksums attached to the ordered extents.
2294 */
2295 btrfs_get_ordered_extents_for_logging(BTRFS_I(inode),
2296 &ctx.ordered_extents);
2297 ret = filemap_fdatawait_range(inode->i_mapping, start, end);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002298 }
Filipe Manana48778172020-08-11 12:43:58 +01002299
2300 if (ret)
2301 goto out_release_extents;
2302
Miao Xie2ecb7922012-09-06 04:04:27 -06002303 atomic_inc(&root->log_batch);
Chris Mason257c62e2009-10-13 13:21:08 -04002304
Josef Bacika4abeea2011-04-11 17:25:13 -04002305 smp_mb();
Filipe Manana626e9f42021-04-27 11:27:20 +01002306 if (skip_inode_logging(&ctx)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04002307 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002308 * We've had everything committed since the last time we were
Josef Bacik5dc562c2012-08-17 13:14:17 -04002309 * modified so clear this flag in case it was set for whatever
2310 * reason, it's no longer relevant.
2311 */
2312 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2313 &BTRFS_I(inode)->runtime_flags);
Filipe Manana0596a902016-06-14 14:18:27 +01002314 /*
2315 * An ordered extent might have started before and completed
2316 * already with io errors, in which case the inode was not
2317 * updated and we end up here. So check the inode's mapping
Jeff Layton333427a2017-07-06 07:02:31 -04002318 * for any errors that might have happened since we last
2319 * checked called fsync.
Filipe Manana0596a902016-06-14 14:18:27 +01002320 */
Jeff Layton333427a2017-07-06 07:02:31 -04002321 ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err);
Filipe Manana48778172020-08-11 12:43:58 +01002322 goto out_release_extents;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002323 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002324
2325 /*
Josef Bacik5039edd2014-01-15 13:34:13 -05002326 * We use start here because we will need to wait on the IO to complete
2327 * in btrfs_sync_log, which could require joining a transaction (for
2328 * example checking cross references in the nocow path). If we use join
2329 * here we could get into a situation where we're waiting on IO to
2330 * happen that is blocked on a transaction trying to commit. With start
2331 * we inc the extwriter counter, so we wait for all extwriters to exit
Andrea Gelmini52042d82018-11-28 12:05:13 +01002332 * before we start blocking joiners. This comment is to keep somebody
Josef Bacik5039edd2014-01-15 13:34:13 -05002333 * from thinking they are super smart and changing this to
2334 * btrfs_join_transaction *cough*Josef*cough*.
2335 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002336 trans = btrfs_start_transaction(root, 0);
2337 if (IS_ERR(trans)) {
2338 ret = PTR_ERR(trans);
Filipe Manana48778172020-08-11 12:43:58 +01002339 goto out_release_extents;
Chris Mason39279cc2007-06-12 06:35:45 -04002340 }
Filipe Mananad0c2f4f2021-01-27 10:35:00 +00002341 trans->in_fsync = true;
Chris Masone02119d2008-09-05 16:13:11 -04002342
Filipe Manana48778172020-08-11 12:43:58 +01002343 ret = btrfs_log_dentry_safe(trans, dentry, &ctx);
2344 btrfs_release_log_ctx_extents(&ctx);
Josef Bacik02c24a82011-07-16 20:44:56 -04002345 if (ret < 0) {
Filipe David Borba Mananaa0634be2013-09-11 20:36:44 +01002346 /* Fallthrough and commit/free transaction. */
2347 ret = 1;
Josef Bacik02c24a82011-07-16 20:44:56 -04002348 }
Chris Mason49eb7e42008-09-11 15:53:12 -04002349
2350 /* we've logged all the items and now have a consistent
2351 * version of the file in the log. It is possible that
2352 * someone will come in and modify the file, but that's
2353 * fine because the log is consistent on disk, and we
2354 * have references to all of the file's extents
2355 *
2356 * It is possible that someone will come in and log the
2357 * file again, but that will end up using the synchronization
2358 * inside btrfs_sync_log to keep things safe.
2359 */
Filipe Manana885f46d2021-02-23 12:08:47 +00002360 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Chris Mason49eb7e42008-09-11 15:53:12 -04002361
Chris Mason257c62e2009-10-13 13:21:08 -04002362 if (ret != BTRFS_NO_LOG_SYNC) {
Josef Bacik0ef8b722013-10-25 16:13:35 -04002363 if (!ret) {
Miao Xie8b050d32014-02-20 18:08:58 +08002364 ret = btrfs_sync_log(trans, root, &ctx);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002365 if (!ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002366 ret = btrfs_end_transaction(trans);
Josef Bacik0ef8b722013-10-25 16:13:35 -04002367 goto out;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002368 }
Chris Mason257c62e2009-10-13 13:21:08 -04002369 }
Filipe Manana48778172020-08-11 12:43:58 +01002370 if (!full_sync) {
2371 ret = btrfs_wait_ordered_range(inode, start, len);
2372 if (ret) {
2373 btrfs_end_transaction(trans);
2374 goto out;
2375 }
2376 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002377 ret = btrfs_commit_transaction(trans);
Chris Mason257c62e2009-10-13 13:21:08 -04002378 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002379 ret = btrfs_end_transaction(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002380 }
Chris Mason39279cc2007-06-12 06:35:45 -04002381out:
Liu Boebb70442017-11-21 14:35:40 -07002382 ASSERT(list_empty(&ctx.list));
Jeff Layton333427a2017-07-06 07:02:31 -04002383 err = file_check_and_advance_wb_err(file);
2384 if (!ret)
2385 ret = err;
Roel Kluin014e4ac2010-01-29 10:42:11 +00002386 return ret > 0 ? -EIO : ret;
Filipe Manana48778172020-08-11 12:43:58 +01002387
2388out_release_extents:
2389 btrfs_release_log_ctx_extents(&ctx);
Filipe Manana885f46d2021-02-23 12:08:47 +00002390 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Filipe Manana48778172020-08-11 12:43:58 +01002391 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002392}
2393
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002394static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04002395 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002396 .map_pages = filemap_map_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04002397 .page_mkwrite = btrfs_page_mkwrite,
2398};
2399
2400static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
2401{
Miao Xie058a4572010-05-20 07:21:50 +00002402 struct address_space *mapping = filp->f_mapping;
2403
2404 if (!mapping->a_ops->readpage)
2405 return -ENOEXEC;
2406
Chris Mason9ebefb182007-06-15 13:50:00 -04002407 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00002408 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00002409
Chris Mason9ebefb182007-06-15 13:50:00 -04002410 return 0;
2411}
2412
Nikolay Borisov35339c22017-02-20 13:50:46 +02002413static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf,
Josef Bacik2aaa6652012-08-29 14:27:18 -04002414 int slot, u64 start, u64 end)
2415{
2416 struct btrfs_file_extent_item *fi;
2417 struct btrfs_key key;
2418
2419 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
2420 return 0;
2421
2422 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov35339c22017-02-20 13:50:46 +02002423 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik2aaa6652012-08-29 14:27:18 -04002424 key.type != BTRFS_EXTENT_DATA_KEY)
2425 return 0;
2426
2427 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
2428
2429 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2430 return 0;
2431
2432 if (btrfs_file_extent_disk_bytenr(leaf, fi))
2433 return 0;
2434
2435 if (key.offset == end)
2436 return 1;
2437 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
2438 return 1;
2439 return 0;
2440}
2441
Nikolay Borisova012a742017-02-20 13:50:47 +02002442static int fill_holes(struct btrfs_trans_handle *trans,
2443 struct btrfs_inode *inode,
2444 struct btrfs_path *path, u64 offset, u64 end)
Josef Bacik2aaa6652012-08-29 14:27:18 -04002445{
David Sterba3ffbd682018-06-29 10:56:42 +02002446 struct btrfs_fs_info *fs_info = trans->fs_info;
Nikolay Borisova012a742017-02-20 13:50:47 +02002447 struct btrfs_root *root = inode->root;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002448 struct extent_buffer *leaf;
2449 struct btrfs_file_extent_item *fi;
2450 struct extent_map *hole_em;
Nikolay Borisova012a742017-02-20 13:50:47 +02002451 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002452 struct btrfs_key key;
2453 int ret;
2454
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 if (btrfs_fs_incompat(fs_info, NO_HOLES))
Josef Bacik16e75492013-10-22 12:18:51 -04002456 goto out;
2457
Nikolay Borisova012a742017-02-20 13:50:47 +02002458 key.objectid = btrfs_ino(inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002459 key.type = BTRFS_EXTENT_DATA_KEY;
2460 key.offset = offset;
2461
Josef Bacik2aaa6652012-08-29 14:27:18 -04002462 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacikf94480b2016-11-14 14:06:22 -05002463 if (ret <= 0) {
2464 /*
2465 * We should have dropped this offset, so if we find it then
2466 * something has gone horribly wrong.
2467 */
2468 if (ret == 0)
2469 ret = -EINVAL;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002470 return ret;
Josef Bacikf94480b2016-11-14 14:06:22 -05002471 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04002472
2473 leaf = path->nodes[0];
Nikolay Borisova012a742017-02-20 13:50:47 +02002474 if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04002475 u64 num_bytes;
2476
2477 path->slots[0]--;
2478 fi = btrfs_item_ptr(leaf, path->slots[0],
2479 struct btrfs_file_extent_item);
2480 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
2481 end - offset;
2482 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2483 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2484 btrfs_set_file_extent_offset(leaf, fi, 0);
2485 btrfs_mark_buffer_dirty(leaf);
2486 goto out;
2487 }
2488
chandan1707e262014-07-01 12:04:28 +05302489 if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04002490 u64 num_bytes;
2491
Josef Bacik2aaa6652012-08-29 14:27:18 -04002492 key.offset = offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 btrfs_set_item_key_safe(fs_info, path, &key);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002494 fi = btrfs_item_ptr(leaf, path->slots[0],
2495 struct btrfs_file_extent_item);
2496 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
2497 offset;
2498 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2499 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2500 btrfs_set_file_extent_offset(leaf, fi, 0);
2501 btrfs_mark_buffer_dirty(leaf);
2502 goto out;
2503 }
2504 btrfs_release_path(path);
2505
Nikolay Borisova012a742017-02-20 13:50:47 +02002506 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01002507 offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002508 if (ret)
2509 return ret;
2510
2511out:
2512 btrfs_release_path(path);
2513
2514 hole_em = alloc_extent_map();
2515 if (!hole_em) {
2516 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
Filipe Manana23e33372022-02-17 12:12:06 +00002517 btrfs_set_inode_full_sync(inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002518 } else {
2519 hole_em->start = offset;
2520 hole_em->len = end - offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04002521 hole_em->ram_bytes = hole_em->len;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002522 hole_em->orig_start = offset;
2523
2524 hole_em->block_start = EXTENT_MAP_HOLE;
2525 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05002526 hole_em->orig_block_len = 0;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002527 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2528 hole_em->generation = trans->transid;
2529
2530 do {
2531 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
2532 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002533 ret = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002534 write_unlock(&em_tree->lock);
2535 } while (ret == -EEXIST);
2536 free_extent_map(hole_em);
2537 if (ret)
Filipe Manana23e33372022-02-17 12:12:06 +00002538 btrfs_set_inode_full_sync(inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002539 }
2540
2541 return 0;
2542}
2543
Qu Wenruod7781542014-05-30 15:16:10 +08002544/*
2545 * Find a hole extent on given inode and change start/len to the end of hole
2546 * extent.(hole/vacuum extent whose em->start <= start &&
2547 * em->start + em->len > start)
2548 * When a hole extent is found, return 1 and modify start/len.
2549 */
Nikolay Borisovdea46d82020-11-02 16:49:01 +02002550static int find_first_non_hole(struct btrfs_inode *inode, u64 *start, u64 *len)
Qu Wenruod7781542014-05-30 15:16:10 +08002551{
Nikolay Borisovdea46d82020-11-02 16:49:01 +02002552 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Qu Wenruod7781542014-05-30 15:16:10 +08002553 struct extent_map *em;
2554 int ret = 0;
2555
Nikolay Borisovdea46d82020-11-02 16:49:01 +02002556 em = btrfs_get_extent(inode, NULL, 0,
Filipe Manana609805d2017-05-30 05:29:09 +01002557 round_down(*start, fs_info->sectorsize),
Omar Sandoval39b07b52019-12-02 17:34:23 -08002558 round_up(*len, fs_info->sectorsize));
Dan Carpenter99862772017-04-11 11:57:15 +03002559 if (IS_ERR(em))
2560 return PTR_ERR(em);
Qu Wenruod7781542014-05-30 15:16:10 +08002561
2562 /* Hole or vacuum extent(only exists in no-hole mode) */
2563 if (em->block_start == EXTENT_MAP_HOLE) {
2564 ret = 1;
2565 *len = em->start + em->len > *start + *len ?
2566 0 : *start + *len - em->start - em->len;
2567 *start = em->start + em->len;
2568 }
2569 free_extent_map(em);
2570 return ret;
2571}
2572
Filipe Mananaf27451f2017-10-25 11:55:28 +01002573static int btrfs_punch_hole_lock_range(struct inode *inode,
2574 const u64 lockstart,
2575 const u64 lockend,
2576 struct extent_state **cached_state)
2577{
Qu Wenruo05284762021-05-31 16:50:54 +08002578 /*
2579 * For subpage case, if the range is not at page boundary, we could
2580 * have pages at the leading/tailing part of the range.
2581 * This could lead to dead loop since filemap_range_has_page()
2582 * will always return true.
2583 * So here we need to do extra page alignment for
2584 * filemap_range_has_page().
2585 */
2586 const u64 page_lockstart = round_up(lockstart, PAGE_SIZE);
2587 const u64 page_lockend = round_down(lockend + 1, PAGE_SIZE) - 1;
2588
Filipe Mananaf27451f2017-10-25 11:55:28 +01002589 while (1) {
2590 struct btrfs_ordered_extent *ordered;
2591 int ret;
2592
2593 truncate_pagecache_range(inode, lockstart, lockend);
2594
2595 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2596 cached_state);
Nikolay Borisov6d072c82020-08-31 14:42:39 +03002597 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
2598 lockend);
Filipe Mananaf27451f2017-10-25 11:55:28 +01002599
2600 /*
2601 * We need to make sure we have no ordered extents in this range
2602 * and nobody raced in and read a page in this range, if we did
2603 * we need to try again.
2604 */
2605 if ((!ordered ||
Omar Sandovalbffe6332019-12-02 17:34:19 -08002606 (ordered->file_offset + ordered->num_bytes <= lockstart ||
Filipe Mananaf27451f2017-10-25 11:55:28 +01002607 ordered->file_offset > lockend)) &&
David Sterba051c98e2018-03-07 15:33:22 +01002608 !filemap_range_has_page(inode->i_mapping,
Qu Wenruo05284762021-05-31 16:50:54 +08002609 page_lockstart, page_lockend)) {
Filipe Mananaf27451f2017-10-25 11:55:28 +01002610 if (ordered)
2611 btrfs_put_ordered_extent(ordered);
2612 break;
2613 }
2614 if (ordered)
2615 btrfs_put_ordered_extent(ordered);
2616 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
2617 lockend, cached_state);
2618 ret = btrfs_wait_ordered_range(inode, lockstart,
2619 lockend - lockstart + 1);
2620 if (ret)
2621 return ret;
2622 }
2623 return 0;
2624}
2625
Filipe Manana0cbb5bd2020-09-08 11:27:24 +01002626static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002627 struct btrfs_inode *inode,
Filipe Manana690a5db2019-07-05 11:09:50 +01002628 struct btrfs_path *path,
Filipe Mananabf385642020-09-08 11:27:22 +01002629 struct btrfs_replace_extent_info *extent_info,
Filipe Manana2766ff62020-11-04 11:07:34 +00002630 const u64 replace_len,
2631 const u64 bytes_to_drop)
Filipe Manana690a5db2019-07-05 11:09:50 +01002632{
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002633 struct btrfs_fs_info *fs_info = trans->fs_info;
2634 struct btrfs_root *root = inode->root;
Filipe Manana690a5db2019-07-05 11:09:50 +01002635 struct btrfs_file_extent_item *extent;
2636 struct extent_buffer *leaf;
2637 struct btrfs_key key;
2638 int slot;
2639 struct btrfs_ref ref = { 0 };
Filipe Manana690a5db2019-07-05 11:09:50 +01002640 int ret;
2641
Filipe Mananabf385642020-09-08 11:27:22 +01002642 if (replace_len == 0)
Filipe Manana690a5db2019-07-05 11:09:50 +01002643 return 0;
2644
Filipe Mananabf385642020-09-08 11:27:22 +01002645 if (extent_info->disk_offset == 0 &&
Filipe Manana2766ff62020-11-04 11:07:34 +00002646 btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002647 btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
Filipe Manana690a5db2019-07-05 11:09:50 +01002648 return 0;
Filipe Manana2766ff62020-11-04 11:07:34 +00002649 }
Filipe Manana690a5db2019-07-05 11:09:50 +01002650
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002651 key.objectid = btrfs_ino(inode);
Filipe Manana690a5db2019-07-05 11:09:50 +01002652 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Mananabf385642020-09-08 11:27:22 +01002653 key.offset = extent_info->file_offset;
Filipe Manana690a5db2019-07-05 11:09:50 +01002654 ret = btrfs_insert_empty_item(trans, root, path, &key,
Filipe Mananafb870f62020-09-08 11:27:21 +01002655 sizeof(struct btrfs_file_extent_item));
Filipe Manana690a5db2019-07-05 11:09:50 +01002656 if (ret)
2657 return ret;
2658 leaf = path->nodes[0];
2659 slot = path->slots[0];
Filipe Mananabf385642020-09-08 11:27:22 +01002660 write_extent_buffer(leaf, extent_info->extent_buf,
Filipe Manana690a5db2019-07-05 11:09:50 +01002661 btrfs_item_ptr_offset(leaf, slot),
Filipe Mananafb870f62020-09-08 11:27:21 +01002662 sizeof(struct btrfs_file_extent_item));
Filipe Manana690a5db2019-07-05 11:09:50 +01002663 extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
Filipe Mananafb870f62020-09-08 11:27:21 +01002664 ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE);
Filipe Mananabf385642020-09-08 11:27:22 +01002665 btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset);
2666 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
2667 if (extent_info->is_new_extent)
Filipe Manana8fccebf2020-09-08 11:27:20 +01002668 btrfs_set_file_extent_generation(leaf, extent, trans->transid);
Filipe Manana690a5db2019-07-05 11:09:50 +01002669 btrfs_mark_buffer_dirty(leaf);
2670 btrfs_release_path(path);
2671
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002672 ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,
2673 replace_len);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002674 if (ret)
2675 return ret;
2676
Filipe Manana690a5db2019-07-05 11:09:50 +01002677 /* If it's a hole, nothing more needs to be done. */
Filipe Manana2766ff62020-11-04 11:07:34 +00002678 if (extent_info->disk_offset == 0) {
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002679 btrfs_update_inode_bytes(inode, 0, bytes_to_drop);
Filipe Manana690a5db2019-07-05 11:09:50 +01002680 return 0;
Filipe Manana2766ff62020-11-04 11:07:34 +00002681 }
Filipe Manana690a5db2019-07-05 11:09:50 +01002682
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002683 btrfs_update_inode_bytes(inode, replace_len, bytes_to_drop);
Filipe Manana8fccebf2020-09-08 11:27:20 +01002684
Filipe Mananabf385642020-09-08 11:27:22 +01002685 if (extent_info->is_new_extent && extent_info->insertions == 0) {
2686 key.objectid = extent_info->disk_offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01002687 key.type = BTRFS_EXTENT_ITEM_KEY;
Filipe Mananabf385642020-09-08 11:27:22 +01002688 key.offset = extent_info->disk_len;
Filipe Manana8fccebf2020-09-08 11:27:20 +01002689 ret = btrfs_alloc_reserved_file_extent(trans, root,
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002690 btrfs_ino(inode),
Filipe Mananabf385642020-09-08 11:27:22 +01002691 extent_info->file_offset,
2692 extent_info->qgroup_reserved,
Filipe Manana8fccebf2020-09-08 11:27:20 +01002693 &key);
2694 } else {
2695 u64 ref_offset;
2696
2697 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
Filipe Mananabf385642020-09-08 11:27:22 +01002698 extent_info->disk_offset,
2699 extent_info->disk_len, 0);
2700 ref_offset = extent_info->file_offset - extent_info->data_offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01002701 btrfs_init_data_ref(&ref, root->root_key.objectid,
Nikolay Borisovf42c5da2021-10-12 11:21:35 +03002702 btrfs_ino(inode), ref_offset, 0, false);
Filipe Manana8fccebf2020-09-08 11:27:20 +01002703 ret = btrfs_inc_extent_ref(trans, &ref);
2704 }
2705
Filipe Mananabf385642020-09-08 11:27:22 +01002706 extent_info->insertions++;
Filipe Manana690a5db2019-07-05 11:09:50 +01002707
2708 return ret;
2709}
2710
Filipe Manana9cba40a2019-06-28 23:11:26 +01002711/*
2712 * The respective range must have been previously locked, as well as the inode.
2713 * The end offset is inclusive (last byte of the range).
Filipe Mananabf385642020-09-08 11:27:22 +01002714 * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing
2715 * the file range with an extent.
2716 * When not punching a hole, we don't want to end up in a state where we dropped
2717 * extents without inserting a new one, so we must abort the transaction to avoid
2718 * a corruption.
Filipe Manana9cba40a2019-06-28 23:11:26 +01002719 */
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002720int btrfs_replace_file_extents(struct btrfs_inode *inode,
2721 struct btrfs_path *path, const u64 start,
2722 const u64 end,
2723 struct btrfs_replace_extent_info *extent_info,
2724 struct btrfs_trans_handle **trans_out)
Filipe Manana9cba40a2019-06-28 23:11:26 +01002725{
Filipe Manana5893dfb2020-11-04 11:07:32 +00002726 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002727 struct btrfs_root *root = inode->root;
2728 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik2bd36e72019-08-22 15:14:33 -04002729 u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1);
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002730 u64 ino_size = round_up(inode->vfs_inode.i_size, fs_info->sectorsize);
Filipe Manana9cba40a2019-06-28 23:11:26 +01002731 struct btrfs_trans_handle *trans = NULL;
2732 struct btrfs_block_rsv *rsv;
2733 unsigned int rsv_count;
2734 u64 cur_offset;
Filipe Manana9cba40a2019-06-28 23:11:26 +01002735 u64 len = end - start;
2736 int ret = 0;
2737
2738 if (end <= start)
2739 return -EINVAL;
2740
2741 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
2742 if (!rsv) {
2743 ret = -ENOMEM;
2744 goto out;
2745 }
Josef Bacik2bd36e72019-08-22 15:14:33 -04002746 rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1);
Filipe Manana9cba40a2019-06-28 23:11:26 +01002747 rsv->failfast = 1;
2748
2749 /*
2750 * 1 - update the inode
2751 * 1 - removing the extents in the range
Filipe Mananabf385642020-09-08 11:27:22 +01002752 * 1 - adding the hole extent if no_holes isn't set or if we are
2753 * replacing the range with a new extent
Filipe Manana9cba40a2019-06-28 23:11:26 +01002754 */
Filipe Mananabf385642020-09-08 11:27:22 +01002755 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info)
Filipe Manana690a5db2019-07-05 11:09:50 +01002756 rsv_count = 3;
2757 else
2758 rsv_count = 2;
2759
Filipe Manana9cba40a2019-06-28 23:11:26 +01002760 trans = btrfs_start_transaction(root, rsv_count);
2761 if (IS_ERR(trans)) {
2762 ret = PTR_ERR(trans);
2763 trans = NULL;
2764 goto out_free;
2765 }
2766
2767 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
2768 min_size, false);
2769 BUG_ON(ret);
2770 trans->block_rsv = rsv;
2771
2772 cur_offset = start;
Filipe Manana5893dfb2020-11-04 11:07:32 +00002773 drop_args.path = path;
2774 drop_args.end = end + 1;
2775 drop_args.drop_cache = true;
Filipe Manana9cba40a2019-06-28 23:11:26 +01002776 while (cur_offset < end) {
Filipe Manana5893dfb2020-11-04 11:07:32 +00002777 drop_args.start = cur_offset;
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002778 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Filipe Manana2766ff62020-11-04 11:07:34 +00002779 /* If we are punching a hole decrement the inode's byte count */
2780 if (!extent_info)
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002781 btrfs_update_inode_bytes(inode, 0,
Filipe Manana2766ff62020-11-04 11:07:34 +00002782 drop_args.bytes_found);
Filipe Manana690a5db2019-07-05 11:09:50 +01002783 if (ret != -ENOSPC) {
2784 /*
Josef Bacik4afb9122021-10-05 16:35:27 -04002785 * The only time we don't want to abort is if we are
2786 * attempting to clone a partial inline extent, in which
2787 * case we'll get EOPNOTSUPP. However if we aren't
2788 * clone we need to abort no matter what, because if we
2789 * got EOPNOTSUPP via prealloc then we messed up and
2790 * need to abort.
Filipe Manana690a5db2019-07-05 11:09:50 +01002791 */
Josef Bacik4afb9122021-10-05 16:35:27 -04002792 if (ret &&
2793 (ret != -EOPNOTSUPP ||
2794 (extent_info && extent_info->is_new_extent)))
Filipe Manana690a5db2019-07-05 11:09:50 +01002795 btrfs_abort_transaction(trans, ret);
Filipe Manana9cba40a2019-06-28 23:11:26 +01002796 break;
Filipe Manana690a5db2019-07-05 11:09:50 +01002797 }
Filipe Manana9cba40a2019-06-28 23:11:26 +01002798
2799 trans->block_rsv = &fs_info->trans_block_rsv;
2800
Filipe Manana5893dfb2020-11-04 11:07:32 +00002801 if (!extent_info && cur_offset < drop_args.drop_end &&
Filipe Manana690a5db2019-07-05 11:09:50 +01002802 cur_offset < ino_size) {
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002803 ret = fill_holes(trans, inode, path, cur_offset,
2804 drop_args.drop_end);
Filipe Manana9cba40a2019-06-28 23:11:26 +01002805 if (ret) {
2806 /*
2807 * If we failed then we didn't insert our hole
2808 * entries for the area we dropped, so now the
2809 * fs is corrupted, so we must abort the
2810 * transaction.
2811 */
2812 btrfs_abort_transaction(trans, ret);
2813 break;
2814 }
Filipe Manana5893dfb2020-11-04 11:07:32 +00002815 } else if (!extent_info && cur_offset < drop_args.drop_end) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05002816 /*
2817 * We are past the i_size here, but since we didn't
2818 * insert holes we need to clear the mapped area so we
2819 * know to not set disk_i_size in this area until a new
2820 * file extent is inserted here.
2821 */
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002822 ret = btrfs_inode_clear_file_extent_range(inode,
Filipe Manana5893dfb2020-11-04 11:07:32 +00002823 cur_offset,
2824 drop_args.drop_end - cur_offset);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002825 if (ret) {
2826 /*
2827 * We couldn't clear our area, so we could
2828 * presumably adjust up and corrupt the fs, so
2829 * we need to abort.
2830 */
2831 btrfs_abort_transaction(trans, ret);
2832 break;
2833 }
Filipe Manana9cba40a2019-06-28 23:11:26 +01002834 }
2835
Filipe Manana5893dfb2020-11-04 11:07:32 +00002836 if (extent_info &&
2837 drop_args.drop_end > extent_info->file_offset) {
2838 u64 replace_len = drop_args.drop_end -
2839 extent_info->file_offset;
Filipe Manana690a5db2019-07-05 11:09:50 +01002840
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002841 ret = btrfs_insert_replace_extent(trans, inode, path,
2842 extent_info, replace_len,
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002843 drop_args.bytes_found);
Filipe Manana690a5db2019-07-05 11:09:50 +01002844 if (ret) {
2845 btrfs_abort_transaction(trans, ret);
2846 break;
2847 }
Filipe Mananabf385642020-09-08 11:27:22 +01002848 extent_info->data_len -= replace_len;
2849 extent_info->data_offset += replace_len;
2850 extent_info->file_offset += replace_len;
Filipe Manana690a5db2019-07-05 11:09:50 +01002851 }
2852
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002853 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9cba40a2019-06-28 23:11:26 +01002854 if (ret)
2855 break;
2856
2857 btrfs_end_transaction(trans);
2858 btrfs_btree_balance_dirty(fs_info);
2859
2860 trans = btrfs_start_transaction(root, rsv_count);
2861 if (IS_ERR(trans)) {
2862 ret = PTR_ERR(trans);
2863 trans = NULL;
2864 break;
2865 }
2866
2867 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
2868 rsv, min_size, false);
2869 BUG_ON(ret); /* shouldn't happen */
2870 trans->block_rsv = rsv;
2871
BingJing Chang32277882021-03-25 09:56:22 +08002872 cur_offset = drop_args.drop_end;
2873 len = end - cur_offset;
2874 if (!extent_info && len) {
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002875 ret = find_first_non_hole(inode, &cur_offset, &len);
Filipe Manana690a5db2019-07-05 11:09:50 +01002876 if (unlikely(ret < 0))
2877 break;
2878 if (ret && !len) {
2879 ret = 0;
2880 break;
2881 }
Filipe Manana9cba40a2019-06-28 23:11:26 +01002882 }
2883 }
2884
Filipe Manana690a5db2019-07-05 11:09:50 +01002885 /*
2886 * If we were cloning, force the next fsync to be a full one since we
2887 * we replaced (or just dropped in the case of cloning holes when
Filipe Mananae2b84212021-03-26 13:14:41 +00002888 * NO_HOLES is enabled) file extent items and did not setup new extent
2889 * maps for the replacement extents (or holes).
Filipe Manana690a5db2019-07-05 11:09:50 +01002890 */
Filipe Mananabf385642020-09-08 11:27:22 +01002891 if (extent_info && !extent_info->is_new_extent)
Filipe Manana23e33372022-02-17 12:12:06 +00002892 btrfs_set_inode_full_sync(inode);
Filipe Manana690a5db2019-07-05 11:09:50 +01002893
Filipe Manana9cba40a2019-06-28 23:11:26 +01002894 if (ret)
2895 goto out_trans;
2896
2897 trans->block_rsv = &fs_info->trans_block_rsv;
2898 /*
2899 * If we are using the NO_HOLES feature we might have had already an
2900 * hole that overlaps a part of the region [lockstart, lockend] and
2901 * ends at (or beyond) lockend. Since we have no file extent items to
2902 * represent holes, drop_end can be less than lockend and so we must
2903 * make sure we have an extent map representing the existing hole (the
2904 * call to __btrfs_drop_extents() might have dropped the existing extent
2905 * map representing the existing hole), otherwise the fast fsync path
2906 * will not record the existence of the hole region
2907 * [existing_hole_start, lockend].
2908 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002909 if (drop_args.drop_end <= end)
2910 drop_args.drop_end = end + 1;
Filipe Manana9cba40a2019-06-28 23:11:26 +01002911 /*
2912 * Don't insert file hole extent item if it's for a range beyond eof
2913 * (because it's useless) or if it represents a 0 bytes range (when
2914 * cur_offset == drop_end).
2915 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002916 if (!extent_info && cur_offset < ino_size &&
2917 cur_offset < drop_args.drop_end) {
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002918 ret = fill_holes(trans, inode, path, cur_offset,
2919 drop_args.drop_end);
Filipe Manana9cba40a2019-06-28 23:11:26 +01002920 if (ret) {
2921 /* Same comment as above. */
2922 btrfs_abort_transaction(trans, ret);
2923 goto out_trans;
2924 }
Filipe Manana5893dfb2020-11-04 11:07:32 +00002925 } else if (!extent_info && cur_offset < drop_args.drop_end) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05002926 /* See the comment in the loop above for the reasoning here. */
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002927 ret = btrfs_inode_clear_file_extent_range(inode, cur_offset,
2928 drop_args.drop_end - cur_offset);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002929 if (ret) {
2930 btrfs_abort_transaction(trans, ret);
2931 goto out_trans;
2932 }
2933
Filipe Manana9cba40a2019-06-28 23:11:26 +01002934 }
Filipe Mananabf385642020-09-08 11:27:22 +01002935 if (extent_info) {
Nikolay Borisovbfc78472021-02-17 15:12:47 +02002936 ret = btrfs_insert_replace_extent(trans, inode, path,
Nikolay Borisov03fcb1a2020-11-02 16:49:02 +02002937 extent_info, extent_info->data_len,
2938 drop_args.bytes_found);
Filipe Manana690a5db2019-07-05 11:09:50 +01002939 if (ret) {
2940 btrfs_abort_transaction(trans, ret);
2941 goto out_trans;
2942 }
2943 }
Filipe Manana9cba40a2019-06-28 23:11:26 +01002944
2945out_trans:
2946 if (!trans)
2947 goto out_free;
2948
2949 trans->block_rsv = &fs_info->trans_block_rsv;
2950 if (ret)
2951 btrfs_end_transaction(trans);
2952 else
2953 *trans_out = trans;
2954out_free:
2955 btrfs_free_block_rsv(fs_info, rsv);
2956out:
2957 return ret;
2958}
2959
Darrick J. Wong05fd9562022-03-14 10:55:32 -07002960static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len)
Josef Bacik2aaa6652012-08-29 14:27:18 -04002961{
Darrick J. Wong05fd9562022-03-14 10:55:32 -07002962 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002963 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04002964 struct btrfs_root *root = BTRFS_I(inode)->root;
2965 struct extent_state *cached_state = NULL;
2966 struct btrfs_path *path;
Filipe Manana9cba40a2019-06-28 23:11:26 +01002967 struct btrfs_trans_handle *trans = NULL;
Qu Wenruod7781542014-05-30 15:16:10 +08002968 u64 lockstart;
2969 u64 lockend;
2970 u64 tail_start;
2971 u64 tail_len;
2972 u64 orig_start = offset;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002973 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302974 bool same_block;
Filipe Mananaa1a50f62014-04-26 01:35:31 +01002975 u64 ino_size;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05302976 bool truncated_block = false;
Filipe Mananae8c1c762015-02-15 22:38:54 +00002977 bool updated_inode = false;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002978
Josef Bacik0ef8b722013-10-25 16:13:35 -04002979 ret = btrfs_wait_ordered_range(inode, offset, len);
2980 if (ret)
2981 return ret;
Josef Bacik2aaa6652012-08-29 14:27:18 -04002982
Josef Bacik8d9b4a12021-02-10 17:14:36 -05002983 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002984 ino_size = round_up(inode->i_size, fs_info->sectorsize);
Nikolay Borisovdea46d82020-11-02 16:49:01 +02002985 ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
Qu Wenruod7781542014-05-30 15:16:10 +08002986 if (ret < 0)
2987 goto out_only_mutex;
2988 if (ret && !len) {
2989 /* Already in a large hole */
2990 ret = 0;
2991 goto out_only_mutex;
2992 }
2993
Darrick J. Wong05fd9562022-03-14 10:55:32 -07002994 ret = file_modified(file);
2995 if (ret)
2996 goto out_only_mutex;
2997
Nikolay Borisov6fee2482020-08-31 14:42:42 +03002998 lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode)));
Qu Wenruod7781542014-05-30 15:16:10 +08002999 lockend = round_down(offset + len,
Nikolay Borisov6fee2482020-08-31 14:42:42 +03003000 btrfs_inode_sectorsize(BTRFS_I(inode))) - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
3002 == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
Miao Xie7426cc02012-12-05 10:54:52 +00003003 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303004 * We needn't truncate any block which is beyond the end of the file
Miao Xie7426cc02012-12-05 10:54:52 +00003005 * because we are sure there is no data there.
3006 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003007 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303008 * Only do this if we are in the same block and we aren't doing the
3009 * entire block.
Josef Bacik2aaa6652012-08-29 14:27:18 -04003010 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003011 if (same_block && len < fs_info->sectorsize) {
Filipe Mananae8c1c762015-02-15 22:38:54 +00003012 if (offset < ino_size) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303013 truncated_block = true;
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003014 ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
3015 0);
Filipe Mananae8c1c762015-02-15 22:38:54 +00003016 } else {
3017 ret = 0;
3018 }
Qu Wenruod7781542014-05-30 15:16:10 +08003019 goto out_only_mutex;
Josef Bacik2aaa6652012-08-29 14:27:18 -04003020 }
3021
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303022 /* zero back part of the first block */
Filipe Manana12870f12014-02-15 15:55:58 +00003023 if (offset < ino_size) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303024 truncated_block = true;
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003025 ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
Miao Xie7426cc02012-12-05 10:54:52 +00003026 if (ret) {
Josef Bacik8d9b4a12021-02-10 17:14:36 -05003027 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Miao Xie7426cc02012-12-05 10:54:52 +00003028 return ret;
3029 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04003030 }
3031
Qu Wenruod7781542014-05-30 15:16:10 +08003032 /* Check the aligned pages after the first unaligned page,
3033 * if offset != orig_start, which means the first unaligned page
Nicholas D Steeves01327612016-05-19 21:18:45 -04003034 * including several following pages are already in holes,
Qu Wenruod7781542014-05-30 15:16:10 +08003035 * the extra check can be skipped */
3036 if (offset == orig_start) {
3037 /* after truncate page, check hole again */
3038 len = offset + len - lockstart;
3039 offset = lockstart;
Nikolay Borisovdea46d82020-11-02 16:49:01 +02003040 ret = find_first_non_hole(BTRFS_I(inode), &offset, &len);
Qu Wenruod7781542014-05-30 15:16:10 +08003041 if (ret < 0)
3042 goto out_only_mutex;
3043 if (ret && !len) {
3044 ret = 0;
3045 goto out_only_mutex;
3046 }
3047 lockstart = offset;
3048 }
3049
3050 /* Check the tail unaligned part is in a hole */
3051 tail_start = lockend + 1;
3052 tail_len = offset + len - tail_start;
3053 if (tail_len) {
Nikolay Borisovdea46d82020-11-02 16:49:01 +02003054 ret = find_first_non_hole(BTRFS_I(inode), &tail_start, &tail_len);
Qu Wenruod7781542014-05-30 15:16:10 +08003055 if (unlikely(ret < 0))
3056 goto out_only_mutex;
3057 if (!ret) {
3058 /* zero the front end of the last page */
3059 if (tail_start + tail_len < ino_size) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303060 truncated_block = true;
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003061 ret = btrfs_truncate_block(BTRFS_I(inode),
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303062 tail_start + tail_len,
3063 0, 1);
Qu Wenruod7781542014-05-30 15:16:10 +08003064 if (ret)
3065 goto out_only_mutex;
Qu Wenruo51f395a2014-08-08 13:06:20 +08003066 }
Miao Xie00612802012-12-05 10:54:12 +00003067 }
Josef Bacik2aaa6652012-08-29 14:27:18 -04003068 }
3069
3070 if (lockend < lockstart) {
Filipe Mananae8c1c762015-02-15 22:38:54 +00003071 ret = 0;
3072 goto out_only_mutex;
Josef Bacik2aaa6652012-08-29 14:27:18 -04003073 }
3074
Filipe Mananaf27451f2017-10-25 11:55:28 +01003075 ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
3076 &cached_state);
Josef Bacik8fca9552019-05-03 11:10:06 -04003077 if (ret)
Filipe Mananaf27451f2017-10-25 11:55:28 +01003078 goto out_only_mutex;
Josef Bacik2aaa6652012-08-29 14:27:18 -04003079
3080 path = btrfs_alloc_path();
3081 if (!path) {
3082 ret = -ENOMEM;
3083 goto out;
3084 }
3085
Nikolay Borisovbfc78472021-02-17 15:12:47 +02003086 ret = btrfs_replace_file_extents(BTRFS_I(inode), path, lockstart,
3087 lockend, NULL, &trans);
Filipe Manana9cba40a2019-06-28 23:11:26 +01003088 btrfs_free_path(path);
3089 if (ret)
3090 goto out;
Josef Bacik2aaa6652012-08-29 14:27:18 -04003091
Filipe Manana9cba40a2019-06-28 23:11:26 +01003092 ASSERT(trans != NULL);
Tsutomu Itohe1f57902012-11-08 04:47:33 +00003093 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003094 inode->i_mtime = inode->i_ctime = current_time(inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003095 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananae8c1c762015-02-15 22:38:54 +00003096 updated_inode = true;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003097 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003098 btrfs_btree_balance_dirty(fs_info);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003099out:
3100 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01003101 &cached_state);
Qu Wenruod7781542014-05-30 15:16:10 +08003102out_only_mutex:
Filipe Manana9cba40a2019-06-28 23:11:26 +01003103 if (!updated_inode && truncated_block && !ret) {
Filipe Mananae8c1c762015-02-15 22:38:54 +00003104 /*
3105 * If we only end up zeroing part of a page, we still need to
3106 * update the inode item, so that all the time fields are
3107 * updated as well as the necessary btrfs inode in memory fields
3108 * for detecting, at fsync time, if the inode isn't yet in the
3109 * log tree or it's there but not up to date.
3110 */
Filipe Manana17900662019-06-19 13:05:50 +01003111 struct timespec64 now = current_time(inode);
3112
3113 inode_inc_iversion(inode);
3114 inode->i_mtime = now;
3115 inode->i_ctime = now;
Filipe Mananae8c1c762015-02-15 22:38:54 +00003116 trans = btrfs_start_transaction(root, 1);
3117 if (IS_ERR(trans)) {
Filipe Manana9cba40a2019-06-28 23:11:26 +01003118 ret = PTR_ERR(trans);
Filipe Mananae8c1c762015-02-15 22:38:54 +00003119 } else {
Filipe Manana9cba40a2019-06-28 23:11:26 +01003120 int ret2;
3121
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003122 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Manana9cba40a2019-06-28 23:11:26 +01003123 ret2 = btrfs_end_transaction(trans);
3124 if (!ret)
3125 ret = ret2;
Filipe Mananae8c1c762015-02-15 22:38:54 +00003126 }
3127 }
Josef Bacik8d9b4a12021-02-10 17:14:36 -05003128 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Filipe Manana9cba40a2019-06-28 23:11:26 +01003129 return ret;
Josef Bacik2aaa6652012-08-29 14:27:18 -04003130}
3131
Qu Wenruo14524a82015-09-08 17:22:44 +08003132/* Helper structure to record which range is already reserved */
3133struct falloc_range {
3134 struct list_head list;
3135 u64 start;
3136 u64 len;
3137};
3138
3139/*
3140 * Helper function to add falloc range
3141 *
3142 * Caller should have locked the larger range of extent containing
3143 * [start, len)
3144 */
3145static int add_falloc_range(struct list_head *head, u64 start, u64 len)
3146{
Qu Wenruo14524a82015-09-08 17:22:44 +08003147 struct falloc_range *range = NULL;
3148
Nikolay Borisov77d25532021-06-01 09:08:15 +03003149 if (!list_empty(head)) {
3150 /*
3151 * As fallocate iterates by bytenr order, we only need to check
3152 * the last range.
3153 */
3154 range = list_last_entry(head, struct falloc_range, list);
3155 if (range->start + range->len == start) {
3156 range->len += len;
3157 return 0;
3158 }
Qu Wenruo14524a82015-09-08 17:22:44 +08003159 }
Nikolay Borisov77d25532021-06-01 09:08:15 +03003160
David Sterba32fc9322016-02-11 14:25:38 +01003161 range = kmalloc(sizeof(*range), GFP_KERNEL);
Qu Wenruo14524a82015-09-08 17:22:44 +08003162 if (!range)
3163 return -ENOMEM;
3164 range->start = start;
3165 range->len = len;
3166 list_add_tail(&range->list, head);
3167 return 0;
3168}
3169
Filipe Mananaf27451f2017-10-25 11:55:28 +01003170static int btrfs_fallocate_update_isize(struct inode *inode,
3171 const u64 end,
3172 const int mode)
3173{
3174 struct btrfs_trans_handle *trans;
3175 struct btrfs_root *root = BTRFS_I(inode)->root;
3176 int ret;
3177 int ret2;
3178
3179 if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
3180 return 0;
3181
3182 trans = btrfs_start_transaction(root, 1);
3183 if (IS_ERR(trans))
3184 return PTR_ERR(trans);
3185
3186 inode->i_ctime = current_time(inode);
3187 i_size_write(inode, end);
Nikolay Borisov76aea532020-11-02 16:48:53 +02003188 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003189 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananaf27451f2017-10-25 11:55:28 +01003190 ret2 = btrfs_end_transaction(trans);
3191
3192 return ret ? ret : ret2;
3193}
3194
Filipe Manana81fdf632018-01-18 11:34:31 +00003195enum {
David Sterbaf262fa82019-06-18 20:00:08 +02003196 RANGE_BOUNDARY_WRITTEN_EXTENT,
3197 RANGE_BOUNDARY_PREALLOC_EXTENT,
3198 RANGE_BOUNDARY_HOLE,
Filipe Manana81fdf632018-01-18 11:34:31 +00003199};
3200
Nikolay Borisov948dfeb2020-08-31 14:42:48 +03003201static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
Filipe Mananaf27451f2017-10-25 11:55:28 +01003202 u64 offset)
3203{
Nikolay Borisov948dfeb2020-08-31 14:42:48 +03003204 const u64 sectorsize = btrfs_inode_sectorsize(inode);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003205 struct extent_map *em;
Filipe Manana81fdf632018-01-18 11:34:31 +00003206 int ret;
Filipe Mananaf27451f2017-10-25 11:55:28 +01003207
3208 offset = round_down(offset, sectorsize);
Nikolay Borisov948dfeb2020-08-31 14:42:48 +03003209 em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003210 if (IS_ERR(em))
3211 return PTR_ERR(em);
3212
3213 if (em->block_start == EXTENT_MAP_HOLE)
Filipe Manana81fdf632018-01-18 11:34:31 +00003214 ret = RANGE_BOUNDARY_HOLE;
3215 else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3216 ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
3217 else
3218 ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
Filipe Mananaf27451f2017-10-25 11:55:28 +01003219
3220 free_extent_map(em);
3221 return ret;
3222}
3223
3224static int btrfs_zero_range(struct inode *inode,
3225 loff_t offset,
3226 loff_t len,
3227 const int mode)
3228{
3229 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3230 struct extent_map *em;
3231 struct extent_changeset *data_reserved = NULL;
3232 int ret;
3233 u64 alloc_hint = 0;
Nikolay Borisov6fee2482020-08-31 14:42:42 +03003234 const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode));
Filipe Mananaf27451f2017-10-25 11:55:28 +01003235 u64 alloc_start = round_down(offset, sectorsize);
3236 u64 alloc_end = round_up(offset + len, sectorsize);
3237 u64 bytes_to_reserve = 0;
3238 bool space_reserved = false;
3239
3240 inode_dio_wait(inode);
3241
Omar Sandoval39b07b52019-12-02 17:34:23 -08003242 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
3243 alloc_end - alloc_start);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003244 if (IS_ERR(em)) {
3245 ret = PTR_ERR(em);
3246 goto out;
3247 }
3248
3249 /*
3250 * Avoid hole punching and extent allocation for some cases. More cases
3251 * could be considered, but these are unlikely common and we keep things
3252 * as simple as possible for now. Also, intentionally, if the target
3253 * range contains one or more prealloc extents together with regular
3254 * extents and holes, we drop all the existing extents and allocate a
3255 * new prealloc extent, so that we get a larger contiguous disk extent.
3256 */
3257 if (em->start <= alloc_start &&
3258 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3259 const u64 em_end = em->start + em->len;
3260
3261 if (em_end >= offset + len) {
3262 /*
3263 * The whole range is already a prealloc extent,
3264 * do nothing except updating the inode's i_size if
3265 * needed.
3266 */
3267 free_extent_map(em);
3268 ret = btrfs_fallocate_update_isize(inode, offset + len,
3269 mode);
3270 goto out;
3271 }
3272 /*
3273 * Part of the range is already a prealloc extent, so operate
3274 * only on the remaining part of the range.
3275 */
3276 alloc_start = em_end;
3277 ASSERT(IS_ALIGNED(alloc_start, sectorsize));
3278 len = offset + len - alloc_start;
3279 offset = alloc_start;
3280 alloc_hint = em->block_start + em->len;
3281 }
3282 free_extent_map(em);
3283
3284 if (BTRFS_BYTES_TO_BLKS(fs_info, offset) ==
3285 BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
Omar Sandoval39b07b52019-12-02 17:34:23 -08003286 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
3287 sectorsize);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003288 if (IS_ERR(em)) {
3289 ret = PTR_ERR(em);
3290 goto out;
3291 }
3292
3293 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3294 free_extent_map(em);
3295 ret = btrfs_fallocate_update_isize(inode, offset + len,
3296 mode);
3297 goto out;
3298 }
3299 if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) {
3300 free_extent_map(em);
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003301 ret = btrfs_truncate_block(BTRFS_I(inode), offset, len,
3302 0);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003303 if (!ret)
3304 ret = btrfs_fallocate_update_isize(inode,
3305 offset + len,
3306 mode);
3307 return ret;
3308 }
3309 free_extent_map(em);
3310 alloc_start = round_down(offset, sectorsize);
3311 alloc_end = alloc_start + sectorsize;
3312 goto reserve_space;
3313 }
3314
3315 alloc_start = round_up(offset, sectorsize);
3316 alloc_end = round_down(offset + len, sectorsize);
3317
3318 /*
3319 * For unaligned ranges, check the pages at the boundaries, they might
3320 * map to an extent, in which case we need to partially zero them, or
3321 * they might map to a hole, in which case we need our allocation range
3322 * to cover them.
3323 */
3324 if (!IS_ALIGNED(offset, sectorsize)) {
Nikolay Borisov948dfeb2020-08-31 14:42:48 +03003325 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
3326 offset);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003327 if (ret < 0)
3328 goto out;
Filipe Manana81fdf632018-01-18 11:34:31 +00003329 if (ret == RANGE_BOUNDARY_HOLE) {
Filipe Mananaf27451f2017-10-25 11:55:28 +01003330 alloc_start = round_down(offset, sectorsize);
3331 ret = 0;
Filipe Manana81fdf632018-01-18 11:34:31 +00003332 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003333 ret = btrfs_truncate_block(BTRFS_I(inode), offset, 0, 0);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003334 if (ret)
3335 goto out;
Filipe Manana81fdf632018-01-18 11:34:31 +00003336 } else {
3337 ret = 0;
Filipe Mananaf27451f2017-10-25 11:55:28 +01003338 }
3339 }
3340
3341 if (!IS_ALIGNED(offset + len, sectorsize)) {
Nikolay Borisov948dfeb2020-08-31 14:42:48 +03003342 ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
Filipe Mananaf27451f2017-10-25 11:55:28 +01003343 offset + len);
3344 if (ret < 0)
3345 goto out;
Filipe Manana81fdf632018-01-18 11:34:31 +00003346 if (ret == RANGE_BOUNDARY_HOLE) {
Filipe Mananaf27451f2017-10-25 11:55:28 +01003347 alloc_end = round_up(offset + len, sectorsize);
3348 ret = 0;
Filipe Manana81fdf632018-01-18 11:34:31 +00003349 } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003350 ret = btrfs_truncate_block(BTRFS_I(inode), offset + len,
3351 0, 1);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003352 if (ret)
3353 goto out;
Filipe Manana81fdf632018-01-18 11:34:31 +00003354 } else {
3355 ret = 0;
Filipe Mananaf27451f2017-10-25 11:55:28 +01003356 }
3357 }
3358
3359reserve_space:
3360 if (alloc_start < alloc_end) {
3361 struct extent_state *cached_state = NULL;
3362 const u64 lockstart = alloc_start;
3363 const u64 lockend = alloc_end - 1;
3364
3365 bytes_to_reserve = alloc_end - alloc_start;
3366 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3367 bytes_to_reserve);
3368 if (ret < 0)
3369 goto out;
3370 space_reserved = true;
Filipe Mananaf27451f2017-10-25 11:55:28 +01003371 ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
3372 &cached_state);
3373 if (ret)
3374 goto out;
Nikolay Borisov7661a3e2020-06-03 08:55:37 +03003375 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
Qu Wenruoa7f8b1c2020-06-10 09:04:42 +08003376 alloc_start, bytes_to_reserve);
Nikolay Borisov4f6a49d2021-02-23 15:20:42 +02003377 if (ret) {
3378 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
3379 lockend, &cached_state);
Qu Wenruoa7f8b1c2020-06-10 09:04:42 +08003380 goto out;
Nikolay Borisov4f6a49d2021-02-23 15:20:42 +02003381 }
Filipe Mananaf27451f2017-10-25 11:55:28 +01003382 ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
3383 alloc_end - alloc_start,
3384 i_blocksize(inode),
3385 offset + len, &alloc_hint);
3386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
3387 lockend, &cached_state);
3388 /* btrfs_prealloc_file_range releases reserved space on error */
Filipe Manana9f13ce72018-01-18 11:34:20 +00003389 if (ret) {
Filipe Mananaf27451f2017-10-25 11:55:28 +01003390 space_reserved = false;
Filipe Manana9f13ce72018-01-18 11:34:20 +00003391 goto out;
3392 }
Filipe Mananaf27451f2017-10-25 11:55:28 +01003393 }
Filipe Manana9f13ce72018-01-18 11:34:20 +00003394 ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003395 out:
3396 if (ret && space_reserved)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03003397 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
Filipe Mananaf27451f2017-10-25 11:55:28 +01003398 alloc_start, bytes_to_reserve);
3399 extent_changeset_free(data_reserved);
3400
3401 return ret;
3402}
3403
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003404static long btrfs_fallocate(struct file *file, int mode,
3405 loff_t offset, loff_t len)
3406{
Al Viro496ad9a2013-01-23 17:07:38 -05003407 struct inode *inode = file_inode(file);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003408 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003409 struct extent_changeset *data_reserved = NULL;
Qu Wenruo14524a82015-09-08 17:22:44 +08003410 struct falloc_range *range;
3411 struct falloc_range *tmp;
3412 struct list_head reserve_list;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003413 u64 cur_offset;
3414 u64 last_byte;
3415 u64 alloc_start;
3416 u64 alloc_end;
3417 u64 alloc_hint = 0;
3418 u64 locked_end;
Qu Wenruo14524a82015-09-08 17:22:44 +08003419 u64 actual_end = 0;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003420 struct extent_map *em;
Nikolay Borisov6fee2482020-08-31 14:42:42 +03003421 int blocksize = btrfs_inode_sectorsize(BTRFS_I(inode));
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003422 int ret;
3423
Naohiro Aotaf1569c42020-11-10 20:26:12 +09003424 /* Do not allow fallocate in ZONED mode */
3425 if (btrfs_is_zoned(btrfs_sb(inode->i_sb)))
3426 return -EOPNOTSUPP;
3427
Miao Xie797f4272012-11-28 10:28:07 +00003428 alloc_start = round_down(offset, blocksize);
3429 alloc_end = round_up(offset + len, blocksize);
Wang Xiaoguang18513092016-07-25 15:51:40 +08003430 cur_offset = alloc_start;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003431
Josef Bacik2aaa6652012-08-29 14:27:18 -04003432 /* Make sure we aren't being give some crap mode */
Filipe Mananaf27451f2017-10-25 11:55:28 +01003433 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3434 FALLOC_FL_ZERO_RANGE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003435 return -EOPNOTSUPP;
3436
Josef Bacik2aaa6652012-08-29 14:27:18 -04003437 if (mode & FALLOC_FL_PUNCH_HOLE)
Darrick J. Wong05fd9562022-03-14 10:55:32 -07003438 return btrfs_punch_hole(file, offset, len);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003439
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003440 /*
Qu Wenruo14524a82015-09-08 17:22:44 +08003441 * Only trigger disk allocation, don't trigger qgroup reserve
3442 *
3443 * For qgroup space, it will be checked later.
Chris Masond98456f2012-01-31 20:27:41 -05003444 */
Filipe Mananaf27451f2017-10-25 11:55:28 +01003445 if (!(mode & FALLOC_FL_ZERO_RANGE)) {
3446 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
3447 alloc_end - alloc_start);
3448 if (ret < 0)
3449 return ret;
3450 }
Chris Masond98456f2012-01-31 20:27:41 -05003451
Josef Bacik8d9b4a12021-02-10 17:14:36 -05003452 btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
Davide Italiano2a162ce2015-04-06 22:09:15 -07003453
3454 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
3455 ret = inode_newsize_ok(inode, offset + len);
3456 if (ret)
3457 goto out;
3458 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003459
Darrick J. Wong05fd9562022-03-14 10:55:32 -07003460 ret = file_modified(file);
3461 if (ret)
3462 goto out;
3463
Qu Wenruo14524a82015-09-08 17:22:44 +08003464 /*
3465 * TODO: Move these two operations after we have checked
3466 * accurate reserved space, or fallocate can still fail but
3467 * with page truncated or size expanded.
3468 *
3469 * But that's a minor problem and won't do much harm BTW.
3470 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003471 if (alloc_start > inode->i_size) {
Nikolay Borisovb06359a32020-11-02 16:49:04 +02003472 ret = btrfs_cont_expand(BTRFS_I(inode), i_size_read(inode),
Josef Bacika41ad392011-01-31 15:30:16 -05003473 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003474 if (ret)
3475 goto out;
Qu Wenruo0f6925f2015-10-14 15:26:13 +08003476 } else if (offset + len > inode->i_size) {
Josef Bacika71754f2013-06-17 17:14:39 -04003477 /*
3478 * If we are fallocating from the end of the file onward we
Chandan Rajendra9703fef2016-01-21 15:55:56 +05303479 * need to zero out the end of the block if i_size lands in the
3480 * middle of a block.
Josef Bacika71754f2013-06-17 17:14:39 -04003481 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02003482 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04003483 if (ret)
3484 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003485 }
3486
Josef Bacika71754f2013-06-17 17:14:39 -04003487 /*
3488 * wait for ordered IO before we have any locks. We'll loop again
3489 * below with the locks held.
3490 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04003491 ret = btrfs_wait_ordered_range(inode, alloc_start,
3492 alloc_end - alloc_start);
3493 if (ret)
3494 goto out;
Josef Bacika71754f2013-06-17 17:14:39 -04003495
Filipe Mananaf27451f2017-10-25 11:55:28 +01003496 if (mode & FALLOC_FL_ZERO_RANGE) {
3497 ret = btrfs_zero_range(inode, offset, len, mode);
Josef Bacik8d9b4a12021-02-10 17:14:36 -05003498 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Filipe Mananaf27451f2017-10-25 11:55:28 +01003499 return ret;
3500 }
3501
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003502 locked_end = alloc_end - 1;
3503 while (1) {
3504 struct btrfs_ordered_extent *ordered;
3505
3506 /* the extent lock is ordered inside the running
3507 * transaction
3508 */
3509 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
David Sterbaff13db42015-12-03 14:30:40 +01003510 locked_end, &cached_state);
Nikolay Borisov6d072c82020-08-31 14:42:39 +03003511 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
3512 locked_end);
Nikolay Borisov96b09dd2017-11-01 11:36:05 +02003513
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003514 if (ordered &&
Omar Sandovalbffe6332019-12-02 17:34:19 -08003515 ordered->file_offset + ordered->num_bytes > alloc_start &&
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003516 ordered->file_offset < alloc_end) {
3517 btrfs_put_ordered_extent(ordered);
3518 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
3519 alloc_start, locked_end,
David Sterbae43bbe52017-12-12 21:43:52 +01003520 &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003521 /*
3522 * we can't wait on the range with the transaction
3523 * running or with the extent lock held
3524 */
Josef Bacik0ef8b722013-10-25 16:13:35 -04003525 ret = btrfs_wait_ordered_range(inode, alloc_start,
3526 alloc_end - alloc_start);
3527 if (ret)
3528 goto out;
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003529 } else {
3530 if (ordered)
3531 btrfs_put_ordered_extent(ordered);
3532 break;
3533 }
3534 }
3535
Qu Wenruo14524a82015-09-08 17:22:44 +08003536 /* First, check if we exceed the qgroup limit */
3537 INIT_LIST_HEAD(&reserve_list);
Nikolay Borisov6b7d6e92017-11-01 11:32:18 +02003538 while (cur_offset < alloc_end) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02003539 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08003540 alloc_end - cur_offset);
Dan Carpenter99862772017-04-11 11:57:15 +03003541 if (IS_ERR(em)) {
3542 ret = PTR_ERR(em);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003543 break;
3544 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003545 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04003546 actual_end = min_t(u64, extent_map_end(em), offset + len);
Miao Xie797f4272012-11-28 10:28:07 +00003547 last_byte = ALIGN(last_byte, blocksize);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003548 if (em->block_start == EXTENT_MAP_HOLE ||
3549 (cur_offset >= inode->i_size &&
3550 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Qu Wenruo14524a82015-09-08 17:22:44 +08003551 ret = add_falloc_range(&reserve_list, cur_offset,
3552 last_byte - cur_offset);
3553 if (ret < 0) {
3554 free_extent_map(em);
3555 break;
Filipe Manana3d850dd2015-03-12 23:23:13 +00003556 }
Nikolay Borisov7661a3e2020-06-03 08:55:37 +03003557 ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
3558 &data_reserved, cur_offset,
3559 last_byte - cur_offset);
Filipe Mananabe2d2532017-04-03 15:57:17 +01003560 if (ret < 0) {
Robbie Ko39ad3172019-03-26 11:56:11 +08003561 cur_offset = last_byte;
Filipe Mananabe2d2532017-04-03 15:57:17 +01003562 free_extent_map(em);
Qu Wenruo14524a82015-09-08 17:22:44 +08003563 break;
Filipe Mananabe2d2532017-04-03 15:57:17 +01003564 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08003565 } else {
3566 /*
3567 * Do not need to reserve unwritten extent for this
3568 * range, free reserved data space first, otherwise
3569 * it'll result in false ENOSPC error.
3570 */
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03003571 btrfs_free_reserved_data_space(BTRFS_I(inode),
3572 data_reserved, cur_offset,
3573 last_byte - cur_offset);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003574 }
3575 free_extent_map(em);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003576 cur_offset = last_byte;
Qu Wenruo14524a82015-09-08 17:22:44 +08003577 }
3578
3579 /*
3580 * If ret is still 0, means we're OK to fallocate.
3581 * Or just cleanup the list and exit.
3582 */
3583 list_for_each_entry_safe(range, tmp, &reserve_list, list) {
3584 if (!ret)
3585 ret = btrfs_prealloc_file_range(inode, mode,
3586 range->start,
Fabian Frederick93407472017-02-27 14:28:32 -08003587 range->len, i_blocksize(inode),
Qu Wenruo14524a82015-09-08 17:22:44 +08003588 offset + len, &alloc_hint);
Wang Xiaoguang18513092016-07-25 15:51:40 +08003589 else
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03003590 btrfs_free_reserved_data_space(BTRFS_I(inode),
Qu Wenruobc42bda2017-02-27 15:10:39 +08003591 data_reserved, range->start,
3592 range->len);
Qu Wenruo14524a82015-09-08 17:22:44 +08003593 list_del(&range->list);
3594 kfree(range);
3595 }
3596 if (ret < 0)
3597 goto out_unlock;
3598
Filipe Mananaf27451f2017-10-25 11:55:28 +01003599 /*
3600 * We didn't need to allocate any more space, but we still extended the
3601 * size of the file so we need to update i_size and the inode item.
3602 */
3603 ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
Qu Wenruo14524a82015-09-08 17:22:44 +08003604out_unlock:
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003605 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
David Sterbae43bbe52017-12-12 21:43:52 +01003606 &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003607out:
Josef Bacik8d9b4a12021-02-10 17:14:36 -05003608 btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
Chris Masond98456f2012-01-31 20:27:41 -05003609 /* Let go of our reservation. */
Filipe Mananaf27451f2017-10-25 11:55:28 +01003610 if (ret != 0 && !(mode & FALLOC_FL_ZERO_RANGE))
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03003611 btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
Robbie Ko39ad3172019-03-26 11:56:11 +08003612 cur_offset, alloc_end - cur_offset);
Qu Wenruo364ecf32017-02-27 15:10:38 +08003613 extent_changeset_free(data_reserved);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003614 return ret;
3615}
3616
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003617static loff_t find_desired_extent(struct btrfs_inode *inode, loff_t offset,
Nikolay Borisovbc802302019-09-27 13:23:18 +03003618 int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04003619{
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003620 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik7f4ca372013-10-18 11:44:46 -04003621 struct extent_map *em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04003622 struct extent_state *cached_state = NULL;
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003623 loff_t i_size = inode->vfs_inode.i_size;
Liu Bo4d1a40c2014-09-16 17:49:30 +08003624 u64 lockstart;
3625 u64 lockend;
3626 u64 start;
3627 u64 len;
Josef Bacikb2675152011-07-18 13:21:36 -04003628 int ret = 0;
3629
Nikolay Borisovbc802302019-09-27 13:23:18 +03003630 if (i_size == 0 || offset >= i_size)
Josef Bacikb2675152011-07-18 13:21:36 -04003631 return -ENXIO;
3632
Liu Bo4d1a40c2014-09-16 17:49:30 +08003633 /*
Nikolay Borisovbc802302019-09-27 13:23:18 +03003634 * offset can be negative, in this case we start finding DATA/HOLE from
Liu Bo4d1a40c2014-09-16 17:49:30 +08003635 * the very start of the file.
3636 */
Nikolay Borisovbc802302019-09-27 13:23:18 +03003637 start = max_t(loff_t, 0, offset);
Liu Bo4d1a40c2014-09-16 17:49:30 +08003638
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003639 lockstart = round_down(start, fs_info->sectorsize);
Nikolay Borisovd79b7c22019-09-27 13:23:16 +03003640 lockend = round_up(i_size, fs_info->sectorsize);
Liu Bo4d1a40c2014-09-16 17:49:30 +08003641 if (lockend <= lockstart)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003642 lockend = lockstart + fs_info->sectorsize;
Liu Bo4d1a40c2014-09-16 17:49:30 +08003643 lockend--;
3644 len = lockend - lockstart + 1;
3645
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003646 lock_extent_bits(&inode->io_tree, lockstart, lockend, &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04003647
Nikolay Borisovd79b7c22019-09-27 13:23:16 +03003648 while (start < i_size) {
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003649 em = btrfs_get_extent_fiemap(inode, start, len);
Josef Bacikb2675152011-07-18 13:21:36 -04003650 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08003651 ret = PTR_ERR(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04003652 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04003653 break;
3654 }
3655
Josef Bacik7f4ca372013-10-18 11:44:46 -04003656 if (whence == SEEK_HOLE &&
3657 (em->block_start == EXTENT_MAP_HOLE ||
3658 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
3659 break;
3660 else if (whence == SEEK_DATA &&
3661 (em->block_start != EXTENT_MAP_HOLE &&
3662 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
3663 break;
Josef Bacikb2675152011-07-18 13:21:36 -04003664
3665 start = em->start + em->len;
Josef Bacikb2675152011-07-18 13:21:36 -04003666 free_extent_map(em);
Josef Bacik7f4ca372013-10-18 11:44:46 -04003667 em = NULL;
Josef Bacikb2675152011-07-18 13:21:36 -04003668 cond_resched();
3669 }
Josef Bacik7f4ca372013-10-18 11:44:46 -04003670 free_extent_map(em);
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003671 unlock_extent_cached(&inode->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01003672 &cached_state);
Nikolay Borisovbc802302019-09-27 13:23:18 +03003673 if (ret) {
3674 offset = ret;
3675 } else {
3676 if (whence == SEEK_DATA && start >= i_size)
3677 offset = -ENXIO;
3678 else
3679 offset = min_t(loff_t, start, i_size);
3680 }
3681
3682 return offset;
Josef Bacikb2675152011-07-18 13:21:36 -04003683}
3684
Andrew Morton965c8e52012-12-17 15:59:39 -08003685static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
Josef Bacikb2675152011-07-18 13:21:36 -04003686{
3687 struct inode *inode = file->f_mapping->host;
Josef Bacikb2675152011-07-18 13:21:36 -04003688
Andrew Morton965c8e52012-12-17 15:59:39 -08003689 switch (whence) {
Nikolay Borisov2034f3b2019-09-27 13:23:17 +03003690 default:
3691 return generic_file_llseek(file, offset, whence);
Josef Bacikb2675152011-07-18 13:21:36 -04003692 case SEEK_DATA:
3693 case SEEK_HOLE:
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -05003694 btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
Nikolay Borisovcca5de92021-02-17 15:12:48 +02003695 offset = find_desired_extent(BTRFS_I(inode), offset, whence);
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -05003696 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
Nikolay Borisovbc802302019-09-27 13:23:18 +03003697 break;
Josef Bacikb2675152011-07-18 13:21:36 -04003698 }
3699
Nikolay Borisovbc802302019-09-27 13:23:18 +03003700 if (offset < 0)
3701 return offset;
3702
Nikolay Borisov2034f3b2019-09-27 13:23:17 +03003703 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Josef Bacikb2675152011-07-18 13:21:36 -04003704}
3705
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05003706static int btrfs_file_open(struct inode *inode, struct file *filp)
3707{
Boris Burkov14605402021-06-30 13:01:49 -07003708 int ret;
3709
Jens Axboe8730f122020-05-22 10:19:22 -06003710 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
Boris Burkov14605402021-06-30 13:01:49 -07003711
3712 ret = fsverity_file_open(inode, filp);
3713 if (ret)
3714 return ret;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05003715 return generic_file_open(inode, filp);
3716}
3717
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05003718static int check_direct_read(struct btrfs_fs_info *fs_info,
3719 const struct iov_iter *iter, loff_t offset)
3720{
3721 int ret;
3722 int i, seg;
3723
3724 ret = check_direct_IO(fs_info, iter, offset);
3725 if (ret < 0)
3726 return ret;
3727
3728 if (!iter_is_iovec(iter))
3729 return 0;
3730
3731 for (seg = 0; seg < iter->nr_segs; seg++)
3732 for (i = seg + 1; i < iter->nr_segs; i++)
3733 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
3734 return -EINVAL;
3735 return 0;
3736}
3737
3738static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)
3739{
3740 struct inode *inode = file_inode(iocb->ki_filp);
Filipe Manana51bd9562021-10-25 17:27:47 +01003741 size_t prev_left = 0;
3742 ssize_t read = 0;
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05003743 ssize_t ret;
3744
Boris Burkov14605402021-06-30 13:01:49 -07003745 if (fsverity_active(inode))
3746 return 0;
3747
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05003748 if (check_direct_read(btrfs_sb(inode->i_sb), to, iocb->ki_pos))
3749 return 0;
3750
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -05003751 btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
Filipe Manana51bd9562021-10-25 17:27:47 +01003752again:
3753 /*
3754 * This is similar to what we do for direct IO writes, see the comment
3755 * at btrfs_direct_write(), but we also disable page faults in addition
3756 * to disabling them only at the iov_iter level. This is because when
3757 * reading from a hole or prealloc extent, iomap calls iov_iter_zero(),
3758 * which can still trigger page fault ins despite having set ->nofault
3759 * to true of our 'to' iov_iter.
3760 *
3761 * The difference to direct IO writes is that we deadlock when trying
3762 * to lock the extent range in the inode's tree during he page reads
3763 * triggered by the fault in (while for writes it is due to waiting for
3764 * our own ordered extent). This is because for direct IO reads,
3765 * btrfs_dio_iomap_begin() returns with the extent range locked, which
3766 * is only unlocked in the endio callback (end_bio_extent_readpage()).
3767 */
3768 pagefault_disable();
3769 to->nofault = true;
Andreas Gruenbacher4fdccaa2021-07-24 12:26:41 +02003770 ret = iomap_dio_rw(iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
Filipe Manana51bd9562021-10-25 17:27:47 +01003771 IOMAP_DIO_PARTIAL, read);
3772 to->nofault = false;
3773 pagefault_enable();
3774
3775 /* No increment (+=) because iomap returns a cumulative value. */
3776 if (ret > 0)
3777 read = ret;
3778
3779 if (iov_iter_count(to) > 0 && (ret == -EFAULT || ret > 0)) {
3780 const size_t left = iov_iter_count(to);
3781
3782 if (left == prev_left) {
3783 /*
3784 * We didn't make any progress since the last attempt,
3785 * fallback to a buffered read for the remainder of the
3786 * range. This is just to avoid any possibility of looping
3787 * for too long.
3788 */
3789 ret = read;
3790 } else {
3791 /*
3792 * We made some progress since the last retry or this is
3793 * the first time we are retrying. Fault in as many pages
3794 * as possible and retry.
3795 */
3796 fault_in_iov_iter_writeable(to, left);
3797 prev_left = left;
3798 goto again;
3799 }
3800 }
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -05003801 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
Filipe Manana51bd9562021-10-25 17:27:47 +01003802 return ret < 0 ? ret : read;
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05003803}
3804
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05003805static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
3806{
3807 ssize_t ret = 0;
3808
3809 if (iocb->ki_flags & IOCB_DIRECT) {
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05003810 ret = btrfs_direct_read(iocb, to);
Johannes Thumshirn0425e7b2020-10-22 23:05:05 +09003811 if (ret < 0 || !iov_iter_count(to) ||
3812 iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05003813 return ret;
3814 }
3815
Christoph Hellwig87fa0f32021-02-24 12:02:42 -08003816 return filemap_read(iocb, to, ret);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05003817}
3818
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003819const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04003820 .llseek = btrfs_file_llseek,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05003821 .read_iter = btrfs_file_read_iter,
Chris Masone9906a92007-12-14 12:56:58 -05003822 .splice_read = generic_file_splice_read,
Al Virob30ac0f2014-04-03 14:29:04 -04003823 .write_iter = btrfs_file_write_iter,
Christoph Hellwigd7776592020-07-09 18:22:06 +02003824 .splice_write = iter_file_splice_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04003825 .mmap = btrfs_file_mmap,
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05003826 .open = btrfs_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04003827 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003828 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003829 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003830 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003831#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +00003832 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003833#endif
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11003834 .remap_file_range = btrfs_remap_file_range,
Chris Mason39279cc2007-06-12 06:35:45 -04003835};
Miao Xie9247f312012-11-26 09:24:43 +00003836
David Sterbae67c7182018-02-19 17:24:18 +01003837void __cold btrfs_auto_defrag_exit(void)
Miao Xie9247f312012-11-26 09:24:43 +00003838{
Kinglong Mee5598e902016-01-29 21:36:35 +08003839 kmem_cache_destroy(btrfs_inode_defrag_cachep);
Miao Xie9247f312012-11-26 09:24:43 +00003840}
3841
Liu Bof5c29bd2017-11-02 17:21:50 -06003842int __init btrfs_auto_defrag_init(void)
Miao Xie9247f312012-11-26 09:24:43 +00003843{
3844 btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
3845 sizeof(struct inode_defrag), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03003846 SLAB_MEM_SPREAD,
Miao Xie9247f312012-11-26 09:24:43 +00003847 NULL);
3848 if (!btrfs_inode_defrag_cachep)
3849 return -ENOMEM;
3850
3851 return 0;
3852}
Filipe Manana728404d2014-10-10 09:43:11 +01003853
3854int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)
3855{
3856 int ret;
3857
3858 /*
3859 * So with compression we will find and lock a dirty page and clear the
3860 * first one as dirty, setup an async extent, and immediately return
3861 * with the entire range locked but with nobody actually marked with
3862 * writeback. So we can't just filemap_write_and_wait_range() and
3863 * expect it to work since it will just kick off a thread to do the
3864 * actual work. So we need to call filemap_fdatawrite_range _again_
3865 * since it will wait on the page lock, which won't be unlocked until
3866 * after the pages have been marked as writeback and so we're good to go
3867 * from there. We have to do this otherwise we'll miss the ordered
3868 * extents and that results in badness. Please Josef, do not think you
3869 * know better and pull this out at some point in the future, it is
3870 * right and you are wrong.
3871 */
3872 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
3873 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
3874 &BTRFS_I(inode)->runtime_flags))
3875 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
3876
3877 return ret;
3878}