]> git.ipfire.org Git - people/ms/linux.git/blame - fs/btrfs/inode.c
btrfs: add missing inode updates on each iteration when replacing extents
[people/ms/linux.git] / fs / btrfs / inode.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
6cbd5570
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
6cbd5570
CM
4 */
5
7999096f 6#include <crypto/hash.h>
8f18cf13 7#include <linux/kernel.h>
065631f6 8#include <linux/bio.h>
348332e0 9#include <linux/blk-cgroup.h>
f2eb0a24 10#include <linux/file.h>
39279cc3
CM
11#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
39279cc3 17#include <linux/backing-dev.h>
39279cc3 18#include <linux/writeback.h>
39279cc3 19#include <linux/compat.h>
5103e947 20#include <linux/xattr.h>
33268eaf 21#include <linux/posix_acl.h>
d899e052 22#include <linux/falloc.h>
5a0e3ad6 23#include <linux/slab.h>
7a36ddec 24#include <linux/ratelimit.h>
55e301fd 25#include <linux/btrfs.h>
53b381b3 26#include <linux/blkdev.h>
f23b5a59 27#include <linux/posix_acl_xattr.h>
e2e40f2c 28#include <linux/uio.h>
69fe2d75 29#include <linux/magic.h>
ae5e165d 30#include <linux/iversion.h>
ed46ff3d 31#include <linux/swap.h>
f8e66081 32#include <linux/migrate.h>
b1c16ac9 33#include <linux/sched/mm.h>
f85781fb 34#include <linux/iomap.h>
92d32170 35#include <asm/unaligned.h>
14605409 36#include <linux/fsverity.h>
602cbe91 37#include "misc.h"
39279cc3
CM
38#include "ctree.h"
39#include "disk-io.h"
40#include "transaction.h"
41#include "btrfs_inode.h"
39279cc3 42#include "print-tree.h"
e6dcd2dc 43#include "ordered-data.h"
95819c05 44#include "xattr.h"
e02119d5 45#include "tree-log.h"
4a54c8c1 46#include "volumes.h"
c8b97818 47#include "compression.h"
b4ce94de 48#include "locking.h"
dc89e982 49#include "free-space-cache.h"
63541927 50#include "props.h"
31193213 51#include "qgroup.h"
86736342 52#include "delalloc-space.h"
aac0023c 53#include "block-group.h"
467dc47e 54#include "space-info.h"
d8e3fb10 55#include "zoned.h"
b945a463 56#include "subpage.h"
26c2c454 57#include "inode-item.h"
39279cc3
CM
58
59struct btrfs_iget_args {
0202e83f 60 u64 ino;
39279cc3
CM
61 struct btrfs_root *root;
62};
63
f28a4928 64struct btrfs_dio_data {
f85781fb
GR
65 ssize_t submitted;
66 struct extent_changeset *data_reserved;
f5585f4f
FM
67 bool data_space_reserved;
68 bool nocow_done;
f28a4928
FM
69};
70
a3e171a0
CH
71struct btrfs_dio_private {
72 struct inode *inode;
73
74 /*
75 * Since DIO can use anonymous page, we cannot use page_offset() to
76 * grab the file offset, thus need a dedicated member for file offset.
77 */
78 u64 file_offset;
79 /* Used for bio::bi_size */
80 u32 bytes;
81
82 /*
83 * References to this structure. There is one reference per in-flight
84 * bio plus one while we're still setting up.
85 */
86 refcount_t refs;
87
a3e171a0 88 /* Array of checksums */
642c5d34
CH
89 u8 *csums;
90
91 /* This must be last */
92 struct bio bio;
a3e171a0
CH
93};
94
642c5d34
CH
95static struct bio_set btrfs_dio_bioset;
96
88d2beec
FM
97struct btrfs_rename_ctx {
98 /* Output field. Stores the index number of the old directory entry. */
99 u64 index;
100};
101
6e1d5dcc
AD
102static const struct inode_operations btrfs_dir_inode_operations;
103static const struct inode_operations btrfs_symlink_inode_operations;
6e1d5dcc
AD
104static const struct inode_operations btrfs_special_inode_operations;
105static const struct inode_operations btrfs_file_inode_operations;
7f09410b 106static const struct address_space_operations btrfs_aops;
828c0950 107static const struct file_operations btrfs_dir_file_operations;
39279cc3
CM
108
109static struct kmem_cache *btrfs_inode_cachep;
110struct kmem_cache *btrfs_trans_handle_cachep;
39279cc3 111struct kmem_cache *btrfs_path_cachep;
dc89e982 112struct kmem_cache *btrfs_free_space_cachep;
3acd4850 113struct kmem_cache *btrfs_free_space_bitmap_cachep;
39279cc3 114
3972f260 115static int btrfs_setsize(struct inode *inode, struct iattr *attr);
213e8c55 116static int btrfs_truncate(struct inode *inode, bool skip_writeback);
5fd02043 117static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
6e26c442 118static noinline int cow_file_range(struct btrfs_inode *inode,
771ed689 119 struct page *locked_page,
74e9194a 120 u64 start, u64 end, int *page_started,
330a5827 121 unsigned long *nr_written, int unlock);
4b67c11d
NB
122static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
123 u64 len, u64 orig_start, u64 block_start,
6f9994db
LB
124 u64 block_len, u64 orig_block_len,
125 u64 ram_bytes, int compress_type,
126 int type);
7b128766 127
b672b5c1 128static void __endio_write_update_ordered(struct btrfs_inode *inode,
52427260
QW
129 const u64 offset, const u64 bytes,
130 const bool uptodate);
131
a14b78ad
GR
132/*
133 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
134 *
135 * ilock_flags can have the following bit set:
136 *
137 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
138 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
139 * return -EAGAIN
8318ba79 140 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
a14b78ad
GR
141 */
142int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
143{
144 if (ilock_flags & BTRFS_ILOCK_SHARED) {
145 if (ilock_flags & BTRFS_ILOCK_TRY) {
146 if (!inode_trylock_shared(inode))
147 return -EAGAIN;
148 else
149 return 0;
150 }
151 inode_lock_shared(inode);
152 } else {
153 if (ilock_flags & BTRFS_ILOCK_TRY) {
154 if (!inode_trylock(inode))
155 return -EAGAIN;
156 else
157 return 0;
158 }
159 inode_lock(inode);
160 }
8318ba79
JB
161 if (ilock_flags & BTRFS_ILOCK_MMAP)
162 down_write(&BTRFS_I(inode)->i_mmap_lock);
a14b78ad
GR
163 return 0;
164}
165
166/*
167 * btrfs_inode_unlock - unock inode i_rwsem
168 *
169 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
170 * to decide whether the lock acquired is shared or exclusive.
171 */
172void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
173{
8318ba79
JB
174 if (ilock_flags & BTRFS_ILOCK_MMAP)
175 up_write(&BTRFS_I(inode)->i_mmap_lock);
a14b78ad
GR
176 if (ilock_flags & BTRFS_ILOCK_SHARED)
177 inode_unlock_shared(inode);
178 else
179 inode_unlock(inode);
180}
181
52427260
QW
182/*
183 * Cleanup all submitted ordered extents in specified range to handle errors
52042d8e 184 * from the btrfs_run_delalloc_range() callback.
52427260
QW
185 *
186 * NOTE: caller must ensure that when an error happens, it can not call
187 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
188 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
189 * to be released, which we want to happen only when finishing the ordered
d1051d6e 190 * extent (btrfs_finish_ordered_io()).
52427260 191 */
64e1db56 192static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
d1051d6e
NB
193 struct page *locked_page,
194 u64 offset, u64 bytes)
52427260 195{
63d71450
NA
196 unsigned long index = offset >> PAGE_SHIFT;
197 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
d1051d6e
NB
198 u64 page_start = page_offset(locked_page);
199 u64 page_end = page_start + PAGE_SIZE - 1;
200
63d71450
NA
201 struct page *page;
202
203 while (index <= end_index) {
968f2566
QW
204 /*
205 * For locked page, we will call end_extent_writepage() on it
206 * in run_delalloc_range() for the error handling. That
207 * end_extent_writepage() function will call
208 * btrfs_mark_ordered_io_finished() to clear page Ordered and
209 * run the ordered extent accounting.
210 *
211 * Here we can't just clear the Ordered bit, or
212 * btrfs_mark_ordered_io_finished() would skip the accounting
213 * for the page range, and the ordered extent will never finish.
214 */
215 if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
216 index++;
217 continue;
218 }
64e1db56 219 page = find_get_page(inode->vfs_inode.i_mapping, index);
63d71450
NA
220 index++;
221 if (!page)
222 continue;
968f2566
QW
223
224 /*
225 * Here we just clear all Ordered bits for every page in the
226 * range, then __endio_write_update_ordered() will handle
227 * the ordered extent accounting for the range.
228 */
b945a463
QW
229 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
230 offset, bytes);
63d71450
NA
231 put_page(page);
232 }
d1051d6e 233
b945a463
QW
234 /* The locked page covers the full range, nothing needs to be done */
235 if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
236 return;
d1051d6e
NB
237 /*
238 * In case this page belongs to the delalloc range being instantiated
239 * then skip it, since the first page of a range is going to be
240 * properly cleaned up by the caller of run_delalloc_range
241 */
242 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
b945a463
QW
243 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
244 offset = page_offset(locked_page) + PAGE_SIZE;
d1051d6e
NB
245 }
246
64e1db56 247 return __endio_write_update_ordered(inode, offset, bytes, false);
52427260
QW
248}
249
48a3b636 250static int btrfs_dirty_inode(struct inode *inode);
7b128766 251
f34f57a3 252static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
3538d68d 253 struct btrfs_new_inode_args *args)
0279b4cd
JO
254{
255 int err;
256
3538d68d
OS
257 if (args->default_acl) {
258 err = __btrfs_set_acl(trans, args->inode, args->default_acl,
259 ACL_TYPE_DEFAULT);
260 if (err)
261 return err;
262 }
263 if (args->acl) {
264 err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS);
265 if (err)
266 return err;
267 }
268 if (!args->default_acl && !args->acl)
269 cache_no_acl(args->inode);
270 return btrfs_xattr_security_init(trans, args->inode, args->dir,
271 &args->dentry->d_name);
0279b4cd
JO
272}
273
c8b97818
CM
274/*
275 * this does all the hard work for inserting an inline extent into
276 * the btree. The caller should have done a btrfs_drop_extents so that
277 * no overlapping inline items exist in the btree
278 */
40f76580 279static int insert_inline_extent(struct btrfs_trans_handle *trans,
8dd9872d
OS
280 struct btrfs_path *path,
281 struct btrfs_inode *inode, bool extent_inserted,
282 size_t size, size_t compressed_size,
fe3f566c 283 int compress_type,
d9496e8a
OS
284 struct page **compressed_pages,
285 bool update_i_size)
c8b97818 286{
8dd9872d 287 struct btrfs_root *root = inode->root;
c8b97818
CM
288 struct extent_buffer *leaf;
289 struct page *page = NULL;
290 char *kaddr;
291 unsigned long ptr;
292 struct btrfs_file_extent_item *ei;
c8b97818
CM
293 int ret;
294 size_t cur_size = size;
d9496e8a 295 u64 i_size;
c8b97818 296
982f1f5d
JJB
297 ASSERT((compressed_size > 0 && compressed_pages) ||
298 (compressed_size == 0 && !compressed_pages));
299
fe3f566c 300 if (compressed_size && compressed_pages)
c8b97818 301 cur_size = compressed_size;
c8b97818 302
1acae57b
FDBM
303 if (!extent_inserted) {
304 struct btrfs_key key;
305 size_t datasize;
c8b97818 306
8dd9872d
OS
307 key.objectid = btrfs_ino(inode);
308 key.offset = 0;
962a298f 309 key.type = BTRFS_EXTENT_DATA_KEY;
c8b97818 310
1acae57b 311 datasize = btrfs_file_extent_calc_inline_size(cur_size);
1acae57b
FDBM
312 ret = btrfs_insert_empty_item(trans, root, path, &key,
313 datasize);
79b4f4c6 314 if (ret)
1acae57b 315 goto fail;
c8b97818
CM
316 }
317 leaf = path->nodes[0];
318 ei = btrfs_item_ptr(leaf, path->slots[0],
319 struct btrfs_file_extent_item);
320 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
321 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
322 btrfs_set_file_extent_encryption(leaf, ei, 0);
323 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
324 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
325 ptr = btrfs_file_extent_inline_start(ei);
326
261507a0 327 if (compress_type != BTRFS_COMPRESS_NONE) {
c8b97818
CM
328 struct page *cpage;
329 int i = 0;
d397712b 330 while (compressed_size > 0) {
c8b97818 331 cpage = compressed_pages[i];
5b050f04 332 cur_size = min_t(unsigned long, compressed_size,
09cbfeaf 333 PAGE_SIZE);
c8b97818 334
3a60f653 335 kaddr = kmap_atomic(cpage);
c8b97818 336 write_extent_buffer(leaf, kaddr, ptr, cur_size);
3a60f653 337 kunmap_atomic(kaddr);
c8b97818
CM
338
339 i++;
340 ptr += cur_size;
341 compressed_size -= cur_size;
342 }
343 btrfs_set_file_extent_compression(leaf, ei,
261507a0 344 compress_type);
c8b97818 345 } else {
8dd9872d 346 page = find_get_page(inode->vfs_inode.i_mapping, 0);
c8b97818 347 btrfs_set_file_extent_compression(leaf, ei, 0);
7ac687d9 348 kaddr = kmap_atomic(page);
8dd9872d 349 write_extent_buffer(leaf, kaddr, ptr, size);
7ac687d9 350 kunmap_atomic(kaddr);
09cbfeaf 351 put_page(page);
c8b97818
CM
352 }
353 btrfs_mark_buffer_dirty(leaf);
1acae57b 354 btrfs_release_path(path);
c8b97818 355
9ddc959e
JB
356 /*
357 * We align size to sectorsize for inline extents just for simplicity
358 * sake.
359 */
8dd9872d
OS
360 ret = btrfs_inode_set_file_extent_range(inode, 0,
361 ALIGN(size, root->fs_info->sectorsize));
9ddc959e
JB
362 if (ret)
363 goto fail;
364
c2167754 365 /*
d9496e8a
OS
366 * We're an inline extent, so nobody can extend the file past i_size
367 * without locking a page we already have locked.
c2167754 368 *
d9496e8a
OS
369 * We must do any i_size and inode updates before we unlock the pages.
370 * Otherwise we could end up racing with unlink.
c2167754 371 */
d9496e8a
OS
372 i_size = i_size_read(&inode->vfs_inode);
373 if (update_i_size && size > i_size) {
374 i_size_write(&inode->vfs_inode, size);
375 i_size = size;
376 }
377 inode->disk_i_size = i_size;
8dd9872d 378
c8b97818 379fail:
79b4f4c6 380 return ret;
c8b97818
CM
381}
382
383
384/*
385 * conditionally insert an inline extent into the file. This
386 * does the checks required to make sure the data is small enough
387 * to fit as an inline extent.
388 */
8dd9872d
OS
389static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size,
390 size_t compressed_size,
00361589 391 int compress_type,
d9496e8a
OS
392 struct page **compressed_pages,
393 bool update_i_size)
c8b97818 394{
5893dfb9 395 struct btrfs_drop_extents_args drop_args = { 0 };
a0349401 396 struct btrfs_root *root = inode->root;
0b246afa 397 struct btrfs_fs_info *fs_info = root->fs_info;
00361589 398 struct btrfs_trans_handle *trans;
8dd9872d 399 u64 data_len = (compressed_size ?: size);
c8b97818 400 int ret;
1acae57b 401 struct btrfs_path *path;
c8b97818 402
8dd9872d
OS
403 /*
404 * We can create an inline extent if it ends at or beyond the current
405 * i_size, is no larger than a sector (decompressed), and the (possibly
406 * compressed) data fits in a leaf and the configured maximum inline
407 * size.
408 */
409 if (size < i_size_read(&inode->vfs_inode) ||
410 size > fs_info->sectorsize ||
0b246afa 411 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
8dd9872d 412 data_len > fs_info->max_inline)
c8b97818 413 return 1;
c8b97818 414
1acae57b
FDBM
415 path = btrfs_alloc_path();
416 if (!path)
417 return -ENOMEM;
418
00361589 419 trans = btrfs_join_transaction(root);
1acae57b
FDBM
420 if (IS_ERR(trans)) {
421 btrfs_free_path(path);
00361589 422 return PTR_ERR(trans);
1acae57b 423 }
a0349401 424 trans->block_rsv = &inode->block_rsv;
00361589 425
5893dfb9 426 drop_args.path = path;
8dd9872d
OS
427 drop_args.start = 0;
428 drop_args.end = fs_info->sectorsize;
5893dfb9
FM
429 drop_args.drop_cache = true;
430 drop_args.replace_extent = true;
8dd9872d 431 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(data_len);
5893dfb9 432 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
00361589 433 if (ret) {
66642832 434 btrfs_abort_transaction(trans, ret);
00361589
JB
435 goto out;
436 }
c8b97818 437
8dd9872d
OS
438 ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted,
439 size, compressed_size, compress_type,
d9496e8a 440 compressed_pages, update_i_size);
2adcac1a 441 if (ret && ret != -ENOSPC) {
66642832 442 btrfs_abort_transaction(trans, ret);
00361589 443 goto out;
2adcac1a 444 } else if (ret == -ENOSPC) {
00361589
JB
445 ret = 1;
446 goto out;
79787eaa 447 }
2adcac1a 448
8dd9872d 449 btrfs_update_inode_bytes(inode, size, drop_args.bytes_found);
9a56fcd1 450 ret = btrfs_update_inode(trans, root, inode);
2766ff61
FM
451 if (ret && ret != -ENOSPC) {
452 btrfs_abort_transaction(trans, ret);
453 goto out;
454 } else if (ret == -ENOSPC) {
455 ret = 1;
456 goto out;
457 }
458
23e3337f 459 btrfs_set_inode_full_sync(inode);
00361589 460out:
94ed938a
QW
461 /*
462 * Don't forget to free the reserved space, as for inlined extent
463 * it won't count as data extent, free them directly here.
464 * And at reserve time, it's always aligned to page size, so
465 * just free one page here.
466 */
a0349401 467 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
1acae57b 468 btrfs_free_path(path);
3a45bb20 469 btrfs_end_transaction(trans);
00361589 470 return ret;
c8b97818
CM
471}
472
771ed689
CM
473struct async_extent {
474 u64 start;
475 u64 ram_size;
476 u64 compressed_size;
477 struct page **pages;
478 unsigned long nr_pages;
261507a0 479 int compress_type;
771ed689
CM
480 struct list_head list;
481};
482
97db1204 483struct async_chunk {
771ed689 484 struct inode *inode;
771ed689
CM
485 struct page *locked_page;
486 u64 start;
487 u64 end;
f82b7359 488 unsigned int write_flags;
771ed689 489 struct list_head extents;
ec39f769 490 struct cgroup_subsys_state *blkcg_css;
771ed689 491 struct btrfs_work work;
9e895a8f 492 struct async_cow *async_cow;
771ed689
CM
493};
494
97db1204 495struct async_cow {
97db1204
NB
496 atomic_t num_chunks;
497 struct async_chunk chunks[];
771ed689
CM
498};
499
97db1204 500static noinline int add_async_extent(struct async_chunk *cow,
771ed689
CM
501 u64 start, u64 ram_size,
502 u64 compressed_size,
503 struct page **pages,
261507a0
LZ
504 unsigned long nr_pages,
505 int compress_type)
771ed689
CM
506{
507 struct async_extent *async_extent;
508
509 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
79787eaa 510 BUG_ON(!async_extent); /* -ENOMEM */
771ed689
CM
511 async_extent->start = start;
512 async_extent->ram_size = ram_size;
513 async_extent->compressed_size = compressed_size;
514 async_extent->pages = pages;
515 async_extent->nr_pages = nr_pages;
261507a0 516 async_extent->compress_type = compress_type;
771ed689
CM
517 list_add_tail(&async_extent->list, &cow->extents);
518 return 0;
519}
520
42c16da6
QW
521/*
522 * Check if the inode needs to be submitted to compression, based on mount
523 * options, defragmentation, properties or heuristics.
524 */
808a1292
NB
525static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
526 u64 end)
f79707b0 527{
808a1292 528 struct btrfs_fs_info *fs_info = inode->root->fs_info;
f79707b0 529
e6f9d696 530 if (!btrfs_inode_can_compress(inode)) {
42c16da6
QW
531 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
532 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
808a1292 533 btrfs_ino(inode));
42c16da6
QW
534 return 0;
535 }
0cf9b244
QW
536 /*
537 * Special check for subpage.
538 *
539 * We lock the full page then run each delalloc range in the page, thus
540 * for the following case, we will hit some subpage specific corner case:
541 *
542 * 0 32K 64K
543 * | |///////| |///////|
544 * \- A \- B
545 *
546 * In above case, both range A and range B will try to unlock the full
547 * page [0, 64K), causing the one finished later will have page
548 * unlocked already, triggering various page lock requirement BUG_ON()s.
549 *
550 * So here we add an artificial limit that subpage compression can only
551 * if the range is fully page aligned.
552 *
553 * In theory we only need to ensure the first page is fully covered, but
554 * the tailing partial page will be locked until the full compression
555 * finishes, delaying the write of other range.
556 *
557 * TODO: Make btrfs_run_delalloc_range() to lock all delalloc range
558 * first to prevent any submitted async extent to unlock the full page.
559 * By this, we can ensure for subpage case that only the last async_cow
560 * will unlock the full page.
561 */
562 if (fs_info->sectorsize < PAGE_SIZE) {
563 if (!IS_ALIGNED(start, PAGE_SIZE) ||
564 !IS_ALIGNED(end + 1, PAGE_SIZE))
565 return 0;
566 }
567
f79707b0 568 /* force compress */
0b246afa 569 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
f79707b0 570 return 1;
eec63c65 571 /* defrag ioctl */
808a1292 572 if (inode->defrag_compress)
eec63c65 573 return 1;
f79707b0 574 /* bad compression ratios */
808a1292 575 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
f79707b0 576 return 0;
0b246afa 577 if (btrfs_test_opt(fs_info, COMPRESS) ||
808a1292
NB
578 inode->flags & BTRFS_INODE_COMPRESS ||
579 inode->prop_compress)
580 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
f79707b0
WS
581 return 0;
582}
583
6158e1ce 584static inline void inode_should_defrag(struct btrfs_inode *inode,
558732df 585 u64 start, u64 end, u64 num_bytes, u32 small_write)
26d30f85
AJ
586{
587 /* If this is a small write inside eof, kick off a defrag */
588 if (num_bytes < small_write &&
6158e1ce 589 (start > 0 || end + 1 < inode->disk_i_size))
558732df 590 btrfs_add_inode_defrag(NULL, inode, small_write);
26d30f85
AJ
591}
592
d352ac68 593/*
771ed689
CM
594 * we create compressed extents in two phases. The first
595 * phase compresses a range of pages that have already been
596 * locked (both pages and state bits are locked).
c8b97818 597 *
771ed689
CM
598 * This is done inside an ordered work queue, and the compression
599 * is spread across many cpus. The actual IO submission is step
600 * two, and the ordered work queue takes care of making sure that
601 * happens in the same order things were put onto the queue by
602 * writepages and friends.
c8b97818 603 *
771ed689
CM
604 * If this code finds it can't get good compression, it puts an
605 * entry onto the work queue to write the uncompressed bytes. This
606 * makes sure that both compressed inodes and uncompressed inodes
b2570314
AB
607 * are written in the same order that the flusher thread sent them
608 * down.
d352ac68 609 */
ac3e9933 610static noinline int compress_file_range(struct async_chunk *async_chunk)
b888db2b 611{
1368c6da 612 struct inode *inode = async_chunk->inode;
0b246afa 613 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
0b246afa 614 u64 blocksize = fs_info->sectorsize;
1368c6da
NB
615 u64 start = async_chunk->start;
616 u64 end = async_chunk->end;
c8b97818 617 u64 actual_end;
d98da499 618 u64 i_size;
e6dcd2dc 619 int ret = 0;
c8b97818
CM
620 struct page **pages = NULL;
621 unsigned long nr_pages;
c8b97818
CM
622 unsigned long total_compressed = 0;
623 unsigned long total_in = 0;
c8b97818
CM
624 int i;
625 int will_compress;
0b246afa 626 int compress_type = fs_info->compress_type;
ac3e9933 627 int compressed_extents = 0;
4adaa611 628 int redirty = 0;
b888db2b 629
6158e1ce
NB
630 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
631 SZ_16K);
4cb5300b 632
d98da499
JB
633 /*
634 * We need to save i_size before now because it could change in between
635 * us evaluating the size and assigning it. This is because we lock and
636 * unlock the page in truncate and fallocate, and then modify the i_size
637 * later on.
638 *
639 * The barriers are to emulate READ_ONCE, remove that once i_size_read
640 * does that for us.
641 */
642 barrier();
643 i_size = i_size_read(inode);
644 barrier();
645 actual_end = min_t(u64, i_size, end + 1);
c8b97818
CM
646again:
647 will_compress = 0;
09cbfeaf 648 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
069eac78
DS
649 nr_pages = min_t(unsigned long, nr_pages,
650 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
be20aa9d 651
f03d9301
CM
652 /*
653 * we don't want to send crud past the end of i_size through
654 * compression, that's just a waste of CPU time. So, if the
655 * end of the file is before the start of our current
656 * requested range of bytes, we bail out to the uncompressed
657 * cleanup code that can deal with all of this.
658 *
659 * It isn't really the fastest way to fix things, but this is a
660 * very uncommon corner.
661 */
662 if (actual_end <= start)
663 goto cleanup_and_bail_uncompressed;
664
c8b97818
CM
665 total_compressed = actual_end - start;
666
4bcbb332 667 /*
0cf9b244 668 * Skip compression for a small file range(<=blocksize) that
01327610 669 * isn't an inline extent, since it doesn't save disk space at all.
4bcbb332
SW
670 */
671 if (total_compressed <= blocksize &&
672 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
673 goto cleanup_and_bail_uncompressed;
674
0cf9b244
QW
675 /*
676 * For subpage case, we require full page alignment for the sector
677 * aligned range.
678 * Thus we must also check against @actual_end, not just @end.
679 */
680 if (blocksize < PAGE_SIZE) {
681 if (!IS_ALIGNED(start, PAGE_SIZE) ||
682 !IS_ALIGNED(round_up(actual_end, blocksize), PAGE_SIZE))
683 goto cleanup_and_bail_uncompressed;
684 }
685
069eac78
DS
686 total_compressed = min_t(unsigned long, total_compressed,
687 BTRFS_MAX_UNCOMPRESSED);
c8b97818
CM
688 total_in = 0;
689 ret = 0;
db94535d 690
771ed689
CM
691 /*
692 * we do compression for mount -o compress and when the
693 * inode has not been flagged as nocompress. This flag can
694 * change at any time if we discover bad compression ratios.
c8b97818 695 */
4e965576 696 if (inode_need_compress(BTRFS_I(inode), start, end)) {
c8b97818 697 WARN_ON(pages);
31e818fe 698 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
560f7d75
LZ
699 if (!pages) {
700 /* just bail out to the uncompressed code */
3527a018 701 nr_pages = 0;
560f7d75
LZ
702 goto cont;
703 }
c8b97818 704
eec63c65
DS
705 if (BTRFS_I(inode)->defrag_compress)
706 compress_type = BTRFS_I(inode)->defrag_compress;
707 else if (BTRFS_I(inode)->prop_compress)
b52aa8c9 708 compress_type = BTRFS_I(inode)->prop_compress;
261507a0 709
4adaa611
CM
710 /*
711 * we need to call clear_page_dirty_for_io on each
712 * page in the range. Otherwise applications with the file
713 * mmap'd can wander in and change the page contents while
714 * we are compressing them.
715 *
716 * If the compression fails for any reason, we set the pages
717 * dirty again later on.
e9679de3
TT
718 *
719 * Note that the remaining part is redirtied, the start pointer
720 * has moved, the end is the original one.
4adaa611 721 */
e9679de3
TT
722 if (!redirty) {
723 extent_range_clear_dirty_for_io(inode, start, end);
724 redirty = 1;
725 }
f51d2b59
DS
726
727 /* Compression level is applied here and only here */
728 ret = btrfs_compress_pages(
729 compress_type | (fs_info->compress_level << 4),
261507a0 730 inode->i_mapping, start,
38c31464 731 pages,
4d3a800e 732 &nr_pages,
261507a0 733 &total_in,
e5d74902 734 &total_compressed);
c8b97818
CM
735
736 if (!ret) {
7073017a 737 unsigned long offset = offset_in_page(total_compressed);
4d3a800e 738 struct page *page = pages[nr_pages - 1];
c8b97818
CM
739
740 /* zero the tail end of the last page, we might be
741 * sending it down to disk
742 */
d048b9c2
IW
743 if (offset)
744 memzero_page(page, offset, PAGE_SIZE - offset);
c8b97818
CM
745 will_compress = 1;
746 }
747 }
560f7d75 748cont:
7367253a
QW
749 /*
750 * Check cow_file_range() for why we don't even try to create inline
751 * extent for subpage case.
752 */
753 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
c8b97818 754 /* lets try to make an inline extent */
6018ba0a 755 if (ret || total_in < actual_end) {
c8b97818 756 /* we didn't compress the entire range, try
771ed689 757 * to make an uncompressed inline extent.
c8b97818 758 */
8dd9872d 759 ret = cow_file_range_inline(BTRFS_I(inode), actual_end,
a0349401 760 0, BTRFS_COMPRESS_NONE,
d9496e8a 761 NULL, false);
c8b97818 762 } else {
771ed689 763 /* try making a compressed inline extent */
8dd9872d 764 ret = cow_file_range_inline(BTRFS_I(inode), actual_end,
fe3f566c 765 total_compressed,
d9496e8a
OS
766 compress_type, pages,
767 false);
c8b97818 768 }
79787eaa 769 if (ret <= 0) {
151a41bc 770 unsigned long clear_flags = EXTENT_DELALLOC |
8b62f87b
JB
771 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
772 EXTENT_DO_ACCOUNTING;
e6eb4314
FM
773 unsigned long page_error_op;
774
e6eb4314 775 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
151a41bc 776
771ed689 777 /*
79787eaa
JM
778 * inline extent creation worked or returned error,
779 * we don't need to create any more async work items.
780 * Unlock and free up our temp pages.
8b62f87b
JB
781 *
782 * We use DO_ACCOUNTING here because we need the
783 * delalloc_release_metadata to be done _after_ we drop
784 * our outstanding extent for clearing delalloc for this
785 * range.
771ed689 786 */
ad7ff17b
NB
787 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
788 NULL,
74e9194a 789 clear_flags,
ba8b04c1 790 PAGE_UNLOCK |
6869b0a8 791 PAGE_START_WRITEBACK |
e6eb4314 792 page_error_op |
c2790a2e 793 PAGE_END_WRITEBACK);
cecc8d90 794
1e6e238c
QW
795 /*
796 * Ensure we only free the compressed pages if we have
797 * them allocated, as we can still reach here with
798 * inode_need_compress() == false.
799 */
800 if (pages) {
801 for (i = 0; i < nr_pages; i++) {
802 WARN_ON(pages[i]->mapping);
803 put_page(pages[i]);
804 }
805 kfree(pages);
cecc8d90 806 }
cecc8d90 807 return 0;
c8b97818
CM
808 }
809 }
810
811 if (will_compress) {
812 /*
813 * we aren't doing an inline extent round the compressed size
814 * up to a block size boundary so the allocator does sane
815 * things
816 */
fda2832f 817 total_compressed = ALIGN(total_compressed, blocksize);
c8b97818
CM
818
819 /*
820 * one last check to make sure the compression is really a
170607eb
TT
821 * win, compare the page count read with the blocks on disk,
822 * compression must free at least one sector size
c8b97818 823 */
4c162778 824 total_in = round_up(total_in, fs_info->sectorsize);
170607eb 825 if (total_compressed + blocksize <= total_in) {
ac3e9933 826 compressed_extents++;
c8bb0c8b
AS
827
828 /*
829 * The async work queues will take care of doing actual
830 * allocation on disk for these compressed pages, and
831 * will submit them to the elevator.
832 */
b5326271 833 add_async_extent(async_chunk, start, total_in,
4d3a800e 834 total_compressed, pages, nr_pages,
c8bb0c8b
AS
835 compress_type);
836
1170862d
TT
837 if (start + total_in < end) {
838 start += total_in;
c8bb0c8b
AS
839 pages = NULL;
840 cond_resched();
841 goto again;
842 }
ac3e9933 843 return compressed_extents;
c8b97818
CM
844 }
845 }
c8bb0c8b 846 if (pages) {
c8b97818
CM
847 /*
848 * the compression code ran but failed to make things smaller,
849 * free any pages it allocated and our page pointer array
850 */
4d3a800e 851 for (i = 0; i < nr_pages; i++) {
70b99e69 852 WARN_ON(pages[i]->mapping);
09cbfeaf 853 put_page(pages[i]);
c8b97818
CM
854 }
855 kfree(pages);
856 pages = NULL;
857 total_compressed = 0;
4d3a800e 858 nr_pages = 0;
c8b97818
CM
859
860 /* flag the file so we don't compress in the future */
0b246afa 861 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
b52aa8c9 862 !(BTRFS_I(inode)->prop_compress)) {
a555f810 863 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
1e701a32 864 }
c8b97818 865 }
f03d9301 866cleanup_and_bail_uncompressed:
c8bb0c8b
AS
867 /*
868 * No compression, but we still need to write the pages in the file
869 * we've been given so far. redirty the locked page if it corresponds
870 * to our extent and set things up for the async work queue to run
871 * cow_file_range to do the normal delalloc dance.
872 */
1d53c9e6
CM
873 if (async_chunk->locked_page &&
874 (page_offset(async_chunk->locked_page) >= start &&
875 page_offset(async_chunk->locked_page)) <= end) {
1368c6da 876 __set_page_dirty_nobuffers(async_chunk->locked_page);
c8bb0c8b 877 /* unlocked later on in the async handlers */
1d53c9e6 878 }
c8bb0c8b
AS
879
880 if (redirty)
881 extent_range_redirty_for_io(inode, start, end);
b5326271 882 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
c8bb0c8b 883 BTRFS_COMPRESS_NONE);
ac3e9933 884 compressed_extents++;
3b951516 885
ac3e9933 886 return compressed_extents;
771ed689 887}
771ed689 888
40ae837b
FM
889static void free_async_extent_pages(struct async_extent *async_extent)
890{
891 int i;
892
893 if (!async_extent->pages)
894 return;
895
896 for (i = 0; i < async_extent->nr_pages; i++) {
897 WARN_ON(async_extent->pages[i]->mapping);
09cbfeaf 898 put_page(async_extent->pages[i]);
40ae837b
FM
899 }
900 kfree(async_extent->pages);
901 async_extent->nr_pages = 0;
902 async_extent->pages = NULL;
771ed689
CM
903}
904
2b83a0ee
QW
905static int submit_uncompressed_range(struct btrfs_inode *inode,
906 struct async_extent *async_extent,
907 struct page *locked_page)
771ed689 908{
2b83a0ee
QW
909 u64 start = async_extent->start;
910 u64 end = async_extent->start + async_extent->ram_size - 1;
911 unsigned long nr_written = 0;
912 int page_started = 0;
913 int ret;
771ed689 914
2b83a0ee
QW
915 /*
916 * Call cow_file_range() to run the delalloc range directly, since we
917 * won't go to NOCOW or async path again.
918 *
919 * Also we call cow_file_range() with @unlock_page == 0, so that we
920 * can directly submit them without interruption.
921 */
922 ret = cow_file_range(inode, locked_page, start, end, &page_started,
923 &nr_written, 0);
924 /* Inline extent inserted, page gets unlocked and everything is done */
925 if (page_started) {
926 ret = 0;
927 goto out;
928 }
929 if (ret < 0) {
930 if (locked_page)
931 unlock_page(locked_page);
932 goto out;
933 }
771ed689 934
2b83a0ee
QW
935 ret = extent_write_locked_range(&inode->vfs_inode, start, end);
936 /* All pages will be unlocked, including @locked_page */
937out:
938 kfree(async_extent);
939 return ret;
940}
79787eaa 941
b4ccace8
QW
942static int submit_one_async_extent(struct btrfs_inode *inode,
943 struct async_chunk *async_chunk,
944 struct async_extent *async_extent,
945 u64 *alloc_hint)
771ed689 946{
b4ccace8
QW
947 struct extent_io_tree *io_tree = &inode->io_tree;
948 struct btrfs_root *root = inode->root;
949 struct btrfs_fs_info *fs_info = root->fs_info;
771ed689 950 struct btrfs_key ins;
2b83a0ee 951 struct page *locked_page = NULL;
771ed689 952 struct extent_map *em;
f5a84ee3 953 int ret = 0;
b4ccace8
QW
954 u64 start = async_extent->start;
955 u64 end = async_extent->start + async_extent->ram_size - 1;
771ed689 956
2b83a0ee
QW
957 /*
958 * If async_chunk->locked_page is in the async_extent range, we need to
959 * handle it.
960 */
961 if (async_chunk->locked_page) {
962 u64 locked_page_start = page_offset(async_chunk->locked_page);
963 u64 locked_page_end = locked_page_start + PAGE_SIZE - 1;
3e04e7f1 964
2b83a0ee
QW
965 if (!(start >= locked_page_end || end <= locked_page_start))
966 locked_page = async_chunk->locked_page;
b4ccace8 967 }
2b83a0ee 968 lock_extent(io_tree, start, end);
ce62003f 969
2b83a0ee
QW
970 /* We have fall back to uncompressed write */
971 if (!async_extent->pages)
972 return submit_uncompressed_range(inode, async_extent, locked_page);
ce62003f 973
b4ccace8
QW
974 ret = btrfs_reserve_extent(root, async_extent->ram_size,
975 async_extent->compressed_size,
976 async_extent->compressed_size,
977 0, *alloc_hint, &ins, 1, 1);
978 if (ret) {
979 free_async_extent_pages(async_extent);
c2167754 980 /*
b4ccace8
QW
981 * Here we used to try again by going back to non-compressed
982 * path for ENOSPC. But we can't reserve space even for
983 * compressed size, how could it work for uncompressed size
984 * which requires larger size? So here we directly go error
985 * path.
c2167754 986 */
b4ccace8
QW
987 goto out_free;
988 }
989
990 /* Here we're doing allocation and writeback of the compressed pages */
991 em = create_io_em(inode, start,
992 async_extent->ram_size, /* len */
993 start, /* orig_start */
994 ins.objectid, /* block_start */
995 ins.offset, /* block_len */
996 ins.offset, /* orig_block_len */
997 async_extent->ram_size, /* ram_bytes */
998 async_extent->compress_type,
999 BTRFS_ORDERED_COMPRESSED);
1000 if (IS_ERR(em)) {
1001 ret = PTR_ERR(em);
1002 goto out_free_reserve;
1003 }
1004 free_extent_map(em);
771ed689 1005
cb36a9bb
OS
1006 ret = btrfs_add_ordered_extent(inode, start, /* file_offset */
1007 async_extent->ram_size, /* num_bytes */
1008 async_extent->ram_size, /* ram_bytes */
1009 ins.objectid, /* disk_bytenr */
1010 ins.offset, /* disk_num_bytes */
1011 0, /* offset */
1012 1 << BTRFS_ORDERED_COMPRESSED,
1013 async_extent->compress_type);
b4ccace8
QW
1014 if (ret) {
1015 btrfs_drop_extent_cache(inode, start, end, 0);
1016 goto out_free_reserve;
771ed689 1017 }
b4ccace8
QW
1018 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1019
1020 /* Clear dirty, set writeback and unlock the pages. */
1021 extent_clear_unlock_delalloc(inode, start, end,
1022 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
1023 PAGE_UNLOCK | PAGE_START_WRITEBACK);
1024 if (btrfs_submit_compressed_write(inode, start, /* file_offset */
1025 async_extent->ram_size, /* num_bytes */
1026 ins.objectid, /* disk_bytenr */
1027 ins.offset, /* compressed_len */
1028 async_extent->pages, /* compressed_pages */
1029 async_extent->nr_pages,
1030 async_chunk->write_flags,
7c0c7269 1031 async_chunk->blkcg_css, true)) {
b4ccace8
QW
1032 const u64 start = async_extent->start;
1033 const u64 end = start + async_extent->ram_size - 1;
1034
1035 btrfs_writepage_endio_finish_ordered(inode, NULL, start, end, 0);
1036
1037 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
1038 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
1039 free_async_extent_pages(async_extent);
771ed689 1040 }
b4ccace8
QW
1041 *alloc_hint = ins.objectid + ins.offset;
1042 kfree(async_extent);
1043 return ret;
1044
3e04e7f1 1045out_free_reserve:
0b246afa 1046 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
2ff7e61e 1047 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
79787eaa 1048out_free:
b4ccace8 1049 extent_clear_unlock_delalloc(inode, start, end,
c2790a2e 1050 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
a7e3b975 1051 EXTENT_DELALLOC_NEW |
151a41bc 1052 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
6869b0a8
QW
1053 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1054 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
40ae837b 1055 free_async_extent_pages(async_extent);
79787eaa 1056 kfree(async_extent);
b4ccace8
QW
1057 return ret;
1058}
1059
1060/*
1061 * Phase two of compressed writeback. This is the ordered portion of the code,
1062 * which only gets called in the order the work was queued. We walk all the
1063 * async extents created by compress_file_range and send them down to the disk.
1064 */
1065static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
1066{
1067 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
1068 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1069 struct async_extent *async_extent;
1070 u64 alloc_hint = 0;
1071 int ret = 0;
1072
1073 while (!list_empty(&async_chunk->extents)) {
1074 u64 extent_start;
1075 u64 ram_size;
1076
1077 async_extent = list_entry(async_chunk->extents.next,
1078 struct async_extent, list);
1079 list_del(&async_extent->list);
1080 extent_start = async_extent->start;
1081 ram_size = async_extent->ram_size;
1082
1083 ret = submit_one_async_extent(inode, async_chunk, async_extent,
1084 &alloc_hint);
1085 btrfs_debug(fs_info,
1086"async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d",
1087 inode->root->root_key.objectid,
1088 btrfs_ino(inode), extent_start, ram_size, ret);
1089 }
771ed689
CM
1090}
1091
43c69849 1092static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
4b46fce2
JB
1093 u64 num_bytes)
1094{
43c69849 1095 struct extent_map_tree *em_tree = &inode->extent_tree;
4b46fce2
JB
1096 struct extent_map *em;
1097 u64 alloc_hint = 0;
1098
1099 read_lock(&em_tree->lock);
1100 em = search_extent_mapping(em_tree, start, num_bytes);
1101 if (em) {
1102 /*
1103 * if block start isn't an actual block number then find the
1104 * first block in this inode and use that as a hint. If that
1105 * block is also bogus then just don't worry about it.
1106 */
1107 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1108 free_extent_map(em);
1109 em = search_extent_mapping(em_tree, 0, 0);
1110 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1111 alloc_hint = em->block_start;
1112 if (em)
1113 free_extent_map(em);
1114 } else {
1115 alloc_hint = em->block_start;
1116 free_extent_map(em);
1117 }
1118 }
1119 read_unlock(&em_tree->lock);
1120
1121 return alloc_hint;
1122}
1123
771ed689
CM
1124/*
1125 * when extent_io.c finds a delayed allocation range in the file,
1126 * the call backs end up in this code. The basic idea is to
1127 * allocate extents on disk for the range, and create ordered data structs
1128 * in ram to track those extents.
1129 *
1130 * locked_page is the page that writepage had locked already. We use
1131 * it to make sure we don't do extra locks or unlocks.
1132 *
1133 * *page_started is set to one if we unlock locked_page and do everything
1134 * required to start IO on it. It may be clean and already done with
1135 * IO when we return.
1136 */
6e26c442 1137static noinline int cow_file_range(struct btrfs_inode *inode,
00361589 1138 struct page *locked_page,
74e9194a 1139 u64 start, u64 end, int *page_started,
330a5827 1140 unsigned long *nr_written, int unlock)
771ed689 1141{
6e26c442
NB
1142 struct btrfs_root *root = inode->root;
1143 struct btrfs_fs_info *fs_info = root->fs_info;
771ed689
CM
1144 u64 alloc_hint = 0;
1145 u64 num_bytes;
1146 unsigned long ram_size;
a315e68f 1147 u64 cur_alloc_size = 0;
432cd2a1 1148 u64 min_alloc_size;
0b246afa 1149 u64 blocksize = fs_info->sectorsize;
771ed689
CM
1150 struct btrfs_key ins;
1151 struct extent_map *em;
a315e68f
FM
1152 unsigned clear_bits;
1153 unsigned long page_ops;
1154 bool extent_reserved = false;
771ed689
CM
1155 int ret = 0;
1156
6e26c442 1157 if (btrfs_is_free_space_inode(inode)) {
29bce2f3
JB
1158 ret = -EINVAL;
1159 goto out_unlock;
02ecd2c2 1160 }
771ed689 1161
fda2832f 1162 num_bytes = ALIGN(end - start + 1, blocksize);
771ed689 1163 num_bytes = max(blocksize, num_bytes);
566b1760 1164 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
771ed689 1165
6e26c442 1166 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
4cb5300b 1167
7367253a
QW
1168 /*
1169 * Due to the page size limit, for subpage we can only trigger the
1170 * writeback for the dirty sectors of page, that means data writeback
1171 * is doing more writeback than what we want.
1172 *
1173 * This is especially unexpected for some call sites like fallocate,
1174 * where we only increase i_size after everything is done.
1175 * This means we can trigger inline extent even if we didn't want to.
1176 * So here we skip inline extent creation completely.
1177 */
1178 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
8dd9872d
OS
1179 u64 actual_end = min_t(u64, i_size_read(&inode->vfs_inode),
1180 end + 1);
1181
771ed689 1182 /* lets try to make an inline extent */
8dd9872d 1183 ret = cow_file_range_inline(inode, actual_end, 0,
d9496e8a 1184 BTRFS_COMPRESS_NONE, NULL, false);
771ed689 1185 if (ret == 0) {
8b62f87b
JB
1186 /*
1187 * We use DO_ACCOUNTING here because we need the
1188 * delalloc_release_metadata to be run _after_ we drop
1189 * our outstanding extent for clearing delalloc for this
1190 * range.
1191 */
4750af3b
QW
1192 extent_clear_unlock_delalloc(inode, start, end,
1193 locked_page,
c2790a2e 1194 EXTENT_LOCKED | EXTENT_DELALLOC |
8b62f87b
JB
1195 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1196 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
6869b0a8 1197 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
771ed689 1198 *nr_written = *nr_written +
09cbfeaf 1199 (end - start + PAGE_SIZE) / PAGE_SIZE;
771ed689 1200 *page_started = 1;
4750af3b
QW
1201 /*
1202 * locked_page is locked by the caller of
1203 * writepage_delalloc(), not locked by
1204 * __process_pages_contig().
1205 *
1206 * We can't let __process_pages_contig() to unlock it,
1207 * as it doesn't have any subpage::writers recorded.
1208 *
1209 * Here we manually unlock the page, since the caller
1210 * can't use page_started to determine if it's an
1211 * inline extent or a compressed extent.
1212 */
1213 unlock_page(locked_page);
771ed689 1214 goto out;
79787eaa 1215 } else if (ret < 0) {
79787eaa 1216 goto out_unlock;
771ed689
CM
1217 }
1218 }
1219
6e26c442
NB
1220 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1221 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
771ed689 1222
432cd2a1
FM
1223 /*
1224 * Relocation relies on the relocated extents to have exactly the same
1225 * size as the original extents. Normally writeback for relocation data
1226 * extents follows a NOCOW path because relocation preallocates the
1227 * extents. However, due to an operation such as scrub turning a block
1228 * group to RO mode, it may fallback to COW mode, so we must make sure
1229 * an extent allocated during COW has exactly the requested size and can
1230 * not be split into smaller extents, otherwise relocation breaks and
1231 * fails during the stage where it updates the bytenr of file extent
1232 * items.
1233 */
37f00a6d 1234 if (btrfs_is_data_reloc_root(root))
432cd2a1
FM
1235 min_alloc_size = num_bytes;
1236 else
1237 min_alloc_size = fs_info->sectorsize;
1238
3752d22f
AJ
1239 while (num_bytes > 0) {
1240 cur_alloc_size = num_bytes;
18513091 1241 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
432cd2a1 1242 min_alloc_size, 0, alloc_hint,
e570fd27 1243 &ins, 1, 1);
00361589 1244 if (ret < 0)
79787eaa 1245 goto out_unlock;
a315e68f
FM
1246 cur_alloc_size = ins.offset;
1247 extent_reserved = true;
d397712b 1248
771ed689 1249 ram_size = ins.offset;
6e26c442 1250 em = create_io_em(inode, start, ins.offset, /* len */
6f9994db
LB
1251 start, /* orig_start */
1252 ins.objectid, /* block_start */
1253 ins.offset, /* block_len */
1254 ins.offset, /* orig_block_len */
1255 ram_size, /* ram_bytes */
1256 BTRFS_COMPRESS_NONE, /* compress_type */
1af4a0aa 1257 BTRFS_ORDERED_REGULAR /* type */);
090a127a
SY
1258 if (IS_ERR(em)) {
1259 ret = PTR_ERR(em);
ace68bac 1260 goto out_reserve;
090a127a 1261 }
6f9994db 1262 free_extent_map(em);
e6dcd2dc 1263
cb36a9bb
OS
1264 ret = btrfs_add_ordered_extent(inode, start, ram_size, ram_size,
1265 ins.objectid, cur_alloc_size, 0,
1266 1 << BTRFS_ORDERED_REGULAR,
1267 BTRFS_COMPRESS_NONE);
ace68bac 1268 if (ret)
d9f85963 1269 goto out_drop_extent_cache;
c8b97818 1270
37f00a6d 1271 if (btrfs_is_data_reloc_root(root)) {
6e26c442 1272 ret = btrfs_reloc_clone_csums(inode, start,
17d217fe 1273 cur_alloc_size);
4dbd80fb
QW
1274 /*
1275 * Only drop cache here, and process as normal.
1276 *
1277 * We must not allow extent_clear_unlock_delalloc()
1278 * at out_unlock label to free meta of this ordered
1279 * extent, as its meta should be freed by
1280 * btrfs_finish_ordered_io().
1281 *
1282 * So we must continue until @start is increased to
1283 * skip current ordered extent.
1284 */
00361589 1285 if (ret)
6e26c442 1286 btrfs_drop_extent_cache(inode, start,
4dbd80fb 1287 start + ram_size - 1, 0);
17d217fe
YZ
1288 }
1289
0b246afa 1290 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
9cfa3e34 1291
f57ad937
QW
1292 /*
1293 * We're not doing compressed IO, don't unlock the first page
1294 * (which the caller expects to stay locked), don't clear any
1295 * dirty bits and don't set any writeback bits
8b62b72b 1296 *
f57ad937
QW
1297 * Do set the Ordered (Private2) bit so we know this page was
1298 * properly setup for writepage.
c8b97818 1299 */
a315e68f 1300 page_ops = unlock ? PAGE_UNLOCK : 0;
f57ad937 1301 page_ops |= PAGE_SET_ORDERED;
a791e35e 1302
6e26c442 1303 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
74e9194a 1304 locked_page,
c2790a2e 1305 EXTENT_LOCKED | EXTENT_DELALLOC,
a315e68f 1306 page_ops);
3752d22f
AJ
1307 if (num_bytes < cur_alloc_size)
1308 num_bytes = 0;
4dbd80fb 1309 else
3752d22f 1310 num_bytes -= cur_alloc_size;
c59f8951
CM
1311 alloc_hint = ins.objectid + ins.offset;
1312 start += cur_alloc_size;
a315e68f 1313 extent_reserved = false;
4dbd80fb
QW
1314
1315 /*
1316 * btrfs_reloc_clone_csums() error, since start is increased
1317 * extent_clear_unlock_delalloc() at out_unlock label won't
1318 * free metadata of current ordered extent, we're OK to exit.
1319 */
1320 if (ret)
1321 goto out_unlock;
b888db2b 1322 }
79787eaa 1323out:
be20aa9d 1324 return ret;
b7d5b0a8 1325
d9f85963 1326out_drop_extent_cache:
6e26c442 1327 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
ace68bac 1328out_reserve:
0b246afa 1329 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
2ff7e61e 1330 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
79787eaa 1331out_unlock:
a7e3b975
FM
1332 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1333 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
6869b0a8 1334 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
a315e68f
FM
1335 /*
1336 * If we reserved an extent for our delalloc range (or a subrange) and
1337 * failed to create the respective ordered extent, then it means that
1338 * when we reserved the extent we decremented the extent's size from
1339 * the data space_info's bytes_may_use counter and incremented the
1340 * space_info's bytes_reserved counter by the same amount. We must make
1341 * sure extent_clear_unlock_delalloc() does not try to decrement again
1342 * the data space_info's bytes_may_use counter, therefore we do not pass
1343 * it the flag EXTENT_CLEAR_DATA_RESV.
1344 */
1345 if (extent_reserved) {
6e26c442 1346 extent_clear_unlock_delalloc(inode, start,
e2c8e92d 1347 start + cur_alloc_size - 1,
a315e68f
FM
1348 locked_page,
1349 clear_bits,
1350 page_ops);
1351 start += cur_alloc_size;
1352 if (start >= end)
1353 goto out;
1354 }
6e26c442 1355 extent_clear_unlock_delalloc(inode, start, end, locked_page,
a315e68f
FM
1356 clear_bits | EXTENT_CLEAR_DATA_RESV,
1357 page_ops);
79787eaa 1358 goto out;
771ed689 1359}
c8b97818 1360
771ed689
CM
1361/*
1362 * work queue call back to started compression on a file and pages
1363 */
1364static noinline void async_cow_start(struct btrfs_work *work)
1365{
b5326271 1366 struct async_chunk *async_chunk;
ac3e9933 1367 int compressed_extents;
771ed689 1368
b5326271 1369 async_chunk = container_of(work, struct async_chunk, work);
771ed689 1370
ac3e9933
NB
1371 compressed_extents = compress_file_range(async_chunk);
1372 if (compressed_extents == 0) {
b5326271
NB
1373 btrfs_add_delayed_iput(async_chunk->inode);
1374 async_chunk->inode = NULL;
8180ef88 1375 }
771ed689
CM
1376}
1377
1378/*
1379 * work queue call back to submit previously compressed pages
1380 */
1381static noinline void async_cow_submit(struct btrfs_work *work)
1382{
c5a68aec
NB
1383 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1384 work);
1385 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
771ed689
CM
1386 unsigned long nr_pages;
1387
b5326271 1388 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
09cbfeaf 1389 PAGE_SHIFT;
771ed689 1390
4546d178 1391 /*
b5326271 1392 * ->inode could be NULL if async_chunk_start has failed to compress,
4546d178
NB
1393 * in which case we don't have anything to submit, yet we need to
1394 * always adjust ->async_delalloc_pages as its paired with the init
1395 * happening in cow_file_range_async
1396 */
b5326271
NB
1397 if (async_chunk->inode)
1398 submit_compressed_extents(async_chunk);
ac98141d
JB
1399
1400 /* atomic_sub_return implies a barrier */
1401 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1402 5 * SZ_1M)
1403 cond_wake_up_nomb(&fs_info->async_submit_wait);
771ed689 1404}
c8b97818 1405
771ed689
CM
1406static noinline void async_cow_free(struct btrfs_work *work)
1407{
b5326271 1408 struct async_chunk *async_chunk;
9e895a8f 1409 struct async_cow *async_cow;
97db1204 1410
b5326271
NB
1411 async_chunk = container_of(work, struct async_chunk, work);
1412 if (async_chunk->inode)
1413 btrfs_add_delayed_iput(async_chunk->inode);
ec39f769
CM
1414 if (async_chunk->blkcg_css)
1415 css_put(async_chunk->blkcg_css);
9e895a8f
QW
1416
1417 async_cow = async_chunk->async_cow;
1418 if (atomic_dec_and_test(&async_cow->num_chunks))
1419 kvfree(async_cow);
771ed689
CM
1420}
1421
751b6431 1422static int cow_file_range_async(struct btrfs_inode *inode,
ec39f769
CM
1423 struct writeback_control *wbc,
1424 struct page *locked_page,
771ed689 1425 u64 start, u64 end, int *page_started,
fac07d2b 1426 unsigned long *nr_written)
771ed689 1427{
751b6431 1428 struct btrfs_fs_info *fs_info = inode->root->fs_info;
ec39f769 1429 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
97db1204
NB
1430 struct async_cow *ctx;
1431 struct async_chunk *async_chunk;
771ed689
CM
1432 unsigned long nr_pages;
1433 u64 cur_end;
97db1204
NB
1434 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1435 int i;
1436 bool should_compress;
b1c16ac9 1437 unsigned nofs_flag;
fac07d2b 1438 const unsigned int write_flags = wbc_to_write_flags(wbc);
771ed689 1439
751b6431 1440 unlock_extent(&inode->io_tree, start, end);
97db1204 1441
751b6431 1442 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
97db1204
NB
1443 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1444 num_chunks = 1;
1445 should_compress = false;
1446 } else {
1447 should_compress = true;
1448 }
1449
b1c16ac9
NB
1450 nofs_flag = memalloc_nofs_save();
1451 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1452 memalloc_nofs_restore(nofs_flag);
1453
97db1204
NB
1454 if (!ctx) {
1455 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1456 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1457 EXTENT_DO_ACCOUNTING;
6869b0a8
QW
1458 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1459 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
97db1204 1460
751b6431
NB
1461 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1462 clear_bits, page_ops);
97db1204
NB
1463 return -ENOMEM;
1464 }
1465
1466 async_chunk = ctx->chunks;
1467 atomic_set(&ctx->num_chunks, num_chunks);
1468
1469 for (i = 0; i < num_chunks; i++) {
1470 if (should_compress)
1471 cur_end = min(end, start + SZ_512K - 1);
1472 else
1473 cur_end = end;
771ed689 1474
bd4691a0
NB
1475 /*
1476 * igrab is called higher up in the call chain, take only the
1477 * lightweight reference for the callback lifetime
1478 */
751b6431 1479 ihold(&inode->vfs_inode);
9e895a8f 1480 async_chunk[i].async_cow = ctx;
751b6431 1481 async_chunk[i].inode = &inode->vfs_inode;
97db1204
NB
1482 async_chunk[i].start = start;
1483 async_chunk[i].end = cur_end;
97db1204
NB
1484 async_chunk[i].write_flags = write_flags;
1485 INIT_LIST_HEAD(&async_chunk[i].extents);
1486
1d53c9e6
CM
1487 /*
1488 * The locked_page comes all the way from writepage and its
1489 * the original page we were actually given. As we spread
1490 * this large delalloc region across multiple async_chunk
1491 * structs, only the first struct needs a pointer to locked_page
1492 *
1493 * This way we don't need racey decisions about who is supposed
1494 * to unlock it.
1495 */
1496 if (locked_page) {
ec39f769
CM
1497 /*
1498 * Depending on the compressibility, the pages might or
1499 * might not go through async. We want all of them to
1500 * be accounted against wbc once. Let's do it here
1501 * before the paths diverge. wbc accounting is used
1502 * only for foreign writeback detection and doesn't
1503 * need full accuracy. Just account the whole thing
1504 * against the first page.
1505 */
1506 wbc_account_cgroup_owner(wbc, locked_page,
1507 cur_end - start);
1d53c9e6
CM
1508 async_chunk[i].locked_page = locked_page;
1509 locked_page = NULL;
1510 } else {
1511 async_chunk[i].locked_page = NULL;
1512 }
1513
ec39f769
CM
1514 if (blkcg_css != blkcg_root_css) {
1515 css_get(blkcg_css);
1516 async_chunk[i].blkcg_css = blkcg_css;
1517 } else {
1518 async_chunk[i].blkcg_css = NULL;
1519 }
1520
a0cac0ec
OS
1521 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1522 async_cow_submit, async_cow_free);
771ed689 1523
97db1204 1524 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
0b246afa 1525 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
771ed689 1526
97db1204 1527 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
771ed689 1528
771ed689
CM
1529 *nr_written += nr_pages;
1530 start = cur_end + 1;
1531 }
1532 *page_started = 1;
1533 return 0;
be20aa9d
CM
1534}
1535
42c01100
NA
1536static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1537 struct page *locked_page, u64 start,
1538 u64 end, int *page_started,
1539 unsigned long *nr_written)
1540{
1541 int ret;
1542
1543 ret = cow_file_range(inode, locked_page, start, end, page_started,
1544 nr_written, 0);
1545 if (ret)
1546 return ret;
1547
1548 if (*page_started)
1549 return 0;
1550
1551 __set_page_dirty_nobuffers(locked_page);
1552 account_page_redirty(locked_page);
2bd0fc93 1553 extent_write_locked_range(&inode->vfs_inode, start, end);
42c01100
NA
1554 *page_started = 1;
1555
1556 return 0;
1557}
1558
2ff7e61e 1559static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
17d217fe
YZ
1560 u64 bytenr, u64 num_bytes)
1561{
fc28b25e 1562 struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr);
17d217fe 1563 struct btrfs_ordered_sum *sums;
fc28b25e 1564 int ret;
17d217fe
YZ
1565 LIST_HEAD(list);
1566
fc28b25e 1567 ret = btrfs_lookup_csums_range(csum_root, bytenr,
a2de733c 1568 bytenr + num_bytes - 1, &list, 0);
17d217fe
YZ
1569 if (ret == 0 && list_empty(&list))
1570 return 0;
1571
1572 while (!list_empty(&list)) {
1573 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1574 list_del(&sums->list);
1575 kfree(sums);
1576 }
58113753
LB
1577 if (ret < 0)
1578 return ret;
17d217fe
YZ
1579 return 1;
1580}
1581
8ba96f3d 1582static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
467dc47e
FM
1583 const u64 start, const u64 end,
1584 int *page_started, unsigned long *nr_written)
1585{
8ba96f3d 1586 const bool is_space_ino = btrfs_is_free_space_inode(inode);
37f00a6d 1587 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
2166e5ed 1588 const u64 range_bytes = end + 1 - start;
8ba96f3d 1589 struct extent_io_tree *io_tree = &inode->io_tree;
467dc47e
FM
1590 u64 range_start = start;
1591 u64 count;
1592
1593 /*
1594 * If EXTENT_NORESERVE is set it means that when the buffered write was
1595 * made we had not enough available data space and therefore we did not
1596 * reserve data space for it, since we though we could do NOCOW for the
1597 * respective file range (either there is prealloc extent or the inode
1598 * has the NOCOW bit set).
1599 *
1600 * However when we need to fallback to COW mode (because for example the
1601 * block group for the corresponding extent was turned to RO mode by a
1602 * scrub or relocation) we need to do the following:
1603 *
1604 * 1) We increment the bytes_may_use counter of the data space info.
1605 * If COW succeeds, it allocates a new data extent and after doing
1606 * that it decrements the space info's bytes_may_use counter and
1607 * increments its bytes_reserved counter by the same amount (we do
1608 * this at btrfs_add_reserved_bytes()). So we need to increment the
1609 * bytes_may_use counter to compensate (when space is reserved at
1610 * buffered write time, the bytes_may_use counter is incremented);
1611 *
1612 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1613 * that if the COW path fails for any reason, it decrements (through
1614 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1615 * data space info, which we incremented in the step above.
2166e5ed
FM
1616 *
1617 * If we need to fallback to cow and the inode corresponds to a free
6bd335b4
FM
1618 * space cache inode or an inode of the data relocation tree, we must
1619 * also increment bytes_may_use of the data space_info for the same
1620 * reason. Space caches and relocated data extents always get a prealloc
2166e5ed 1621 * extent for them, however scrub or balance may have set the block
6bd335b4
FM
1622 * group that contains that extent to RO mode and therefore force COW
1623 * when starting writeback.
467dc47e 1624 */
2166e5ed 1625 count = count_range_bits(io_tree, &range_start, end, range_bytes,
467dc47e 1626 EXTENT_NORESERVE, 0);
6bd335b4
FM
1627 if (count > 0 || is_space_ino || is_reloc_ino) {
1628 u64 bytes = count;
8ba96f3d 1629 struct btrfs_fs_info *fs_info = inode->root->fs_info;
467dc47e
FM
1630 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1631
6bd335b4
FM
1632 if (is_space_ino || is_reloc_ino)
1633 bytes = range_bytes;
1634
467dc47e 1635 spin_lock(&sinfo->lock);
2166e5ed 1636 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
467dc47e
FM
1637 spin_unlock(&sinfo->lock);
1638
2166e5ed
FM
1639 if (count > 0)
1640 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1641 0, 0, NULL);
467dc47e
FM
1642 }
1643
8ba96f3d
NB
1644 return cow_file_range(inode, locked_page, start, end, page_started,
1645 nr_written, 1);
467dc47e
FM
1646}
1647
619104ba
FM
1648struct can_nocow_file_extent_args {
1649 /* Input fields. */
1650
1651 /* Start file offset of the range we want to NOCOW. */
1652 u64 start;
1653 /* End file offset (inclusive) of the range we want to NOCOW. */
1654 u64 end;
1655 bool writeback_path;
1656 bool strict;
1657 /*
1658 * Free the path passed to can_nocow_file_extent() once it's not needed
1659 * anymore.
1660 */
1661 bool free_path;
1662
1663 /* Output fields. Only set when can_nocow_file_extent() returns 1. */
1664
1665 u64 disk_bytenr;
1666 u64 disk_num_bytes;
1667 u64 extent_offset;
1668 /* Number of bytes that can be written to in NOCOW mode. */
1669 u64 num_bytes;
1670};
1671
1672/*
1673 * Check if we can NOCOW the file extent that the path points to.
1674 * This function may return with the path released, so the caller should check
1675 * if path->nodes[0] is NULL or not if it needs to use the path afterwards.
1676 *
1677 * Returns: < 0 on error
1678 * 0 if we can not NOCOW
1679 * 1 if we can NOCOW
1680 */
1681static int can_nocow_file_extent(struct btrfs_path *path,
1682 struct btrfs_key *key,
1683 struct btrfs_inode *inode,
1684 struct can_nocow_file_extent_args *args)
1685{
1686 const bool is_freespace_inode = btrfs_is_free_space_inode(inode);
1687 struct extent_buffer *leaf = path->nodes[0];
1688 struct btrfs_root *root = inode->root;
1689 struct btrfs_file_extent_item *fi;
1690 u64 extent_end;
1691 u8 extent_type;
1692 int can_nocow = 0;
1693 int ret = 0;
1694
1695 fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
1696 extent_type = btrfs_file_extent_type(leaf, fi);
1697
1698 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1699 goto out;
1700
1701 /* Can't access these fields unless we know it's not an inline extent. */
1702 args->disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1703 args->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1704 args->extent_offset = btrfs_file_extent_offset(leaf, fi);
1705
1706 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1707 extent_type == BTRFS_FILE_EXTENT_REG)
1708 goto out;
1709
1710 /*
1711 * If the extent was created before the generation where the last snapshot
1712 * for its subvolume was created, then this implies the extent is shared,
1713 * hence we must COW.
1714 */
a7bb6bd4 1715 if (!args->strict &&
619104ba
FM
1716 btrfs_file_extent_generation(leaf, fi) <=
1717 btrfs_root_last_snapshot(&root->root_item))
1718 goto out;
1719
1720 /* An explicit hole, must COW. */
1721 if (args->disk_bytenr == 0)
1722 goto out;
1723
1724 /* Compressed/encrypted/encoded extents must be COWed. */
1725 if (btrfs_file_extent_compression(leaf, fi) ||
1726 btrfs_file_extent_encryption(leaf, fi) ||
1727 btrfs_file_extent_other_encoding(leaf, fi))
1728 goto out;
1729
1730 extent_end = btrfs_file_extent_end(path);
1731
1732 /*
1733 * The following checks can be expensive, as they need to take other
1734 * locks and do btree or rbtree searches, so release the path to avoid
1735 * blocking other tasks for too long.
1736 */
1737 btrfs_release_path(path);
1738
1739 ret = btrfs_cross_ref_exist(root, btrfs_ino(inode),
1740 key->offset - args->extent_offset,
1741 args->disk_bytenr, false, path);
1742 WARN_ON_ONCE(ret > 0 && is_freespace_inode);
1743 if (ret != 0)
1744 goto out;
1745
1746 if (args->free_path) {
1747 /*
1748 * We don't need the path anymore, plus through the
1749 * csum_exist_in_range() call below we will end up allocating
1750 * another path. So free the path to avoid unnecessary extra
1751 * memory usage.
1752 */
1753 btrfs_free_path(path);
1754 path = NULL;
1755 }
1756
1757 /* If there are pending snapshots for this root, we must COW. */
1758 if (args->writeback_path && !is_freespace_inode &&
1759 atomic_read(&root->snapshot_force_cow))
1760 goto out;
1761
1762 args->disk_bytenr += args->extent_offset;
1763 args->disk_bytenr += args->start - key->offset;
1764 args->num_bytes = min(args->end + 1, extent_end) - args->start;
1765
1766 /*
1767 * Force COW if csums exist in the range. This ensures that csums for a
1768 * given extent are either valid or do not exist.
1769 */
1770 ret = csum_exist_in_range(root->fs_info, args->disk_bytenr, args->num_bytes);
1771 WARN_ON_ONCE(ret > 0 && is_freespace_inode);
1772 if (ret != 0)
1773 goto out;
1774
1775 can_nocow = 1;
1776 out:
1777 if (args->free_path && path)
1778 btrfs_free_path(path);
1779
1780 return ret < 0 ? ret : can_nocow;
1781}
1782
d352ac68
CM
1783/*
1784 * when nowcow writeback call back. This checks for snapshots or COW copies
1785 * of the extents that exist in the file, and COWs the file as required.
1786 *
1787 * If no cow copies or snapshots exist, we write directly to the existing
1788 * blocks on disk
1789 */
968322c8 1790static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
7f366cfe 1791 struct page *locked_page,
3e024846 1792 const u64 start, const u64 end,
6e65ae76 1793 int *page_started,
3e024846 1794 unsigned long *nr_written)
be20aa9d 1795{
968322c8
NB
1796 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1797 struct btrfs_root *root = inode->root;
be20aa9d 1798 struct btrfs_path *path;
3e024846
NB
1799 u64 cow_start = (u64)-1;
1800 u64 cur_offset = start;
8ecebf4d 1801 int ret;
3e024846 1802 bool check_prev = true;
968322c8 1803 u64 ino = btrfs_ino(inode);
2306e83e 1804 struct btrfs_block_group *bg;
762bf098 1805 bool nocow = false;
619104ba 1806 struct can_nocow_file_extent_args nocow_args = { 0 };
be20aa9d
CM
1807
1808 path = btrfs_alloc_path();
17ca04af 1809 if (!path) {
968322c8 1810 extent_clear_unlock_delalloc(inode, start, end, locked_page,
c2790a2e 1811 EXTENT_LOCKED | EXTENT_DELALLOC |
151a41bc
JB
1812 EXTENT_DO_ACCOUNTING |
1813 EXTENT_DEFRAG, PAGE_UNLOCK |
6869b0a8 1814 PAGE_START_WRITEBACK |
c2790a2e 1815 PAGE_END_WRITEBACK);
d8926bb3 1816 return -ENOMEM;
17ca04af 1817 }
82d5902d 1818
619104ba
FM
1819 nocow_args.end = end;
1820 nocow_args.writeback_path = true;
1821
80ff3856 1822 while (1) {
3e024846
NB
1823 struct btrfs_key found_key;
1824 struct btrfs_file_extent_item *fi;
1825 struct extent_buffer *leaf;
1826 u64 extent_end;
3e024846 1827 u64 ram_bytes;
619104ba 1828 u64 nocow_end;
3e024846 1829 int extent_type;
762bf098
NB
1830
1831 nocow = false;
3e024846 1832
e4c3b2dc 1833 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
80ff3856 1834 cur_offset, 0);
d788a349 1835 if (ret < 0)
79787eaa 1836 goto error;
a6bd9cd1
NB
1837
1838 /*
1839 * If there is no extent for our range when doing the initial
1840 * search, then go back to the previous slot as it will be the
1841 * one containing the search offset
1842 */
80ff3856
YZ
1843 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1844 leaf = path->nodes[0];
1845 btrfs_item_key_to_cpu(leaf, &found_key,
1846 path->slots[0] - 1);
33345d01 1847 if (found_key.objectid == ino &&
80ff3856
YZ
1848 found_key.type == BTRFS_EXTENT_DATA_KEY)
1849 path->slots[0]--;
1850 }
3e024846 1851 check_prev = false;
80ff3856 1852next_slot:
a6bd9cd1 1853 /* Go to next leaf if we have exhausted the current one */
80ff3856
YZ
1854 leaf = path->nodes[0];
1855 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1856 ret = btrfs_next_leaf(root, path);
e8916699
LB
1857 if (ret < 0) {
1858 if (cow_start != (u64)-1)
1859 cur_offset = cow_start;
79787eaa 1860 goto error;
e8916699 1861 }
80ff3856
YZ
1862 if (ret > 0)
1863 break;
1864 leaf = path->nodes[0];
1865 }
be20aa9d 1866
80ff3856
YZ
1867 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1868
a6bd9cd1 1869 /* Didn't find anything for our INO */
1d512cb7
FM
1870 if (found_key.objectid > ino)
1871 break;
a6bd9cd1
NB
1872 /*
1873 * Keep searching until we find an EXTENT_ITEM or there are no
1874 * more extents for this inode
1875 */
1d512cb7
FM
1876 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1877 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1878 path->slots[0]++;
1879 goto next_slot;
1880 }
a6bd9cd1
NB
1881
1882 /* Found key is not EXTENT_DATA_KEY or starts after req range */
1d512cb7 1883 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
80ff3856
YZ
1884 found_key.offset > end)
1885 break;
1886
a6bd9cd1
NB
1887 /*
1888 * If the found extent starts after requested offset, then
1889 * adjust extent_end to be right before this extent begins
1890 */
80ff3856
YZ
1891 if (found_key.offset > cur_offset) {
1892 extent_end = found_key.offset;
e9061e21 1893 extent_type = 0;
80ff3856
YZ
1894 goto out_check;
1895 }
1896
a6bd9cd1
NB
1897 /*
1898 * Found extent which begins before our range and potentially
1899 * intersect it
1900 */
80ff3856
YZ
1901 fi = btrfs_item_ptr(leaf, path->slots[0],
1902 struct btrfs_file_extent_item);
1903 extent_type = btrfs_file_extent_type(leaf, fi);
619104ba
FM
1904 /* If this is triggered then we have a memory corruption. */
1905 ASSERT(extent_type < BTRFS_NR_FILE_EXTENT_TYPES);
1906 if (WARN_ON(extent_type >= BTRFS_NR_FILE_EXTENT_TYPES)) {
1907 ret = -EUCLEAN;
1908 goto error;
1909 }
cc95bef6 1910 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
619104ba 1911 extent_end = btrfs_file_extent_end(path);
c65ca98f 1912
619104ba
FM
1913 /*
1914 * If the extent we got ends before our current offset, skip to
1915 * the next extent.
1916 */
1917 if (extent_end <= cur_offset) {
1918 path->slots[0]++;
1919 goto next_slot;
1920 }
c65ca98f 1921
619104ba
FM
1922 nocow_args.start = cur_offset;
1923 ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args);
1924 if (ret < 0) {
1925 if (cow_start != (u64)-1)
1926 cur_offset = cow_start;
1927 goto error;
1928 } else if (ret == 0) {
1929 goto out_check;
1930 }
58113753 1931
619104ba 1932 ret = 0;
2306e83e
FM
1933 bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr);
1934 if (bg)
3e024846 1935 nocow = true;
80ff3856 1936out_check:
a6bd9cd1
NB
1937 /*
1938 * If nocow is false then record the beginning of the range
1939 * that needs to be COWed
1940 */
80ff3856
YZ
1941 if (!nocow) {
1942 if (cow_start == (u64)-1)
1943 cow_start = cur_offset;
1944 cur_offset = extent_end;
1945 if (cur_offset > end)
1946 break;
c65ca98f
FM
1947 if (!path->nodes[0])
1948 continue;
80ff3856
YZ
1949 path->slots[0]++;
1950 goto next_slot;
7ea394f1
YZ
1951 }
1952
a6bd9cd1
NB
1953 /*
1954 * COW range from cow_start to found_key.offset - 1. As the key
1955 * will contain the beginning of the first extent that can be
1956 * NOCOW, following one which needs to be COW'ed
1957 */
80ff3856 1958 if (cow_start != (u64)-1) {
968322c8 1959 ret = fallback_to_cow(inode, locked_page,
8ba96f3d 1960 cow_start, found_key.offset - 1,
467dc47e 1961 page_started, nr_written);
230ed397 1962 if (ret)
79787eaa 1963 goto error;
80ff3856 1964 cow_start = (u64)-1;
7ea394f1 1965 }
80ff3856 1966
619104ba
FM
1967 nocow_end = cur_offset + nocow_args.num_bytes - 1;
1968
d899e052 1969 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
619104ba 1970 u64 orig_start = found_key.offset - nocow_args.extent_offset;
3e024846 1971 struct extent_map *em;
6f9994db 1972
619104ba 1973 em = create_io_em(inode, cur_offset, nocow_args.num_bytes,
6f9994db 1974 orig_start,
619104ba
FM
1975 nocow_args.disk_bytenr, /* block_start */
1976 nocow_args.num_bytes, /* block_len */
1977 nocow_args.disk_num_bytes, /* orig_block_len */
6f9994db
LB
1978 ram_bytes, BTRFS_COMPRESS_NONE,
1979 BTRFS_ORDERED_PREALLOC);
1980 if (IS_ERR(em)) {
6f9994db
LB
1981 ret = PTR_ERR(em);
1982 goto error;
d899e052 1983 }
6f9994db 1984 free_extent_map(em);
cb36a9bb 1985 ret = btrfs_add_ordered_extent(inode,
619104ba
FM
1986 cur_offset, nocow_args.num_bytes,
1987 nocow_args.num_bytes,
1988 nocow_args.disk_bytenr,
1989 nocow_args.num_bytes, 0,
cb36a9bb
OS
1990 1 << BTRFS_ORDERED_PREALLOC,
1991 BTRFS_COMPRESS_NONE);
762bf098 1992 if (ret) {
968322c8 1993 btrfs_drop_extent_cache(inode, cur_offset,
619104ba 1994 nocow_end, 0);
762bf098
NB
1995 goto error;
1996 }
d899e052 1997 } else {
968322c8 1998 ret = btrfs_add_ordered_extent(inode, cur_offset,
619104ba
FM
1999 nocow_args.num_bytes,
2000 nocow_args.num_bytes,
2001 nocow_args.disk_bytenr,
2002 nocow_args.num_bytes,
cb36a9bb
OS
2003 0,
2004 1 << BTRFS_ORDERED_NOCOW,
2005 BTRFS_COMPRESS_NONE);
762bf098
NB
2006 if (ret)
2007 goto error;
d899e052 2008 }
80ff3856 2009
2306e83e
FM
2010 if (nocow) {
2011 btrfs_dec_nocow_writers(bg);
2012 nocow = false;
2013 }
771ed689 2014
37f00a6d 2015 if (btrfs_is_data_reloc_root(root))
4dbd80fb
QW
2016 /*
2017 * Error handled later, as we must prevent
2018 * extent_clear_unlock_delalloc() in error handler
2019 * from freeing metadata of created ordered extent.
2020 */
968322c8 2021 ret = btrfs_reloc_clone_csums(inode, cur_offset,
619104ba 2022 nocow_args.num_bytes);
efa56464 2023
619104ba 2024 extent_clear_unlock_delalloc(inode, cur_offset, nocow_end,
c2790a2e 2025 locked_page, EXTENT_LOCKED |
18513091
WX
2026 EXTENT_DELALLOC |
2027 EXTENT_CLEAR_DATA_RESV,
f57ad937 2028 PAGE_UNLOCK | PAGE_SET_ORDERED);
18513091 2029
80ff3856 2030 cur_offset = extent_end;
4dbd80fb
QW
2031
2032 /*
2033 * btrfs_reloc_clone_csums() error, now we're OK to call error
2034 * handler, as metadata for created ordered extent will only
2035 * be freed by btrfs_finish_ordered_io().
2036 */
2037 if (ret)
2038 goto error;
80ff3856
YZ
2039 if (cur_offset > end)
2040 break;
be20aa9d 2041 }
b3b4aa74 2042 btrfs_release_path(path);
80ff3856 2043
506481b2 2044 if (cur_offset <= end && cow_start == (u64)-1)
80ff3856 2045 cow_start = cur_offset;
17ca04af 2046
80ff3856 2047 if (cow_start != (u64)-1) {
506481b2 2048 cur_offset = end;
968322c8
NB
2049 ret = fallback_to_cow(inode, locked_page, cow_start, end,
2050 page_started, nr_written);
d788a349 2051 if (ret)
79787eaa 2052 goto error;
80ff3856
YZ
2053 }
2054
79787eaa 2055error:
762bf098 2056 if (nocow)
2306e83e 2057 btrfs_dec_nocow_writers(bg);
762bf098 2058
17ca04af 2059 if (ret && cur_offset < end)
968322c8 2060 extent_clear_unlock_delalloc(inode, cur_offset, end,
c2790a2e 2061 locked_page, EXTENT_LOCKED |
151a41bc
JB
2062 EXTENT_DELALLOC | EXTENT_DEFRAG |
2063 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
6869b0a8 2064 PAGE_START_WRITEBACK |
c2790a2e 2065 PAGE_END_WRITEBACK);
7ea394f1 2066 btrfs_free_path(path);
79787eaa 2067 return ret;
be20aa9d
CM
2068}
2069
6e65ae76 2070static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
47059d93 2071{
6e65ae76
GR
2072 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
2073 if (inode->defrag_bytes &&
2074 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
2075 0, NULL))
2076 return false;
2077 return true;
2078 }
2079 return false;
47059d93
WS
2080}
2081
d352ac68 2082/*
5eaad97a
NB
2083 * Function to process delayed allocation (create CoW) for ranges which are
2084 * being touched for the first time.
d352ac68 2085 */
98456b9c 2086int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
5eaad97a
NB
2087 u64 start, u64 end, int *page_started, unsigned long *nr_written,
2088 struct writeback_control *wbc)
be20aa9d 2089{
be20aa9d 2090 int ret;
42c01100 2091 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
a2135011 2092
2749f7ef
QW
2093 /*
2094 * The range must cover part of the @locked_page, or the returned
2095 * @page_started can confuse the caller.
2096 */
2097 ASSERT(!(end <= page_offset(locked_page) ||
2098 start >= page_offset(locked_page) + PAGE_SIZE));
2099
6e65ae76 2100 if (should_nocow(inode, start, end)) {
2adada88
JT
2101 /*
2102 * Normally on a zoned device we're only doing COW writes, but
2103 * in case of relocation on a zoned filesystem we have taken
2104 * precaution, that we're only writing sequentially. It's safe
2105 * to use run_delalloc_nocow() here, like for regular
2106 * preallocated inodes.
2107 */
9435be73 2108 ASSERT(!zoned || btrfs_is_data_reloc_root(inode->root));
98456b9c 2109 ret = run_delalloc_nocow(inode, locked_page, start, end,
6e65ae76 2110 page_started, nr_written);
e6f9d696 2111 } else if (!btrfs_inode_can_compress(inode) ||
98456b9c 2112 !inode_need_compress(inode, start, end)) {
42c01100
NA
2113 if (zoned)
2114 ret = run_delalloc_zoned(inode, locked_page, start, end,
2115 page_started, nr_written);
2116 else
2117 ret = cow_file_range(inode, locked_page, start, end,
2118 page_started, nr_written, 1);
7ddf5a42 2119 } else {
98456b9c
NB
2120 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
2121 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
fac07d2b 2122 page_started, nr_written);
7ddf5a42 2123 }
7361b4ae 2124 ASSERT(ret <= 0);
52427260 2125 if (ret)
98456b9c 2126 btrfs_cleanup_ordered_extents(inode, locked_page, start,
d1051d6e 2127 end - start + 1);
b888db2b
CM
2128 return ret;
2129}
2130
abbb55f4
NB
2131void btrfs_split_delalloc_extent(struct inode *inode,
2132 struct extent_state *orig, u64 split)
9ed74f2d 2133{
dcab6a3b
JB
2134 u64 size;
2135
0ca1f7ce 2136 /* not delalloc, ignore it */
9ed74f2d 2137 if (!(orig->state & EXTENT_DELALLOC))
1bf85046 2138 return;
9ed74f2d 2139
dcab6a3b
JB
2140 size = orig->end - orig->start + 1;
2141 if (size > BTRFS_MAX_EXTENT_SIZE) {
823bb20a 2142 u32 num_extents;
dcab6a3b
JB
2143 u64 new_size;
2144
2145 /*
5c848198 2146 * See the explanation in btrfs_merge_delalloc_extent, the same
ba117213 2147 * applies here, just in reverse.
dcab6a3b
JB
2148 */
2149 new_size = orig->end - split + 1;
823bb20a 2150 num_extents = count_max_extents(new_size);
ba117213 2151 new_size = split - orig->start;
823bb20a
DS
2152 num_extents += count_max_extents(new_size);
2153 if (count_max_extents(size) >= num_extents)
dcab6a3b
JB
2154 return;
2155 }
2156
9e0baf60 2157 spin_lock(&BTRFS_I(inode)->lock);
8b62f87b 2158 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
9e0baf60 2159 spin_unlock(&BTRFS_I(inode)->lock);
9ed74f2d
JB
2160}
2161
2162/*
5c848198
NB
2163 * Handle merged delayed allocation extents so we can keep track of new extents
2164 * that are just merged onto old extents, such as when we are doing sequential
2165 * writes, so we can properly account for the metadata space we'll need.
9ed74f2d 2166 */
5c848198
NB
2167void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2168 struct extent_state *other)
9ed74f2d 2169{
dcab6a3b 2170 u64 new_size, old_size;
823bb20a 2171 u32 num_extents;
dcab6a3b 2172
9ed74f2d
JB
2173 /* not delalloc, ignore it */
2174 if (!(other->state & EXTENT_DELALLOC))
1bf85046 2175 return;
9ed74f2d 2176
8461a3de
JB
2177 if (new->start > other->start)
2178 new_size = new->end - other->start + 1;
2179 else
2180 new_size = other->end - new->start + 1;
dcab6a3b
JB
2181
2182 /* we're not bigger than the max, unreserve the space and go */
2183 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2184 spin_lock(&BTRFS_I(inode)->lock);
8b62f87b 2185 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
dcab6a3b
JB
2186 spin_unlock(&BTRFS_I(inode)->lock);
2187 return;
2188 }
2189
2190 /*
ba117213
JB
2191 * We have to add up either side to figure out how many extents were
2192 * accounted for before we merged into one big extent. If the number of
2193 * extents we accounted for is <= the amount we need for the new range
2194 * then we can return, otherwise drop. Think of it like this
2195 *
2196 * [ 4k][MAX_SIZE]
2197 *
2198 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2199 * need 2 outstanding extents, on one side we have 1 and the other side
2200 * we have 1 so they are == and we can return. But in this case
2201 *
2202 * [MAX_SIZE+4k][MAX_SIZE+4k]
2203 *
2204 * Each range on their own accounts for 2 extents, but merged together
2205 * they are only 3 extents worth of accounting, so we need to drop in
2206 * this case.
dcab6a3b 2207 */
ba117213 2208 old_size = other->end - other->start + 1;
823bb20a 2209 num_extents = count_max_extents(old_size);
ba117213 2210 old_size = new->end - new->start + 1;
823bb20a
DS
2211 num_extents += count_max_extents(old_size);
2212 if (count_max_extents(new_size) >= num_extents)
dcab6a3b
JB
2213 return;
2214
9e0baf60 2215 spin_lock(&BTRFS_I(inode)->lock);
8b62f87b 2216 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
9e0baf60 2217 spin_unlock(&BTRFS_I(inode)->lock);
9ed74f2d
JB
2218}
2219
eb73c1b7
MX
2220static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2221 struct inode *inode)
2222{
0b246afa
JM
2223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2224
eb73c1b7
MX
2225 spin_lock(&root->delalloc_lock);
2226 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2227 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2228 &root->delalloc_inodes);
2229 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2230 &BTRFS_I(inode)->runtime_flags);
2231 root->nr_delalloc_inodes++;
2232 if (root->nr_delalloc_inodes == 1) {
0b246afa 2233 spin_lock(&fs_info->delalloc_root_lock);
eb73c1b7
MX
2234 BUG_ON(!list_empty(&root->delalloc_root));
2235 list_add_tail(&root->delalloc_root,
0b246afa
JM
2236 &fs_info->delalloc_roots);
2237 spin_unlock(&fs_info->delalloc_root_lock);
eb73c1b7
MX
2238 }
2239 }
2240 spin_unlock(&root->delalloc_lock);
2241}
2242
2b877331
NB
2243
2244void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2245 struct btrfs_inode *inode)
eb73c1b7 2246{
3ffbd68c 2247 struct btrfs_fs_info *fs_info = root->fs_info;
0b246afa 2248
9e3e97f4
NB
2249 if (!list_empty(&inode->delalloc_inodes)) {
2250 list_del_init(&inode->delalloc_inodes);
eb73c1b7 2251 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
9e3e97f4 2252 &inode->runtime_flags);
eb73c1b7
MX
2253 root->nr_delalloc_inodes--;
2254 if (!root->nr_delalloc_inodes) {
7c8a0d36 2255 ASSERT(list_empty(&root->delalloc_inodes));
0b246afa 2256 spin_lock(&fs_info->delalloc_root_lock);
eb73c1b7
MX
2257 BUG_ON(list_empty(&root->delalloc_root));
2258 list_del_init(&root->delalloc_root);
0b246afa 2259 spin_unlock(&fs_info->delalloc_root_lock);
eb73c1b7
MX
2260 }
2261 }
2b877331
NB
2262}
2263
2264static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2265 struct btrfs_inode *inode)
2266{
2267 spin_lock(&root->delalloc_lock);
2268 __btrfs_del_delalloc_inode(root, inode);
eb73c1b7
MX
2269 spin_unlock(&root->delalloc_lock);
2270}
2271
d352ac68 2272/*
e06a1fc9
NB
2273 * Properly track delayed allocation bytes in the inode and to maintain the
2274 * list of inodes that have pending delalloc work to be done.
d352ac68 2275 */
e06a1fc9
NB
2276void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2277 unsigned *bits)
291d673e 2278{
0b246afa
JM
2279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2280
47059d93
WS
2281 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2282 WARN_ON(1);
75eff68e
CM
2283 /*
2284 * set_bit and clear bit hooks normally require _irqsave/restore
27160b6b 2285 * but in this case, we are only testing for the DELALLOC
75eff68e
CM
2286 * bit, which is only set or cleared with irqs on
2287 */
0ca1f7ce 2288 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
291d673e 2289 struct btrfs_root *root = BTRFS_I(inode)->root;
0ca1f7ce 2290 u64 len = state->end + 1 - state->start;
8b62f87b 2291 u32 num_extents = count_max_extents(len);
70ddc553 2292 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
9ed74f2d 2293
8b62f87b
JB
2294 spin_lock(&BTRFS_I(inode)->lock);
2295 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2296 spin_unlock(&BTRFS_I(inode)->lock);
287a0ab9 2297
6a3891c5 2298 /* For sanity tests */
0b246afa 2299 if (btrfs_is_testing(fs_info))
6a3891c5
JB
2300 return;
2301
104b4e51
NB
2302 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2303 fs_info->delalloc_batch);
df0af1a5 2304 spin_lock(&BTRFS_I(inode)->lock);
0ca1f7ce 2305 BTRFS_I(inode)->delalloc_bytes += len;
47059d93
WS
2306 if (*bits & EXTENT_DEFRAG)
2307 BTRFS_I(inode)->defrag_bytes += len;
df0af1a5 2308 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
eb73c1b7
MX
2309 &BTRFS_I(inode)->runtime_flags))
2310 btrfs_add_delalloc_inodes(root, inode);
df0af1a5 2311 spin_unlock(&BTRFS_I(inode)->lock);
291d673e 2312 }
a7e3b975
FM
2313
2314 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2315 (*bits & EXTENT_DELALLOC_NEW)) {
2316 spin_lock(&BTRFS_I(inode)->lock);
2317 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2318 state->start;
2319 spin_unlock(&BTRFS_I(inode)->lock);
2320 }
291d673e
CM
2321}
2322
d352ac68 2323/*
a36bb5f9
NB
2324 * Once a range is no longer delalloc this function ensures that proper
2325 * accounting happens.
d352ac68 2326 */
a36bb5f9
NB
2327void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2328 struct extent_state *state, unsigned *bits)
291d673e 2329{
a36bb5f9
NB
2330 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2331 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
47059d93 2332 u64 len = state->end + 1 - state->start;
823bb20a 2333 u32 num_extents = count_max_extents(len);
47059d93 2334
4a4b964f
FM
2335 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2336 spin_lock(&inode->lock);
6fc0ef68 2337 inode->defrag_bytes -= len;
4a4b964f
FM
2338 spin_unlock(&inode->lock);
2339 }
47059d93 2340
75eff68e
CM
2341 /*
2342 * set_bit and clear bit hooks normally require _irqsave/restore
27160b6b 2343 * but in this case, we are only testing for the DELALLOC
75eff68e
CM
2344 * bit, which is only set or cleared with irqs on
2345 */
0ca1f7ce 2346 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
6fc0ef68 2347 struct btrfs_root *root = inode->root;
83eea1f1 2348 bool do_list = !btrfs_is_free_space_inode(inode);
bcbfce8a 2349
8b62f87b
JB
2350 spin_lock(&inode->lock);
2351 btrfs_mod_outstanding_extents(inode, -num_extents);
2352 spin_unlock(&inode->lock);
0ca1f7ce 2353
b6d08f06
JB
2354 /*
2355 * We don't reserve metadata space for space cache inodes so we
52042d8e 2356 * don't need to call delalloc_release_metadata if there is an
b6d08f06
JB
2357 * error.
2358 */
a315e68f 2359 if (*bits & EXTENT_CLEAR_META_RESV &&
0b246afa 2360 root != fs_info->tree_root)
43b18595 2361 btrfs_delalloc_release_metadata(inode, len, false);
0ca1f7ce 2362
6a3891c5 2363 /* For sanity tests. */
0b246afa 2364 if (btrfs_is_testing(fs_info))
6a3891c5
JB
2365 return;
2366
37f00a6d 2367 if (!btrfs_is_data_reloc_root(root) &&
a315e68f
FM
2368 do_list && !(state->state & EXTENT_NORESERVE) &&
2369 (*bits & EXTENT_CLEAR_DATA_RESV))
9db5d510 2370 btrfs_free_reserved_data_space_noquota(fs_info, len);
9ed74f2d 2371
104b4e51
NB
2372 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2373 fs_info->delalloc_batch);
6fc0ef68
NB
2374 spin_lock(&inode->lock);
2375 inode->delalloc_bytes -= len;
2376 if (do_list && inode->delalloc_bytes == 0 &&
df0af1a5 2377 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
9e3e97f4 2378 &inode->runtime_flags))
eb73c1b7 2379 btrfs_del_delalloc_inode(root, inode);
6fc0ef68 2380 spin_unlock(&inode->lock);
291d673e 2381 }
a7e3b975
FM
2382
2383 if ((state->state & EXTENT_DELALLOC_NEW) &&
2384 (*bits & EXTENT_DELALLOC_NEW)) {
2385 spin_lock(&inode->lock);
2386 ASSERT(inode->new_delalloc_bytes >= len);
2387 inode->new_delalloc_bytes -= len;
2766ff61
FM
2388 if (*bits & EXTENT_ADD_INODE_BYTES)
2389 inode_add_bytes(&inode->vfs_inode, len);
a7e3b975
FM
2390 spin_unlock(&inode->lock);
2391 }
291d673e
CM
2392}
2393
d352ac68
CM
2394/*
2395 * in order to insert checksums into the metadata in large chunks,
2396 * we wait until bio submission time. All the pages in the bio are
2397 * checksummed and sums are attached onto the ordered extent record.
2398 *
2399 * At IO completion time the cums attached on the ordered extent record
2400 * are inserted into the btree
2401 */
8896a08d 2402static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1941b64b 2403 u64 dio_file_offset)
065631f6 2404{
e331f6b1 2405 return btrfs_csum_one_bio(BTRFS_I(inode), bio, (u64)-1, false);
4a69a410 2406}
e015640f 2407
abb99cfd
NA
2408/*
2409 * Split an extent_map at [start, start + len]
2410 *
2411 * This function is intended to be used only for extract_ordered_extent().
2412 */
2413static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2414 u64 pre, u64 post)
2415{
2416 struct extent_map_tree *em_tree = &inode->extent_tree;
2417 struct extent_map *em;
2418 struct extent_map *split_pre = NULL;
2419 struct extent_map *split_mid = NULL;
2420 struct extent_map *split_post = NULL;
2421 int ret = 0;
abb99cfd
NA
2422 unsigned long flags;
2423
2424 /* Sanity check */
2425 if (pre == 0 && post == 0)
2426 return 0;
2427
2428 split_pre = alloc_extent_map();
2429 if (pre)
2430 split_mid = alloc_extent_map();
2431 if (post)
2432 split_post = alloc_extent_map();
2433 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2434 ret = -ENOMEM;
2435 goto out;
2436 }
2437
2438 ASSERT(pre + post < len);
2439
2440 lock_extent(&inode->io_tree, start, start + len - 1);
2441 write_lock(&em_tree->lock);
2442 em = lookup_extent_mapping(em_tree, start, len);
2443 if (!em) {
2444 ret = -EIO;
2445 goto out_unlock;
2446 }
2447
2448 ASSERT(em->len == len);
2449 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2450 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
63fb5879
NA
2451 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2452 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2453 ASSERT(!list_empty(&em->list));
abb99cfd
NA
2454
2455 flags = em->flags;
2456 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
abb99cfd
NA
2457
2458 /* First, replace the em with a new extent_map starting from * em->start */
2459 split_pre->start = em->start;
2460 split_pre->len = (pre ? pre : em->len - post);
2461 split_pre->orig_start = split_pre->start;
2462 split_pre->block_start = em->block_start;
2463 split_pre->block_len = split_pre->len;
2464 split_pre->orig_block_len = split_pre->block_len;
2465 split_pre->ram_bytes = split_pre->len;
2466 split_pre->flags = flags;
2467 split_pre->compress_type = em->compress_type;
2468 split_pre->generation = em->generation;
2469
63fb5879 2470 replace_extent_mapping(em_tree, em, split_pre, 1);
abb99cfd
NA
2471
2472 /*
2473 * Now we only have an extent_map at:
2474 * [em->start, em->start + pre] if pre != 0
2475 * [em->start, em->start + em->len - post] if pre == 0
2476 */
2477
2478 if (pre) {
2479 /* Insert the middle extent_map */
2480 split_mid->start = em->start + pre;
2481 split_mid->len = em->len - pre - post;
2482 split_mid->orig_start = split_mid->start;
2483 split_mid->block_start = em->block_start + pre;
2484 split_mid->block_len = split_mid->len;
2485 split_mid->orig_block_len = split_mid->block_len;
2486 split_mid->ram_bytes = split_mid->len;
2487 split_mid->flags = flags;
2488 split_mid->compress_type = em->compress_type;
2489 split_mid->generation = em->generation;
63fb5879 2490 add_extent_mapping(em_tree, split_mid, 1);
abb99cfd
NA
2491 }
2492
2493 if (post) {
2494 split_post->start = em->start + em->len - post;
2495 split_post->len = post;
2496 split_post->orig_start = split_post->start;
2497 split_post->block_start = em->block_start + em->len - post;
2498 split_post->block_len = split_post->len;
2499 split_post->orig_block_len = split_post->block_len;
2500 split_post->ram_bytes = split_post->len;
2501 split_post->flags = flags;
2502 split_post->compress_type = em->compress_type;
2503 split_post->generation = em->generation;
63fb5879 2504 add_extent_mapping(em_tree, split_post, 1);
abb99cfd
NA
2505 }
2506
2507 /* Once for us */
2508 free_extent_map(em);
2509 /* Once for the tree */
2510 free_extent_map(em);
2511
2512out_unlock:
2513 write_unlock(&em_tree->lock);
2514 unlock_extent(&inode->io_tree, start, start + len - 1);
2515out:
2516 free_extent_map(split_pre);
2517 free_extent_map(split_mid);
2518 free_extent_map(split_post);
2519
2520 return ret;
2521}
2522
d22002fd
NA
2523static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2524 struct bio *bio, loff_t file_offset)
2525{
2526 struct btrfs_ordered_extent *ordered;
d22002fd 2527 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
abb99cfd 2528 u64 file_len;
d22002fd
NA
2529 u64 len = bio->bi_iter.bi_size;
2530 u64 end = start + len;
2531 u64 ordered_end;
2532 u64 pre, post;
2533 int ret = 0;
2534
2535 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2536 if (WARN_ON_ONCE(!ordered))
2537 return BLK_STS_IOERR;
2538
2539 /* No need to split */
2540 if (ordered->disk_num_bytes == len)
2541 goto out;
2542
2543 /* We cannot split once end_bio'd ordered extent */
2544 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2545 ret = -EINVAL;
2546 goto out;
2547 }
2548
2549 /* We cannot split a compressed ordered extent */
2550 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2551 ret = -EINVAL;
2552 goto out;
2553 }
2554
2555 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2556 /* bio must be in one ordered extent */
2557 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2558 ret = -EINVAL;
2559 goto out;
2560 }
2561
2562 /* Checksum list should be empty */
2563 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2564 ret = -EINVAL;
2565 goto out;
2566 }
2567
abb99cfd 2568 file_len = ordered->num_bytes;
d22002fd
NA
2569 pre = start - ordered->disk_bytenr;
2570 post = ordered_end - end;
2571
2572 ret = btrfs_split_ordered_extent(ordered, pre, post);
2573 if (ret)
2574 goto out;
abb99cfd 2575 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
d22002fd
NA
2576
2577out:
d22002fd
NA
2578 btrfs_put_ordered_extent(ordered);
2579
2580 return errno_to_blk_status(ret);
2581}
2582
d352ac68 2583/*
cad321ad 2584 * extent_io.c submission hook. This does the right thing for csum calculation
4c274bc6
LB
2585 * on write, or reading the csums from the tree before a read.
2586 *
2587 * Rules about async/sync submit,
2588 * a) read: sync submit
2589 *
2590 * b) write without checksum: sync submit
2591 *
2592 * c) write with checksum:
2593 * c-1) if bio is issued by fsync: sync submit
2594 * (sync_writers != 0)
2595 *
2596 * c-2) if root is reloc root: sync submit
2597 * (only in case of buffered IO)
2598 *
2599 * c-3) otherwise: async submit
d352ac68 2600 */
ad357938 2601void btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
cb3a12d9 2602 int mirror_num, enum btrfs_compression_type compress_type)
44b8bd7e 2603{
0b246afa 2604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
44b8bd7e 2605 struct btrfs_root *root = BTRFS_I(inode)->root;
0d51e28a 2606 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
4e4cbee9 2607 blk_status_t ret = 0;
19b9bdb0 2608 int skip_sum;
b812ce28 2609 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
44b8bd7e 2610
42437a63 2611 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
056c8311 2612 test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state);
cad321ad 2613
70ddc553 2614 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
0d51e28a 2615 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
0417341e 2616
d22002fd
NA
2617 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2618 struct page *page = bio_first_bvec_all(bio)->bv_page;
2619 loff_t file_offset = page_offset(page);
2620
2621 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2622 if (ret)
2623 goto out;
2624 }
2625
cfe94440 2626 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
0b246afa 2627 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
5fd02043 2628 if (ret)
61891923 2629 goto out;
5fd02043 2630
cb3a12d9 2631 if (compress_type != BTRFS_COMPRESS_NONE) {
f9f15de8
JB
2632 /*
2633 * btrfs_submit_compressed_read will handle completing
2634 * the bio if there were any errors, so just return
2635 * here.
2636 */
1d8fa2e2 2637 btrfs_submit_compressed_read(inode, bio, mirror_num);
ad357938 2638 return;
334c16d8
JB
2639 } else {
2640 /*
2641 * Lookup bio sums does extra checks around whether we
2642 * need to csum or not, which is why we ignore skip_sum
2643 * here.
2644 */
6275193e 2645 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
c2db1073 2646 if (ret)
61891923 2647 goto out;
c2db1073 2648 }
4d1b5fb4 2649 goto mapit;
b812ce28 2650 } else if (async && !skip_sum) {
17d217fe 2651 /* csum items have already been cloned */
37f00a6d 2652 if (btrfs_is_data_reloc_root(root))
17d217fe 2653 goto mapit;
19b9bdb0 2654 /* we're doing a write, do the async checksumming */
a6f5e39e 2655 ret = btrfs_wq_submit_bio(inode, bio, mirror_num,
8896a08d 2656 0, btrfs_submit_bio_start);
61891923 2657 goto out;
b812ce28 2658 } else if (!skip_sum) {
e331f6b1 2659 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, (u64)-1, false);
b812ce28
JB
2660 if (ret)
2661 goto out;
19b9bdb0
CM
2662 }
2663
0b86a832 2664mapit:
08635bae 2665 ret = btrfs_map_bio(fs_info, bio, mirror_num);
61891923
SB
2666
2667out:
4e4cbee9
CH
2668 if (ret) {
2669 bio->bi_status = ret;
4246a0b6
CH
2670 bio_endio(bio);
2671 }
065631f6 2672}
6885f308 2673
d352ac68
CM
2674/*
2675 * given a list of ordered sums record them in the inode. This happens
2676 * at IO completion time based on sums calculated at bio submission time.
2677 */
510f85ed
NB
2678static int add_pending_csums(struct btrfs_trans_handle *trans,
2679 struct list_head *list)
e6dcd2dc 2680{
e6dcd2dc 2681 struct btrfs_ordered_sum *sum;
fc28b25e 2682 struct btrfs_root *csum_root = NULL;
ac01f26a 2683 int ret;
e6dcd2dc 2684
c6e30871 2685 list_for_each_entry(sum, list, list) {
7c2871a2 2686 trans->adding_csums = true;
fc28b25e
JB
2687 if (!csum_root)
2688 csum_root = btrfs_csum_root(trans->fs_info,
2689 sum->bytenr);
2690 ret = btrfs_csum_file_blocks(trans, csum_root, sum);
7c2871a2 2691 trans->adding_csums = false;
ac01f26a
NB
2692 if (ret)
2693 return ret;
e6dcd2dc
CM
2694 }
2695 return 0;
2696}
2697
c3347309
FM
2698static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2699 const u64 start,
2700 const u64 len,
2701 struct extent_state **cached_state)
2702{
2703 u64 search_start = start;
2704 const u64 end = start + len - 1;
2705
2706 while (search_start < end) {
2707 const u64 search_len = end - search_start + 1;
2708 struct extent_map *em;
2709 u64 em_len;
2710 int ret = 0;
2711
2712 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2713 if (IS_ERR(em))
2714 return PTR_ERR(em);
2715
2716 if (em->block_start != EXTENT_MAP_HOLE)
2717 goto next;
2718
2719 em_len = em->len;
2720 if (em->start < search_start)
2721 em_len -= search_start - em->start;
2722 if (em_len > search_len)
2723 em_len = search_len;
2724
2725 ret = set_extent_bit(&inode->io_tree, search_start,
2726 search_start + em_len - 1,
1cab5e72
NB
2727 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2728 GFP_NOFS, NULL);
c3347309
FM
2729next:
2730 search_start = extent_map_end(em);
2731 free_extent_map(em);
2732 if (ret)
2733 return ret;
2734 }
2735 return 0;
2736}
2737
c2566f22 2738int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
e3b8a485 2739 unsigned int extra_bits,
330a5827 2740 struct extent_state **cached_state)
ea8c2819 2741{
fdb1e121 2742 WARN_ON(PAGE_ALIGNED(end));
c3347309
FM
2743
2744 if (start >= i_size_read(&inode->vfs_inode) &&
2745 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2746 /*
2747 * There can't be any extents following eof in this case so just
2748 * set the delalloc new bit for the range directly.
2749 */
2750 extra_bits |= EXTENT_DELALLOC_NEW;
2751 } else {
2752 int ret;
2753
2754 ret = btrfs_find_new_delalloc_bytes(inode, start,
2755 end + 1 - start,
2756 cached_state);
2757 if (ret)
2758 return ret;
2759 }
2760
c2566f22
NB
2761 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2762 cached_state);
ea8c2819
CM
2763}
2764
d352ac68 2765/* see btrfs_writepage_start_hook for details on why this is required */
247e743c
CM
2766struct btrfs_writepage_fixup {
2767 struct page *page;
f4b1363c 2768 struct inode *inode;
247e743c
CM
2769 struct btrfs_work work;
2770};
2771
b2950863 2772static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
247e743c
CM
2773{
2774 struct btrfs_writepage_fixup *fixup;
2775 struct btrfs_ordered_extent *ordered;
2ac55d41 2776 struct extent_state *cached_state = NULL;
364ecf36 2777 struct extent_changeset *data_reserved = NULL;
247e743c 2778 struct page *page;
65d87f79 2779 struct btrfs_inode *inode;
247e743c
CM
2780 u64 page_start;
2781 u64 page_end;
25f3c502 2782 int ret = 0;
f4b1363c 2783 bool free_delalloc_space = true;
247e743c
CM
2784
2785 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2786 page = fixup->page;
65d87f79 2787 inode = BTRFS_I(fixup->inode);
f4b1363c
JB
2788 page_start = page_offset(page);
2789 page_end = page_offset(page) + PAGE_SIZE - 1;
2790
2791 /*
2792 * This is similar to page_mkwrite, we need to reserve the space before
2793 * we take the page lock.
2794 */
65d87f79
NB
2795 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2796 PAGE_SIZE);
4a096752 2797again:
247e743c 2798 lock_page(page);
25f3c502
CM
2799
2800 /*
2801 * Before we queued this fixup, we took a reference on the page.
2802 * page->mapping may go NULL, but it shouldn't be moved to a different
2803 * address space.
2804 */
f4b1363c
JB
2805 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2806 /*
2807 * Unfortunately this is a little tricky, either
2808 *
2809 * 1) We got here and our page had already been dealt with and
2810 * we reserved our space, thus ret == 0, so we need to just
2811 * drop our space reservation and bail. This can happen the
2812 * first time we come into the fixup worker, or could happen
2813 * while waiting for the ordered extent.
2814 * 2) Our page was already dealt with, but we happened to get an
2815 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2816 * this case we obviously don't have anything to release, but
2817 * because the page was already dealt with we don't want to
2818 * mark the page with an error, so make sure we're resetting
2819 * ret to 0. This is why we have this check _before_ the ret
2820 * check, because we do not want to have a surprise ENOSPC
2821 * when the page was already properly dealt with.
2822 */
2823 if (!ret) {
65d87f79
NB
2824 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2825 btrfs_delalloc_release_space(inode, data_reserved,
f4b1363c
JB
2826 page_start, PAGE_SIZE,
2827 true);
2828 }
2829 ret = 0;
247e743c 2830 goto out_page;
f4b1363c 2831 }
247e743c 2832
25f3c502 2833 /*
f4b1363c
JB
2834 * We can't mess with the page state unless it is locked, so now that
2835 * it is locked bail if we failed to make our space reservation.
25f3c502 2836 */
f4b1363c
JB
2837 if (ret)
2838 goto out_page;
247e743c 2839
65d87f79 2840 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
4a096752
CM
2841
2842 /* already ordered? We're done */
f57ad937 2843 if (PageOrdered(page))
f4b1363c 2844 goto out_reserved;
4a096752 2845
65d87f79 2846 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
4a096752 2847 if (ordered) {
65d87f79
NB
2848 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2849 &cached_state);
4a096752 2850 unlock_page(page);
c0a43603 2851 btrfs_start_ordered_extent(ordered, 1);
87826df0 2852 btrfs_put_ordered_extent(ordered);
4a096752
CM
2853 goto again;
2854 }
247e743c 2855
65d87f79 2856 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
330a5827 2857 &cached_state);
25f3c502 2858 if (ret)
53687007 2859 goto out_reserved;
f3038ee3 2860
25f3c502
CM
2861 /*
2862 * Everything went as planned, we're now the owner of a dirty page with
2863 * delayed allocation bits set and space reserved for our COW
2864 * destination.
2865 *
2866 * The page was dirty when we started, nothing should have cleaned it.
2867 */
2868 BUG_ON(!PageDirty(page));
f4b1363c 2869 free_delalloc_space = false;
53687007 2870out_reserved:
65d87f79 2871 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
f4b1363c 2872 if (free_delalloc_space)
65d87f79
NB
2873 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2874 PAGE_SIZE, true);
2875 unlock_extent_cached(&inode->io_tree, page_start, page_end,
e43bbe5e 2876 &cached_state);
247e743c 2877out_page:
25f3c502
CM
2878 if (ret) {
2879 /*
2880 * We hit ENOSPC or other errors. Update the mapping and page
2881 * to reflect the errors and clean the page.
2882 */
2883 mapping_set_error(page->mapping, ret);
2884 end_extent_writepage(page, ret, page_start, page_end);
2885 clear_page_dirty_for_io(page);
2886 SetPageError(page);
2887 }
e4f94347 2888 btrfs_page_clear_checked(inode->root->fs_info, page, page_start, PAGE_SIZE);
247e743c 2889 unlock_page(page);
09cbfeaf 2890 put_page(page);
b897abec 2891 kfree(fixup);
364ecf36 2892 extent_changeset_free(data_reserved);
f4b1363c
JB
2893 /*
2894 * As a precaution, do a delayed iput in case it would be the last iput
2895 * that could need flushing space. Recursing back to fixup worker would
2896 * deadlock.
2897 */
65d87f79 2898 btrfs_add_delayed_iput(&inode->vfs_inode);
247e743c
CM
2899}
2900
2901/*
2902 * There are a few paths in the higher layers of the kernel that directly
2903 * set the page dirty bit without asking the filesystem if it is a
2904 * good idea. This causes problems because we want to make sure COW
2905 * properly happens and the data=ordered rules are followed.
2906 *
c8b97818 2907 * In our case any range that doesn't have the ORDERED bit set
247e743c
CM
2908 * hasn't been properly setup for IO. We kick off an async process
2909 * to fix it up. The async helper will wait for ordered extents, set
2910 * the delalloc bit and make it safe to write the page.
2911 */
a129ffb8 2912int btrfs_writepage_cow_fixup(struct page *page)
247e743c
CM
2913{
2914 struct inode *inode = page->mapping->host;
0b246afa 2915 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
247e743c 2916 struct btrfs_writepage_fixup *fixup;
247e743c 2917
f57ad937
QW
2918 /* This page has ordered extent covering it already */
2919 if (PageOrdered(page))
247e743c
CM
2920 return 0;
2921
25f3c502
CM
2922 /*
2923 * PageChecked is set below when we create a fixup worker for this page,
2924 * don't try to create another one if we're already PageChecked()
2925 *
2926 * The extent_io writepage code will redirty the page if we send back
2927 * EAGAIN.
2928 */
247e743c
CM
2929 if (PageChecked(page))
2930 return -EAGAIN;
2931
2932 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2933 if (!fixup)
2934 return -EAGAIN;
f421950f 2935
f4b1363c
JB
2936 /*
2937 * We are already holding a reference to this inode from
2938 * write_cache_pages. We need to hold it because the space reservation
2939 * takes place outside of the page lock, and we can't trust
2940 * page->mapping outside of the page lock.
2941 */
2942 ihold(inode);
e4f94347 2943 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);
09cbfeaf 2944 get_page(page);
a0cac0ec 2945 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
247e743c 2946 fixup->page = page;
f4b1363c 2947 fixup->inode = inode;
0b246afa 2948 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
25f3c502
CM
2949
2950 return -EAGAIN;
247e743c
CM
2951}
2952
d899e052 2953static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
c553f94d 2954 struct btrfs_inode *inode, u64 file_pos,
9729f10a 2955 struct btrfs_file_extent_item *stack_fi,
2766ff61 2956 const bool update_inode_bytes,
9729f10a 2957 u64 qgroup_reserved)
d899e052 2958{
c553f94d 2959 struct btrfs_root *root = inode->root;
2766ff61 2960 const u64 sectorsize = root->fs_info->sectorsize;
d899e052
YZ
2961 struct btrfs_path *path;
2962 struct extent_buffer *leaf;
2963 struct btrfs_key ins;
203f44c5
QW
2964 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2965 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
cb36a9bb 2966 u64 offset = btrfs_stack_file_extent_offset(stack_fi);
203f44c5
QW
2967 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2968 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
5893dfb9 2969 struct btrfs_drop_extents_args drop_args = { 0 };
d899e052
YZ
2970 int ret;
2971
2972 path = btrfs_alloc_path();
d8926bb3
MF
2973 if (!path)
2974 return -ENOMEM;
d899e052 2975
a1ed835e
CM
2976 /*
2977 * we may be replacing one extent in the tree with another.
2978 * The new extent is pinned in the extent map, and we don't want
2979 * to drop it from the cache until it is completely in the btree.
2980 *
2981 * So, tell btrfs_drop_extents to leave this extent in the cache.
2982 * the caller is expected to unpin it and allow it to be merged
2983 * with the others.
2984 */
5893dfb9
FM
2985 drop_args.path = path;
2986 drop_args.start = file_pos;
2987 drop_args.end = file_pos + num_bytes;
2988 drop_args.replace_extent = true;
2989 drop_args.extent_item_size = sizeof(*stack_fi);
2990 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
79787eaa
JM
2991 if (ret)
2992 goto out;
d899e052 2993
5893dfb9 2994 if (!drop_args.extent_inserted) {
c553f94d 2995 ins.objectid = btrfs_ino(inode);
1acae57b
FDBM
2996 ins.offset = file_pos;
2997 ins.type = BTRFS_EXTENT_DATA_KEY;
2998
1acae57b 2999 ret = btrfs_insert_empty_item(trans, root, path, &ins,
203f44c5 3000 sizeof(*stack_fi));
1acae57b
FDBM
3001 if (ret)
3002 goto out;
3003 }
d899e052 3004 leaf = path->nodes[0];
203f44c5
QW
3005 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
3006 write_extent_buffer(leaf, stack_fi,
3007 btrfs_item_ptr_offset(leaf, path->slots[0]),
3008 sizeof(struct btrfs_file_extent_item));
b9473439 3009
d899e052 3010 btrfs_mark_buffer_dirty(leaf);
ce195332 3011 btrfs_release_path(path);
d899e052 3012
2766ff61
FM
3013 /*
3014 * If we dropped an inline extent here, we know the range where it is
3015 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
1a9fd417 3016 * number of bytes only for that range containing the inline extent.
2766ff61
FM
3017 * The remaining of the range will be processed when clearning the
3018 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
3019 */
3020 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
3021 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
3022
3023 inline_size = drop_args.bytes_found - inline_size;
3024 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
3025 drop_args.bytes_found -= inline_size;
3026 num_bytes -= sectorsize;
3027 }
3028
3029 if (update_inode_bytes)
3030 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
d899e052
YZ
3031
3032 ins.objectid = disk_bytenr;
3033 ins.offset = disk_num_bytes;
3034 ins.type = BTRFS_EXTENT_ITEM_KEY;
a12b877b 3035
c553f94d 3036 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
9ddc959e
JB
3037 if (ret)
3038 goto out;
3039
c553f94d 3040 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
cb36a9bb
OS
3041 file_pos - offset,
3042 qgroup_reserved, &ins);
79787eaa 3043out:
d899e052 3044 btrfs_free_path(path);
b9473439 3045
79787eaa 3046 return ret;
d899e052
YZ
3047}
3048
2ff7e61e 3049static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
e570fd27
MX
3050 u64 start, u64 len)
3051{
32da5386 3052 struct btrfs_block_group *cache;
e570fd27 3053
0b246afa 3054 cache = btrfs_lookup_block_group(fs_info, start);
e570fd27
MX
3055 ASSERT(cache);
3056
3057 spin_lock(&cache->lock);
3058 cache->delalloc_bytes -= len;
3059 spin_unlock(&cache->lock);
3060
3061 btrfs_put_block_group(cache);
3062}
3063
203f44c5 3064static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
203f44c5
QW
3065 struct btrfs_ordered_extent *oe)
3066{
3067 struct btrfs_file_extent_item stack_fi;
2766ff61 3068 bool update_inode_bytes;
cb36a9bb
OS
3069 u64 num_bytes = oe->num_bytes;
3070 u64 ram_bytes = oe->ram_bytes;
203f44c5
QW
3071
3072 memset(&stack_fi, 0, sizeof(stack_fi));
3073 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
3074 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
3075 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
3076 oe->disk_num_bytes);
cb36a9bb 3077 btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset);
203f44c5 3078 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
cb36a9bb
OS
3079 num_bytes = ram_bytes = oe->truncated_len;
3080 btrfs_set_stack_file_extent_num_bytes(&stack_fi, num_bytes);
3081 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, ram_bytes);
203f44c5
QW
3082 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
3083 /* Encryption and other encoding is reserved and all 0 */
3084
2766ff61
FM
3085 /*
3086 * For delalloc, when completing an ordered extent we update the inode's
3087 * bytes when clearing the range in the inode's io tree, so pass false
3088 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
3089 * except if the ordered extent was truncated.
3090 */
3091 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
7c0c7269 3092 test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) ||
2766ff61
FM
3093 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
3094
3c38c877
NB
3095 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
3096 oe->file_offset, &stack_fi,
2766ff61 3097 update_inode_bytes, oe->qgroup_rsv);
203f44c5
QW
3098}
3099
3100/*
3101 * As ordered data IO finishes, this gets called so we can finish
d352ac68
CM
3102 * an ordered extent if the range of bytes in the file it covers are
3103 * fully written.
3104 */
5fd02043 3105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
e6dcd2dc 3106{
72e7e6ed
NB
3107 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
3108 struct btrfs_root *root = inode->root;
3109 struct btrfs_fs_info *fs_info = root->fs_info;
0ca1f7ce 3110 struct btrfs_trans_handle *trans = NULL;
72e7e6ed 3111 struct extent_io_tree *io_tree = &inode->io_tree;
2ac55d41 3112 struct extent_state *cached_state = NULL;
bffe633e 3113 u64 start, end;
261507a0 3114 int compress_type = 0;
77cef2ec 3115 int ret = 0;
bffe633e 3116 u64 logical_len = ordered_extent->num_bytes;
8d510121 3117 bool freespace_inode;
77cef2ec 3118 bool truncated = false;
49940bdd 3119 bool clear_reserved_extent = true;
2766ff61 3120 unsigned int clear_bits = EXTENT_DEFRAG;
a7e3b975 3121
bffe633e
OS
3122 start = ordered_extent->file_offset;
3123 end = start + ordered_extent->num_bytes - 1;
3124
a7e3b975
FM
3125 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3126 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
7c0c7269
OS
3127 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) &&
3128 !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags))
2766ff61 3129 clear_bits |= EXTENT_DELALLOC_NEW;
e6dcd2dc 3130
72e7e6ed 3131 freespace_inode = btrfs_is_free_space_inode(inode);
0cb59c99 3132
5fd02043
JB
3133 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3134 ret = -EIO;
3135 goto out;
3136 }
3137
be1a1d7a
NA
3138 /* A valid bdev implies a write on a sequential zone */
3139 if (ordered_extent->bdev) {
d8e3fb10 3140 btrfs_rewrite_logical_zoned(ordered_extent);
be1a1d7a
NA
3141 btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr,
3142 ordered_extent->disk_num_bytes);
3143 }
d8e3fb10 3144
72e7e6ed 3145 btrfs_free_io_failure_record(inode, start, end);
f612496b 3146
77cef2ec
JB
3147 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3148 truncated = true;
3149 logical_len = ordered_extent->truncated_len;
3150 /* Truncated the entire extent, don't bother adding */
3151 if (!logical_len)
3152 goto out;
3153 }
3154
c2167754 3155 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
79787eaa 3156 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
94ed938a 3157
72e7e6ed 3158 btrfs_inode_safe_disk_i_size_write(inode, 0);
8d510121
NB
3159 if (freespace_inode)
3160 trans = btrfs_join_transaction_spacecache(root);
6c760c07
JB
3161 else
3162 trans = btrfs_join_transaction(root);
3163 if (IS_ERR(trans)) {
3164 ret = PTR_ERR(trans);
3165 trans = NULL;
3166 goto out;
c2167754 3167 }
72e7e6ed 3168 trans->block_rsv = &inode->block_rsv;
729f7961 3169 ret = btrfs_update_inode_fallback(trans, root, inode);
6c760c07 3170 if (ret) /* -ENOMEM or corruption */
66642832 3171 btrfs_abort_transaction(trans, ret);
c2167754
YZ
3172 goto out;
3173 }
e6dcd2dc 3174
2766ff61 3175 clear_bits |= EXTENT_LOCKED;
bffe633e 3176 lock_extent_bits(io_tree, start, end, &cached_state);
e6dcd2dc 3177
8d510121
NB
3178 if (freespace_inode)
3179 trans = btrfs_join_transaction_spacecache(root);
0cb59c99 3180 else
7a7eaa40 3181 trans = btrfs_join_transaction(root);
79787eaa
JM
3182 if (IS_ERR(trans)) {
3183 ret = PTR_ERR(trans);
3184 trans = NULL;
a7e3b975 3185 goto out;
79787eaa 3186 }
a79b7d4b 3187
72e7e6ed 3188 trans->block_rsv = &inode->block_rsv;
c2167754 3189
c8b97818 3190 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
261507a0 3191 compress_type = ordered_extent->compress_type;
d899e052 3192 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
261507a0 3193 BUG_ON(compress_type);
72e7e6ed 3194 ret = btrfs_mark_extent_written(trans, inode,
d899e052
YZ
3195 ordered_extent->file_offset,
3196 ordered_extent->file_offset +
77cef2ec 3197 logical_len);
d899e052 3198 } else {
0b246afa 3199 BUG_ON(root == fs_info->tree_root);
3c38c877 3200 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
49940bdd
JB
3201 if (!ret) {
3202 clear_reserved_extent = false;
2ff7e61e 3203 btrfs_release_delalloc_bytes(fs_info,
bffe633e
OS
3204 ordered_extent->disk_bytenr,
3205 ordered_extent->disk_num_bytes);
49940bdd 3206 }
d899e052 3207 }
72e7e6ed 3208 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
bffe633e 3209 ordered_extent->num_bytes, trans->transid);
79787eaa 3210 if (ret < 0) {
66642832 3211 btrfs_abort_transaction(trans, ret);
a7e3b975 3212 goto out;
79787eaa 3213 }
2ac55d41 3214
510f85ed 3215 ret = add_pending_csums(trans, &ordered_extent->list);
ac01f26a
NB
3216 if (ret) {
3217 btrfs_abort_transaction(trans, ret);
3218 goto out;
3219 }
e6dcd2dc 3220
2766ff61
FM
3221 /*
3222 * If this is a new delalloc range, clear its new delalloc flag to
3223 * update the inode's number of bytes. This needs to be done first
3224 * before updating the inode item.
3225 */
3226 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3227 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
72e7e6ed 3228 clear_extent_bit(&inode->io_tree, start, end,
2766ff61
FM
3229 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3230 0, 0, &cached_state);
3231
72e7e6ed 3232 btrfs_inode_safe_disk_i_size_write(inode, 0);
729f7961 3233 ret = btrfs_update_inode_fallback(trans, root, inode);
6c760c07 3234 if (ret) { /* -ENOMEM or corruption */
66642832 3235 btrfs_abort_transaction(trans, ret);
a7e3b975 3236 goto out;
1ef30be1
JB
3237 }
3238 ret = 0;
c2167754 3239out:
72e7e6ed 3240 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
bffe633e 3241 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
313facc5 3242 &cached_state);
a7e3b975 3243
a698d075 3244 if (trans)
3a45bb20 3245 btrfs_end_transaction(trans);
0cb59c99 3246
77cef2ec 3247 if (ret || truncated) {
bffe633e 3248 u64 unwritten_start = start;
77cef2ec 3249
d61bec08
JB
3250 /*
3251 * If we failed to finish this ordered extent for any reason we
3252 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3253 * extent, and mark the inode with the error if it wasn't
3254 * already set. Any error during writeback would have already
3255 * set the mapping error, so we need to set it if we're the ones
3256 * marking this ordered extent as failed.
3257 */
3258 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3259 &ordered_extent->flags))
3260 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3261
77cef2ec 3262 if (truncated)
bffe633e
OS
3263 unwritten_start += logical_len;
3264 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
77cef2ec
JB
3265
3266 /* Drop the cache for the part of the extent we didn't write. */
72e7e6ed 3267 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
5fd02043 3268
0bec9ef5
JB
3269 /*
3270 * If the ordered extent had an IOERR or something else went
3271 * wrong we need to return the space for this ordered extent
77cef2ec
JB
3272 * back to the allocator. We only free the extent in the
3273 * truncated case if we didn't write out the extent at all.
49940bdd
JB
3274 *
3275 * If we made it past insert_reserved_file_extent before we
3276 * errored out then we don't need to do this as the accounting
3277 * has already been done.
0bec9ef5 3278 */
77cef2ec 3279 if ((ret || !logical_len) &&
49940bdd 3280 clear_reserved_extent &&
77cef2ec 3281 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
4eaaec24
NB
3282 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3283 /*
3284 * Discard the range before returning it back to the
3285 * free space pool
3286 */
46b27f50 3287 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
4eaaec24 3288 btrfs_discard_extent(fs_info,
bffe633e
OS
3289 ordered_extent->disk_bytenr,
3290 ordered_extent->disk_num_bytes,
3291 NULL);
2ff7e61e 3292 btrfs_free_reserved_extent(fs_info,
bffe633e
OS
3293 ordered_extent->disk_bytenr,
3294 ordered_extent->disk_num_bytes, 1);
4eaaec24 3295 }
0bec9ef5
JB
3296 }
3297
5fd02043 3298 /*
8bad3c02
LB
3299 * This needs to be done to make sure anybody waiting knows we are done
3300 * updating everything for this ordered extent.
5fd02043 3301 */
72e7e6ed 3302 btrfs_remove_ordered_extent(inode, ordered_extent);
5fd02043 3303
e6dcd2dc
CM
3304 /* once for us */
3305 btrfs_put_ordered_extent(ordered_extent);
3306 /* once for the tree */
3307 btrfs_put_ordered_extent(ordered_extent);
3308
5fd02043
JB
3309 return ret;
3310}
3311
3312static void finish_ordered_fn(struct btrfs_work *work)
3313{
3314 struct btrfs_ordered_extent *ordered_extent;
3315 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3316 btrfs_finish_ordered_io(ordered_extent);
e6dcd2dc
CM
3317}
3318
38a39ac7
QW
3319void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3320 struct page *page, u64 start,
25c1252a 3321 u64 end, bool uptodate)
211f90e6 3322{
38a39ac7 3323 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
1abe9b8a 3324
e65f152e
QW
3325 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3326 finish_ordered_fn, uptodate);
211f90e6
CM
3327}
3328
265d4ac0
QW
3329/*
3330 * check_data_csum - verify checksum of one sector of uncompressed data
7ffd27e3 3331 * @inode: inode
265d4ac0 3332 * @io_bio: btrfs_io_bio which contains the csum
7ffd27e3 3333 * @bio_offset: offset to the beginning of the bio (in bytes)
265d4ac0
QW
3334 * @page: page where is the data to be verified
3335 * @pgoff: offset inside the page
c1d6abda 3336 * @start: logical offset in the file
265d4ac0
QW
3337 *
3338 * The length of such check is always one sector size.
3339 */
c3a3b19b 3340static int check_data_csum(struct inode *inode, struct btrfs_bio *bbio,
c1d6abda
OS
3341 u32 bio_offset, struct page *page, u32 pgoff,
3342 u64 start)
dc380aea 3343{
d5178578
JT
3344 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3345 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
dc380aea 3346 char *kaddr;
265d4ac0 3347 u32 len = fs_info->sectorsize;
223486c2 3348 const u32 csum_size = fs_info->csum_size;
7ffd27e3 3349 unsigned int offset_sectors;
d5178578
JT
3350 u8 *csum_expected;
3351 u8 csum[BTRFS_CSUM_SIZE];
dc380aea 3352
265d4ac0
QW
3353 ASSERT(pgoff + len <= PAGE_SIZE);
3354
7ffd27e3 3355 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
c3a3b19b 3356 csum_expected = ((u8 *)bbio->csum) + offset_sectors * csum_size;
dc380aea
MX
3357
3358 kaddr = kmap_atomic(page);
d5178578
JT
3359 shash->tfm = fs_info->csum_shash;
3360
fd08001f 3361 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
b06660b5 3362 kunmap_atomic(kaddr);
d5178578
JT
3363
3364 if (memcmp(csum, csum_expected, csum_size))
dc380aea
MX
3365 goto zeroit;
3366
dc380aea
MX
3367 return 0;
3368zeroit:
c1d6abda 3369 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
c3a3b19b
QW
3370 bbio->mirror_num);
3371 if (bbio->device)
3372 btrfs_dev_stat_inc_and_print(bbio->device,
814723e0 3373 BTRFS_DEV_STAT_CORRUPTION_ERRS);
b06660b5 3374 memzero_page(page, pgoff, len);
dc380aea
MX
3375 return -EIO;
3376}
3377
d352ac68 3378/*
7ffd27e3 3379 * When reads are done, we need to check csums to verify the data is correct.
4a54c8c1
JS
3380 * if there's a match, we allow the bio to finish. If not, the code in
3381 * extent_io.c will try to find good copies for us.
7ffd27e3
QW
3382 *
3383 * @bio_offset: offset to the beginning of the bio (in bytes)
3384 * @start: file offset of the range start
3385 * @end: file offset of the range end (inclusive)
08508fea
QW
3386 *
3387 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3388 * csum match.
d352ac68 3389 */
c3a3b19b
QW
3390unsigned int btrfs_verify_data_csum(struct btrfs_bio *bbio,
3391 u32 bio_offset, struct page *page,
3392 u64 start, u64 end)
07157aac 3393{
07157aac 3394 struct inode *inode = page->mapping->host;
e4f94347 3395 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
d1310b2e 3396 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
ff79f819 3397 struct btrfs_root *root = BTRFS_I(inode)->root;
f44cf410
QW
3398 const u32 sectorsize = root->fs_info->sectorsize;
3399 u32 pg_off;
08508fea 3400 unsigned int result = 0;
d1310b2e 3401
e4f94347
QW
3402 if (btrfs_page_test_checked(fs_info, page, start, end + 1 - start)) {
3403 btrfs_page_clear_checked(fs_info, page, start, end + 1 - start);
dc380aea 3404 return 0;
d20f7043 3405 }
6cbff00f 3406
3670e645 3407 /*
e4f94347
QW
3408 * This only happens for NODATASUM or compressed read.
3409 * Normally this should be covered by above check for compressed read
3410 * or the next check for NODATASUM. Just do a quicker exit here.
3670e645 3411 */
c3a3b19b 3412 if (bbio->csum == NULL)
dc380aea 3413 return 0;
17d217fe 3414
6cbff00f 3415 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
42437a63
JB
3416 return 0;
3417
056c8311 3418 if (unlikely(test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state)))
b6cda9bc 3419 return 0;
d20f7043 3420
f44cf410
QW
3421 ASSERT(page_offset(page) <= start &&
3422 end <= page_offset(page) + PAGE_SIZE - 1);
3423 for (pg_off = offset_in_page(start);
3424 pg_off < offset_in_page(end);
3425 pg_off += sectorsize, bio_offset += sectorsize) {
e3c62324 3426 u64 file_offset = pg_off + page_offset(page);
f44cf410
QW
3427 int ret;
3428
37f00a6d 3429 if (btrfs_is_data_reloc_root(root) &&
e3c62324
QW
3430 test_range_bit(io_tree, file_offset,
3431 file_offset + sectorsize - 1,
3432 EXTENT_NODATASUM, 1, NULL)) {
3433 /* Skip the range without csum for data reloc inode */
3434 clear_extent_bits(io_tree, file_offset,
3435 file_offset + sectorsize - 1,
3436 EXTENT_NODATASUM);
3437 continue;
3438 }
c3a3b19b 3439 ret = check_data_csum(inode, bbio, bio_offset, page, pg_off,
c1d6abda 3440 page_offset(page) + pg_off);
08508fea
QW
3441 if (ret < 0) {
3442 const int nr_bit = (pg_off - offset_in_page(start)) >>
3443 root->fs_info->sectorsize_bits;
3444
3445 result |= (1U << nr_bit);
3446 }
f44cf410 3447 }
08508fea 3448 return result;
07157aac 3449}
b888db2b 3450
c1c3fac2
NB
3451/*
3452 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3453 *
3454 * @inode: The inode we want to perform iput on
3455 *
3456 * This function uses the generic vfs_inode::i_count to track whether we should
3457 * just decrement it (in case it's > 1) or if this is the last iput then link
3458 * the inode to the delayed iput machinery. Delayed iputs are processed at
3459 * transaction commit time/superblock commit/cleaner kthread.
3460 */
24bbcf04
YZ
3461void btrfs_add_delayed_iput(struct inode *inode)
3462{
0b246afa 3463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8089fe62 3464 struct btrfs_inode *binode = BTRFS_I(inode);
24bbcf04
YZ
3465
3466 if (atomic_add_unless(&inode->i_count, -1, 1))
3467 return;
3468
034f784d 3469 atomic_inc(&fs_info->nr_delayed_iputs);
24bbcf04 3470 spin_lock(&fs_info->delayed_iput_lock);
c1c3fac2
NB
3471 ASSERT(list_empty(&binode->delayed_iput));
3472 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
24bbcf04 3473 spin_unlock(&fs_info->delayed_iput_lock);
fd340d0f
JB
3474 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3475 wake_up_process(fs_info->cleaner_kthread);
24bbcf04
YZ
3476}
3477
63611e73
JB
3478static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3479 struct btrfs_inode *inode)
3480{
3481 list_del_init(&inode->delayed_iput);
3482 spin_unlock(&fs_info->delayed_iput_lock);
3483 iput(&inode->vfs_inode);
3484 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3485 wake_up(&fs_info->delayed_iputs_wait);
3486 spin_lock(&fs_info->delayed_iput_lock);
3487}
3488
3489static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3490 struct btrfs_inode *inode)
3491{
3492 if (!list_empty(&inode->delayed_iput)) {
3493 spin_lock(&fs_info->delayed_iput_lock);
3494 if (!list_empty(&inode->delayed_iput))
3495 run_delayed_iput_locked(fs_info, inode);
3496 spin_unlock(&fs_info->delayed_iput_lock);
3497 }
3498}
3499
2ff7e61e 3500void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
24bbcf04 3501{
24bbcf04 3502
24bbcf04 3503 spin_lock(&fs_info->delayed_iput_lock);
8089fe62
DS
3504 while (!list_empty(&fs_info->delayed_iputs)) {
3505 struct btrfs_inode *inode;
3506
3507 inode = list_first_entry(&fs_info->delayed_iputs,
3508 struct btrfs_inode, delayed_iput);
63611e73 3509 run_delayed_iput_locked(fs_info, inode);
71795ee5 3510 cond_resched_lock(&fs_info->delayed_iput_lock);
24bbcf04 3511 }
8089fe62 3512 spin_unlock(&fs_info->delayed_iput_lock);
24bbcf04
YZ
3513}
3514
034f784d 3515/**
2639631d
NB
3516 * Wait for flushing all delayed iputs
3517 *
3518 * @fs_info: the filesystem
034f784d
JB
3519 *
3520 * This will wait on any delayed iputs that are currently running with KILLABLE
3521 * set. Once they are all done running we will return, unless we are killed in
3522 * which case we return EINTR. This helps in user operations like fallocate etc
3523 * that might get blocked on the iputs.
2639631d
NB
3524 *
3525 * Return EINTR if we were killed, 0 if nothing's pending
034f784d
JB
3526 */
3527int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3528{
3529 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3530 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3531 if (ret)
3532 return -EINTR;
3533 return 0;
3534}
3535
7b128766 3536/*
f7e9e8fc
OS
3537 * This creates an orphan entry for the given inode in case something goes wrong
3538 * in the middle of an unlink.
7b128766 3539 */
73f2e545 3540int btrfs_orphan_add(struct btrfs_trans_handle *trans,
27919067 3541 struct btrfs_inode *inode)
7b128766 3542{
d68fc57b 3543 int ret;
7b128766 3544
27919067
OS
3545 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3546 if (ret && ret != -EEXIST) {
3547 btrfs_abort_transaction(trans, ret);
3548 return ret;
d68fc57b
YZ
3549 }
3550
d68fc57b 3551 return 0;
7b128766
JB
3552}
3553
3554/*
f7e9e8fc
OS
3555 * We have done the delete so we can go ahead and remove the orphan item for
3556 * this particular inode.
7b128766 3557 */
48a3b636 3558static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3d6ae7bb 3559 struct btrfs_inode *inode)
7b128766 3560{
27919067 3561 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
7b128766
JB
3562}
3563
3564/*
3565 * this cleans up any orphans that may be left on the list from the last use
3566 * of this root.
3567 */
66b4ffd1 3568int btrfs_orphan_cleanup(struct btrfs_root *root)
7b128766 3569{
0b246afa 3570 struct btrfs_fs_info *fs_info = root->fs_info;
7b128766
JB
3571 struct btrfs_path *path;
3572 struct extent_buffer *leaf;
7b128766
JB
3573 struct btrfs_key key, found_key;
3574 struct btrfs_trans_handle *trans;
3575 struct inode *inode;
8f6d7f4f 3576 u64 last_objectid = 0;
f7e9e8fc 3577 int ret = 0, nr_unlink = 0;
7b128766 3578
48b36a60 3579 /* Bail out if the cleanup is already running. */
54230013 3580 if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state))
66b4ffd1 3581 return 0;
c71bf099
YZ
3582
3583 path = btrfs_alloc_path();
66b4ffd1
JB
3584 if (!path) {
3585 ret = -ENOMEM;
3586 goto out;
3587 }
e4058b54 3588 path->reada = READA_BACK;
7b128766
JB
3589
3590 key.objectid = BTRFS_ORPHAN_OBJECTID;
962a298f 3591 key.type = BTRFS_ORPHAN_ITEM_KEY;
7b128766
JB
3592 key.offset = (u64)-1;
3593
7b128766
JB
3594 while (1) {
3595 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
66b4ffd1
JB
3596 if (ret < 0)
3597 goto out;
7b128766
JB
3598
3599 /*
3600 * if ret == 0 means we found what we were searching for, which
25985edc 3601 * is weird, but possible, so only screw with path if we didn't
7b128766
JB
3602 * find the key and see if we have stuff that matches
3603 */
3604 if (ret > 0) {
66b4ffd1 3605 ret = 0;
7b128766
JB
3606 if (path->slots[0] == 0)
3607 break;
3608 path->slots[0]--;
3609 }
3610
3611 /* pull out the item */
3612 leaf = path->nodes[0];
7b128766
JB
3613 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3614
3615 /* make sure the item matches what we want */
3616 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3617 break;
962a298f 3618 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
7b128766
JB
3619 break;
3620
3621 /* release the path since we're done with it */
b3b4aa74 3622 btrfs_release_path(path);
7b128766
JB
3623
3624 /*
3625 * this is where we are basically btrfs_lookup, without the
3626 * crossing root thing. we store the inode number in the
3627 * offset of the orphan item.
3628 */
8f6d7f4f
JB
3629
3630 if (found_key.offset == last_objectid) {
0b246afa
JM
3631 btrfs_err(fs_info,
3632 "Error removing orphan entry, stopping orphan cleanup");
8f6d7f4f
JB
3633 ret = -EINVAL;
3634 goto out;
3635 }
3636
3637 last_objectid = found_key.offset;
3638
5d4f98a2
YZ
3639 found_key.objectid = found_key.offset;
3640 found_key.type = BTRFS_INODE_ITEM_KEY;
3641 found_key.offset = 0;
0202e83f 3642 inode = btrfs_iget(fs_info->sb, last_objectid, root);
8c6ffba0 3643 ret = PTR_ERR_OR_ZERO(inode);
67710892 3644 if (ret && ret != -ENOENT)
66b4ffd1 3645 goto out;
7b128766 3646
0b246afa 3647 if (ret == -ENOENT && root == fs_info->tree_root) {
f8e9e0b0 3648 struct btrfs_root *dead_root;
f8e9e0b0
AJ
3649 int is_dead_root = 0;
3650
3651 /*
0c0218e9 3652 * This is an orphan in the tree root. Currently these
f8e9e0b0 3653 * could come from 2 sources:
0c0218e9 3654 * a) a root (snapshot/subvolume) deletion in progress
f8e9e0b0 3655 * b) a free space cache inode
0c0218e9
FM
3656 * We need to distinguish those two, as the orphan item
3657 * for a root must not get deleted before the deletion
3658 * of the snapshot/subvolume's tree completes.
3659 *
3660 * btrfs_find_orphan_roots() ran before us, which has
3661 * found all deleted roots and loaded them into
48b36a60 3662 * fs_info->fs_roots. So here we can find if an
0c0218e9 3663 * orphan item corresponds to a deleted root by looking
48b36a60 3664 * up the root from that xarray.
f8e9e0b0 3665 */
a619b3c7 3666
48b36a60
GN
3667 spin_lock(&fs_info->fs_roots_lock);
3668 dead_root = xa_load(&fs_info->fs_roots,
3669 (unsigned long)found_key.objectid);
a619b3c7
RK
3670 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3671 is_dead_root = 1;
48b36a60 3672 spin_unlock(&fs_info->fs_roots_lock);
a619b3c7 3673
f8e9e0b0
AJ
3674 if (is_dead_root) {
3675 /* prevent this orphan from being found again */
3676 key.offset = found_key.objectid - 1;
3677 continue;
3678 }
f7e9e8fc 3679
f8e9e0b0 3680 }
f7e9e8fc 3681
7b128766 3682 /*
f7e9e8fc 3683 * If we have an inode with links, there are a couple of
70524253
BB
3684 * possibilities:
3685 *
3686 * 1. We were halfway through creating fsverity metadata for the
3687 * file. In that case, the orphan item represents incomplete
3688 * fsverity metadata which must be cleaned up with
3689 * btrfs_drop_verity_items and deleting the orphan item.
3690
3691 * 2. Old kernels (before v3.12) used to create an
f7e9e8fc
OS
3692 * orphan item for truncate indicating that there were possibly
3693 * extent items past i_size that needed to be deleted. In v3.12,
3694 * truncate was changed to update i_size in sync with the extent
3695 * items, but the (useless) orphan item was still created. Since
3696 * v4.18, we don't create the orphan item for truncate at all.
3697 *
3698 * So, this item could mean that we need to do a truncate, but
3699 * only if this filesystem was last used on a pre-v3.12 kernel
3700 * and was not cleanly unmounted. The odds of that are quite
3701 * slim, and it's a pain to do the truncate now, so just delete
3702 * the orphan item.
3703 *
3704 * It's also possible that this orphan item was supposed to be
3705 * deleted but wasn't. The inode number may have been reused,
3706 * but either way, we can delete the orphan item.
7b128766 3707 */
f7e9e8fc 3708 if (ret == -ENOENT || inode->i_nlink) {
70524253
BB
3709 if (!ret) {
3710 ret = btrfs_drop_verity_items(BTRFS_I(inode));
f7e9e8fc 3711 iput(inode);
70524253
BB
3712 if (ret)
3713 goto out;
3714 }
a8c9e576 3715 trans = btrfs_start_transaction(root, 1);
66b4ffd1
JB
3716 if (IS_ERR(trans)) {
3717 ret = PTR_ERR(trans);
3718 goto out;
3719 }
0b246afa
JM
3720 btrfs_debug(fs_info, "auto deleting %Lu",
3721 found_key.objectid);
a8c9e576
JB
3722 ret = btrfs_del_orphan_item(trans, root,
3723 found_key.objectid);
3a45bb20 3724 btrfs_end_transaction(trans);
4ef31a45
JB
3725 if (ret)
3726 goto out;
7b128766
JB
3727 continue;
3728 }
3729
f7e9e8fc 3730 nr_unlink++;
7b128766
JB
3731
3732 /* this will do delete_inode and everything for us */
3733 iput(inode);
3734 }
3254c876
MX
3735 /* release the path since we're done with it */
3736 btrfs_release_path(path);
3737
a575ceeb 3738 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
7a7eaa40 3739 trans = btrfs_join_transaction(root);
66b4ffd1 3740 if (!IS_ERR(trans))
3a45bb20 3741 btrfs_end_transaction(trans);
d68fc57b 3742 }
7b128766
JB
3743
3744 if (nr_unlink)
0b246afa 3745 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
66b4ffd1
JB
3746
3747out:
3748 if (ret)
0b246afa 3749 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
66b4ffd1
JB
3750 btrfs_free_path(path);
3751 return ret;
7b128766
JB
3752}
3753
46a53cca
CM
3754/*
3755 * very simple check to peek ahead in the leaf looking for xattrs. If we
3756 * don't find any xattrs, we know there can't be any acls.
3757 *
3758 * slot is the slot the inode is in, objectid is the objectid of the inode
3759 */
3760static noinline int acls_after_inode_item(struct extent_buffer *leaf,
63541927
FDBM
3761 int slot, u64 objectid,
3762 int *first_xattr_slot)
46a53cca
CM
3763{
3764 u32 nritems = btrfs_header_nritems(leaf);
3765 struct btrfs_key found_key;
f23b5a59
JB
3766 static u64 xattr_access = 0;
3767 static u64 xattr_default = 0;
46a53cca
CM
3768 int scanned = 0;
3769
f23b5a59 3770 if (!xattr_access) {
97d79299
AG
3771 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3772 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3773 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3774 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
f23b5a59
JB
3775 }
3776
46a53cca 3777 slot++;
63541927 3778 *first_xattr_slot = -1;
46a53cca
CM
3779 while (slot < nritems) {
3780 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3781
3782 /* we found a different objectid, there must not be acls */
3783 if (found_key.objectid != objectid)
3784 return 0;
3785
3786 /* we found an xattr, assume we've got an acl */
f23b5a59 3787 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
63541927
FDBM
3788 if (*first_xattr_slot == -1)
3789 *first_xattr_slot = slot;
f23b5a59
JB
3790 if (found_key.offset == xattr_access ||
3791 found_key.offset == xattr_default)
3792 return 1;
3793 }
46a53cca
CM
3794
3795 /*
3796 * we found a key greater than an xattr key, there can't
3797 * be any acls later on
3798 */
3799 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3800 return 0;
3801
3802 slot++;
3803 scanned++;
3804
3805 /*
3806 * it goes inode, inode backrefs, xattrs, extents,
3807 * so if there are a ton of hard links to an inode there can
3808 * be a lot of backrefs. Don't waste time searching too hard,
3809 * this is just an optimization
3810 */
3811 if (scanned >= 8)
3812 break;
3813 }
3814 /* we hit the end of the leaf before we found an xattr or
3815 * something larger than an xattr. We have to assume the inode
3816 * has acls
3817 */
63541927
FDBM
3818 if (*first_xattr_slot == -1)
3819 *first_xattr_slot = slot;
46a53cca
CM
3820 return 1;
3821}
3822
d352ac68
CM
3823/*
3824 * read an inode from the btree into the in-memory inode
3825 */
4222ea71
FM
3826static int btrfs_read_locked_inode(struct inode *inode,
3827 struct btrfs_path *in_path)
39279cc3 3828{
0b246afa 3829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4222ea71 3830 struct btrfs_path *path = in_path;
5f39d397 3831 struct extent_buffer *leaf;
39279cc3
CM
3832 struct btrfs_inode_item *inode_item;
3833 struct btrfs_root *root = BTRFS_I(inode)->root;
3834 struct btrfs_key location;
67de1176 3835 unsigned long ptr;
46a53cca 3836 int maybe_acls;
618e21d5 3837 u32 rdev;
39279cc3 3838 int ret;
2f7e33d4 3839 bool filled = false;
63541927 3840 int first_xattr_slot;
2f7e33d4
MX
3841
3842 ret = btrfs_fill_inode(inode, &rdev);
3843 if (!ret)
3844 filled = true;
39279cc3 3845
4222ea71
FM
3846 if (!path) {
3847 path = btrfs_alloc_path();
3848 if (!path)
3849 return -ENOMEM;
3850 }
1748f843 3851
39279cc3 3852 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
dc17ff8f 3853
39279cc3 3854 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
67710892 3855 if (ret) {
4222ea71
FM
3856 if (path != in_path)
3857 btrfs_free_path(path);
f5b3a417 3858 return ret;
67710892 3859 }
39279cc3 3860
5f39d397 3861 leaf = path->nodes[0];
2f7e33d4
MX
3862
3863 if (filled)
67de1176 3864 goto cache_index;
2f7e33d4 3865
5f39d397
CM
3866 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3867 struct btrfs_inode_item);
5f39d397 3868 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
bfe86848 3869 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
2f2f43d3
EB
3870 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3871 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
6ef06d27 3872 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
41a2ee75
JB
3873 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3874 round_up(i_size_read(inode), fs_info->sectorsize));
5f39d397 3875
a937b979
DS
3876 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3877 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
5f39d397 3878
a937b979
DS
3879 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3880 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
5f39d397 3881
a937b979
DS
3882 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3883 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
5f39d397 3884
9cc97d64 3885 BTRFS_I(inode)->i_otime.tv_sec =
3886 btrfs_timespec_sec(leaf, &inode_item->otime);
3887 BTRFS_I(inode)->i_otime.tv_nsec =
3888 btrfs_timespec_nsec(leaf, &inode_item->otime);
5f39d397 3889
a76a3cd4 3890 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
e02119d5 3891 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
5dc562c5
JB
3892 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3893
c7f88c4e
JL
3894 inode_set_iversion_queried(inode,
3895 btrfs_inode_sequence(leaf, inode_item));
6e17d30b
YD
3896 inode->i_generation = BTRFS_I(inode)->generation;
3897 inode->i_rdev = 0;
3898 rdev = btrfs_inode_rdev(leaf, inode_item);
3899
3900 BTRFS_I(inode)->index_cnt = (u64)-1;
77eea05e
BB
3901 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3902 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
6e17d30b
YD
3903
3904cache_index:
5dc562c5
JB
3905 /*
3906 * If we were modified in the current generation and evicted from memory
3907 * and then re-read we need to do a full sync since we don't have any
3908 * idea about which extents were modified before we were evicted from
3909 * cache.
6e17d30b
YD
3910 *
3911 * This is required for both inode re-read from disk and delayed inode
253bf575 3912 * in the delayed_nodes xarray.
5dc562c5 3913 */
0b246afa 3914 if (BTRFS_I(inode)->last_trans == fs_info->generation)
5dc562c5
JB
3915 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3916 &BTRFS_I(inode)->runtime_flags);
3917
bde6c242
FM
3918 /*
3919 * We don't persist the id of the transaction where an unlink operation
3920 * against the inode was last made. So here we assume the inode might
3921 * have been evicted, and therefore the exact value of last_unlink_trans
3922 * lost, and set it to last_trans to avoid metadata inconsistencies
3923 * between the inode and its parent if the inode is fsync'ed and the log
3924 * replayed. For example, in the scenario:
3925 *
3926 * touch mydir/foo
3927 * ln mydir/foo mydir/bar
3928 * sync
3929 * unlink mydir/bar
3930 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3931 * xfs_io -c fsync mydir/foo
3932 * <power failure>
3933 * mount fs, triggers fsync log replay
3934 *
3935 * We must make sure that when we fsync our inode foo we also log its
3936 * parent inode, otherwise after log replay the parent still has the
3937 * dentry with the "bar" name but our inode foo has a link count of 1
3938 * and doesn't have an inode ref with the name "bar" anymore.
3939 *
3940 * Setting last_unlink_trans to last_trans is a pessimistic approach,
01327610 3941 * but it guarantees correctness at the expense of occasional full
bde6c242
FM
3942 * transaction commits on fsync if our inode is a directory, or if our
3943 * inode is not a directory, logging its parent unnecessarily.
3944 */
3945 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3946
3ebac17c
FM
3947 /*
3948 * Same logic as for last_unlink_trans. We don't persist the generation
3949 * of the last transaction where this inode was used for a reflink
3950 * operation, so after eviction and reloading the inode we must be
3951 * pessimistic and assume the last transaction that modified the inode.
3952 */
3953 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3954
67de1176
MX
3955 path->slots[0]++;
3956 if (inode->i_nlink != 1 ||
3957 path->slots[0] >= btrfs_header_nritems(leaf))
3958 goto cache_acl;
3959
3960 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
4a0cc7ca 3961 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
67de1176
MX
3962 goto cache_acl;
3963
3964 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3965 if (location.type == BTRFS_INODE_REF_KEY) {
3966 struct btrfs_inode_ref *ref;
3967
3968 ref = (struct btrfs_inode_ref *)ptr;
3969 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3970 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3971 struct btrfs_inode_extref *extref;
3972
3973 extref = (struct btrfs_inode_extref *)ptr;
3974 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3975 extref);
3976 }
2f7e33d4 3977cache_acl:
46a53cca
CM
3978 /*
3979 * try to precache a NULL acl entry for files that don't have
3980 * any xattrs or acls
3981 */
33345d01 3982 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
f85b7379 3983 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
63541927
FDBM
3984 if (first_xattr_slot != -1) {
3985 path->slots[0] = first_xattr_slot;
3986 ret = btrfs_load_inode_props(inode, path);
3987 if (ret)
0b246afa 3988 btrfs_err(fs_info,
351fd353 3989 "error loading props for ino %llu (root %llu): %d",
4a0cc7ca 3990 btrfs_ino(BTRFS_I(inode)),
63541927
FDBM
3991 root->root_key.objectid, ret);
3992 }
4222ea71
FM
3993 if (path != in_path)
3994 btrfs_free_path(path);
63541927 3995
72c04902
AV
3996 if (!maybe_acls)
3997 cache_no_acl(inode);
46a53cca 3998
39279cc3 3999 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
4000 case S_IFREG:
4001 inode->i_mapping->a_ops = &btrfs_aops;
4002 inode->i_fop = &btrfs_file_operations;
4003 inode->i_op = &btrfs_file_inode_operations;
4004 break;
4005 case S_IFDIR:
4006 inode->i_fop = &btrfs_dir_file_operations;
67ade058 4007 inode->i_op = &btrfs_dir_inode_operations;
39279cc3
CM
4008 break;
4009 case S_IFLNK:
4010 inode->i_op = &btrfs_symlink_inode_operations;
21fc61c7 4011 inode_nohighmem(inode);
4779cc04 4012 inode->i_mapping->a_ops = &btrfs_aops;
39279cc3 4013 break;
618e21d5 4014 default:
0279b4cd 4015 inode->i_op = &btrfs_special_inode_operations;
618e21d5
JB
4016 init_special_inode(inode, inode->i_mode, rdev);
4017 break;
39279cc3 4018 }
6cbff00f 4019
7b6a221e 4020 btrfs_sync_inode_flags_to_i_flags(inode);
67710892 4021 return 0;
39279cc3
CM
4022}
4023
d352ac68
CM
4024/*
4025 * given a leaf and an inode, copy the inode fields into the leaf
4026 */
e02119d5
CM
4027static void fill_inode_item(struct btrfs_trans_handle *trans,
4028 struct extent_buffer *leaf,
5f39d397 4029 struct btrfs_inode_item *item,
39279cc3
CM
4030 struct inode *inode)
4031{
51fab693 4032 struct btrfs_map_token token;
77eea05e 4033 u64 flags;
51fab693 4034
c82f823c 4035 btrfs_init_map_token(&token, leaf);
5f39d397 4036
cc4c13d5
DS
4037 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
4038 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
4039 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
4040 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
4041 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
4042
4043 btrfs_set_token_timespec_sec(&token, &item->atime,
4044 inode->i_atime.tv_sec);
4045 btrfs_set_token_timespec_nsec(&token, &item->atime,
4046 inode->i_atime.tv_nsec);
4047
4048 btrfs_set_token_timespec_sec(&token, &item->mtime,
4049 inode->i_mtime.tv_sec);
4050 btrfs_set_token_timespec_nsec(&token, &item->mtime,
4051 inode->i_mtime.tv_nsec);
4052
4053 btrfs_set_token_timespec_sec(&token, &item->ctime,
4054 inode->i_ctime.tv_sec);
4055 btrfs_set_token_timespec_nsec(&token, &item->ctime,
4056 inode->i_ctime.tv_nsec);
4057
4058 btrfs_set_token_timespec_sec(&token, &item->otime,
4059 BTRFS_I(inode)->i_otime.tv_sec);
4060 btrfs_set_token_timespec_nsec(&token, &item->otime,
4061 BTRFS_I(inode)->i_otime.tv_nsec);
4062
4063 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
4064 btrfs_set_token_inode_generation(&token, item,
4065 BTRFS_I(inode)->generation);
4066 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
4067 btrfs_set_token_inode_transid(&token, item, trans->transid);
4068 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
77eea05e
BB
4069 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
4070 BTRFS_I(inode)->ro_flags);
4071 btrfs_set_token_inode_flags(&token, item, flags);
cc4c13d5 4072 btrfs_set_token_inode_block_group(&token, item, 0);
39279cc3
CM
4073}
4074
d352ac68
CM
4075/*
4076 * copy everything in the in-memory inode into the btree.
4077 */
2115133f 4078static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
dfeb9e7c
NB
4079 struct btrfs_root *root,
4080 struct btrfs_inode *inode)
39279cc3
CM
4081{
4082 struct btrfs_inode_item *inode_item;
4083 struct btrfs_path *path;
5f39d397 4084 struct extent_buffer *leaf;
39279cc3
CM
4085 int ret;
4086
4087 path = btrfs_alloc_path();
16cdcec7
MX
4088 if (!path)
4089 return -ENOMEM;
4090
dfeb9e7c 4091 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
39279cc3
CM
4092 if (ret) {
4093 if (ret > 0)
4094 ret = -ENOENT;
4095 goto failed;
4096 }
4097
5f39d397
CM
4098 leaf = path->nodes[0];
4099 inode_item = btrfs_item_ptr(leaf, path->slots[0],
16cdcec7 4100 struct btrfs_inode_item);
39279cc3 4101
dfeb9e7c 4102 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
5f39d397 4103 btrfs_mark_buffer_dirty(leaf);
dfeb9e7c 4104 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
4105 ret = 0;
4106failed:
39279cc3
CM
4107 btrfs_free_path(path);
4108 return ret;
4109}
4110
2115133f
CM
4111/*
4112 * copy everything in the in-memory inode into the btree.
4113 */
4114noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
9a56fcd1
NB
4115 struct btrfs_root *root,
4116 struct btrfs_inode *inode)
2115133f 4117{
0b246afa 4118 struct btrfs_fs_info *fs_info = root->fs_info;
2115133f
CM
4119 int ret;
4120
4121 /*
4122 * If the inode is a free space inode, we can deadlock during commit
4123 * if we put it into the delayed code.
4124 *
4125 * The data relocation inode should also be directly updated
4126 * without delay
4127 */
9a56fcd1 4128 if (!btrfs_is_free_space_inode(inode)
37f00a6d 4129 && !btrfs_is_data_reloc_root(root)
0b246afa 4130 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
8ea05e3a
AB
4131 btrfs_update_root_times(trans, root);
4132
9a56fcd1 4133 ret = btrfs_delayed_update_inode(trans, root, inode);
2115133f 4134 if (!ret)
9a56fcd1 4135 btrfs_set_inode_last_trans(trans, inode);
2115133f
CM
4136 return ret;
4137 }
4138
9a56fcd1 4139 return btrfs_update_inode_item(trans, root, inode);
2115133f
CM
4140}
4141
729f7961
NB
4142int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4143 struct btrfs_root *root, struct btrfs_inode *inode)
2115133f
CM
4144{
4145 int ret;
4146
729f7961 4147 ret = btrfs_update_inode(trans, root, inode);
2115133f 4148 if (ret == -ENOSPC)
729f7961 4149 return btrfs_update_inode_item(trans, root, inode);
2115133f
CM
4150 return ret;
4151}
4152
d352ac68
CM
4153/*
4154 * unlink helper that gets used here in inode.c and in the tree logging
4155 * recovery code. It remove a link in a directory with a given name, and
4156 * also drops the back refs in the inode to the directory
4157 */
92986796 4158static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4ec5934e
NB
4159 struct btrfs_inode *dir,
4160 struct btrfs_inode *inode,
88d2beec
FM
4161 const char *name, int name_len,
4162 struct btrfs_rename_ctx *rename_ctx)
39279cc3 4163{
4467af88 4164 struct btrfs_root *root = dir->root;
0b246afa 4165 struct btrfs_fs_info *fs_info = root->fs_info;
39279cc3 4166 struct btrfs_path *path;
39279cc3 4167 int ret = 0;
39279cc3 4168 struct btrfs_dir_item *di;
aec7477b 4169 u64 index;
33345d01
LZ
4170 u64 ino = btrfs_ino(inode);
4171 u64 dir_ino = btrfs_ino(dir);
39279cc3
CM
4172
4173 path = btrfs_alloc_path();
54aa1f4d
CM
4174 if (!path) {
4175 ret = -ENOMEM;
554233a6 4176 goto out;
54aa1f4d
CM
4177 }
4178
33345d01 4179 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
39279cc3 4180 name, name_len, -1);
3cf5068f
LB
4181 if (IS_ERR_OR_NULL(di)) {
4182 ret = di ? PTR_ERR(di) : -ENOENT;
39279cc3
CM
4183 goto err;
4184 }
39279cc3 4185 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
4186 if (ret)
4187 goto err;
b3b4aa74 4188 btrfs_release_path(path);
39279cc3 4189
67de1176
MX
4190 /*
4191 * If we don't have dir index, we have to get it by looking up
4192 * the inode ref, since we get the inode ref, remove it directly,
4193 * it is unnecessary to do delayed deletion.
4194 *
4195 * But if we have dir index, needn't search inode ref to get it.
4196 * Since the inode ref is close to the inode item, it is better
4197 * that we delay to delete it, and just do this deletion when
4198 * we update the inode item.
4199 */
4ec5934e 4200 if (inode->dir_index) {
67de1176
MX
4201 ret = btrfs_delayed_delete_inode_ref(inode);
4202 if (!ret) {
4ec5934e 4203 index = inode->dir_index;
67de1176
MX
4204 goto skip_backref;
4205 }
4206 }
4207
33345d01
LZ
4208 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4209 dir_ino, &index);
aec7477b 4210 if (ret) {
0b246afa 4211 btrfs_info(fs_info,
c2cf52eb 4212 "failed to delete reference to %.*s, inode %llu parent %llu",
c1c9ff7c 4213 name_len, name, ino, dir_ino);
66642832 4214 btrfs_abort_transaction(trans, ret);
aec7477b
JB
4215 goto err;
4216 }
67de1176 4217skip_backref:
88d2beec
FM
4218 if (rename_ctx)
4219 rename_ctx->index = index;
4220
9add2945 4221 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
79787eaa 4222 if (ret) {
66642832 4223 btrfs_abort_transaction(trans, ret);
39279cc3 4224 goto err;
79787eaa 4225 }
39279cc3 4226
259c4b96
FM
4227 /*
4228 * If we are in a rename context, we don't need to update anything in the
4229 * log. That will be done later during the rename by btrfs_log_new_name().
4230 * Besides that, doing it here would only cause extra unncessary btree
4231 * operations on the log tree, increasing latency for applications.
4232 */
4233 if (!rename_ctx) {
4234 btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4235 dir_ino);
4236 btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4237 index);
4238 }
63611e73
JB
4239
4240 /*
4241 * If we have a pending delayed iput we could end up with the final iput
4242 * being run in btrfs-cleaner context. If we have enough of these built
4243 * up we can end up burning a lot of time in btrfs-cleaner without any
4244 * way to throttle the unlinks. Since we're currently holding a ref on
4245 * the inode we can run the delayed iput here without any issues as the
4246 * final iput won't be done until after we drop the ref we're currently
4247 * holding.
4248 */
4249 btrfs_run_delayed_iput(fs_info, inode);
39279cc3
CM
4250err:
4251 btrfs_free_path(path);
e02119d5
CM
4252 if (ret)
4253 goto out;
4254
6ef06d27 4255 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
4ec5934e
NB
4256 inode_inc_iversion(&inode->vfs_inode);
4257 inode_inc_iversion(&dir->vfs_inode);
4258 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4259 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
9a56fcd1 4260 ret = btrfs_update_inode(trans, root, dir);
e02119d5 4261out:
39279cc3
CM
4262 return ret;
4263}
4264
92986796 4265int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4ec5934e 4266 struct btrfs_inode *dir, struct btrfs_inode *inode,
92986796
AV
4267 const char *name, int name_len)
4268{
4269 int ret;
88d2beec 4270 ret = __btrfs_unlink_inode(trans, dir, inode, name, name_len, NULL);
92986796 4271 if (!ret) {
4ec5934e 4272 drop_nlink(&inode->vfs_inode);
4467af88 4273 ret = btrfs_update_inode(trans, inode->root, inode);
92986796
AV
4274 }
4275 return ret;
4276}
39279cc3 4277
a22285a6
YZ
4278/*
4279 * helper to start transaction for unlink and rmdir.
4280 *
d52be818
JB
4281 * unlink and rmdir are special in btrfs, they do not always free space, so
4282 * if we cannot make our reservations the normal way try and see if there is
4283 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4284 * allow the unlink to occur.
a22285a6 4285 */
d52be818 4286static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4df27c4d 4287{
a22285a6 4288 struct btrfs_root *root = BTRFS_I(dir)->root;
4df27c4d 4289
e70bea5f
JB
4290 /*
4291 * 1 for the possible orphan item
4292 * 1 for the dir item
4293 * 1 for the dir index
4294 * 1 for the inode ref
e70bea5f 4295 * 1 for the inode
bca4ad7c 4296 * 1 for the parent inode
e70bea5f 4297 */
bca4ad7c 4298 return btrfs_start_transaction_fallback_global_rsv(root, 6);
a22285a6
YZ
4299}
4300
4301static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4302{
a22285a6 4303 struct btrfs_trans_handle *trans;
2b0143b5 4304 struct inode *inode = d_inode(dentry);
a22285a6 4305 int ret;
a22285a6 4306
d52be818 4307 trans = __unlink_start_trans(dir);
a22285a6
YZ
4308 if (IS_ERR(trans))
4309 return PTR_ERR(trans);
5f39d397 4310
4ec5934e
NB
4311 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4312 0);
12fcfd22 4313
4467af88 4314 ret = btrfs_unlink_inode(trans, BTRFS_I(dir),
4ec5934e
NB
4315 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4316 dentry->d_name.len);
b532402e
TI
4317 if (ret)
4318 goto out;
7b128766 4319
a22285a6 4320 if (inode->i_nlink == 0) {
73f2e545 4321 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
b532402e
TI
4322 if (ret)
4323 goto out;
a22285a6 4324 }
7b128766 4325
b532402e 4326out:
3a45bb20 4327 btrfs_end_transaction(trans);
4467af88 4328 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info);
39279cc3
CM
4329 return ret;
4330}
4331
f60a2364 4332static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
045d3967 4333 struct inode *dir, struct dentry *dentry)
4df27c4d 4334{
401b3b19 4335 struct btrfs_root *root = BTRFS_I(dir)->root;
045d3967 4336 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4df27c4d
YZ
4337 struct btrfs_path *path;
4338 struct extent_buffer *leaf;
4339 struct btrfs_dir_item *di;
4340 struct btrfs_key key;
045d3967
JB
4341 const char *name = dentry->d_name.name;
4342 int name_len = dentry->d_name.len;
4df27c4d
YZ
4343 u64 index;
4344 int ret;
045d3967 4345 u64 objectid;
4a0cc7ca 4346 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
4df27c4d 4347
045d3967
JB
4348 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4349 objectid = inode->root->root_key.objectid;
4350 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4351 objectid = inode->location.objectid;
4352 } else {
4353 WARN_ON(1);
4354 return -EINVAL;
4355 }
4356
4df27c4d
YZ
4357 path = btrfs_alloc_path();
4358 if (!path)
4359 return -ENOMEM;
4360
33345d01 4361 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4df27c4d 4362 name, name_len, -1);
79787eaa 4363 if (IS_ERR_OR_NULL(di)) {
3cf5068f 4364 ret = di ? PTR_ERR(di) : -ENOENT;
79787eaa
JM
4365 goto out;
4366 }
4df27c4d
YZ
4367
4368 leaf = path->nodes[0];
4369 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4370 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4371 ret = btrfs_delete_one_dir_name(trans, root, path, di);
79787eaa 4372 if (ret) {
66642832 4373 btrfs_abort_transaction(trans, ret);
79787eaa
JM
4374 goto out;
4375 }
b3b4aa74 4376 btrfs_release_path(path);
4df27c4d 4377
d49d3287
JB
4378 /*
4379 * This is a placeholder inode for a subvolume we didn't have a
4380 * reference to at the time of the snapshot creation. In the meantime
4381 * we could have renamed the real subvol link into our snapshot, so
1a9fd417 4382 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
d49d3287
JB
4383 * Instead simply lookup the dir_index_item for this entry so we can
4384 * remove it. Otherwise we know we have a ref to the root and we can
4385 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4386 */
4387 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
33345d01 4388 di = btrfs_search_dir_index_item(root, path, dir_ino,
4df27c4d 4389 name, name_len);
79787eaa
JM
4390 if (IS_ERR_OR_NULL(di)) {
4391 if (!di)
4392 ret = -ENOENT;
4393 else
4394 ret = PTR_ERR(di);
66642832 4395 btrfs_abort_transaction(trans, ret);
79787eaa
JM
4396 goto out;
4397 }
4df27c4d
YZ
4398
4399 leaf = path->nodes[0];
4400 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4df27c4d 4401 index = key.offset;
d49d3287
JB
4402 btrfs_release_path(path);
4403 } else {
4404 ret = btrfs_del_root_ref(trans, objectid,
4405 root->root_key.objectid, dir_ino,
4406 &index, name, name_len);
4407 if (ret) {
4408 btrfs_abort_transaction(trans, ret);
4409 goto out;
4410 }
4df27c4d
YZ
4411 }
4412
9add2945 4413 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
79787eaa 4414 if (ret) {
66642832 4415 btrfs_abort_transaction(trans, ret);
79787eaa
JM
4416 goto out;
4417 }
4df27c4d 4418
6ef06d27 4419 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
0c4d2d95 4420 inode_inc_iversion(dir);
c2050a45 4421 dir->i_mtime = dir->i_ctime = current_time(dir);
729f7961 4422 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
79787eaa 4423 if (ret)
66642832 4424 btrfs_abort_transaction(trans, ret);
79787eaa 4425out:
71d7aed0 4426 btrfs_free_path(path);
79787eaa 4427 return ret;
4df27c4d
YZ
4428}
4429
ec42f167
MT
4430/*
4431 * Helper to check if the subvolume references other subvolumes or if it's
4432 * default.
4433 */
f60a2364 4434static noinline int may_destroy_subvol(struct btrfs_root *root)
ec42f167
MT
4435{
4436 struct btrfs_fs_info *fs_info = root->fs_info;
4437 struct btrfs_path *path;
4438 struct btrfs_dir_item *di;
4439 struct btrfs_key key;
4440 u64 dir_id;
4441 int ret;
4442
4443 path = btrfs_alloc_path();
4444 if (!path)
4445 return -ENOMEM;
4446
4447 /* Make sure this root isn't set as the default subvol */
4448 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4449 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4450 dir_id, "default", 7, 0);
4451 if (di && !IS_ERR(di)) {
4452 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4453 if (key.objectid == root->root_key.objectid) {
4454 ret = -EPERM;
4455 btrfs_err(fs_info,
4456 "deleting default subvolume %llu is not allowed",
4457 key.objectid);
4458 goto out;
4459 }
4460 btrfs_release_path(path);
4461 }
4462
4463 key.objectid = root->root_key.objectid;
4464 key.type = BTRFS_ROOT_REF_KEY;
4465 key.offset = (u64)-1;
4466
4467 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4468 if (ret < 0)
4469 goto out;
4470 BUG_ON(ret == 0);
4471
4472 ret = 0;
4473 if (path->slots[0] > 0) {
4474 path->slots[0]--;
4475 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4476 if (key.objectid == root->root_key.objectid &&
4477 key.type == BTRFS_ROOT_REF_KEY)
4478 ret = -ENOTEMPTY;
4479 }
4480out:
4481 btrfs_free_path(path);
4482 return ret;
4483}
4484
20a68004
NB
4485/* Delete all dentries for inodes belonging to the root */
4486static void btrfs_prune_dentries(struct btrfs_root *root)
4487{
4488 struct btrfs_fs_info *fs_info = root->fs_info;
4489 struct rb_node *node;
4490 struct rb_node *prev;
4491 struct btrfs_inode *entry;
4492 struct inode *inode;
4493 u64 objectid = 0;
4494
84961539 4495 if (!BTRFS_FS_ERROR(fs_info))
20a68004
NB
4496 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4497
4498 spin_lock(&root->inode_lock);
4499again:
4500 node = root->inode_tree.rb_node;
4501 prev = NULL;
4502 while (node) {
4503 prev = node;
4504 entry = rb_entry(node, struct btrfs_inode, rb_node);
4505
37508515 4506 if (objectid < btrfs_ino(entry))
20a68004 4507 node = node->rb_left;
37508515 4508 else if (objectid > btrfs_ino(entry))
20a68004
NB
4509 node = node->rb_right;
4510 else
4511 break;
4512 }
4513 if (!node) {
4514 while (prev) {
4515 entry = rb_entry(prev, struct btrfs_inode, rb_node);
37508515 4516 if (objectid <= btrfs_ino(entry)) {
20a68004
NB
4517 node = prev;
4518 break;
4519 }
4520 prev = rb_next(prev);
4521 }
4522 }
4523 while (node) {
4524 entry = rb_entry(node, struct btrfs_inode, rb_node);
37508515 4525 objectid = btrfs_ino(entry) + 1;
20a68004
NB
4526 inode = igrab(&entry->vfs_inode);
4527 if (inode) {
4528 spin_unlock(&root->inode_lock);
4529 if (atomic_read(&inode->i_count) > 1)
4530 d_prune_aliases(inode);
4531 /*
4532 * btrfs_drop_inode will have it removed from the inode
4533 * cache when its usage count hits zero.
4534 */
4535 iput(inode);
4536 cond_resched();
4537 spin_lock(&root->inode_lock);
4538 goto again;
4539 }
4540
4541 if (cond_resched_lock(&root->inode_lock))
4542 goto again;
4543
4544 node = rb_next(node);
4545 }
4546 spin_unlock(&root->inode_lock);
4547}
4548
f60a2364
MT
4549int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4550{
4551 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4552 struct btrfs_root *root = BTRFS_I(dir)->root;
4553 struct inode *inode = d_inode(dentry);
4554 struct btrfs_root *dest = BTRFS_I(inode)->root;
4555 struct btrfs_trans_handle *trans;
4556 struct btrfs_block_rsv block_rsv;
4557 u64 root_flags;
f60a2364 4558 int ret;
f60a2364
MT
4559
4560 /*
4561 * Don't allow to delete a subvolume with send in progress. This is
4562 * inside the inode lock so the error handling that has to drop the bit
4563 * again is not run concurrently.
4564 */
4565 spin_lock(&dest->root_item_lock);
a7176f74 4566 if (dest->send_in_progress) {
f60a2364
MT
4567 spin_unlock(&dest->root_item_lock);
4568 btrfs_warn(fs_info,
4569 "attempt to delete subvolume %llu during send",
4570 dest->root_key.objectid);
4571 return -EPERM;
4572 }
60021bd7
KH
4573 if (atomic_read(&dest->nr_swapfiles)) {
4574 spin_unlock(&dest->root_item_lock);
4575 btrfs_warn(fs_info,
4576 "attempt to delete subvolume %llu with active swapfile",
4577 root->root_key.objectid);
4578 return -EPERM;
4579 }
a7176f74
LF
4580 root_flags = btrfs_root_flags(&dest->root_item);
4581 btrfs_set_root_flags(&dest->root_item,
4582 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4583 spin_unlock(&dest->root_item_lock);
f60a2364
MT
4584
4585 down_write(&fs_info->subvol_sem);
4586
ee0d904f
NB
4587 ret = may_destroy_subvol(dest);
4588 if (ret)
f60a2364
MT
4589 goto out_up_write;
4590
4591 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4592 /*
4593 * One for dir inode,
4594 * two for dir entries,
4595 * two for root ref/backref.
4596 */
ee0d904f
NB
4597 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4598 if (ret)
f60a2364
MT
4599 goto out_up_write;
4600
4601 trans = btrfs_start_transaction(root, 0);
4602 if (IS_ERR(trans)) {
ee0d904f 4603 ret = PTR_ERR(trans);
f60a2364
MT
4604 goto out_release;
4605 }
4606 trans->block_rsv = &block_rsv;
4607 trans->bytes_reserved = block_rsv.size;
4608
4609 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4610
045d3967 4611 ret = btrfs_unlink_subvol(trans, dir, dentry);
f60a2364 4612 if (ret) {
f60a2364
MT
4613 btrfs_abort_transaction(trans, ret);
4614 goto out_end_trans;
4615 }
4616
2731f518
JB
4617 ret = btrfs_record_root_in_trans(trans, dest);
4618 if (ret) {
4619 btrfs_abort_transaction(trans, ret);
4620 goto out_end_trans;
4621 }
f60a2364
MT
4622
4623 memset(&dest->root_item.drop_progress, 0,
4624 sizeof(dest->root_item.drop_progress));
c8422684 4625 btrfs_set_root_drop_level(&dest->root_item, 0);
f60a2364
MT
4626 btrfs_set_root_refs(&dest->root_item, 0);
4627
4628 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4629 ret = btrfs_insert_orphan_item(trans,
4630 fs_info->tree_root,
4631 dest->root_key.objectid);
4632 if (ret) {
4633 btrfs_abort_transaction(trans, ret);
f60a2364
MT
4634 goto out_end_trans;
4635 }
4636 }
4637
d1957791 4638 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
f60a2364
MT
4639 BTRFS_UUID_KEY_SUBVOL,
4640 dest->root_key.objectid);
4641 if (ret && ret != -ENOENT) {
4642 btrfs_abort_transaction(trans, ret);
f60a2364
MT
4643 goto out_end_trans;
4644 }
4645 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
d1957791 4646 ret = btrfs_uuid_tree_remove(trans,
f60a2364
MT
4647 dest->root_item.received_uuid,
4648 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4649 dest->root_key.objectid);
4650 if (ret && ret != -ENOENT) {
4651 btrfs_abort_transaction(trans, ret);
f60a2364
MT
4652 goto out_end_trans;
4653 }
4654 }
4655
082b6c97
QW
4656 free_anon_bdev(dest->anon_dev);
4657 dest->anon_dev = 0;
f60a2364
MT
4658out_end_trans:
4659 trans->block_rsv = NULL;
4660 trans->bytes_reserved = 0;
4661 ret = btrfs_end_transaction(trans);
f60a2364
MT
4662 inode->i_flags |= S_DEAD;
4663out_release:
e85fde51 4664 btrfs_subvolume_release_metadata(root, &block_rsv);
f60a2364
MT
4665out_up_write:
4666 up_write(&fs_info->subvol_sem);
ee0d904f 4667 if (ret) {
f60a2364
MT
4668 spin_lock(&dest->root_item_lock);
4669 root_flags = btrfs_root_flags(&dest->root_item);
4670 btrfs_set_root_flags(&dest->root_item,
4671 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4672 spin_unlock(&dest->root_item_lock);
4673 } else {
4674 d_invalidate(dentry);
20a68004 4675 btrfs_prune_dentries(dest);
f60a2364 4676 ASSERT(dest->send_in_progress == 0);
f60a2364
MT
4677 }
4678
ee0d904f 4679 return ret;
f60a2364
MT
4680}
4681
39279cc3
CM
4682static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4683{
2b0143b5 4684 struct inode *inode = d_inode(dentry);
813febdb 4685 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1832a6d5 4686 int err = 0;
39279cc3 4687 struct btrfs_trans_handle *trans;
44f714da 4688 u64 last_unlink_trans;
39279cc3 4689
b3ae244e 4690 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
134d4512 4691 return -ENOTEMPTY;
813febdb
JB
4692 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) {
4693 if (unlikely(btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))) {
4694 btrfs_err(fs_info,
4695 "extent tree v2 doesn't support snapshot deletion yet");
4696 return -EOPNOTSUPP;
4697 }
a79a464d 4698 return btrfs_delete_subvolume(dir, dentry);
813febdb 4699 }
134d4512 4700
d52be818 4701 trans = __unlink_start_trans(dir);
a22285a6 4702 if (IS_ERR(trans))
5df6a9f6 4703 return PTR_ERR(trans);
5df6a9f6 4704
4a0cc7ca 4705 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
045d3967 4706 err = btrfs_unlink_subvol(trans, dir, dentry);
4df27c4d
YZ
4707 goto out;
4708 }
4709
73f2e545 4710 err = btrfs_orphan_add(trans, BTRFS_I(inode));
7b128766 4711 if (err)
4df27c4d 4712 goto out;
7b128766 4713
44f714da
FM
4714 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4715
39279cc3 4716 /* now the directory is empty */
4467af88 4717 err = btrfs_unlink_inode(trans, BTRFS_I(dir),
4ec5934e
NB
4718 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4719 dentry->d_name.len);
44f714da 4720 if (!err) {
6ef06d27 4721 btrfs_i_size_write(BTRFS_I(inode), 0);
44f714da
FM
4722 /*
4723 * Propagate the last_unlink_trans value of the deleted dir to
4724 * its parent directory. This is to prevent an unrecoverable
4725 * log tree in the case we do something like this:
4726 * 1) create dir foo
4727 * 2) create snapshot under dir foo
4728 * 3) delete the snapshot
4729 * 4) rmdir foo
4730 * 5) mkdir foo
4731 * 6) fsync foo or some file inside foo
4732 */
4733 if (last_unlink_trans >= trans->transid)
4734 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4735 }
4df27c4d 4736out:
3a45bb20 4737 btrfs_end_transaction(trans);
813febdb 4738 btrfs_btree_balance_dirty(fs_info);
3954401f 4739
39279cc3
CM
4740 return err;
4741}
4742
39279cc3 4743/*
9703fefe 4744 * btrfs_truncate_block - read, zero a chunk and write a block
2aaa6655
JB
4745 * @inode - inode that we're zeroing
4746 * @from - the offset to start zeroing
4747 * @len - the length to zero, 0 to zero the entire range respective to the
4748 * offset
4749 * @front - zero up to the offset instead of from the offset on
4750 *
9703fefe 4751 * This will find the block for the "from" offset and cow the block and zero the
2aaa6655 4752 * part we want to zero. This is used with truncate and hole punching.
39279cc3 4753 */
217f42eb
NB
4754int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4755 int front)
39279cc3 4756{
217f42eb
NB
4757 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4758 struct address_space *mapping = inode->vfs_inode.i_mapping;
4759 struct extent_io_tree *io_tree = &inode->io_tree;
e6dcd2dc 4760 struct btrfs_ordered_extent *ordered;
2ac55d41 4761 struct extent_state *cached_state = NULL;
364ecf36 4762 struct extent_changeset *data_reserved = NULL;
6d4572a9 4763 bool only_release_metadata = false;
0b246afa 4764 u32 blocksize = fs_info->sectorsize;
09cbfeaf 4765 pgoff_t index = from >> PAGE_SHIFT;
9703fefe 4766 unsigned offset = from & (blocksize - 1);
39279cc3 4767 struct page *page;
3b16a4e3 4768 gfp_t mask = btrfs_alloc_write_mask(mapping);
6d4572a9 4769 size_t write_bytes = blocksize;
39279cc3 4770 int ret = 0;
9703fefe
CR
4771 u64 block_start;
4772 u64 block_end;
39279cc3 4773
b03ebd99
NB
4774 if (IS_ALIGNED(offset, blocksize) &&
4775 (!len || IS_ALIGNED(len, blocksize)))
39279cc3 4776 goto out;
9703fefe 4777
8b62f87b
JB
4778 block_start = round_down(from, blocksize);
4779 block_end = block_start + blocksize - 1;
4780
217f42eb
NB
4781 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4782 blocksize);
6d4572a9 4783 if (ret < 0) {
217f42eb 4784 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
6d4572a9
QW
4785 /* For nocow case, no need to reserve data space */
4786 only_release_metadata = true;
4787 } else {
4788 goto out;
4789 }
4790 }
d4135134 4791 ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false);
6d4572a9
QW
4792 if (ret < 0) {
4793 if (!only_release_metadata)
217f42eb
NB
4794 btrfs_free_reserved_data_space(inode, data_reserved,
4795 block_start, blocksize);
6d4572a9
QW
4796 goto out;
4797 }
211c17f5 4798again:
3b16a4e3 4799 page = find_or_create_page(mapping, index, mask);
5d5e103a 4800 if (!page) {
217f42eb
NB
4801 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4802 blocksize, true);
4803 btrfs_delalloc_release_extents(inode, blocksize);
ac6a2b36 4804 ret = -ENOMEM;
39279cc3 4805 goto out;
5d5e103a 4806 }
32443de3
QW
4807 ret = set_page_extent_mapped(page);
4808 if (ret < 0)
4809 goto out_unlock;
e6dcd2dc 4810
39279cc3 4811 if (!PageUptodate(page)) {
9ebefb18 4812 ret = btrfs_readpage(NULL, page);
39279cc3 4813 lock_page(page);
211c17f5
CM
4814 if (page->mapping != mapping) {
4815 unlock_page(page);
09cbfeaf 4816 put_page(page);
211c17f5
CM
4817 goto again;
4818 }
39279cc3
CM
4819 if (!PageUptodate(page)) {
4820 ret = -EIO;
89642229 4821 goto out_unlock;
39279cc3
CM
4822 }
4823 }
211c17f5 4824 wait_on_page_writeback(page);
e6dcd2dc 4825
9703fefe 4826 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
e6dcd2dc 4827
217f42eb 4828 ordered = btrfs_lookup_ordered_extent(inode, block_start);
e6dcd2dc 4829 if (ordered) {
9703fefe 4830 unlock_extent_cached(io_tree, block_start, block_end,
e43bbe5e 4831 &cached_state);
e6dcd2dc 4832 unlock_page(page);
09cbfeaf 4833 put_page(page);
c0a43603 4834 btrfs_start_ordered_extent(ordered, 1);
e6dcd2dc
CM
4835 btrfs_put_ordered_extent(ordered);
4836 goto again;
4837 }
4838
217f42eb 4839 clear_extent_bit(&inode->io_tree, block_start, block_end,
e182163d
OS
4840 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4841 0, 0, &cached_state);
5d5e103a 4842
217f42eb 4843 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
330a5827 4844 &cached_state);
9ed74f2d 4845 if (ret) {
9703fefe 4846 unlock_extent_cached(io_tree, block_start, block_end,
e43bbe5e 4847 &cached_state);
9ed74f2d
JB
4848 goto out_unlock;
4849 }
4850
9703fefe 4851 if (offset != blocksize) {
2aaa6655 4852 if (!len)
9703fefe 4853 len = blocksize - offset;
2aaa6655 4854 if (front)
d048b9c2
IW
4855 memzero_page(page, (block_start - page_offset(page)),
4856 offset);
2aaa6655 4857 else
d048b9c2
IW
4858 memzero_page(page, (block_start - page_offset(page)) + offset,
4859 len);
e6dcd2dc 4860 flush_dcache_page(page);
e6dcd2dc 4861 }
e4f94347
QW
4862 btrfs_page_clear_checked(fs_info, page, block_start,
4863 block_end + 1 - block_start);
6c9ac8be 4864 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
e43bbe5e 4865 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
39279cc3 4866
6d4572a9 4867 if (only_release_metadata)
217f42eb 4868 set_extent_bit(&inode->io_tree, block_start, block_end,
1cab5e72 4869 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
6d4572a9 4870
89642229 4871out_unlock:
6d4572a9
QW
4872 if (ret) {
4873 if (only_release_metadata)
217f42eb 4874 btrfs_delalloc_release_metadata(inode, blocksize, true);
6d4572a9 4875 else
217f42eb 4876 btrfs_delalloc_release_space(inode, data_reserved,
6d4572a9
QW
4877 block_start, blocksize, true);
4878 }
217f42eb 4879 btrfs_delalloc_release_extents(inode, blocksize);
39279cc3 4880 unlock_page(page);
09cbfeaf 4881 put_page(page);
39279cc3 4882out:
6d4572a9 4883 if (only_release_metadata)
217f42eb 4884 btrfs_check_nocow_unlock(inode);
364ecf36 4885 extent_changeset_free(data_reserved);
39279cc3
CM
4886 return ret;
4887}
4888
a4ba6cc0 4889static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
16e7549f
JB
4890 u64 offset, u64 len)
4891{
a4ba6cc0 4892 struct btrfs_fs_info *fs_info = root->fs_info;
16e7549f 4893 struct btrfs_trans_handle *trans;
5893dfb9 4894 struct btrfs_drop_extents_args drop_args = { 0 };
16e7549f
JB
4895 int ret;
4896
4897 /*
cceaa89f
FM
4898 * If NO_HOLES is enabled, we don't need to do anything.
4899 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
4900 * or btrfs_update_inode() will be called, which guarantee that the next
4901 * fsync will know this inode was changed and needs to be logged.
16e7549f 4902 */
cceaa89f 4903 if (btrfs_fs_incompat(fs_info, NO_HOLES))
16e7549f 4904 return 0;
16e7549f
JB
4905
4906 /*
4907 * 1 - for the one we're dropping
4908 * 1 - for the one we're adding
4909 * 1 - for updating the inode.
4910 */
4911 trans = btrfs_start_transaction(root, 3);
4912 if (IS_ERR(trans))
4913 return PTR_ERR(trans);
4914
5893dfb9
FM
4915 drop_args.start = offset;
4916 drop_args.end = offset + len;
4917 drop_args.drop_cache = true;
4918
a4ba6cc0 4919 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
16e7549f 4920 if (ret) {
66642832 4921 btrfs_abort_transaction(trans, ret);
3a45bb20 4922 btrfs_end_transaction(trans);
16e7549f
JB
4923 return ret;
4924 }
4925
a4ba6cc0 4926 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
f85b7379 4927 offset, 0, 0, len, 0, len, 0, 0, 0);
2766ff61 4928 if (ret) {
66642832 4929 btrfs_abort_transaction(trans, ret);
2766ff61 4930 } else {
a4ba6cc0
NB
4931 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4932 btrfs_update_inode(trans, root, inode);
2766ff61 4933 }
3a45bb20 4934 btrfs_end_transaction(trans);
16e7549f
JB
4935 return ret;
4936}
4937
695a0d0d
JB
4938/*
4939 * This function puts in dummy file extents for the area we're creating a hole
4940 * for. So if we are truncating this file to a larger size we need to insert
4941 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4942 * the range between oldsize and size
4943 */
b06359a3 4944int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
39279cc3 4945{
b06359a3
NB
4946 struct btrfs_root *root = inode->root;
4947 struct btrfs_fs_info *fs_info = root->fs_info;
4948 struct extent_io_tree *io_tree = &inode->io_tree;
a22285a6 4949 struct extent_map *em = NULL;
2ac55d41 4950 struct extent_state *cached_state = NULL;
b06359a3 4951 struct extent_map_tree *em_tree = &inode->extent_tree;
0b246afa
JM
4952 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4953 u64 block_end = ALIGN(size, fs_info->sectorsize);
9036c102
YZ
4954 u64 last_byte;
4955 u64 cur_offset;
4956 u64 hole_size;
9ed74f2d 4957 int err = 0;
39279cc3 4958
a71754fc 4959 /*
9703fefe
CR
4960 * If our size started in the middle of a block we need to zero out the
4961 * rest of the block before we expand the i_size, otherwise we could
a71754fc
JB
4962 * expose stale data.
4963 */
b06359a3 4964 err = btrfs_truncate_block(inode, oldsize, 0, 0);
a71754fc
JB
4965 if (err)
4966 return err;
4967
9036c102
YZ
4968 if (size <= hole_start)
4969 return 0;
4970
b06359a3
NB
4971 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4972 &cached_state);
9036c102
YZ
4973 cur_offset = hole_start;
4974 while (1) {
b06359a3 4975 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
39b07b5d 4976 block_end - cur_offset);
79787eaa
JM
4977 if (IS_ERR(em)) {
4978 err = PTR_ERR(em);
f2767956 4979 em = NULL;
79787eaa
JM
4980 break;
4981 }
9036c102 4982 last_byte = min(extent_map_end(em), block_end);
0b246afa 4983 last_byte = ALIGN(last_byte, fs_info->sectorsize);
9ddc959e
JB
4984 hole_size = last_byte - cur_offset;
4985
8082510e 4986 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
5dc562c5 4987 struct extent_map *hole_em;
9ed74f2d 4988
b06359a3
NB
4989 err = maybe_insert_hole(root, inode, cur_offset,
4990 hole_size);
16e7549f 4991 if (err)
3893e33b 4992 break;
9ddc959e 4993
b06359a3 4994 err = btrfs_inode_set_file_extent_range(inode,
9ddc959e
JB
4995 cur_offset, hole_size);
4996 if (err)
4997 break;
4998
b06359a3 4999 btrfs_drop_extent_cache(inode, cur_offset,
5dc562c5
JB
5000 cur_offset + hole_size - 1, 0);
5001 hole_em = alloc_extent_map();
5002 if (!hole_em) {
23e3337f 5003 btrfs_set_inode_full_sync(inode);
5dc562c5
JB
5004 goto next;
5005 }
5006 hole_em->start = cur_offset;
5007 hole_em->len = hole_size;
5008 hole_em->orig_start = cur_offset;
8082510e 5009
5dc562c5
JB
5010 hole_em->block_start = EXTENT_MAP_HOLE;
5011 hole_em->block_len = 0;
b4939680 5012 hole_em->orig_block_len = 0;
cc95bef6 5013 hole_em->ram_bytes = hole_size;
5dc562c5 5014 hole_em->compress_type = BTRFS_COMPRESS_NONE;
0b246afa 5015 hole_em->generation = fs_info->generation;
8082510e 5016
5dc562c5
JB
5017 while (1) {
5018 write_lock(&em_tree->lock);
09a2a8f9 5019 err = add_extent_mapping(em_tree, hole_em, 1);
5dc562c5
JB
5020 write_unlock(&em_tree->lock);
5021 if (err != -EEXIST)
5022 break;
b06359a3 5023 btrfs_drop_extent_cache(inode, cur_offset,
5dc562c5
JB
5024 cur_offset +
5025 hole_size - 1, 0);
5026 }
5027 free_extent_map(hole_em);
9ddc959e 5028 } else {
b06359a3 5029 err = btrfs_inode_set_file_extent_range(inode,
9ddc959e
JB
5030 cur_offset, hole_size);
5031 if (err)
5032 break;
9036c102 5033 }
16e7549f 5034next:
9036c102 5035 free_extent_map(em);
a22285a6 5036 em = NULL;
9036c102 5037 cur_offset = last_byte;
8082510e 5038 if (cur_offset >= block_end)
9036c102
YZ
5039 break;
5040 }
a22285a6 5041 free_extent_map(em);
e43bbe5e 5042 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
9036c102
YZ
5043 return err;
5044}
39279cc3 5045
3972f260 5046static int btrfs_setsize(struct inode *inode, struct iattr *attr)
8082510e 5047{
f4a2f4c5
MX
5048 struct btrfs_root *root = BTRFS_I(inode)->root;
5049 struct btrfs_trans_handle *trans;
a41ad394 5050 loff_t oldsize = i_size_read(inode);
3972f260
ES
5051 loff_t newsize = attr->ia_size;
5052 int mask = attr->ia_valid;
8082510e
YZ
5053 int ret;
5054
3972f260
ES
5055 /*
5056 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5057 * special case where we need to update the times despite not having
5058 * these flags set. For all other operations the VFS set these flags
5059 * explicitly if it wants a timestamp update.
5060 */
dff6efc3
CH
5061 if (newsize != oldsize) {
5062 inode_inc_iversion(inode);
5063 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5064 inode->i_ctime = inode->i_mtime =
c2050a45 5065 current_time(inode);
dff6efc3 5066 }
3972f260 5067
a41ad394 5068 if (newsize > oldsize) {
9ea24bbe 5069 /*
ea14b57f 5070 * Don't do an expanding truncate while snapshotting is ongoing.
9ea24bbe
FM
5071 * This is to ensure the snapshot captures a fully consistent
5072 * state of this file - if the snapshot captures this expanding
5073 * truncation, it must capture all writes that happened before
5074 * this truncation.
5075 */
dcc3eb96 5076 btrfs_drew_write_lock(&root->snapshot_lock);
b06359a3 5077 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
9ea24bbe 5078 if (ret) {
dcc3eb96 5079 btrfs_drew_write_unlock(&root->snapshot_lock);
8082510e 5080 return ret;
9ea24bbe 5081 }
8082510e 5082
f4a2f4c5 5083 trans = btrfs_start_transaction(root, 1);
9ea24bbe 5084 if (IS_ERR(trans)) {
dcc3eb96 5085 btrfs_drew_write_unlock(&root->snapshot_lock);
f4a2f4c5 5086 return PTR_ERR(trans);
9ea24bbe 5087 }
f4a2f4c5
MX
5088
5089 i_size_write(inode, newsize);
76aea537 5090 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
27772b68 5091 pagecache_isize_extended(inode, oldsize, newsize);
9a56fcd1 5092 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
dcc3eb96 5093 btrfs_drew_write_unlock(&root->snapshot_lock);
3a45bb20 5094 btrfs_end_transaction(trans);
a41ad394 5095 } else {
24c0a722
NA
5096 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5097
5098 if (btrfs_is_zoned(fs_info)) {
5099 ret = btrfs_wait_ordered_range(inode,
5100 ALIGN(newsize, fs_info->sectorsize),
5101 (u64)-1);
5102 if (ret)
5103 return ret;
5104 }
8082510e 5105
a41ad394
JB
5106 /*
5107 * We're truncating a file that used to have good data down to
1fd4033d
NB
5108 * zero. Make sure any new writes to the file get on disk
5109 * on close.
a41ad394
JB
5110 */
5111 if (newsize == 0)
1fd4033d 5112 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
72ac3c0d 5113 &BTRFS_I(inode)->runtime_flags);
8082510e 5114
a41ad394 5115 truncate_setsize(inode, newsize);
2e60a51e 5116
2e60a51e 5117 inode_dio_wait(inode);
2e60a51e 5118
213e8c55 5119 ret = btrfs_truncate(inode, newsize == oldsize);
7f4f6e0a
JB
5120 if (ret && inode->i_nlink) {
5121 int err;
5122
5123 /*
f7e9e8fc
OS
5124 * Truncate failed, so fix up the in-memory size. We
5125 * adjusted disk_i_size down as we removed extents, so
5126 * wait for disk_i_size to be stable and then update the
5127 * in-memory size to match.
7f4f6e0a 5128 */
f7e9e8fc 5129 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
7f4f6e0a 5130 if (err)
f7e9e8fc
OS
5131 return err;
5132 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
7f4f6e0a 5133 }
8082510e
YZ
5134 }
5135
a41ad394 5136 return ret;
8082510e
YZ
5137}
5138
549c7297
CB
5139static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5140 struct iattr *attr)
9036c102 5141{
2b0143b5 5142 struct inode *inode = d_inode(dentry);
b83cc969 5143 struct btrfs_root *root = BTRFS_I(inode)->root;
9036c102 5144 int err;
39279cc3 5145
b83cc969
LZ
5146 if (btrfs_root_readonly(root))
5147 return -EROFS;
5148
d4d09464 5149 err = setattr_prepare(mnt_userns, dentry, attr);
9036c102
YZ
5150 if (err)
5151 return err;
2bf5a725 5152
5a3f23d5 5153 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3972f260 5154 err = btrfs_setsize(inode, attr);
8082510e
YZ
5155 if (err)
5156 return err;
39279cc3 5157 }
9036c102 5158
1025774c 5159 if (attr->ia_valid) {
d4d09464 5160 setattr_copy(mnt_userns, inode, attr);
0c4d2d95 5161 inode_inc_iversion(inode);
22c44fe6 5162 err = btrfs_dirty_inode(inode);
1025774c 5163
22c44fe6 5164 if (!err && attr->ia_valid & ATTR_MODE)
d4d09464 5165 err = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
1025774c 5166 }
33268eaf 5167
39279cc3
CM
5168 return err;
5169}
61295eb8 5170
131e404a 5171/*
895586eb
MWO
5172 * While truncating the inode pages during eviction, we get the VFS
5173 * calling btrfs_invalidate_folio() against each folio of the inode. This
5174 * is slow because the calls to btrfs_invalidate_folio() result in a
5175 * huge amount of calls to lock_extent_bits() and clear_extent_bit(),
5176 * which keep merging and splitting extent_state structures over and over,
5177 * wasting lots of time.
131e404a 5178 *
895586eb
MWO
5179 * Therefore if the inode is being evicted, let btrfs_invalidate_folio()
5180 * skip all those expensive operations on a per folio basis and do only
5181 * the ordered io finishing, while we release here the extent_map and
5182 * extent_state structures, without the excessive merging and splitting.
131e404a
FDBM
5183 */
5184static void evict_inode_truncate_pages(struct inode *inode)
5185{
5186 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5187 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5188 struct rb_node *node;
5189
5190 ASSERT(inode->i_state & I_FREEING);
91b0abe3 5191 truncate_inode_pages_final(&inode->i_data);
131e404a
FDBM
5192
5193 write_lock(&map_tree->lock);
07e1ce09 5194 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
131e404a
FDBM
5195 struct extent_map *em;
5196
07e1ce09 5197 node = rb_first_cached(&map_tree->map);
131e404a 5198 em = rb_entry(node, struct extent_map, rb_node);
180589ef
WS
5199 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5200 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
131e404a
FDBM
5201 remove_extent_mapping(map_tree, em);
5202 free_extent_map(em);
7064dd5c
FM
5203 if (need_resched()) {
5204 write_unlock(&map_tree->lock);
5205 cond_resched();
5206 write_lock(&map_tree->lock);
5207 }
131e404a
FDBM
5208 }
5209 write_unlock(&map_tree->lock);
5210
6ca07097
FM
5211 /*
5212 * Keep looping until we have no more ranges in the io tree.
ba206a02
MWO
5213 * We can have ongoing bios started by readahead that have
5214 * their endio callback (extent_io.c:end_bio_extent_readpage)
9c6429d9
FM
5215 * still in progress (unlocked the pages in the bio but did not yet
5216 * unlocked the ranges in the io tree). Therefore this means some
6ca07097
FM
5217 * ranges can still be locked and eviction started because before
5218 * submitting those bios, which are executed by a separate task (work
5219 * queue kthread), inode references (inode->i_count) were not taken
5220 * (which would be dropped in the end io callback of each bio).
5221 * Therefore here we effectively end up waiting for those bios and
5222 * anyone else holding locked ranges without having bumped the inode's
5223 * reference count - if we don't do it, when they access the inode's
5224 * io_tree to unlock a range it may be too late, leading to an
5225 * use-after-free issue.
5226 */
131e404a
FDBM
5227 spin_lock(&io_tree->lock);
5228 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5229 struct extent_state *state;
5230 struct extent_state *cached_state = NULL;
6ca07097
FM
5231 u64 start;
5232 u64 end;
421f0922 5233 unsigned state_flags;
131e404a
FDBM
5234
5235 node = rb_first(&io_tree->state);
5236 state = rb_entry(node, struct extent_state, rb_node);
6ca07097
FM
5237 start = state->start;
5238 end = state->end;
421f0922 5239 state_flags = state->state;
131e404a
FDBM
5240 spin_unlock(&io_tree->lock);
5241
ff13db41 5242 lock_extent_bits(io_tree, start, end, &cached_state);
b9d0b389
QW
5243
5244 /*
5245 * If still has DELALLOC flag, the extent didn't reach disk,
5246 * and its reserved space won't be freed by delayed_ref.
5247 * So we need to free its reserved space here.
895586eb 5248 * (Refer to comment in btrfs_invalidate_folio, case 2)
b9d0b389
QW
5249 *
5250 * Note, end is the bytenr of last byte, so we need + 1 here.
5251 */
421f0922 5252 if (state_flags & EXTENT_DELALLOC)
8b8a979f
NB
5253 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5254 end - start + 1);
b9d0b389 5255
6ca07097 5256 clear_extent_bit(io_tree, start, end,
e182163d
OS
5257 EXTENT_LOCKED | EXTENT_DELALLOC |
5258 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5259 &cached_state);
131e404a 5260
7064dd5c 5261 cond_resched();
131e404a
FDBM
5262 spin_lock(&io_tree->lock);
5263 }
5264 spin_unlock(&io_tree->lock);
5265}
5266
4b9d7b59 5267static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
ad80cf50 5268 struct btrfs_block_rsv *rsv)
4b9d7b59
OS
5269{
5270 struct btrfs_fs_info *fs_info = root->fs_info;
d3984c90 5271 struct btrfs_trans_handle *trans;
2bd36e7b 5272 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
d3984c90 5273 int ret;
4b9d7b59 5274
d3984c90
JB
5275 /*
5276 * Eviction should be taking place at some place safe because of our
5277 * delayed iputs. However the normal flushing code will run delayed
5278 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5279 *
5280 * We reserve the delayed_refs_extra here again because we can't use
5281 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5282 * above. We reserve our extra bit here because we generate a ton of
5283 * delayed refs activity by truncating.
5284 *
ee6adbfd
JB
5285 * BTRFS_RESERVE_FLUSH_EVICT will steal from the global_rsv if it can,
5286 * if we fail to make this reservation we can re-try without the
5287 * delayed_refs_extra so we can make some forward progress.
d3984c90 5288 */
9270501c 5289 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra,
d3984c90
JB
5290 BTRFS_RESERVE_FLUSH_EVICT);
5291 if (ret) {
9270501c 5292 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size,
ee6adbfd
JB
5293 BTRFS_RESERVE_FLUSH_EVICT);
5294 if (ret) {
d3984c90
JB
5295 btrfs_warn(fs_info,
5296 "could not allocate space for delete; will truncate on mount");
5297 return ERR_PTR(-ENOSPC);
5298 }
5299 delayed_refs_extra = 0;
5300 }
4b9d7b59 5301
d3984c90
JB
5302 trans = btrfs_join_transaction(root);
5303 if (IS_ERR(trans))
5304 return trans;
5305
5306 if (delayed_refs_extra) {
5307 trans->block_rsv = &fs_info->trans_block_rsv;
5308 trans->bytes_reserved = delayed_refs_extra;
5309 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5310 delayed_refs_extra, 1);
4b9d7b59 5311 }
d3984c90 5312 return trans;
4b9d7b59
OS
5313}
5314
bd555975 5315void btrfs_evict_inode(struct inode *inode)
39279cc3 5316{
0b246afa 5317 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
39279cc3
CM
5318 struct btrfs_trans_handle *trans;
5319 struct btrfs_root *root = BTRFS_I(inode)->root;
4b9d7b59 5320 struct btrfs_block_rsv *rsv;
39279cc3
CM
5321 int ret;
5322
1abe9b8a 5323 trace_btrfs_inode_evict(inode);
5324
3d48d981 5325 if (!root) {
14605409 5326 fsverity_cleanup_inode(inode);
e8f1bc14 5327 clear_inode(inode);
3d48d981
NB
5328 return;
5329 }
5330
131e404a
FDBM
5331 evict_inode_truncate_pages(inode);
5332
69e9c6c6
SB
5333 if (inode->i_nlink &&
5334 ((btrfs_root_refs(&root->root_item) != 0 &&
5335 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
70ddc553 5336 btrfs_is_free_space_inode(BTRFS_I(inode))))
bd555975
AV
5337 goto no_delete;
5338
27919067 5339 if (is_bad_inode(inode))
39279cc3 5340 goto no_delete;
5f39d397 5341
7ab7956e 5342 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
f612496b 5343
7b40b695 5344 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
c71bf099 5345 goto no_delete;
c71bf099 5346
76dda93c 5347 if (inode->i_nlink > 0) {
69e9c6c6
SB
5348 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5349 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
76dda93c
YZ
5350 goto no_delete;
5351 }
5352
2adc75d6
JB
5353 /*
5354 * This makes sure the inode item in tree is uptodate and the space for
5355 * the inode update is released.
5356 */
aa79021f 5357 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
27919067 5358 if (ret)
0e8c36a9 5359 goto no_delete;
0e8c36a9 5360
2adc75d6
JB
5361 /*
5362 * This drops any pending insert or delete operations we have for this
5363 * inode. We could have a delayed dir index deletion queued up, but
5364 * we're removing the inode completely so that'll be taken care of in
5365 * the truncate.
5366 */
5367 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
5368
2ff7e61e 5369 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
27919067 5370 if (!rsv)
4289a667 5371 goto no_delete;
2bd36e7b 5372 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
ca7e70f5 5373 rsv->failfast = 1;
4289a667 5374
6ef06d27 5375 btrfs_i_size_write(BTRFS_I(inode), 0);
5f39d397 5376
8082510e 5377 while (1) {
d9ac19c3 5378 struct btrfs_truncate_control control = {
71d18b53 5379 .inode = BTRFS_I(inode),
487e81d2 5380 .ino = btrfs_ino(BTRFS_I(inode)),
d9ac19c3
JB
5381 .new_size = 0,
5382 .min_type = 0,
5383 };
5384
ad80cf50 5385 trans = evict_refill_and_join(root, rsv);
27919067
OS
5386 if (IS_ERR(trans))
5387 goto free_rsv;
7b128766 5388
4289a667
JB
5389 trans->block_rsv = rsv;
5390
71d18b53 5391 ret = btrfs_truncate_inode_items(trans, root, &control);
27919067
OS
5392 trans->block_rsv = &fs_info->trans_block_rsv;
5393 btrfs_end_transaction(trans);
5394 btrfs_btree_balance_dirty(fs_info);
5395 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5396 goto free_rsv;
5397 else if (!ret)
8082510e 5398 break;
8082510e 5399 }
5f39d397 5400
4ef31a45 5401 /*
27919067
OS
5402 * Errors here aren't a big deal, it just means we leave orphan items in
5403 * the tree. They will be cleaned up on the next mount. If the inode
5404 * number gets reused, cleanup deletes the orphan item without doing
5405 * anything, and unlink reuses the existing orphan item.
5406 *
5407 * If it turns out that we are dropping too many of these, we might want
5408 * to add a mechanism for retrying these after a commit.
4ef31a45 5409 */
ad80cf50 5410 trans = evict_refill_and_join(root, rsv);
27919067
OS
5411 if (!IS_ERR(trans)) {
5412 trans->block_rsv = rsv;
5413 btrfs_orphan_del(trans, BTRFS_I(inode));
5414 trans->block_rsv = &fs_info->trans_block_rsv;
5415 btrfs_end_transaction(trans);
5416 }
54aa1f4d 5417
27919067
OS
5418free_rsv:
5419 btrfs_free_block_rsv(fs_info, rsv);
39279cc3 5420no_delete:
27919067
OS
5421 /*
5422 * If we didn't successfully delete, the orphan item will still be in
5423 * the tree and we'll retry on the next mount. Again, we might also want
5424 * to retry these periodically in the future.
5425 */
f48d1cf5 5426 btrfs_remove_delayed_node(BTRFS_I(inode));
14605409 5427 fsverity_cleanup_inode(inode);
dbd5768f 5428 clear_inode(inode);
39279cc3
CM
5429}
5430
5431/*
6bf9e4bd
QW
5432 * Return the key found in the dir entry in the location pointer, fill @type
5433 * with BTRFS_FT_*, and return 0.
5434 *
005d6712
SY
5435 * If no dir entries were found, returns -ENOENT.
5436 * If found a corrupted location in dir entry, returns -EUCLEAN.
39279cc3
CM
5437 */
5438static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
6bf9e4bd 5439 struct btrfs_key *location, u8 *type)
39279cc3
CM
5440{
5441 const char *name = dentry->d_name.name;
5442 int namelen = dentry->d_name.len;
5443 struct btrfs_dir_item *di;
5444 struct btrfs_path *path;
5445 struct btrfs_root *root = BTRFS_I(dir)->root;
0d9f7f3e 5446 int ret = 0;
39279cc3
CM
5447
5448 path = btrfs_alloc_path();
d8926bb3
MF
5449 if (!path)
5450 return -ENOMEM;
3954401f 5451
f85b7379
DS
5452 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5453 name, namelen, 0);
3cf5068f
LB
5454 if (IS_ERR_OR_NULL(di)) {
5455 ret = di ? PTR_ERR(di) : -ENOENT;
005d6712
SY
5456 goto out;
5457 }
d397712b 5458
5f39d397 5459 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
56a0e706
LB
5460 if (location->type != BTRFS_INODE_ITEM_KEY &&
5461 location->type != BTRFS_ROOT_ITEM_KEY) {
005d6712 5462 ret = -EUCLEAN;
56a0e706
LB
5463 btrfs_warn(root->fs_info,
5464"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5465 __func__, name, btrfs_ino(BTRFS_I(dir)),
5466 location->objectid, location->type, location->offset);
56a0e706 5467 }
6bf9e4bd
QW
5468 if (!ret)
5469 *type = btrfs_dir_type(path->nodes[0], di);
39279cc3 5470out:
39279cc3
CM
5471 btrfs_free_path(path);
5472 return ret;
5473}
5474
5475/*
5476 * when we hit a tree root in a directory, the btrfs part of the inode
5477 * needs to be changed to reflect the root directory of the tree root. This
5478 * is kind of like crossing a mount point.
5479 */
2ff7e61e 5480static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
4df27c4d
YZ
5481 struct inode *dir,
5482 struct dentry *dentry,
5483 struct btrfs_key *location,
5484 struct btrfs_root **sub_root)
39279cc3 5485{
4df27c4d
YZ
5486 struct btrfs_path *path;
5487 struct btrfs_root *new_root;
5488 struct btrfs_root_ref *ref;
5489 struct extent_buffer *leaf;
1d4c08e0 5490 struct btrfs_key key;
4df27c4d
YZ
5491 int ret;
5492 int err = 0;
39279cc3 5493
4df27c4d
YZ
5494 path = btrfs_alloc_path();
5495 if (!path) {
5496 err = -ENOMEM;
5497 goto out;
5498 }
39279cc3 5499
4df27c4d 5500 err = -ENOENT;
1d4c08e0
DS
5501 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5502 key.type = BTRFS_ROOT_REF_KEY;
5503 key.offset = location->objectid;
5504
0b246afa 5505 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4df27c4d
YZ
5506 if (ret) {
5507 if (ret < 0)
5508 err = ret;
5509 goto out;
5510 }
39279cc3 5511
4df27c4d
YZ
5512 leaf = path->nodes[0];
5513 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
4a0cc7ca 5514 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
4df27c4d
YZ
5515 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5516 goto out;
39279cc3 5517
4df27c4d
YZ
5518 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5519 (unsigned long)(ref + 1),
5520 dentry->d_name.len);
5521 if (ret)
5522 goto out;
5523
b3b4aa74 5524 btrfs_release_path(path);
4df27c4d 5525
56e9357a 5526 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
4df27c4d
YZ
5527 if (IS_ERR(new_root)) {
5528 err = PTR_ERR(new_root);
5529 goto out;
5530 }
5531
4df27c4d
YZ
5532 *sub_root = new_root;
5533 location->objectid = btrfs_root_dirid(&new_root->root_item);
5534 location->type = BTRFS_INODE_ITEM_KEY;
5535 location->offset = 0;
5536 err = 0;
5537out:
5538 btrfs_free_path(path);
5539 return err;
39279cc3
CM
5540}
5541
5d4f98a2
YZ
5542static void inode_tree_add(struct inode *inode)
5543{
5544 struct btrfs_root *root = BTRFS_I(inode)->root;
5545 struct btrfs_inode *entry;
03e860bd
NP
5546 struct rb_node **p;
5547 struct rb_node *parent;
cef21937 5548 struct rb_node *new = &BTRFS_I(inode)->rb_node;
4a0cc7ca 5549 u64 ino = btrfs_ino(BTRFS_I(inode));
5d4f98a2 5550
1d3382cb 5551 if (inode_unhashed(inode))
76dda93c 5552 return;
e1409cef 5553 parent = NULL;
5d4f98a2 5554 spin_lock(&root->inode_lock);
e1409cef 5555 p = &root->inode_tree.rb_node;
5d4f98a2
YZ
5556 while (*p) {
5557 parent = *p;
5558 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5559
37508515 5560 if (ino < btrfs_ino(entry))
03e860bd 5561 p = &parent->rb_left;
37508515 5562 else if (ino > btrfs_ino(entry))
03e860bd 5563 p = &parent->rb_right;
5d4f98a2
YZ
5564 else {
5565 WARN_ON(!(entry->vfs_inode.i_state &
a4ffdde6 5566 (I_WILL_FREE | I_FREEING)));
cef21937 5567 rb_replace_node(parent, new, &root->inode_tree);
03e860bd
NP
5568 RB_CLEAR_NODE(parent);
5569 spin_unlock(&root->inode_lock);
cef21937 5570 return;
5d4f98a2
YZ
5571 }
5572 }
cef21937
FDBM
5573 rb_link_node(new, parent, p);
5574 rb_insert_color(new, &root->inode_tree);
5d4f98a2
YZ
5575 spin_unlock(&root->inode_lock);
5576}
5577
b79b7249 5578static void inode_tree_del(struct btrfs_inode *inode)
5d4f98a2 5579{
b79b7249 5580 struct btrfs_root *root = inode->root;
76dda93c 5581 int empty = 0;
5d4f98a2 5582
03e860bd 5583 spin_lock(&root->inode_lock);
b79b7249
NB
5584 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5585 rb_erase(&inode->rb_node, &root->inode_tree);
5586 RB_CLEAR_NODE(&inode->rb_node);
76dda93c 5587 empty = RB_EMPTY_ROOT(&root->inode_tree);
5d4f98a2 5588 }
03e860bd 5589 spin_unlock(&root->inode_lock);
76dda93c 5590
69e9c6c6 5591 if (empty && btrfs_root_refs(&root->root_item) == 0) {
76dda93c
YZ
5592 spin_lock(&root->inode_lock);
5593 empty = RB_EMPTY_ROOT(&root->inode_tree);
5594 spin_unlock(&root->inode_lock);
5595 if (empty)
5596 btrfs_add_dead_root(root);
5597 }
5598}
5599
5d4f98a2 5600
e02119d5
CM
5601static int btrfs_init_locked_inode(struct inode *inode, void *p)
5602{
5603 struct btrfs_iget_args *args = p;
0202e83f
DS
5604
5605 inode->i_ino = args->ino;
5606 BTRFS_I(inode)->location.objectid = args->ino;
5607 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5608 BTRFS_I(inode)->location.offset = 0;
5c8fd99f
JB
5609 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5610 BUG_ON(args->root && !BTRFS_I(inode)->root);
39279cc3
CM
5611 return 0;
5612}
5613
5614static int btrfs_find_actor(struct inode *inode, void *opaque)
5615{
5616 struct btrfs_iget_args *args = opaque;
0202e83f
DS
5617
5618 return args->ino == BTRFS_I(inode)->location.objectid &&
d397712b 5619 args->root == BTRFS_I(inode)->root;
39279cc3
CM
5620}
5621
0202e83f 5622static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5d4f98a2 5623 struct btrfs_root *root)
39279cc3
CM
5624{
5625 struct inode *inode;
5626 struct btrfs_iget_args args;
0202e83f 5627 unsigned long hashval = btrfs_inode_hash(ino, root);
778ba82b 5628
0202e83f 5629 args.ino = ino;
39279cc3
CM
5630 args.root = root;
5631
778ba82b 5632 inode = iget5_locked(s, hashval, btrfs_find_actor,
39279cc3
CM
5633 btrfs_init_locked_inode,
5634 (void *)&args);
5635 return inode;
5636}
5637
4c66e0d4 5638/*
0202e83f 5639 * Get an inode object given its inode number and corresponding root.
4c66e0d4
DS
5640 * Path can be preallocated to prevent recursing back to iget through
5641 * allocator. NULL is also valid but may require an additional allocation
5642 * later.
1a54ef8c 5643 */
0202e83f 5644struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
4c66e0d4 5645 struct btrfs_root *root, struct btrfs_path *path)
1a54ef8c
BR
5646{
5647 struct inode *inode;
5648
0202e83f 5649 inode = btrfs_iget_locked(s, ino, root);
1a54ef8c 5650 if (!inode)
5d4f98a2 5651 return ERR_PTR(-ENOMEM);
1a54ef8c
BR
5652
5653 if (inode->i_state & I_NEW) {
67710892
FM
5654 int ret;
5655
4222ea71 5656 ret = btrfs_read_locked_inode(inode, path);
9bc2ceff 5657 if (!ret) {
1748f843
MF
5658 inode_tree_add(inode);
5659 unlock_new_inode(inode);
1748f843 5660 } else {
f5b3a417
AV
5661 iget_failed(inode);
5662 /*
5663 * ret > 0 can come from btrfs_search_slot called by
5664 * btrfs_read_locked_inode, this means the inode item
5665 * was not found.
5666 */
5667 if (ret > 0)
5668 ret = -ENOENT;
5669 inode = ERR_PTR(ret);
1748f843
MF
5670 }
5671 }
5672
1a54ef8c
BR
5673 return inode;
5674}
5675
0202e83f 5676struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
4222ea71 5677{
0202e83f 5678 return btrfs_iget_path(s, ino, root, NULL);
4222ea71
FM
5679}
5680
4df27c4d
YZ
5681static struct inode *new_simple_dir(struct super_block *s,
5682 struct btrfs_key *key,
5683 struct btrfs_root *root)
5684{
5685 struct inode *inode = new_inode(s);
5686
5687 if (!inode)
5688 return ERR_PTR(-ENOMEM);
5689
5c8fd99f 5690 BTRFS_I(inode)->root = btrfs_grab_root(root);
4df27c4d 5691 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
72ac3c0d 5692 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
4df27c4d
YZ
5693
5694 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
6bb6b514
OS
5695 /*
5696 * We only need lookup, the rest is read-only and there's no inode
5697 * associated with the dentry
5698 */
5699 inode->i_op = &simple_dir_inode_operations;
1fdf4194 5700 inode->i_opflags &= ~IOP_XATTR;
4df27c4d
YZ
5701 inode->i_fop = &simple_dir_operations;
5702 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
c2050a45 5703 inode->i_mtime = current_time(inode);
9cc97d64 5704 inode->i_atime = inode->i_mtime;
5705 inode->i_ctime = inode->i_mtime;
d3c6be6f 5706 BTRFS_I(inode)->i_otime = inode->i_mtime;
4df27c4d
YZ
5707
5708 return inode;
5709}
5710
a55e65b8
DS
5711static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN);
5712static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE);
5713static_assert(BTRFS_FT_DIR == FT_DIR);
5714static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV);
5715static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV);
5716static_assert(BTRFS_FT_FIFO == FT_FIFO);
5717static_assert(BTRFS_FT_SOCK == FT_SOCK);
5718static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK);
5719
6bf9e4bd
QW
5720static inline u8 btrfs_inode_type(struct inode *inode)
5721{
6bf9e4bd
QW
5722 return fs_umode_to_ftype(inode->i_mode);
5723}
5724
3de4586c 5725struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
39279cc3 5726{
0b246afa 5727 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
d397712b 5728 struct inode *inode;
4df27c4d 5729 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3
CM
5730 struct btrfs_root *sub_root = root;
5731 struct btrfs_key location;
6bf9e4bd 5732 u8 di_type = 0;
b4aff1f8 5733 int ret = 0;
39279cc3
CM
5734
5735 if (dentry->d_name.len > BTRFS_NAME_LEN)
5736 return ERR_PTR(-ENAMETOOLONG);
5f39d397 5737
6bf9e4bd 5738 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
39279cc3
CM
5739 if (ret < 0)
5740 return ERR_PTR(ret);
5f39d397 5741
4df27c4d 5742 if (location.type == BTRFS_INODE_ITEM_KEY) {
0202e83f 5743 inode = btrfs_iget(dir->i_sb, location.objectid, root);
6bf9e4bd
QW
5744 if (IS_ERR(inode))
5745 return inode;
5746
5747 /* Do extra check against inode mode with di_type */
5748 if (btrfs_inode_type(inode) != di_type) {
5749 btrfs_crit(fs_info,
5750"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5751 inode->i_mode, btrfs_inode_type(inode),
5752 di_type);
5753 iput(inode);
5754 return ERR_PTR(-EUCLEAN);
5755 }
4df27c4d
YZ
5756 return inode;
5757 }
5758
2ff7e61e 5759 ret = fixup_tree_root_location(fs_info, dir, dentry,
4df27c4d
YZ
5760 &location, &sub_root);
5761 if (ret < 0) {
5762 if (ret != -ENOENT)
5763 inode = ERR_PTR(ret);
5764 else
5765 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5766 } else {
0202e83f 5767 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
39279cc3 5768 }
8727002f 5769 if (root != sub_root)
00246528 5770 btrfs_put_root(sub_root);
76dda93c 5771
34d19bad 5772 if (!IS_ERR(inode) && root != sub_root) {
0b246afa 5773 down_read(&fs_info->cleanup_work_sem);
bc98a42c 5774 if (!sb_rdonly(inode->i_sb))
66b4ffd1 5775 ret = btrfs_orphan_cleanup(sub_root);
0b246afa 5776 up_read(&fs_info->cleanup_work_sem);
01cd3367
JB
5777 if (ret) {
5778 iput(inode);
66b4ffd1 5779 inode = ERR_PTR(ret);
01cd3367 5780 }
c71bf099
YZ
5781 }
5782
3de4586c
CM
5783 return inode;
5784}
5785
fe15ce44 5786static int btrfs_dentry_delete(const struct dentry *dentry)
76dda93c
YZ
5787{
5788 struct btrfs_root *root;
2b0143b5 5789 struct inode *inode = d_inode(dentry);
76dda93c 5790
848cce0d 5791 if (!inode && !IS_ROOT(dentry))
2b0143b5 5792 inode = d_inode(dentry->d_parent);
76dda93c 5793
848cce0d
LZ
5794 if (inode) {
5795 root = BTRFS_I(inode)->root;
efefb143
YZ
5796 if (btrfs_root_refs(&root->root_item) == 0)
5797 return 1;
848cce0d 5798
4a0cc7ca 5799 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
848cce0d 5800 return 1;
efefb143 5801 }
76dda93c
YZ
5802 return 0;
5803}
5804
3de4586c 5805static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 5806 unsigned int flags)
3de4586c 5807{
3837d208 5808 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
5662344b 5809
3837d208
AV
5810 if (inode == ERR_PTR(-ENOENT))
5811 inode = NULL;
41d28bca 5812 return d_splice_alias(inode, dentry);
39279cc3
CM
5813}
5814
23b5ec74
JB
5815/*
5816 * All this infrastructure exists because dir_emit can fault, and we are holding
5817 * the tree lock when doing readdir. For now just allocate a buffer and copy
5818 * our information into that, and then dir_emit from the buffer. This is
5819 * similar to what NFS does, only we don't keep the buffer around in pagecache
5820 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5821 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5822 * tree lock.
5823 */
5824static int btrfs_opendir(struct inode *inode, struct file *file)
5825{
5826 struct btrfs_file_private *private;
5827
5828 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5829 if (!private)
5830 return -ENOMEM;
5831 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5832 if (!private->filldir_buf) {
5833 kfree(private);
5834 return -ENOMEM;
5835 }
5836 file->private_data = private;
5837 return 0;
5838}
5839
5840struct dir_entry {
5841 u64 ino;
5842 u64 offset;
5843 unsigned type;
5844 int name_len;
5845};
5846
5847static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5848{
5849 while (entries--) {
5850 struct dir_entry *entry = addr;
5851 char *name = (char *)(entry + 1);
5852
92d32170
DS
5853 ctx->pos = get_unaligned(&entry->offset);
5854 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5855 get_unaligned(&entry->ino),
5856 get_unaligned(&entry->type)))
23b5ec74 5857 return 1;
92d32170
DS
5858 addr += sizeof(struct dir_entry) +
5859 get_unaligned(&entry->name_len);
23b5ec74
JB
5860 ctx->pos++;
5861 }
5862 return 0;
5863}
5864
9cdda8d3 5865static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
39279cc3 5866{
9cdda8d3 5867 struct inode *inode = file_inode(file);
39279cc3 5868 struct btrfs_root *root = BTRFS_I(inode)->root;
23b5ec74 5869 struct btrfs_file_private *private = file->private_data;
39279cc3
CM
5870 struct btrfs_dir_item *di;
5871 struct btrfs_key key;
5f39d397 5872 struct btrfs_key found_key;
39279cc3 5873 struct btrfs_path *path;
23b5ec74 5874 void *addr;
16cdcec7
MX
5875 struct list_head ins_list;
5876 struct list_head del_list;
39279cc3 5877 int ret;
5f39d397
CM
5878 char *name_ptr;
5879 int name_len;
23b5ec74
JB
5880 int entries = 0;
5881 int total_len = 0;
02dbfc99 5882 bool put = false;
c2951f32 5883 struct btrfs_key location;
5f39d397 5884
9cdda8d3
AV
5885 if (!dir_emit_dots(file, ctx))
5886 return 0;
5887
49593bfa 5888 path = btrfs_alloc_path();
16cdcec7
MX
5889 if (!path)
5890 return -ENOMEM;
ff5714cc 5891
23b5ec74 5892 addr = private->filldir_buf;
e4058b54 5893 path->reada = READA_FORWARD;
49593bfa 5894
c2951f32
JM
5895 INIT_LIST_HEAD(&ins_list);
5896 INIT_LIST_HEAD(&del_list);
5897 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
16cdcec7 5898
23b5ec74 5899again:
c2951f32 5900 key.type = BTRFS_DIR_INDEX_KEY;
9cdda8d3 5901 key.offset = ctx->pos;
4a0cc7ca 5902 key.objectid = btrfs_ino(BTRFS_I(inode));
5f39d397 5903
a8ce68fd 5904 btrfs_for_each_slot(root, &key, &found_key, path, ret) {
23b5ec74 5905 struct dir_entry *entry;
a8ce68fd 5906 struct extent_buffer *leaf = path->nodes[0];
5f39d397
CM
5907
5908 if (found_key.objectid != key.objectid)
39279cc3 5909 break;
c2951f32 5910 if (found_key.type != BTRFS_DIR_INDEX_KEY)
39279cc3 5911 break;
9cdda8d3 5912 if (found_key.offset < ctx->pos)
a8ce68fd 5913 continue;
c2951f32 5914 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
a8ce68fd
GN
5915 continue;
5916 di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
c2951f32 5917 name_len = btrfs_dir_name_len(leaf, di);
23b5ec74
JB
5918 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5919 PAGE_SIZE) {
5920 btrfs_release_path(path);
5921 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5922 if (ret)
5923 goto nopos;
5924 addr = private->filldir_buf;
5925 entries = 0;
5926 total_len = 0;
5927 goto again;
c2951f32 5928 }
23b5ec74
JB
5929
5930 entry = addr;
92d32170 5931 put_unaligned(name_len, &entry->name_len);
23b5ec74 5932 name_ptr = (char *)(entry + 1);
c2951f32
JM
5933 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5934 name_len);
7d157c3d 5935 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
92d32170 5936 &entry->type);
c2951f32 5937 btrfs_dir_item_key_to_cpu(leaf, di, &location);
92d32170
DS
5938 put_unaligned(location.objectid, &entry->ino);
5939 put_unaligned(found_key.offset, &entry->offset);
23b5ec74
JB
5940 entries++;
5941 addr += sizeof(struct dir_entry) + name_len;
5942 total_len += sizeof(struct dir_entry) + name_len;
39279cc3 5943 }
a8ce68fd
GN
5944 /* Catch error encountered during iteration */
5945 if (ret < 0)
5946 goto err;
5947
23b5ec74
JB
5948 btrfs_release_path(path);
5949
5950 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5951 if (ret)
5952 goto nopos;
49593bfa 5953
d2fbb2b5 5954 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
c2951f32 5955 if (ret)
bc4ef759
DS
5956 goto nopos;
5957
db62efbb
ZB
5958 /*
5959 * Stop new entries from being returned after we return the last
5960 * entry.
5961 *
5962 * New directory entries are assigned a strictly increasing
5963 * offset. This means that new entries created during readdir
5964 * are *guaranteed* to be seen in the future by that readdir.
5965 * This has broken buggy programs which operate on names as
5966 * they're returned by readdir. Until we re-use freed offsets
5967 * we have this hack to stop new entries from being returned
5968 * under the assumption that they'll never reach this huge
5969 * offset.
5970 *
5971 * This is being careful not to overflow 32bit loff_t unless the
5972 * last entry requires it because doing so has broken 32bit apps
5973 * in the past.
5974 */
c2951f32
JM
5975 if (ctx->pos >= INT_MAX)
5976 ctx->pos = LLONG_MAX;
5977 else
5978 ctx->pos = INT_MAX;
39279cc3
CM
5979nopos:
5980 ret = 0;
5981err:
02dbfc99
OS
5982 if (put)
5983 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
39279cc3 5984 btrfs_free_path(path);
39279cc3
CM
5985 return ret;
5986}
5987
39279cc3 5988/*
54aa1f4d 5989 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
5990 * inode changes. But, it is most likely to find the inode in cache.
5991 * FIXME, needs more benchmarking...there are no reasons other than performance
5992 * to keep or drop this code.
5993 */
48a3b636 5994static int btrfs_dirty_inode(struct inode *inode)
39279cc3 5995{
2ff7e61e 5996 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
39279cc3
CM
5997 struct btrfs_root *root = BTRFS_I(inode)->root;
5998 struct btrfs_trans_handle *trans;
8929ecfa
YZ
5999 int ret;
6000
72ac3c0d 6001 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
22c44fe6 6002 return 0;
39279cc3 6003
7a7eaa40 6004 trans = btrfs_join_transaction(root);
22c44fe6
JB
6005 if (IS_ERR(trans))
6006 return PTR_ERR(trans);
8929ecfa 6007
9a56fcd1 6008 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
4d14c5cd 6009 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
94b60442 6010 /* whoops, lets try again with the full transaction */
3a45bb20 6011 btrfs_end_transaction(trans);
94b60442 6012 trans = btrfs_start_transaction(root, 1);
22c44fe6
JB
6013 if (IS_ERR(trans))
6014 return PTR_ERR(trans);
8929ecfa 6015
9a56fcd1 6016 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
94b60442 6017 }
3a45bb20 6018 btrfs_end_transaction(trans);
16cdcec7 6019 if (BTRFS_I(inode)->delayed_node)
2ff7e61e 6020 btrfs_balance_delayed_items(fs_info);
22c44fe6
JB
6021
6022 return ret;
6023}
6024
6025/*
6026 * This is a copy of file_update_time. We need this so we can return error on
6027 * ENOSPC for updating the inode in the case of file write and mmap writes.
6028 */
95582b00 6029static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
e41f941a 6030 int flags)
22c44fe6 6031{
2bc55652 6032 struct btrfs_root *root = BTRFS_I(inode)->root;
3a8c7231 6033 bool dirty = flags & ~S_VERSION;
2bc55652
AB
6034
6035 if (btrfs_root_readonly(root))
6036 return -EROFS;
6037
e41f941a 6038 if (flags & S_VERSION)
3a8c7231 6039 dirty |= inode_maybe_inc_iversion(inode, dirty);
e41f941a
JB
6040 if (flags & S_CTIME)
6041 inode->i_ctime = *now;
6042 if (flags & S_MTIME)
6043 inode->i_mtime = *now;
6044 if (flags & S_ATIME)
6045 inode->i_atime = *now;
3a8c7231 6046 return dirty ? btrfs_dirty_inode(inode) : 0;
39279cc3
CM
6047}
6048
d352ac68
CM
6049/*
6050 * find the highest existing sequence number in a directory
6051 * and then set the in-memory index_cnt variable to reflect
6052 * free sequence numbers
6053 */
4c570655 6054static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
aec7477b 6055{
4c570655 6056 struct btrfs_root *root = inode->root;
aec7477b
JB
6057 struct btrfs_key key, found_key;
6058 struct btrfs_path *path;
6059 struct extent_buffer *leaf;
6060 int ret;
6061
4c570655 6062 key.objectid = btrfs_ino(inode);
962a298f 6063 key.type = BTRFS_DIR_INDEX_KEY;
aec7477b
JB
6064 key.offset = (u64)-1;
6065
6066 path = btrfs_alloc_path();
6067 if (!path)
6068 return -ENOMEM;
6069
6070 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6071 if (ret < 0)
6072 goto out;
6073 /* FIXME: we should be able to handle this */
6074 if (ret == 0)
6075 goto out;
6076 ret = 0;
6077
aec7477b 6078 if (path->slots[0] == 0) {
528ee697 6079 inode->index_cnt = BTRFS_DIR_START_INDEX;
aec7477b
JB
6080 goto out;
6081 }
6082
6083 path->slots[0]--;
6084
6085 leaf = path->nodes[0];
6086 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6087
4c570655 6088 if (found_key.objectid != btrfs_ino(inode) ||
962a298f 6089 found_key.type != BTRFS_DIR_INDEX_KEY) {
528ee697 6090 inode->index_cnt = BTRFS_DIR_START_INDEX;
aec7477b
JB
6091 goto out;
6092 }
6093
4c570655 6094 inode->index_cnt = found_key.offset + 1;
aec7477b
JB
6095out:
6096 btrfs_free_path(path);
6097 return ret;
6098}
6099
d352ac68
CM
6100/*
6101 * helper to find a free sequence number in a given directory. This current
6102 * code is very simple, later versions will do smarter things in the btree
6103 */
877574e2 6104int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
aec7477b
JB
6105{
6106 int ret = 0;
6107
877574e2
NB
6108 if (dir->index_cnt == (u64)-1) {
6109 ret = btrfs_inode_delayed_dir_index_count(dir);
16cdcec7
MX
6110 if (ret) {
6111 ret = btrfs_set_inode_index_count(dir);
6112 if (ret)
6113 return ret;
6114 }
aec7477b
JB
6115 }
6116
877574e2
NB
6117 *index = dir->index_cnt;
6118 dir->index_cnt++;
aec7477b
JB
6119
6120 return ret;
6121}
6122
b0d5d10f
CM
6123static int btrfs_insert_inode_locked(struct inode *inode)
6124{
6125 struct btrfs_iget_args args;
0202e83f
DS
6126
6127 args.ino = BTRFS_I(inode)->location.objectid;
b0d5d10f
CM
6128 args.root = BTRFS_I(inode)->root;
6129
6130 return insert_inode_locked4(inode,
6131 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6132 btrfs_find_actor, &args);
6133}
6134
3538d68d
OS
6135int btrfs_new_inode_prepare(struct btrfs_new_inode_args *args,
6136 unsigned int *trans_num_items)
6137{
6138 struct inode *dir = args->dir;
6139 struct inode *inode = args->inode;
6140 int ret;
6141
6142 ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl);
6143 if (ret)
6144 return ret;
6145
6146 /* 1 to add inode item */
6147 *trans_num_items = 1;
6148 /* 1 to add compression property */
6149 if (BTRFS_I(dir)->prop_compress)
6150 (*trans_num_items)++;
6151 /* 1 to add default ACL xattr */
6152 if (args->default_acl)
6153 (*trans_num_items)++;
6154 /* 1 to add access ACL xattr */
6155 if (args->acl)
6156 (*trans_num_items)++;
6157#ifdef CONFIG_SECURITY
6158 /* 1 to add LSM xattr */
6159 if (dir->i_security)
6160 (*trans_num_items)++;
6161#endif
6162 if (args->orphan) {
6163 /* 1 to add orphan item */
6164 (*trans_num_items)++;
6165 } else {
6166 /*
3538d68d
OS
6167 * 1 to add dir item
6168 * 1 to add dir index
6169 * 1 to update parent inode item
97bdf1a9
FM
6170 *
6171 * No need for 1 unit for the inode ref item because it is
6172 * inserted in a batch together with the inode item at
6173 * btrfs_create_new_inode().
3538d68d 6174 */
97bdf1a9 6175 *trans_num_items += 3;
3538d68d
OS
6176 }
6177 return 0;
6178}
6179
6180void btrfs_new_inode_args_destroy(struct btrfs_new_inode_args *args)
6181{
6182 posix_acl_release(args->acl);
6183 posix_acl_release(args->default_acl);
6184}
6185
19aee8de
AJ
6186/*
6187 * Inherit flags from the parent inode.
6188 *
6189 * Currently only the compression flags and the cow flags are inherited.
6190 */
6191static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6192{
6193 unsigned int flags;
6194
19aee8de
AJ
6195 flags = BTRFS_I(dir)->flags;
6196
6197 if (flags & BTRFS_INODE_NOCOMPRESS) {
6198 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6199 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6200 } else if (flags & BTRFS_INODE_COMPRESS) {
6201 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6202 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6203 }
6204
6205 if (flags & BTRFS_INODE_NODATACOW) {
6206 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6207 if (S_ISREG(inode->i_mode))
6208 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6209 }
6210
7b6a221e 6211 btrfs_sync_inode_flags_to_i_flags(inode);
19aee8de
AJ
6212}
6213
3538d68d 6214int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
caae78e0 6215 struct btrfs_new_inode_args *args)
39279cc3 6216{
caae78e0 6217 struct inode *dir = args->dir;
3538d68d 6218 struct inode *inode = args->inode;
caae78e0
OS
6219 const char *name = args->orphan ? NULL : args->dentry->d_name.name;
6220 int name_len = args->orphan ? 0 : args->dentry->d_name.len;
6221 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
3538d68d 6222 struct btrfs_root *root;
5f39d397 6223 struct btrfs_inode_item *inode_item;
39279cc3 6224 struct btrfs_key *location;
5f39d397 6225 struct btrfs_path *path;
6437d458 6226 u64 objectid;
9c58309d
CM
6227 struct btrfs_inode_ref *ref;
6228 struct btrfs_key key[2];
6229 u32 sizes[2];
b7ef5f3a 6230 struct btrfs_item_batch batch;
9c58309d 6231 unsigned long ptr;
39279cc3 6232 int ret;
39279cc3 6233
5f39d397 6234 path = btrfs_alloc_path();
d8926bb3 6235 if (!path)
a1fd0c35 6236 return -ENOMEM;
39279cc3 6237
3538d68d
OS
6238 if (!args->subvol)
6239 BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root);
6240 root = BTRFS_I(inode)->root;
6241
6437d458 6242 ret = btrfs_get_free_objectid(root, &objectid);
caae78e0
OS
6243 if (ret)
6244 goto out;
581bb050
LZ
6245 inode->i_ino = objectid;
6246
caae78e0
OS
6247 if (args->orphan) {
6248 /*
6249 * O_TMPFILE, set link count to 0, so that after this point, we
6250 * fill in an inode item with the correct link count.
6251 */
6252 set_nlink(inode, 0);
6253 } else {
1abe9b8a 6254 trace_btrfs_inode_request(dir);
6255
caae78e0
OS
6256 ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index);
6257 if (ret)
6258 goto out;
aec7477b 6259 }
49024388
FM
6260 /* index_cnt is ignored for everything but a dir. */
6261 BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX;
e02119d5 6262 BTRFS_I(inode)->generation = trans->transid;
76195853 6263 inode->i_generation = BTRFS_I(inode)->generation;
b888db2b 6264
caae78e0
OS
6265 /*
6266 * Subvolumes don't inherit flags from their parent directory.
6267 * Originally this was probably by accident, but we probably can't
6268 * change it now without compatibility issues.
6269 */
6270 if (!args->subvol)
6271 btrfs_inherit_iflags(inode, dir);
305eaac0 6272
a1fd0c35 6273 if (S_ISREG(inode->i_mode)) {
305eaac0
OS
6274 if (btrfs_test_opt(fs_info, NODATASUM))
6275 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6276 if (btrfs_test_opt(fs_info, NODATACOW))
6277 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6278 BTRFS_INODE_NODATASUM;
6279 }
6280
caae78e0
OS
6281 location = &BTRFS_I(inode)->location;
6282 location->objectid = objectid;
6283 location->offset = 0;
6284 location->type = BTRFS_INODE_ITEM_KEY;
6285
6286 ret = btrfs_insert_inode_locked(inode);
6287 if (ret < 0) {
6288 if (!args->orphan)
6289 BTRFS_I(dir)->index_cnt--;
6290 goto out;
6291 }
6292
5dc562c5
JB
6293 /*
6294 * We could have gotten an inode number from somebody who was fsynced
6295 * and then removed in this same transaction, so let's just set full
6296 * sync since it will be a full sync anyway and this will blow away the
6297 * old info in the log.
6298 */
23e3337f 6299 btrfs_set_inode_full_sync(BTRFS_I(inode));
5dc562c5 6300
9c58309d 6301 key[0].objectid = objectid;
962a298f 6302 key[0].type = BTRFS_INODE_ITEM_KEY;
9c58309d
CM
6303 key[0].offset = 0;
6304
9c58309d 6305 sizes[0] = sizeof(struct btrfs_inode_item);
ef3b9af5 6306
caae78e0 6307 if (!args->orphan) {
ef3b9af5
FM
6308 /*
6309 * Start new inodes with an inode_ref. This is slightly more
6310 * efficient for small numbers of hard links since they will
6311 * be packed into one item. Extended refs will kick in if we
6312 * add more hard links than can fit in the ref item.
6313 */
6314 key[1].objectid = objectid;
962a298f 6315 key[1].type = BTRFS_INODE_REF_KEY;
caae78e0 6316 if (args->subvol) {
23c24ef8 6317 key[1].offset = objectid;
caae78e0
OS
6318 sizes[1] = 2 + sizeof(*ref);
6319 } else {
6320 key[1].offset = btrfs_ino(BTRFS_I(dir));
6321 sizes[1] = name_len + sizeof(*ref);
6322 }
ef3b9af5 6323 }
9c58309d 6324
b7ef5f3a
FM
6325 batch.keys = &key[0];
6326 batch.data_sizes = &sizes[0];
caae78e0
OS
6327 batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]);
6328 batch.nr = args->orphan ? 1 : 2;
b7ef5f3a 6329 ret = btrfs_insert_empty_items(trans, root, path, &batch);
caae78e0
OS
6330 if (ret != 0) {
6331 btrfs_abort_transaction(trans, ret);
6332 goto discard;
6333 }
5f39d397 6334
c2050a45 6335 inode->i_mtime = current_time(inode);
9cc97d64 6336 inode->i_atime = inode->i_mtime;
6337 inode->i_ctime = inode->i_mtime;
d3c6be6f 6338 BTRFS_I(inode)->i_otime = inode->i_mtime;
9cc97d64 6339
caae78e0
OS
6340 /*
6341 * We're going to fill the inode item now, so at this point the inode
6342 * must be fully initialized.
6343 */
6344
5f39d397
CM
6345 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6346 struct btrfs_inode_item);
b159fa28 6347 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
293f7e07 6348 sizeof(*inode_item));
e02119d5 6349 fill_inode_item(trans, path->nodes[0], inode_item, inode);
9c58309d 6350
caae78e0 6351 if (!args->orphan) {
ef3b9af5
FM
6352 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6353 struct btrfs_inode_ref);
ef3b9af5 6354 ptr = (unsigned long)(ref + 1);
caae78e0
OS
6355 if (args->subvol) {
6356 btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2);
6357 btrfs_set_inode_ref_index(path->nodes[0], ref, 0);
6358 write_extent_buffer(path->nodes[0], "..", ptr, 2);
6359 } else {
6360 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6361 btrfs_set_inode_ref_index(path->nodes[0], ref,
6362 BTRFS_I(inode)->dir_index);
6363 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6364 }
ef3b9af5 6365 }
9c58309d 6366
5f39d397 6367 btrfs_mark_buffer_dirty(path->nodes[0]);
caae78e0 6368 btrfs_release_path(path);
5f39d397 6369
6c3636eb
STD
6370 if (args->subvol) {
6371 struct inode *parent;
6372
6373 /*
6374 * Subvolumes inherit properties from their parent subvolume,
6375 * not the directory they were created in.
6376 */
6377 parent = btrfs_iget(fs_info->sb, BTRFS_FIRST_FREE_OBJECTID,
6378 BTRFS_I(dir)->root);
6379 if (IS_ERR(parent)) {
6380 ret = PTR_ERR(parent);
6381 } else {
6382 ret = btrfs_inode_inherit_props(trans, inode, parent);
6383 iput(parent);
6384 }
6385 } else {
6386 ret = btrfs_inode_inherit_props(trans, inode, dir);
6387 }
6388 if (ret) {
6389 btrfs_err(fs_info,
6390 "error inheriting props for ino %llu (root %llu): %d",
6391 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid,
6392 ret);
6393 }
6394
6395 /*
6396 * Subvolumes don't inherit ACLs or get passed to the LSM. This is
6397 * probably a bug.
6398 */
6399 if (!args->subvol) {
6400 ret = btrfs_init_inode_security(trans, args);
6401 if (ret) {
6402 btrfs_abort_transaction(trans, ret);
6403 goto discard;
6404 }
6405 }
6406
5d4f98a2 6407 inode_tree_add(inode);
1abe9b8a 6408
6409 trace_btrfs_inode_new(inode);
d9094414 6410 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
1abe9b8a 6411
8ea05e3a
AB
6412 btrfs_update_root_times(trans, root);
6413
caae78e0
OS
6414 if (args->orphan) {
6415 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
6416 } else {
6417 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
6418 name_len, 0, BTRFS_I(inode)->dir_index);
6419 }
6420 if (ret) {
6421 btrfs_abort_transaction(trans, ret);
6422 goto discard;
6423 }
63541927 6424
caae78e0
OS
6425 ret = 0;
6426 goto out;
b0d5d10f 6427
caae78e0 6428discard:
a1fd0c35
OS
6429 /*
6430 * discard_new_inode() calls iput(), but the caller owns the reference
6431 * to the inode.
6432 */
6433 ihold(inode);
32955c54 6434 discard_new_inode(inode);
caae78e0 6435out:
5f39d397 6436 btrfs_free_path(path);
a1fd0c35 6437 return ret;
39279cc3
CM
6438}
6439
d352ac68
CM
6440/*
6441 * utility function to add 'inode' into 'parent_inode' with
6442 * a give name and a given sequence number.
6443 * if 'add_backref' is true, also insert a backref from the
6444 * inode to the parent directory.
6445 */
e02119d5 6446int btrfs_add_link(struct btrfs_trans_handle *trans,
db0a669f 6447 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
e02119d5 6448 const char *name, int name_len, int add_backref, u64 index)
39279cc3 6449{
4df27c4d 6450 int ret = 0;
39279cc3 6451 struct btrfs_key key;
db0a669f
NB
6452 struct btrfs_root *root = parent_inode->root;
6453 u64 ino = btrfs_ino(inode);
6454 u64 parent_ino = btrfs_ino(parent_inode);
5f39d397 6455
33345d01 6456 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
db0a669f 6457 memcpy(&key, &inode->root->root_key, sizeof(key));
4df27c4d 6458 } else {
33345d01 6459 key.objectid = ino;
962a298f 6460 key.type = BTRFS_INODE_ITEM_KEY;
4df27c4d
YZ
6461 key.offset = 0;
6462 }
6463
33345d01 6464 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6025c19f 6465 ret = btrfs_add_root_ref(trans, key.objectid,
0b246afa
JM
6466 root->root_key.objectid, parent_ino,
6467 index, name, name_len);
4df27c4d 6468 } else if (add_backref) {
33345d01
LZ
6469 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6470 parent_ino, index);
4df27c4d 6471 }
39279cc3 6472
79787eaa
JM
6473 /* Nothing to clean up yet */
6474 if (ret)
6475 return ret;
4df27c4d 6476
684572df 6477 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
db0a669f 6478 btrfs_inode_type(&inode->vfs_inode), index);
9c52057c 6479 if (ret == -EEXIST || ret == -EOVERFLOW)
79787eaa
JM
6480 goto fail_dir_item;
6481 else if (ret) {
66642832 6482 btrfs_abort_transaction(trans, ret);
79787eaa 6483 return ret;
39279cc3 6484 }
79787eaa 6485
db0a669f 6486 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
79787eaa 6487 name_len * 2);
db0a669f 6488 inode_inc_iversion(&parent_inode->vfs_inode);
5338e43a
FM
6489 /*
6490 * If we are replaying a log tree, we do not want to update the mtime
6491 * and ctime of the parent directory with the current time, since the
6492 * log replay procedure is responsible for setting them to their correct
6493 * values (the ones it had when the fsync was done).
6494 */
6495 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6496 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6497
6498 parent_inode->vfs_inode.i_mtime = now;
6499 parent_inode->vfs_inode.i_ctime = now;
6500 }
9a56fcd1 6501 ret = btrfs_update_inode(trans, root, parent_inode);
79787eaa 6502 if (ret)
66642832 6503 btrfs_abort_transaction(trans, ret);
39279cc3 6504 return ret;
fe66a05a
CM
6505
6506fail_dir_item:
6507 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6508 u64 local_index;
6509 int err;
3ee1c553 6510 err = btrfs_del_root_ref(trans, key.objectid,
0b246afa
JM
6511 root->root_key.objectid, parent_ino,
6512 &local_index, name, name_len);
1690dd41
JT
6513 if (err)
6514 btrfs_abort_transaction(trans, err);
fe66a05a
CM
6515 } else if (add_backref) {
6516 u64 local_index;
6517 int err;
6518
6519 err = btrfs_del_inode_ref(trans, root, name, name_len,
6520 ino, parent_ino, &local_index);
1690dd41
JT
6521 if (err)
6522 btrfs_abort_transaction(trans, err);
fe66a05a 6523 }
1690dd41
JT
6524
6525 /* Return the original error code */
fe66a05a 6526 return ret;
39279cc3
CM
6527}
6528
5f465bf1
OS
6529static int btrfs_create_common(struct inode *dir, struct dentry *dentry,
6530 struct inode *inode)
618e21d5 6531{
2ff7e61e 6532 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
618e21d5 6533 struct btrfs_root *root = BTRFS_I(dir)->root;
3538d68d
OS
6534 struct btrfs_new_inode_args new_inode_args = {
6535 .dir = dir,
6536 .dentry = dentry,
6537 .inode = inode,
6538 };
6539 unsigned int trans_num_items;
5f465bf1 6540 struct btrfs_trans_handle *trans;
618e21d5 6541 int err;
618e21d5 6542
3538d68d 6543 err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
caae78e0
OS
6544 if (err)
6545 goto out_inode;
3538d68d
OS
6546
6547 trans = btrfs_start_transaction(root, trans_num_items);
a1fd0c35 6548 if (IS_ERR(trans)) {
3538d68d
OS
6549 err = PTR_ERR(trans);
6550 goto out_new_inode_args;
a1fd0c35 6551 }
1832a6d5 6552
caae78e0
OS
6553 err = btrfs_create_new_inode(trans, &new_inode_args);
6554 if (!err)
6555 d_instantiate_new(dentry, inode);
b0d5d10f 6556
3a45bb20 6557 btrfs_end_transaction(trans);
5f465bf1 6558 btrfs_btree_balance_dirty(fs_info);
3538d68d
OS
6559out_new_inode_args:
6560 btrfs_new_inode_args_destroy(&new_inode_args);
caae78e0
OS
6561out_inode:
6562 if (err)
6563 iput(inode);
618e21d5
JB
6564 return err;
6565}
6566
5f465bf1
OS
6567static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6568 struct dentry *dentry, umode_t mode, dev_t rdev)
6569{
6570 struct inode *inode;
6571
6572 inode = new_inode(dir->i_sb);
6573 if (!inode)
6574 return -ENOMEM;
6575 inode_init_owner(mnt_userns, inode, dir, mode);
6576 inode->i_op = &btrfs_special_inode_operations;
6577 init_special_inode(inode, inode->i_mode, rdev);
6578 return btrfs_create_common(dir, dentry, inode);
6579}
6580
549c7297
CB
6581static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6582 struct dentry *dentry, umode_t mode, bool excl)
39279cc3 6583{
a1fd0c35 6584 struct inode *inode;
39279cc3 6585
a1fd0c35
OS
6586 inode = new_inode(dir->i_sb);
6587 if (!inode)
6588 return -ENOMEM;
6589 inode_init_owner(mnt_userns, inode, dir, mode);
6590 inode->i_fop = &btrfs_file_operations;
6591 inode->i_op = &btrfs_file_inode_operations;
6592 inode->i_mapping->a_ops = &btrfs_aops;
5f465bf1 6593 return btrfs_create_common(dir, dentry, inode);
39279cc3
CM
6594}
6595
6596static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6597 struct dentry *dentry)
6598{
271dba45 6599 struct btrfs_trans_handle *trans = NULL;
39279cc3 6600 struct btrfs_root *root = BTRFS_I(dir)->root;
2b0143b5 6601 struct inode *inode = d_inode(old_dentry);
2ff7e61e 6602 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
00e4e6b3 6603 u64 index;
39279cc3
CM
6604 int err;
6605 int drop_inode = 0;
6606
4a8be425 6607 /* do not allow sys_link's with other subvols of the same device */
4fd786e6 6608 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
3ab3564f 6609 return -EXDEV;
4a8be425 6610
f186373f 6611 if (inode->i_nlink >= BTRFS_LINK_MAX)
c055e99e 6612 return -EMLINK;
4a8be425 6613
877574e2 6614 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
aec7477b
JB
6615 if (err)
6616 goto fail;
6617
a22285a6 6618 /*
7e6b6465 6619 * 2 items for inode and inode ref
a22285a6 6620 * 2 items for dir items
7e6b6465 6621 * 1 item for parent inode
399b0bbf 6622 * 1 item for orphan item deletion if O_TMPFILE
a22285a6 6623 */
399b0bbf 6624 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
a22285a6
YZ
6625 if (IS_ERR(trans)) {
6626 err = PTR_ERR(trans);
271dba45 6627 trans = NULL;
a22285a6
YZ
6628 goto fail;
6629 }
5f39d397 6630
67de1176
MX
6631 /* There are several dir indexes for this inode, clear the cache. */
6632 BTRFS_I(inode)->dir_index = 0ULL;
8b558c5f 6633 inc_nlink(inode);
0c4d2d95 6634 inode_inc_iversion(inode);
c2050a45 6635 inode->i_ctime = current_time(inode);
7de9c6ee 6636 ihold(inode);
e9976151 6637 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
aec7477b 6638
81512e89
OS
6639 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6640 dentry->d_name.name, dentry->d_name.len, 1, index);
5f39d397 6641
a5719521 6642 if (err) {
54aa1f4d 6643 drop_inode = 1;
a5719521 6644 } else {
10d9f309 6645 struct dentry *parent = dentry->d_parent;
d4682ba0 6646
9a56fcd1 6647 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
79787eaa
JM
6648 if (err)
6649 goto fail;
ef3b9af5
FM
6650 if (inode->i_nlink == 1) {
6651 /*
6652 * If new hard link count is 1, it's a file created
6653 * with open(2) O_TMPFILE flag.
6654 */
3d6ae7bb 6655 err = btrfs_orphan_del(trans, BTRFS_I(inode));
ef3b9af5
FM
6656 if (err)
6657 goto fail;
6658 }
08c422c2 6659 d_instantiate(dentry, inode);
88d2beec 6660 btrfs_log_new_name(trans, old_dentry, NULL, 0, parent);
a5719521 6661 }
39279cc3 6662
1832a6d5 6663fail:
271dba45 6664 if (trans)
3a45bb20 6665 btrfs_end_transaction(trans);
39279cc3
CM
6666 if (drop_inode) {
6667 inode_dec_link_count(inode);
6668 iput(inode);
6669 }
2ff7e61e 6670 btrfs_btree_balance_dirty(fs_info);
39279cc3
CM
6671 return err;
6672}
6673
549c7297
CB
6674static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6675 struct dentry *dentry, umode_t mode)
39279cc3 6676{
a1fd0c35 6677 struct inode *inode;
39279cc3 6678
a1fd0c35
OS
6679 inode = new_inode(dir->i_sb);
6680 if (!inode)
6681 return -ENOMEM;
6682 inode_init_owner(mnt_userns, inode, dir, S_IFDIR | mode);
6683 inode->i_op = &btrfs_dir_inode_operations;
6684 inode->i_fop = &btrfs_dir_file_operations;
5f465bf1 6685 return btrfs_create_common(dir, dentry, inode);
39279cc3
CM
6686}
6687
c8b97818 6688static noinline int uncompress_inline(struct btrfs_path *path,
e40da0e5 6689 struct page *page,
c8b97818
CM
6690 size_t pg_offset, u64 extent_offset,
6691 struct btrfs_file_extent_item *item)
6692{
6693 int ret;
6694 struct extent_buffer *leaf = path->nodes[0];
6695 char *tmp;
6696 size_t max_size;
6697 unsigned long inline_size;
6698 unsigned long ptr;
261507a0 6699 int compress_type;
c8b97818
CM
6700
6701 WARN_ON(pg_offset != 0);
261507a0 6702 compress_type = btrfs_file_extent_compression(leaf, item);
c8b97818 6703 max_size = btrfs_file_extent_ram_bytes(leaf, item);
437bd07e 6704 inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
c8b97818 6705 tmp = kmalloc(inline_size, GFP_NOFS);
8d413713
TI
6706 if (!tmp)
6707 return -ENOMEM;
c8b97818
CM
6708 ptr = btrfs_file_extent_inline_start(item);
6709
6710 read_extent_buffer(leaf, tmp, ptr, inline_size);
6711
09cbfeaf 6712 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
261507a0
LZ
6713 ret = btrfs_decompress(compress_type, tmp, page,
6714 extent_offset, inline_size, max_size);
e1699d2d
ZB
6715
6716 /*
6717 * decompression code contains a memset to fill in any space between the end
6718 * of the uncompressed data and the end of max_size in case the decompressed
6719 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6720 * the end of an inline extent and the beginning of the next block, so we
6721 * cover that region here.
6722 */
6723
d048b9c2
IW
6724 if (max_size + pg_offset < PAGE_SIZE)
6725 memzero_page(page, pg_offset + max_size,
6726 PAGE_SIZE - max_size - pg_offset);
c8b97818 6727 kfree(tmp);
166ae5a4 6728 return ret;
c8b97818
CM
6729}
6730
39b07b5d
OS
6731/**
6732 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6733 * @inode: file to search in
6734 * @page: page to read extent data into if the extent is inline
6735 * @pg_offset: offset into @page to copy to
6736 * @start: file offset
6737 * @len: length of range starting at @start
6738 *
6739 * This returns the first &struct extent_map which overlaps with the given
6740 * range, reading it from the B-tree and caching it if necessary. Note that
6741 * there may be more extents which overlap the given range after the returned
6742 * extent_map.
d352ac68 6743 *
39b07b5d
OS
6744 * If @page is not NULL and the extent is inline, this also reads the extent
6745 * data directly into the page and marks the extent up to date in the io_tree.
6746 *
6747 * Return: ERR_PTR on error, non-NULL extent_map on success.
d352ac68 6748 */
fc4f21b1 6749struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
39b07b5d
OS
6750 struct page *page, size_t pg_offset,
6751 u64 start, u64 len)
a52d9a80 6752{
3ffbd68c 6753 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1028d1c4 6754 int ret = 0;
a52d9a80
CM
6755 u64 extent_start = 0;
6756 u64 extent_end = 0;
fc4f21b1 6757 u64 objectid = btrfs_ino(inode);
7e74e235 6758 int extent_type = -1;
f421950f 6759 struct btrfs_path *path = NULL;
fc4f21b1 6760 struct btrfs_root *root = inode->root;
a52d9a80 6761 struct btrfs_file_extent_item *item;
5f39d397
CM
6762 struct extent_buffer *leaf;
6763 struct btrfs_key found_key;
a52d9a80 6764 struct extent_map *em = NULL;
fc4f21b1
NB
6765 struct extent_map_tree *em_tree = &inode->extent_tree;
6766 struct extent_io_tree *io_tree = &inode->io_tree;
a52d9a80 6767
890871be 6768 read_lock(&em_tree->lock);
d1310b2e 6769 em = lookup_extent_mapping(em_tree, start, len);
890871be 6770 read_unlock(&em_tree->lock);
d1310b2e 6771
a52d9a80 6772 if (em) {
e1c4b745
CM
6773 if (em->start > start || em->start + em->len <= start)
6774 free_extent_map(em);
6775 else if (em->block_start == EXTENT_MAP_INLINE && page)
70dec807
CM
6776 free_extent_map(em);
6777 else
6778 goto out;
a52d9a80 6779 }
172ddd60 6780 em = alloc_extent_map();
a52d9a80 6781 if (!em) {
1028d1c4 6782 ret = -ENOMEM;
d1310b2e 6783 goto out;
a52d9a80 6784 }
d1310b2e 6785 em->start = EXTENT_MAP_HOLE;
445a6944 6786 em->orig_start = EXTENT_MAP_HOLE;
d1310b2e 6787 em->len = (u64)-1;
c8b97818 6788 em->block_len = (u64)-1;
f421950f 6789
bee6ec82 6790 path = btrfs_alloc_path();
f421950f 6791 if (!path) {
1028d1c4 6792 ret = -ENOMEM;
bee6ec82 6793 goto out;
f421950f
CM
6794 }
6795
bee6ec82
LB
6796 /* Chances are we'll be called again, so go ahead and do readahead */
6797 path->reada = READA_FORWARD;
4d7240f0
JB
6798
6799 /*
6800 * The same explanation in load_free_space_cache applies here as well,
6801 * we only read when we're loading the free space cache, and at that
6802 * point the commit_root has everything we need.
6803 */
6804 if (btrfs_is_free_space_inode(inode)) {
6805 path->search_commit_root = 1;
6806 path->skip_locking = 1;
6807 }
51899412 6808
5c9a702e 6809 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
a52d9a80 6810 if (ret < 0) {
a52d9a80 6811 goto out;
b8eeab7f 6812 } else if (ret > 0) {
a52d9a80
CM
6813 if (path->slots[0] == 0)
6814 goto not_found;
6815 path->slots[0]--;
1028d1c4 6816 ret = 0;
a52d9a80
CM
6817 }
6818
5f39d397
CM
6819 leaf = path->nodes[0];
6820 item = btrfs_item_ptr(leaf, path->slots[0],
a52d9a80 6821 struct btrfs_file_extent_item);
5f39d397 6822 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5f39d397 6823 if (found_key.objectid != objectid ||
694c12ed 6824 found_key.type != BTRFS_EXTENT_DATA_KEY) {
25a50341
JB
6825 /*
6826 * If we backup past the first extent we want to move forward
6827 * and see if there is an extent in front of us, otherwise we'll
6828 * say there is a hole for our whole search range which can
6829 * cause problems.
6830 */
6831 extent_end = start;
6832 goto next;
a52d9a80
CM
6833 }
6834
694c12ed 6835 extent_type = btrfs_file_extent_type(leaf, item);
5f39d397 6836 extent_start = found_key.offset;
a5eeb3d1 6837 extent_end = btrfs_file_extent_end(path);
694c12ed
NB
6838 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6839 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
6bf9e4bd
QW
6840 /* Only regular file could have regular/prealloc extent */
6841 if (!S_ISREG(inode->vfs_inode.i_mode)) {
1028d1c4 6842 ret = -EUCLEAN;
6bf9e4bd
QW
6843 btrfs_crit(fs_info,
6844 "regular/prealloc extent found for non-regular inode %llu",
6845 btrfs_ino(inode));
6846 goto out;
6847 }
09ed2f16
LB
6848 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6849 extent_start);
694c12ed 6850 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
09ed2f16
LB
6851 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6852 path->slots[0],
6853 extent_start);
9036c102 6854 }
25a50341 6855next:
9036c102
YZ
6856 if (start >= extent_end) {
6857 path->slots[0]++;
6858 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6859 ret = btrfs_next_leaf(root, path);
1028d1c4 6860 if (ret < 0)
9036c102 6861 goto out;
1028d1c4 6862 else if (ret > 0)
9036c102 6863 goto not_found;
1028d1c4 6864
9036c102 6865 leaf = path->nodes[0];
a52d9a80 6866 }
9036c102
YZ
6867 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6868 if (found_key.objectid != objectid ||
6869 found_key.type != BTRFS_EXTENT_DATA_KEY)
6870 goto not_found;
6871 if (start + len <= found_key.offset)
6872 goto not_found;
e2eca69d
WS
6873 if (start > found_key.offset)
6874 goto next;
02a033df
NB
6875
6876 /* New extent overlaps with existing one */
9036c102 6877 em->start = start;
70c8a91c 6878 em->orig_start = start;
9036c102 6879 em->len = found_key.offset - start;
02a033df
NB
6880 em->block_start = EXTENT_MAP_HOLE;
6881 goto insert;
9036c102
YZ
6882 }
6883
39b07b5d 6884 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
7ffbb598 6885
694c12ed
NB
6886 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6887 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
a52d9a80 6888 goto insert;
694c12ed 6889 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
5f39d397 6890 unsigned long ptr;
a52d9a80 6891 char *map;
3326d1b0
CM
6892 size_t size;
6893 size_t extent_offset;
6894 size_t copy_size;
a52d9a80 6895
39b07b5d 6896 if (!page)
689f9346 6897 goto out;
5f39d397 6898
e41ca589 6899 size = btrfs_file_extent_ram_bytes(leaf, item);
9036c102 6900 extent_offset = page_offset(page) + pg_offset - extent_start;
09cbfeaf
KS
6901 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6902 size - extent_offset);
3326d1b0 6903 em->start = extent_start + extent_offset;
0b246afa 6904 em->len = ALIGN(copy_size, fs_info->sectorsize);
b4939680 6905 em->orig_block_len = em->len;
70c8a91c 6906 em->orig_start = em->start;
689f9346 6907 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
e49aabd9 6908
bf46f52d 6909 if (!PageUptodate(page)) {
261507a0
LZ
6910 if (btrfs_file_extent_compression(leaf, item) !=
6911 BTRFS_COMPRESS_NONE) {
e40da0e5 6912 ret = uncompress_inline(path, page, pg_offset,
c8b97818 6913 extent_offset, item);
1028d1c4 6914 if (ret)
166ae5a4 6915 goto out;
c8b97818 6916 } else {
58c1a35c 6917 map = kmap_local_page(page);
c8b97818
CM
6918 read_extent_buffer(leaf, map + pg_offset, ptr,
6919 copy_size);
09cbfeaf 6920 if (pg_offset + copy_size < PAGE_SIZE) {
93c82d57 6921 memset(map + pg_offset + copy_size, 0,
09cbfeaf 6922 PAGE_SIZE - pg_offset -
93c82d57
CM
6923 copy_size);
6924 }
58c1a35c 6925 kunmap_local(map);
c8b97818 6926 }
179e29e4 6927 flush_dcache_page(page);
a52d9a80 6928 }
d1310b2e 6929 set_extent_uptodate(io_tree, em->start,
507903b8 6930 extent_map_end(em) - 1, NULL, GFP_NOFS);
a52d9a80 6931 goto insert;
a52d9a80
CM
6932 }
6933not_found:
6934 em->start = start;
70c8a91c 6935 em->orig_start = start;
d1310b2e 6936 em->len = len;
5f39d397 6937 em->block_start = EXTENT_MAP_HOLE;
a52d9a80 6938insert:
1028d1c4 6939 ret = 0;
b3b4aa74 6940 btrfs_release_path(path);
d1310b2e 6941 if (em->start > start || extent_map_end(em) <= start) {
0b246afa 6942 btrfs_err(fs_info,
5d163e0e
JM
6943 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6944 em->start, em->len, start, len);
1028d1c4 6945 ret = -EIO;
a52d9a80
CM
6946 goto out;
6947 }
d1310b2e 6948
890871be 6949 write_lock(&em_tree->lock);
1028d1c4 6950 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
890871be 6951 write_unlock(&em_tree->lock);
a52d9a80 6952out:
c6414280 6953 btrfs_free_path(path);
1abe9b8a 6954
fc4f21b1 6955 trace_btrfs_get_extent(root, inode, em);
1abe9b8a 6956
1028d1c4 6957 if (ret) {
a52d9a80 6958 free_extent_map(em);
1028d1c4 6959 return ERR_PTR(ret);
a52d9a80
CM
6960 }
6961 return em;
6962}
6963
fc4f21b1 6964struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
4ab47a8d 6965 u64 start, u64 len)
ec29ed5b
CM
6966{
6967 struct extent_map *em;
6968 struct extent_map *hole_em = NULL;
f3714ef4 6969 u64 delalloc_start = start;
ec29ed5b 6970 u64 end;
f3714ef4
NB
6971 u64 delalloc_len;
6972 u64 delalloc_end;
ec29ed5b
CM
6973 int err = 0;
6974
39b07b5d 6975 em = btrfs_get_extent(inode, NULL, 0, start, len);
ec29ed5b
CM
6976 if (IS_ERR(em))
6977 return em;
9986277e
DC
6978 /*
6979 * If our em maps to:
6980 * - a hole or
6981 * - a pre-alloc extent,
6982 * there might actually be delalloc bytes behind it.
6983 */
6984 if (em->block_start != EXTENT_MAP_HOLE &&
6985 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6986 return em;
6987 else
6988 hole_em = em;
ec29ed5b
CM
6989
6990 /* check to see if we've wrapped (len == -1 or similar) */
6991 end = start + len;
6992 if (end < start)
6993 end = (u64)-1;
6994 else
6995 end -= 1;
6996
6997 em = NULL;
6998
6999 /* ok, we didn't find anything, lets look for delalloc */
f3714ef4 7000 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
ec29ed5b 7001 end, len, EXTENT_DELALLOC, 1);
f3714ef4
NB
7002 delalloc_end = delalloc_start + delalloc_len;
7003 if (delalloc_end < delalloc_start)
7004 delalloc_end = (u64)-1;
ec29ed5b
CM
7005
7006 /*
f3714ef4
NB
7007 * We didn't find anything useful, return the original results from
7008 * get_extent()
ec29ed5b 7009 */
f3714ef4 7010 if (delalloc_start > end || delalloc_end <= start) {
ec29ed5b
CM
7011 em = hole_em;
7012 hole_em = NULL;
7013 goto out;
7014 }
7015
f3714ef4
NB
7016 /*
7017 * Adjust the delalloc_start to make sure it doesn't go backwards from
7018 * the start they passed in
ec29ed5b 7019 */
f3714ef4
NB
7020 delalloc_start = max(start, delalloc_start);
7021 delalloc_len = delalloc_end - delalloc_start;
ec29ed5b 7022
f3714ef4
NB
7023 if (delalloc_len > 0) {
7024 u64 hole_start;
02950af4 7025 u64 hole_len;
f3714ef4 7026 const u64 hole_end = extent_map_end(hole_em);
ec29ed5b 7027
172ddd60 7028 em = alloc_extent_map();
ec29ed5b
CM
7029 if (!em) {
7030 err = -ENOMEM;
7031 goto out;
7032 }
f3714ef4
NB
7033
7034 ASSERT(hole_em);
ec29ed5b 7035 /*
f3714ef4
NB
7036 * When btrfs_get_extent can't find anything it returns one
7037 * huge hole
ec29ed5b 7038 *
f3714ef4
NB
7039 * Make sure what it found really fits our range, and adjust to
7040 * make sure it is based on the start from the caller
ec29ed5b 7041 */
f3714ef4
NB
7042 if (hole_end <= start || hole_em->start > end) {
7043 free_extent_map(hole_em);
7044 hole_em = NULL;
7045 } else {
7046 hole_start = max(hole_em->start, start);
7047 hole_len = hole_end - hole_start;
ec29ed5b 7048 }
f3714ef4
NB
7049
7050 if (hole_em && delalloc_start > hole_start) {
7051 /*
7052 * Our hole starts before our delalloc, so we have to
7053 * return just the parts of the hole that go until the
7054 * delalloc starts
ec29ed5b 7055 */
f3714ef4 7056 em->len = min(hole_len, delalloc_start - hole_start);
ec29ed5b
CM
7057 em->start = hole_start;
7058 em->orig_start = hole_start;
7059 /*
f3714ef4
NB
7060 * Don't adjust block start at all, it is fixed at
7061 * EXTENT_MAP_HOLE
ec29ed5b
CM
7062 */
7063 em->block_start = hole_em->block_start;
7064 em->block_len = hole_len;
f9e4fb53
LB
7065 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7066 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
ec29ed5b 7067 } else {
f3714ef4
NB
7068 /*
7069 * Hole is out of passed range or it starts after
7070 * delalloc range
7071 */
7072 em->start = delalloc_start;
7073 em->len = delalloc_len;
7074 em->orig_start = delalloc_start;
ec29ed5b 7075 em->block_start = EXTENT_MAP_DELALLOC;
f3714ef4 7076 em->block_len = delalloc_len;
ec29ed5b 7077 }
bf8d32b9 7078 } else {
ec29ed5b
CM
7079 return hole_em;
7080 }
7081out:
7082
7083 free_extent_map(hole_em);
7084 if (err) {
7085 free_extent_map(em);
7086 return ERR_PTR(err);
7087 }
7088 return em;
7089}
7090
64f54188 7091static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
5f9a8a51
FM
7092 const u64 start,
7093 const u64 len,
7094 const u64 orig_start,
7095 const u64 block_start,
7096 const u64 block_len,
7097 const u64 orig_block_len,
7098 const u64 ram_bytes,
7099 const int type)
7100{
7101 struct extent_map *em = NULL;
7102 int ret;
7103
5f9a8a51 7104 if (type != BTRFS_ORDERED_NOCOW) {
64f54188
NB
7105 em = create_io_em(inode, start, len, orig_start, block_start,
7106 block_len, orig_block_len, ram_bytes,
6f9994db
LB
7107 BTRFS_COMPRESS_NONE, /* compress_type */
7108 type);
5f9a8a51
FM
7109 if (IS_ERR(em))
7110 goto out;
7111 }
cb36a9bb
OS
7112 ret = btrfs_add_ordered_extent(inode, start, len, len, block_start,
7113 block_len, 0,
7114 (1 << type) |
7115 (1 << BTRFS_ORDERED_DIRECT),
7116 BTRFS_COMPRESS_NONE);
5f9a8a51
FM
7117 if (ret) {
7118 if (em) {
7119 free_extent_map(em);
64f54188 7120 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5f9a8a51
FM
7121 }
7122 em = ERR_PTR(ret);
7123 }
7124 out:
5f9a8a51
FM
7125
7126 return em;
7127}
7128
9fc6f911 7129static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
4b46fce2
JB
7130 u64 start, u64 len)
7131{
9fc6f911
NB
7132 struct btrfs_root *root = inode->root;
7133 struct btrfs_fs_info *fs_info = root->fs_info;
70c8a91c 7134 struct extent_map *em;
4b46fce2
JB
7135 struct btrfs_key ins;
7136 u64 alloc_hint;
7137 int ret;
4b46fce2 7138
9fc6f911 7139 alloc_hint = get_extent_allocation_hint(inode, start, len);
0b246afa 7140 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
da17066c 7141 0, alloc_hint, &ins, 1, 1);
00361589
JB
7142 if (ret)
7143 return ERR_PTR(ret);
4b46fce2 7144
9fc6f911 7145 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
5f9a8a51 7146 ins.objectid, ins.offset, ins.offset,
6288d6ea 7147 ins.offset, BTRFS_ORDERED_REGULAR);
0b246afa 7148 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
5f9a8a51 7149 if (IS_ERR(em))
9fc6f911
NB
7150 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7151 1);
de0ee0ed 7152
4b46fce2
JB
7153 return em;
7154}
7155
f4639636 7156static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
05947ae1
AJ
7157{
7158 struct btrfs_block_group *block_group;
f4639636 7159 bool readonly = false;
05947ae1
AJ
7160
7161 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7162 if (!block_group || block_group->ro)
f4639636 7163 readonly = true;
05947ae1
AJ
7164 if (block_group)
7165 btrfs_put_block_group(block_group);
7166 return readonly;
7167}
7168
46bfbb5c 7169/*
e4ecaf90
QW
7170 * Check if we can do nocow write into the range [@offset, @offset + @len)
7171 *
7172 * @offset: File offset
7173 * @len: The length to write, will be updated to the nocow writeable
7174 * range
7175 * @orig_start: (optional) Return the original file offset of the file extent
7176 * @orig_len: (optional) Return the original on-disk length of the file extent
7177 * @ram_bytes: (optional) Return the ram_bytes of the file extent
a84d5d42
BB
7178 * @strict: if true, omit optimizations that might force us into unnecessary
7179 * cow. e.g., don't trust generation number.
e4ecaf90 7180 *
e4ecaf90
QW
7181 * Return:
7182 * >0 and update @len if we can do nocow write
7183 * 0 if we can't do nocow write
7184 * <0 if error happened
7185 *
7186 * NOTE: This only checks the file extents, caller is responsible to wait for
7187 * any ordered extents.
46bfbb5c 7188 */
00361589 7189noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7ee9e440 7190 u64 *orig_start, u64 *orig_block_len,
a84d5d42 7191 u64 *ram_bytes, bool strict)
46bfbb5c 7192{
2ff7e61e 7193 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
619104ba 7194 struct can_nocow_file_extent_args nocow_args = { 0 };
46bfbb5c
CM
7195 struct btrfs_path *path;
7196 int ret;
7197 struct extent_buffer *leaf;
7198 struct btrfs_root *root = BTRFS_I(inode)->root;
7b2b7085 7199 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
46bfbb5c
CM
7200 struct btrfs_file_extent_item *fi;
7201 struct btrfs_key key;
46bfbb5c 7202 int found_type;
e77751aa 7203
46bfbb5c
CM
7204 path = btrfs_alloc_path();
7205 if (!path)
7206 return -ENOMEM;
7207
f85b7379
DS
7208 ret = btrfs_lookup_file_extent(NULL, root, path,
7209 btrfs_ino(BTRFS_I(inode)), offset, 0);
46bfbb5c
CM
7210 if (ret < 0)
7211 goto out;
7212
46bfbb5c 7213 if (ret == 1) {
619104ba 7214 if (path->slots[0] == 0) {
46bfbb5c
CM
7215 /* can't find the item, must cow */
7216 ret = 0;
7217 goto out;
7218 }
619104ba 7219 path->slots[0]--;
46bfbb5c
CM
7220 }
7221 ret = 0;
7222 leaf = path->nodes[0];
619104ba 7223 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4a0cc7ca 7224 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
46bfbb5c
CM
7225 key.type != BTRFS_EXTENT_DATA_KEY) {
7226 /* not our file or wrong item type, must cow */
7227 goto out;
7228 }
7229
7230 if (key.offset > offset) {
7231 /* Wrong offset, must cow */
7232 goto out;
7233 }
7234
619104ba 7235 if (btrfs_file_extent_end(path) <= offset)
7ee9e440
JB
7236 goto out;
7237
619104ba
FM
7238 fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
7239 found_type = btrfs_file_extent_type(leaf, fi);
7240 if (ram_bytes)
7241 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
e77751aa 7242
619104ba
FM
7243 nocow_args.start = offset;
7244 nocow_args.end = offset + *len - 1;
7245 nocow_args.strict = strict;
7246 nocow_args.free_path = true;
7ee9e440 7247
619104ba
FM
7248 ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args);
7249 /* can_nocow_file_extent() has freed the path. */
7250 path = NULL;
7ee9e440 7251
619104ba
FM
7252 if (ret != 1) {
7253 /* Treat errors as not being able to NOCOW. */
7254 ret = 0;
78d4295b 7255 goto out;
7ee9e440 7256 }
eb384b55 7257
619104ba
FM
7258 ret = 0;
7259 if (btrfs_extent_readonly(fs_info, nocow_args.disk_bytenr))
46bfbb5c 7260 goto out;
7b2b7085 7261
619104ba
FM
7262 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7263 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7b2b7085
MX
7264 u64 range_end;
7265
619104ba 7266 range_end = round_up(offset + nocow_args.num_bytes,
da17066c 7267 root->fs_info->sectorsize) - 1;
7b2b7085
MX
7268 ret = test_range_bit(io_tree, offset, range_end,
7269 EXTENT_DELALLOC, 0, NULL);
7270 if (ret) {
7271 ret = -EAGAIN;
7272 goto out;
7273 }
7274 }
7275
619104ba
FM
7276 if (orig_start)
7277 *orig_start = key.offset - nocow_args.extent_offset;
7278 if (orig_block_len)
7279 *orig_block_len = nocow_args.disk_num_bytes;
00361589 7280
619104ba 7281 *len = nocow_args.num_bytes;
46bfbb5c
CM
7282 ret = 1;
7283out:
7284 btrfs_free_path(path);
7285 return ret;
7286}
7287
eb838e73 7288static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
59094403
FM
7289 struct extent_state **cached_state,
7290 unsigned int iomap_flags)
eb838e73 7291{
59094403
FM
7292 const bool writing = (iomap_flags & IOMAP_WRITE);
7293 const bool nowait = (iomap_flags & IOMAP_NOWAIT);
7294 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
eb838e73
JB
7295 struct btrfs_ordered_extent *ordered;
7296 int ret = 0;
7297
7298 while (1) {
59094403
FM
7299 if (nowait) {
7300 if (!try_lock_extent(io_tree, lockstart, lockend))
7301 return -EAGAIN;
7302 } else {
7303 lock_extent_bits(io_tree, lockstart, lockend, cached_state);
7304 }
eb838e73
JB
7305 /*
7306 * We're concerned with the entire range that we're going to be
01327610 7307 * doing DIO to, so we need to make sure there's no ordered
eb838e73
JB
7308 * extents in this range.
7309 */
a776c6fa 7310 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
eb838e73
JB
7311 lockend - lockstart + 1);
7312
7313 /*
7314 * We need to make sure there are no buffered pages in this
7315 * range either, we could have raced between the invalidate in
7316 * generic_file_direct_write and locking the extent. The
7317 * invalidate needs to happen so that reads after a write do not
7318 * get stale data.
7319 */
fc4adbff 7320 if (!ordered &&
051c98eb
DS
7321 (!writing || !filemap_range_has_page(inode->i_mapping,
7322 lockstart, lockend)))
eb838e73
JB
7323 break;
7324
59094403 7325 unlock_extent_cached(io_tree, lockstart, lockend, cached_state);
eb838e73
JB
7326
7327 if (ordered) {
59094403
FM
7328 if (nowait) {
7329 btrfs_put_ordered_extent(ordered);
7330 ret = -EAGAIN;
7331 break;
7332 }
ade77029
FM
7333 /*
7334 * If we are doing a DIO read and the ordered extent we
7335 * found is for a buffered write, we can not wait for it
7336 * to complete and retry, because if we do so we can
7337 * deadlock with concurrent buffered writes on page
7338 * locks. This happens only if our DIO read covers more
7339 * than one extent map, if at this point has already
7340 * created an ordered extent for a previous extent map
7341 * and locked its range in the inode's io tree, and a
7342 * concurrent write against that previous extent map's
7343 * range and this range started (we unlock the ranges
7344 * in the io tree only when the bios complete and
7345 * buffered writes always lock pages before attempting
7346 * to lock range in the io tree).
7347 */
7348 if (writing ||
7349 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
c0a43603 7350 btrfs_start_ordered_extent(ordered, 1);
ade77029 7351 else
59094403 7352 ret = nowait ? -EAGAIN : -ENOTBLK;
eb838e73
JB
7353 btrfs_put_ordered_extent(ordered);
7354 } else {
eb838e73 7355 /*
b850ae14
FM
7356 * We could trigger writeback for this range (and wait
7357 * for it to complete) and then invalidate the pages for
7358 * this range (through invalidate_inode_pages2_range()),
7359 * but that can lead us to a deadlock with a concurrent
ba206a02 7360 * call to readahead (a buffered read or a defrag call
b850ae14
FM
7361 * triggered a readahead) on a page lock due to an
7362 * ordered dio extent we created before but did not have
7363 * yet a corresponding bio submitted (whence it can not
ba206a02 7364 * complete), which makes readahead wait for that
b850ae14
FM
7365 * ordered extent to complete while holding a lock on
7366 * that page.
eb838e73 7367 */
59094403 7368 ret = nowait ? -EAGAIN : -ENOTBLK;
eb838e73
JB
7369 }
7370
ade77029
FM
7371 if (ret)
7372 break;
7373
eb838e73
JB
7374 cond_resched();
7375 }
7376
7377 return ret;
7378}
7379
6f9994db 7380/* The callers of this must take lock_extent() */
4b67c11d
NB
7381static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7382 u64 len, u64 orig_start, u64 block_start,
6f9994db
LB
7383 u64 block_len, u64 orig_block_len,
7384 u64 ram_bytes, int compress_type,
7385 int type)
69ffb543
JB
7386{
7387 struct extent_map_tree *em_tree;
7388 struct extent_map *em;
69ffb543
JB
7389 int ret;
7390
6f9994db
LB
7391 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7392 type == BTRFS_ORDERED_COMPRESSED ||
7393 type == BTRFS_ORDERED_NOCOW ||
1af4a0aa 7394 type == BTRFS_ORDERED_REGULAR);
6f9994db 7395
4b67c11d 7396 em_tree = &inode->extent_tree;
69ffb543
JB
7397 em = alloc_extent_map();
7398 if (!em)
7399 return ERR_PTR(-ENOMEM);
7400
7401 em->start = start;
7402 em->orig_start = orig_start;
7403 em->len = len;
7404 em->block_len = block_len;
7405 em->block_start = block_start;
b4939680 7406 em->orig_block_len = orig_block_len;
cc95bef6 7407 em->ram_bytes = ram_bytes;
70c8a91c 7408 em->generation = -1;
69ffb543 7409 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1af4a0aa 7410 if (type == BTRFS_ORDERED_PREALLOC) {
b11e234d 7411 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1af4a0aa 7412 } else if (type == BTRFS_ORDERED_COMPRESSED) {
6f9994db
LB
7413 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7414 em->compress_type = compress_type;
7415 }
69ffb543
JB
7416
7417 do {
4b67c11d
NB
7418 btrfs_drop_extent_cache(inode, em->start,
7419 em->start + em->len - 1, 0);
69ffb543 7420 write_lock(&em_tree->lock);
09a2a8f9 7421 ret = add_extent_mapping(em_tree, em, 1);
69ffb543 7422 write_unlock(&em_tree->lock);
6f9994db
LB
7423 /*
7424 * The caller has taken lock_extent(), who could race with us
7425 * to add em?
7426 */
69ffb543
JB
7427 } while (ret == -EEXIST);
7428
7429 if (ret) {
7430 free_extent_map(em);
7431 return ERR_PTR(ret);
7432 }
7433
6f9994db 7434 /* em got 2 refs now, callers needs to do free_extent_map once. */
69ffb543
JB
7435 return em;
7436}
7437
1c8d0175 7438
c5794e51 7439static int btrfs_get_blocks_direct_write(struct extent_map **map,
c5794e51
NB
7440 struct inode *inode,
7441 struct btrfs_dio_data *dio_data,
d7a8ab4e
FM
7442 u64 start, u64 len,
7443 unsigned int iomap_flags)
c5794e51 7444{
d4135134 7445 const bool nowait = (iomap_flags & IOMAP_NOWAIT);
c5794e51
NB
7446 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7447 struct extent_map *em = *map;
f0bfa76a
FM
7448 int type;
7449 u64 block_start, orig_start, orig_block_len, ram_bytes;
2306e83e 7450 struct btrfs_block_group *bg;
f0bfa76a
FM
7451 bool can_nocow = false;
7452 bool space_reserved = false;
6d82ad13 7453 u64 prev_len;
c5794e51
NB
7454 int ret = 0;
7455
7456 /*
7457 * We don't allocate a new extent in the following cases
7458 *
7459 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7460 * existing extent.
7461 * 2) The extent is marked as PREALLOC. We're good to go here and can
7462 * just use the extent.
7463 *
7464 */
7465 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7466 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7467 em->block_start != EXTENT_MAP_HOLE)) {
c5794e51
NB
7468 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7469 type = BTRFS_ORDERED_PREALLOC;
7470 else
7471 type = BTRFS_ORDERED_NOCOW;
7472 len = min(len, em->len - (start - em->start));
7473 block_start = em->block_start + (start - em->start);
7474
7475 if (can_nocow_extent(inode, start, &len, &orig_start,
2306e83e
FM
7476 &orig_block_len, &ram_bytes, false) == 1) {
7477 bg = btrfs_inc_nocow_writers(fs_info, block_start);
7478 if (bg)
7479 can_nocow = true;
7480 }
f0bfa76a 7481 }
c5794e51 7482
6d82ad13 7483 prev_len = len;
f0bfa76a
FM
7484 if (can_nocow) {
7485 struct extent_map *em2;
7486
7487 /* We can NOCOW, so only need to reserve metadata space. */
d4135134
FM
7488 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7489 nowait);
f0bfa76a
FM
7490 if (ret < 0) {
7491 /* Our caller expects us to free the input extent map. */
7492 free_extent_map(em);
7493 *map = NULL;
2306e83e 7494 btrfs_dec_nocow_writers(bg);
d4135134
FM
7495 if (nowait && (ret == -ENOSPC || ret == -EDQUOT))
7496 ret = -EAGAIN;
f0bfa76a
FM
7497 goto out;
7498 }
7499 space_reserved = true;
7500
7501 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
7502 orig_start, block_start,
7503 len, orig_block_len,
7504 ram_bytes, type);
2306e83e 7505 btrfs_dec_nocow_writers(bg);
f0bfa76a
FM
7506 if (type == BTRFS_ORDERED_PREALLOC) {
7507 free_extent_map(em);
7508 *map = em = em2;
7509 }
c5794e51 7510
f0bfa76a
FM
7511 if (IS_ERR(em2)) {
7512 ret = PTR_ERR(em2);
7513 goto out;
c5794e51 7514 }
f5585f4f
FM
7515
7516 dio_data->nocow_done = true;
f0bfa76a 7517 } else {
f0bfa76a
FM
7518 /* Our caller expects us to free the input extent map. */
7519 free_extent_map(em);
7520 *map = NULL;
7521
d4135134 7522 if (nowait)
d7a8ab4e
FM
7523 return -EAGAIN;
7524
f5585f4f
FM
7525 /*
7526 * If we could not allocate data space before locking the file
7527 * range and we can't do a NOCOW write, then we have to fail.
7528 */
7529 if (!dio_data->data_space_reserved)
7530 return -ENOSPC;
7531
7532 /*
7533 * We have to COW and we have already reserved data space before,
7534 * so now we reserve only metadata.
7535 */
7536 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7537 false);
f0bfa76a
FM
7538 if (ret < 0)
7539 goto out;
7540 space_reserved = true;
7541
7542 em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
7543 if (IS_ERR(em)) {
7544 ret = PTR_ERR(em);
7545 goto out;
7546 }
7547 *map = em;
7548 len = min(len, em->len - (start - em->start));
7549 if (len < prev_len)
f5585f4f
FM
7550 btrfs_delalloc_release_metadata(BTRFS_I(inode),
7551 prev_len - len, true);
c5794e51
NB
7552 }
7553
f0bfa76a
FM
7554 /*
7555 * We have created our ordered extent, so we can now release our reservation
7556 * for an outstanding extent.
7557 */
6d82ad13 7558 btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len);
c5794e51 7559
c5794e51
NB
7560 /*
7561 * Need to update the i_size under the extent lock so buffered
7562 * readers will get the updated i_size when we unlock.
7563 */
f85781fb 7564 if (start + len > i_size_read(inode))
c5794e51 7565 i_size_write(inode, start + len);
c5794e51 7566out:
f0bfa76a
FM
7567 if (ret && space_reserved) {
7568 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
f5585f4f 7569 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true);
f0bfa76a 7570 }
c5794e51
NB
7571 return ret;
7572}
7573
f85781fb
GR
7574static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7575 loff_t length, unsigned int flags, struct iomap *iomap,
7576 struct iomap *srcmap)
4b46fce2 7577{
491a6d01 7578 struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
0b246afa 7579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4b46fce2 7580 struct extent_map *em;
eb838e73 7581 struct extent_state *cached_state = NULL;
491a6d01 7582 struct btrfs_dio_data *dio_data = iter->private;
eb838e73 7583 u64 lockstart, lockend;
f85781fb 7584 const bool write = !!(flags & IOMAP_WRITE);
0934856d 7585 int ret = 0;
f85781fb 7586 u64 len = length;
f5585f4f 7587 const u64 data_alloc_len = length;
f85781fb 7588 bool unlock_extents = false;
eb838e73 7589
f85781fb 7590 if (!write)
0b246afa 7591 len = min_t(u64, len, fs_info->sectorsize);
eb838e73 7592
c329861d
JB
7593 lockstart = start;
7594 lockend = start + len - 1;
7595
f85781fb 7596 /*
b023e675
FM
7597 * iomap_dio_rw() only does filemap_write_and_wait_range(), which isn't
7598 * enough if we've written compressed pages to this area, so we need to
7599 * flush the dirty pages again to make absolutely sure that any
7600 * outstanding dirty pages are on disk - the first flush only starts
7601 * compression on the data, while keeping the pages locked, so by the
7602 * time the second flush returns we know bios for the compressed pages
7603 * were submitted and finished, and the pages no longer under writeback.
7604 *
7605 * If we have a NOWAIT request and we have any pages in the range that
7606 * are locked, likely due to compression still in progress, we don't want
7607 * to block on page locks. We also don't want to block on pages marked as
7608 * dirty or under writeback (same as for the non-compression case).
7609 * iomap_dio_rw() did the same check, but after that and before we got
7610 * here, mmap'ed writes may have happened or buffered reads started
7611 * (readpage() and readahead(), which lock pages), as we haven't locked
7612 * the file range yet.
f85781fb
GR
7613 */
7614 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7615 &BTRFS_I(inode)->runtime_flags)) {
b023e675
FM
7616 if (flags & IOMAP_NOWAIT) {
7617 if (filemap_range_needs_writeback(inode->i_mapping,
7618 lockstart, lockend))
7619 return -EAGAIN;
7620 } else {
7621 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7622 start + length - 1);
7623 if (ret)
7624 return ret;
7625 }
f85781fb
GR
7626 }
7627
491a6d01 7628 memset(dio_data, 0, sizeof(*dio_data));
f85781fb 7629
f5585f4f
FM
7630 /*
7631 * We always try to allocate data space and must do it before locking
7632 * the file range, to avoid deadlocks with concurrent writes to the same
7633 * range if the range has several extents and the writes don't expand the
7634 * current i_size (the inode lock is taken in shared mode). If we fail to
7635 * allocate data space here we continue and later, after locking the
7636 * file range, we fail with ENOSPC only if we figure out we can not do a
7637 * NOCOW write.
7638 */
7639 if (write && !(flags & IOMAP_NOWAIT)) {
7640 ret = btrfs_check_data_free_space(BTRFS_I(inode),
7641 &dio_data->data_reserved,
7642 start, data_alloc_len);
7643 if (!ret)
7644 dio_data->data_space_reserved = true;
7645 else if (ret && !(BTRFS_I(inode)->flags &
7646 (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
7647 goto err;
7648 }
e1cbbfa5 7649
eb838e73
JB
7650 /*
7651 * If this errors out it's because we couldn't invalidate pagecache for
59094403
FM
7652 * this range and we need to fallback to buffered IO, or we are doing a
7653 * NOWAIT read/write and we need to block.
eb838e73 7654 */
59094403
FM
7655 ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags);
7656 if (ret < 0)
9c9464cc 7657 goto err;
eb838e73 7658
39b07b5d 7659 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
eb838e73
JB
7660 if (IS_ERR(em)) {
7661 ret = PTR_ERR(em);
7662 goto unlock_err;
7663 }
4b46fce2
JB
7664
7665 /*
7666 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7667 * io. INLINE is special, and we could probably kludge it in here, but
7668 * it's still buffered so for safety lets just fall back to the generic
7669 * buffered path.
7670 *
7671 * For COMPRESSED we _have_ to read the entire extent in so we can
7672 * decompress it, so there will be buffering required no matter what we
7673 * do, so go ahead and fallback to buffered.
7674 *
01327610 7675 * We return -ENOTBLK because that's what makes DIO go ahead and go back
4b46fce2
JB
7676 * to buffered IO. Don't blame me, this is the price we pay for using
7677 * the generic code.
7678 */
7679 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7680 em->block_start == EXTENT_MAP_INLINE) {
7681 free_extent_map(em);
eb838e73
JB
7682 ret = -ENOTBLK;
7683 goto unlock_err;
4b46fce2
JB
7684 }
7685
f85781fb 7686 len = min(len, em->len - (start - em->start));
ca93e44b
FM
7687
7688 /*
7689 * If we have a NOWAIT request and the range contains multiple extents
7690 * (or a mix of extents and holes), then we return -EAGAIN to make the
7691 * caller fallback to a context where it can do a blocking (without
7692 * NOWAIT) request. This way we avoid doing partial IO and returning
7693 * success to the caller, which is not optimal for writes and for reads
7694 * it can result in unexpected behaviour for an application.
7695 *
7696 * When doing a read, because we use IOMAP_DIO_PARTIAL when calling
7697 * iomap_dio_rw(), we can end up returning less data then what the caller
7698 * asked for, resulting in an unexpected, and incorrect, short read.
7699 * That is, the caller asked to read N bytes and we return less than that,
7700 * which is wrong unless we are crossing EOF. This happens if we get a
7701 * page fault error when trying to fault in pages for the buffer that is
7702 * associated to the struct iov_iter passed to iomap_dio_rw(), and we
7703 * have previously submitted bios for other extents in the range, in
7704 * which case iomap_dio_rw() may return us EIOCBQUEUED if not all of
7705 * those bios have completed by the time we get the page fault error,
7706 * which we return back to our caller - we should only return EIOCBQUEUED
7707 * after we have submitted bios for all the extents in the range.
7708 */
7709 if ((flags & IOMAP_NOWAIT) && len < length) {
7710 free_extent_map(em);
7711 ret = -EAGAIN;
7712 goto unlock_err;
7713 }
7714
f85781fb
GR
7715 if (write) {
7716 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
d7a8ab4e 7717 start, len, flags);
c5794e51
NB
7718 if (ret < 0)
7719 goto unlock_err;
f85781fb
GR
7720 unlock_extents = true;
7721 /* Recalc len in case the new em is smaller than requested */
7722 len = min(len, em->len - (start - em->start));
f5585f4f
FM
7723 if (dio_data->data_space_reserved) {
7724 u64 release_offset;
7725 u64 release_len = 0;
7726
7727 if (dio_data->nocow_done) {
7728 release_offset = start;
7729 release_len = data_alloc_len;
7730 } else if (len < data_alloc_len) {
7731 release_offset = start + len;
7732 release_len = data_alloc_len - len;
7733 }
7734
7735 if (release_len > 0)
7736 btrfs_free_reserved_data_space(BTRFS_I(inode),
7737 dio_data->data_reserved,
7738 release_offset,
7739 release_len);
7740 }
c5794e51 7741 } else {
1c8d0175
NB
7742 /*
7743 * We need to unlock only the end area that we aren't using.
7744 * The rest is going to be unlocked by the endio routine.
7745 */
f85781fb
GR
7746 lockstart = start + len;
7747 if (lockstart < lockend)
7748 unlock_extents = true;
7749 }
7750
7751 if (unlock_extents)
7752 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7753 lockstart, lockend, &cached_state);
7754 else
7755 free_extent_state(cached_state);
7756
7757 /*
7758 * Translate extent map information to iomap.
7759 * We trim the extents (and move the addr) even though iomap code does
7760 * that, since we have locked only the parts we are performing I/O in.
7761 */
7762 if ((em->block_start == EXTENT_MAP_HOLE) ||
7763 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7764 iomap->addr = IOMAP_NULL_ADDR;
7765 iomap->type = IOMAP_HOLE;
7766 } else {
7767 iomap->addr = em->block_start + (start - em->start);
7768 iomap->type = IOMAP_MAPPED;
a43a67a2 7769 }
f85781fb 7770 iomap->offset = start;
d24fa5c1 7771 iomap->bdev = fs_info->fs_devices->latest_dev->bdev;
f85781fb 7772 iomap->length = len;
a43a67a2 7773
e380adfc 7774 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
544d24f9
NA
7775 iomap->flags |= IOMAP_F_ZONE_APPEND;
7776
4b46fce2
JB
7777 free_extent_map(em);
7778
7779 return 0;
eb838e73
JB
7780
7781unlock_err:
e182163d
OS
7782 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7783 &cached_state);
9c9464cc 7784err:
f5585f4f
FM
7785 if (dio_data->data_space_reserved) {
7786 btrfs_free_reserved_data_space(BTRFS_I(inode),
7787 dio_data->data_reserved,
7788 start, data_alloc_len);
7789 extent_changeset_free(dio_data->data_reserved);
7790 }
7791
f85781fb
GR
7792 return ret;
7793}
7794
7795static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7796 ssize_t written, unsigned int flags, struct iomap *iomap)
7797{
491a6d01
CH
7798 struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
7799 struct btrfs_dio_data *dio_data = iter->private;
f85781fb
GR
7800 size_t submitted = dio_data->submitted;
7801 const bool write = !!(flags & IOMAP_WRITE);
491a6d01 7802 int ret = 0;
f85781fb
GR
7803
7804 if (!write && (iomap->type == IOMAP_HOLE)) {
7805 /* If reading from a hole, unlock and return */
7806 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
491a6d01 7807 return 0;
f85781fb
GR
7808 }
7809
7810 if (submitted < length) {
7811 pos += submitted;
7812 length -= submitted;
7813 if (write)
7814 __endio_write_update_ordered(BTRFS_I(inode), pos,
7815 length, false);
7816 else
7817 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7818 pos + length - 1);
7819 ret = -ENOTBLK;
7820 }
7821
f0bfa76a 7822 if (write)
f85781fb 7823 extent_changeset_free(dio_data->data_reserved);
8b110e39
MX
7824 return ret;
7825}
7826
769b4f24 7827static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8b110e39 7828{
769b4f24
OS
7829 /*
7830 * This implies a barrier so that stores to dio_bio->bi_status before
7831 * this and loads of dio_bio->bi_status after this are fully ordered.
7832 */
7833 if (!refcount_dec_and_test(&dip->refs))
7834 return;
8b110e39 7835
642c5d34 7836 if (btrfs_op(&dip->bio) == BTRFS_MAP_WRITE) {
b672b5c1 7837 __endio_write_update_ordered(BTRFS_I(dip->inode),
47926ab5 7838 dip->file_offset,
769b4f24 7839 dip->bytes,
642c5d34 7840 !dip->bio.bi_status);
769b4f24
OS
7841 } else {
7842 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
47926ab5
QW
7843 dip->file_offset,
7844 dip->file_offset + dip->bytes - 1);
8b110e39
MX
7845 }
7846
642c5d34
CH
7847 kfree(dip->csums);
7848 bio_endio(&dip->bio);
8b110e39
MX
7849}
7850
ad357938 7851static void submit_dio_repair_bio(struct inode *inode, struct bio *bio,
cb3a12d9
DS
7852 int mirror_num,
7853 enum btrfs_compression_type compress_type)
8b110e39 7854{
77d5d689 7855 struct btrfs_dio_private *dip = bio->bi_private;
2ff7e61e 7856 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8b110e39 7857
37226b21 7858 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
8b110e39 7859
ad357938
CH
7860 if (btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA))
7861 return;
8b110e39 7862
77d5d689 7863 refcount_inc(&dip->refs);
ad357938 7864 if (btrfs_map_bio(fs_info, bio, mirror_num))
fd9d6670 7865 refcount_dec(&dip->refs);
8b110e39
MX
7866}
7867
f4f39fc5 7868static blk_status_t btrfs_check_read_dio_bio(struct btrfs_dio_private *dip,
c3a3b19b 7869 struct btrfs_bio *bbio,
fd9d6670 7870 const bool uptodate)
4b46fce2 7871{
f4f39fc5 7872 struct inode *inode = dip->inode;
fd9d6670
OS
7873 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7874 const u32 sectorsize = fs_info->sectorsize;
7875 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
7876 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7877 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
17347cec
LB
7878 struct bio_vec bvec;
7879 struct bvec_iter iter;
7ffd27e3 7880 u32 bio_offset = 0;
58efbc9f 7881 blk_status_t err = BLK_STS_OK;
4b46fce2 7882
c3a3b19b 7883 __bio_for_each_segment(bvec, &bbio->bio, iter, bbio->iter) {
fd9d6670 7884 unsigned int i, nr_sectors, pgoff;
8b110e39 7885
17347cec
LB
7886 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7887 pgoff = bvec.bv_offset;
fd9d6670 7888 for (i = 0; i < nr_sectors; i++) {
00d82525
CH
7889 u64 start = bbio->file_offset + bio_offset;
7890
97bf5a55 7891 ASSERT(pgoff < PAGE_SIZE);
fd9d6670 7892 if (uptodate &&
c3a3b19b 7893 (!csum || !check_data_csum(inode, bbio,
c1d6abda
OS
7894 bio_offset, bvec.bv_page,
7895 pgoff, start))) {
fd9d6670
OS
7896 clean_io_failure(fs_info, failure_tree, io_tree,
7897 start, bvec.bv_page,
7898 btrfs_ino(BTRFS_I(inode)),
7899 pgoff);
7900 } else {
150e4b05 7901 int ret;
fd9d6670 7902
00d82525
CH
7903 ret = btrfs_repair_one_sector(inode, &bbio->bio,
7904 bio_offset, bvec.bv_page, pgoff,
c3a3b19b 7905 start, bbio->mirror_num,
150e4b05
QW
7906 submit_dio_repair_bio);
7907 if (ret)
7908 err = errno_to_blk_status(ret);
fd9d6670 7909 }
7ffd27e3
QW
7910 ASSERT(bio_offset + sectorsize > bio_offset);
7911 bio_offset += sectorsize;
2dabb324 7912 pgoff += sectorsize;
2dabb324 7913 }
2c30c71b 7914 }
c1dc0896
MX
7915 return err;
7916}
7917
b672b5c1 7918static void __endio_write_update_ordered(struct btrfs_inode *inode,
52427260
QW
7919 const u64 offset, const u64 bytes,
7920 const bool uptodate)
4b46fce2 7921{
e65f152e
QW
7922 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
7923 finish_ordered_fn, uptodate);
14543774
FM
7924}
7925
8896a08d 7926static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
1941b64b
QW
7927 struct bio *bio,
7928 u64 dio_file_offset)
eaf25d93 7929{
e331f6b1 7930 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, false);
eaf25d93
CM
7931}
7932
4246a0b6 7933static void btrfs_end_dio_bio(struct bio *bio)
e65e1535
MX
7934{
7935 struct btrfs_dio_private *dip = bio->bi_private;
0fdf977d 7936 struct btrfs_bio *bbio = btrfs_bio(bio);
4e4cbee9 7937 blk_status_t err = bio->bi_status;
e65e1535 7938
8b110e39
MX
7939 if (err)
7940 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
6296b960 7941 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
f85b7379 7942 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
1201b58b 7943 bio->bi_opf, bio->bi_iter.bi_sector,
8b110e39
MX
7944 bio->bi_iter.bi_size, err);
7945
f4f39fc5 7946 if (bio_op(bio) == REQ_OP_READ)
0fdf977d 7947 err = btrfs_check_read_dio_bio(dip, bbio, !err);
e65e1535 7948
769b4f24 7949 if (err)
642c5d34 7950 dip->bio.bi_status = err;
e65e1535 7951
0fdf977d 7952 btrfs_record_physical_zoned(dip->inode, bbio->file_offset, bio);
544d24f9 7953
e65e1535 7954 bio_put(bio);
769b4f24 7955 btrfs_dio_private_put(dip);
c1dc0896
MX
7956}
7957
d0ee3934
DS
7958static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7959 struct inode *inode, u64 file_offset, int async_submit)
e65e1535 7960{
0b246afa 7961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
facc8a22 7962 struct btrfs_dio_private *dip = bio->bi_private;
cfe94440 7963 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
4e4cbee9 7964 blk_status_t ret;
e65e1535 7965
4c274bc6 7966 /* Check btrfs_submit_bio_hook() for rules about async submit. */
b812ce28
JB
7967 if (async_submit)
7968 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7969
5fd02043 7970 if (!write) {
0b246afa 7971 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
5fd02043
JB
7972 if (ret)
7973 goto err;
7974 }
e65e1535 7975
e6961cac 7976 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1ae39938
JB
7977 goto map;
7978
7979 if (write && async_submit) {
a6f5e39e 7980 ret = btrfs_wq_submit_bio(inode, bio, 0, file_offset,
e288c080 7981 btrfs_submit_bio_start_direct_io);
e65e1535 7982 goto err;
1ae39938
JB
7983 } else if (write) {
7984 /*
7985 * If we aren't doing async submit, calculate the csum of the
7986 * bio now.
7987 */
e331f6b1 7988 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, false);
1ae39938
JB
7989 if (ret)
7990 goto err;
23ea8e5a 7991 } else {
85879573
OS
7992 u64 csum_offset;
7993
47926ab5 7994 csum_offset = file_offset - dip->file_offset;
265fdfa6 7995 csum_offset >>= fs_info->sectorsize_bits;
55fc29be 7996 csum_offset *= fs_info->csum_size;
c3a3b19b 7997 btrfs_bio(bio)->csum = dip->csums + csum_offset;
c2db1073 7998 }
1ae39938 7999map:
08635bae 8000 ret = btrfs_map_bio(fs_info, bio, 0);
e65e1535 8001err:
e65e1535
MX
8002 return ret;
8003}
8004
3e08773c 8005static void btrfs_submit_direct(const struct iomap_iter *iter,
f85781fb 8006 struct bio *dio_bio, loff_t file_offset)
c36cac28 8007{
642c5d34
CH
8008 struct btrfs_dio_private *dip =
8009 container_of(dio_bio, struct btrfs_dio_private, bio);
a6d3d495 8010 struct inode *inode = iter->inode;
cfe94440 8011 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
0b246afa 8012 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
769b4f24
OS
8013 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8014 BTRFS_BLOCK_GROUP_RAID56_MASK);
e65e1535 8015 struct bio *bio;
c36cac28 8016 u64 start_sector;
1ae39938 8017 int async_submit = 0;
725130ba 8018 u64 submit_len;
42b5d73b
NA
8019 u64 clone_offset = 0;
8020 u64 clone_len;
42034313 8021 u64 logical;
5f4dc8fc 8022 int ret;
58efbc9f 8023 blk_status_t status;
89b798ad 8024 struct btrfs_io_geometry geom;
491a6d01 8025 struct btrfs_dio_data *dio_data = iter->private;
42034313 8026 struct extent_map *em = NULL;
e65e1535 8027
642c5d34
CH
8028 dip->inode = inode;
8029 dip->file_offset = file_offset;
8030 dip->bytes = dio_bio->bi_iter.bi_size;
8031 refcount_set(&dip->refs, 1);
8032 dip->csums = NULL;
8033
8034 if (!write && !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
8035 unsigned int nr_sectors =
8036 (dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits);
facc8a22 8037
85879573
OS
8038 /*
8039 * Load the csums up front to reduce csum tree searches and
8040 * contention when submitting bios.
8041 */
642c5d34
CH
8042 status = BLK_STS_RESOURCE;
8043 dip->csums = kcalloc(nr_sectors, fs_info->csum_size, GFP_NOFS);
8044 if (!dip)
8045 goto out_err;
8046
6275193e 8047 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
85879573
OS
8048 if (status != BLK_STS_OK)
8049 goto out_err;
02f57c7a
JB
8050 }
8051
769b4f24
OS
8052 start_sector = dio_bio->bi_iter.bi_sector;
8053 submit_len = dio_bio->bi_iter.bi_size;
53b381b3 8054
3c91ee69 8055 do {
42034313
MR
8056 logical = start_sector << 9;
8057 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8058 if (IS_ERR(em)) {
8059 status = errno_to_blk_status(PTR_ERR(em));
8060 em = NULL;
8061 goto out_err_em;
8062 }
8063 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
43c0d1a5 8064 logical, &geom);
769b4f24
OS
8065 if (ret) {
8066 status = errno_to_blk_status(ret);
42034313 8067 goto out_err_em;
769b4f24 8068 }
769b4f24 8069
42b5d73b
NA
8070 clone_len = min(submit_len, geom.len);
8071 ASSERT(clone_len <= UINT_MAX);
02f57c7a 8072
725130ba
LB
8073 /*
8074 * This will never fail as it's passing GPF_NOFS and
8075 * the allocation is backed by btrfs_bioset.
8076 */
769b4f24 8077 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
725130ba
LB
8078 bio->bi_private = dip;
8079 bio->bi_end_io = btrfs_end_dio_bio;
00d82525 8080 btrfs_bio(bio)->file_offset = file_offset;
725130ba 8081
544d24f9
NA
8082 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8083 status = extract_ordered_extent(BTRFS_I(inode), bio,
8084 file_offset);
8085 if (status) {
8086 bio_put(bio);
8087 goto out_err;
8088 }
8089 }
8090
725130ba
LB
8091 ASSERT(submit_len >= clone_len);
8092 submit_len -= clone_len;
e65e1535 8093
725130ba
LB
8094 /*
8095 * Increase the count before we submit the bio so we know
8096 * the end IO handler won't happen before we increase the
8097 * count. Otherwise, the dip might get freed before we're
8098 * done setting it up.
769b4f24
OS
8099 *
8100 * We transfer the initial reference to the last bio, so we
8101 * don't need to increment the reference count for the last one.
725130ba 8102 */
769b4f24
OS
8103 if (submit_len > 0) {
8104 refcount_inc(&dip->refs);
8105 /*
8106 * If we are submitting more than one bio, submit them
8107 * all asynchronously. The exception is RAID 5 or 6, as
8108 * asynchronous checksums make it difficult to collect
8109 * full stripe writes.
8110 */
8111 if (!raid56)
8112 async_submit = 1;
8113 }
e65e1535 8114
d0ee3934 8115 status = btrfs_submit_dio_bio(bio, inode, file_offset,
58efbc9f
OS
8116 async_submit);
8117 if (status) {
725130ba 8118 bio_put(bio);
769b4f24
OS
8119 if (submit_len > 0)
8120 refcount_dec(&dip->refs);
42034313 8121 goto out_err_em;
725130ba 8122 }
e65e1535 8123
f85781fb 8124 dio_data->submitted += clone_len;
725130ba
LB
8125 clone_offset += clone_len;
8126 start_sector += clone_len >> 9;
8127 file_offset += clone_len;
42034313
MR
8128
8129 free_extent_map(em);
3c91ee69 8130 } while (submit_len > 0);
3e08773c 8131 return;
e65e1535 8132
42034313
MR
8133out_err_em:
8134 free_extent_map(em);
e65e1535 8135out_err:
642c5d34 8136 dio_bio->bi_status = status;
769b4f24 8137 btrfs_dio_private_put(dip);
4b46fce2
JB
8138}
8139
36e8c622 8140static const struct iomap_ops btrfs_dio_iomap_ops = {
f85781fb
GR
8141 .iomap_begin = btrfs_dio_iomap_begin,
8142 .iomap_end = btrfs_dio_iomap_end,
8143};
8144
36e8c622 8145static const struct iomap_dio_ops btrfs_dio_ops = {
f85781fb 8146 .submit_io = btrfs_submit_direct,
642c5d34 8147 .bio_set = &btrfs_dio_bioset,
f85781fb
GR
8148};
8149
36e8c622
CH
8150ssize_t btrfs_dio_rw(struct kiocb *iocb, struct iov_iter *iter, size_t done_before)
8151{
491a6d01
CH
8152 struct btrfs_dio_data data;
8153
36e8c622 8154 return iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops, &btrfs_dio_ops,
491a6d01 8155 IOMAP_DIO_PARTIAL, &data, done_before);
36e8c622
CH
8156}
8157
1506fcc8 8158static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
bab16e21 8159 u64 start, u64 len)
1506fcc8 8160{
05dadc09
TI
8161 int ret;
8162
45dd052e 8163 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
05dadc09
TI
8164 if (ret)
8165 return ret;
8166
facee0a0 8167 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
1506fcc8
YS
8168}
8169
a52d9a80 8170static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 8171{
be7bd730
JB
8172 struct inode *inode = page->mapping->host;
8173 int ret;
b888db2b
CM
8174
8175 if (current->flags & PF_MEMALLOC) {
8176 redirty_page_for_writepage(wbc, page);
8177 unlock_page(page);
8178 return 0;
8179 }
be7bd730
JB
8180
8181 /*
8182 * If we are under memory pressure we will call this directly from the
8183 * VM, we need to make sure we have the inode referenced for the ordered
8184 * extent. If not just return like we didn't do anything.
8185 */
8186 if (!igrab(inode)) {
8187 redirty_page_for_writepage(wbc, page);
8188 return AOP_WRITEPAGE_ACTIVATE;
8189 }
0a9b0e53 8190 ret = extent_write_full_page(page, wbc);
be7bd730
JB
8191 btrfs_add_delayed_iput(inode);
8192 return ret;
9ebefb18
CM
8193}
8194
48a3b636
ES
8195static int btrfs_writepages(struct address_space *mapping,
8196 struct writeback_control *wbc)
b293f02e 8197{
8ae225a8 8198 return extent_writepages(mapping, wbc);
b293f02e
CM
8199}
8200
ba206a02 8201static void btrfs_readahead(struct readahead_control *rac)
3ab2fb5a 8202{
ba206a02 8203 extent_readahead(rac);
3ab2fb5a 8204}
2a3ff0ad 8205
7c11d0ae 8206/*
895586eb
MWO
8207 * For releasepage() and invalidate_folio() we have a race window where
8208 * folio_end_writeback() is called but the subpage spinlock is not yet released.
7c11d0ae
QW
8209 * If we continue to release/invalidate the page, we could cause use-after-free
8210 * for subpage spinlock. So this function is to spin and wait for subpage
8211 * spinlock.
8212 */
8213static void wait_subpage_spinlock(struct page *page)
8214{
8215 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8216 struct btrfs_subpage *subpage;
8217
fbca46eb 8218 if (!btrfs_is_subpage(fs_info, page))
7c11d0ae
QW
8219 return;
8220
8221 ASSERT(PagePrivate(page) && page->private);
8222 subpage = (struct btrfs_subpage *)page->private;
8223
8224 /*
8225 * This may look insane as we just acquire the spinlock and release it,
8226 * without doing anything. But we just want to make sure no one is
8227 * still holding the subpage spinlock.
8228 * And since the page is not dirty nor writeback, and we have page
8229 * locked, the only possible way to hold a spinlock is from the endio
8230 * function to clear page writeback.
8231 *
8232 * Here we just acquire the spinlock so that all existing callers
8233 * should exit and we're safe to release/invalidate the page.
8234 */
8235 spin_lock_irq(&subpage->lock);
8236 spin_unlock_irq(&subpage->lock);
8237}
8238
e6dcd2dc 8239static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9ebefb18 8240{
477a30ba 8241 int ret = try_release_extent_mapping(page, gfp_flags);
7c11d0ae
QW
8242
8243 if (ret == 1) {
8244 wait_subpage_spinlock(page);
32443de3 8245 clear_page_extent_mapped(page);
7c11d0ae 8246 }
a52d9a80 8247 return ret;
39279cc3
CM
8248}
8249
e6dcd2dc
CM
8250static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8251{
98509cfc
CM
8252 if (PageWriteback(page) || PageDirty(page))
8253 return 0;
3ba7ab22 8254 return __btrfs_releasepage(page, gfp_flags);
e6dcd2dc
CM
8255}
8256
f8e66081
RG
8257#ifdef CONFIG_MIGRATION
8258static int btrfs_migratepage(struct address_space *mapping,
8259 struct page *newpage, struct page *page,
8260 enum migrate_mode mode)
8261{
8262 int ret;
8263
8264 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8265 if (ret != MIGRATEPAGE_SUCCESS)
8266 return ret;
8267
d1b89bc0
GJ
8268 if (page_has_private(page))
8269 attach_page_private(newpage, detach_page_private(page));
f8e66081 8270
f57ad937
QW
8271 if (PageOrdered(page)) {
8272 ClearPageOrdered(page);
8273 SetPageOrdered(newpage);
f8e66081
RG
8274 }
8275
8276 if (mode != MIGRATE_SYNC_NO_COPY)
8277 migrate_page_copy(newpage, page);
8278 else
8279 migrate_page_states(newpage, page);
8280 return MIGRATEPAGE_SUCCESS;
8281}
8282#endif
8283
895586eb
MWO
8284static void btrfs_invalidate_folio(struct folio *folio, size_t offset,
8285 size_t length)
39279cc3 8286{
895586eb 8287 struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
b945a463 8288 struct btrfs_fs_info *fs_info = inode->root->fs_info;
53ac7ead 8289 struct extent_io_tree *tree = &inode->io_tree;
2ac55d41 8290 struct extent_state *cached_state = NULL;
895586eb
MWO
8291 u64 page_start = folio_pos(folio);
8292 u64 page_end = page_start + folio_size(folio) - 1;
3b835840 8293 u64 cur;
53ac7ead 8294 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
39279cc3 8295
8b62b72b 8296 /*
895586eb
MWO
8297 * We have folio locked so no new ordered extent can be created on this
8298 * page, nor bio can be submitted for this folio.
8b62b72b 8299 *
895586eb
MWO
8300 * But already submitted bio can still be finished on this folio.
8301 * Furthermore, endio function won't skip folio which has Ordered
f57ad937 8302 * (Private2) already cleared, so it's possible for endio and
895586eb
MWO
8303 * invalidate_folio to do the same ordered extent accounting twice
8304 * on one folio.
266a2586
QW
8305 *
8306 * So here we wait for any submitted bios to finish, so that we won't
895586eb 8307 * do double ordered extent accounting on the same folio.
8b62b72b 8308 */
895586eb
MWO
8309 folio_wait_writeback(folio);
8310 wait_subpage_spinlock(&folio->page);
8b62b72b 8311
bcd77455
QW
8312 /*
8313 * For subpage case, we have call sites like
8314 * btrfs_punch_hole_lock_range() which passes range not aligned to
8315 * sectorsize.
895586eb
MWO
8316 * If the range doesn't cover the full folio, we don't need to and
8317 * shouldn't clear page extent mapped, as folio->private can still
bcd77455
QW
8318 * record subpage dirty bits for other part of the range.
8319 *
895586eb
MWO
8320 * For cases that invalidate the full folio even the range doesn't
8321 * cover the full folio, like invalidating the last folio, we're
bcd77455
QW
8322 * still safe to wait for ordered extent to finish.
8323 */
5a60542c 8324 if (!(offset == 0 && length == folio_size(folio))) {
895586eb 8325 btrfs_releasepage(&folio->page, GFP_NOFS);
e6dcd2dc
CM
8326 return;
8327 }
131e404a
FDBM
8328
8329 if (!inode_evicting)
ff13db41 8330 lock_extent_bits(tree, page_start, page_end, &cached_state);
951c80f8 8331
3b835840
QW
8332 cur = page_start;
8333 while (cur < page_end) {
8334 struct btrfs_ordered_extent *ordered;
8335 bool delete_states;
8336 u64 range_end;
b945a463 8337 u32 range_len;
3b835840
QW
8338
8339 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8340 page_end + 1 - cur);
8341 if (!ordered) {
8342 range_end = page_end;
8343 /*
8344 * No ordered extent covering this range, we are safe
8345 * to delete all extent states in the range.
8346 */
8347 delete_states = true;
8348 goto next;
8349 }
8350 if (ordered->file_offset > cur) {
8351 /*
8352 * There is a range between [cur, oe->file_offset) not
8353 * covered by any ordered extent.
8354 * We are safe to delete all extent states, and handle
8355 * the ordered extent in the next iteration.
8356 */
8357 range_end = ordered->file_offset - 1;
8358 delete_states = true;
8359 goto next;
8360 }
8361
8362 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8363 page_end);
b945a463
QW
8364 ASSERT(range_end + 1 - cur < U32_MAX);
8365 range_len = range_end + 1 - cur;
895586eb 8366 if (!btrfs_page_test_ordered(fs_info, &folio->page, cur, range_len)) {
3b835840 8367 /*
f57ad937
QW
8368 * If Ordered (Private2) is cleared, it means endio has
8369 * already been executed for the range.
3b835840
QW
8370 * We can't delete the extent states as
8371 * btrfs_finish_ordered_io() may still use some of them.
8372 */
8373 delete_states = false;
8374 goto next;
8375 }
895586eb 8376 btrfs_page_clear_ordered(fs_info, &folio->page, cur, range_len);
3b835840 8377
eb84ae03 8378 /*
2766ff61
FM
8379 * IO on this page will never be started, so we need to account
8380 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8381 * here, must leave that up for the ordered extent completion.
3b835840
QW
8382 *
8383 * This will also unlock the range for incoming
8384 * btrfs_finish_ordered_io().
eb84ae03 8385 */
131e404a 8386 if (!inode_evicting)
3b835840 8387 clear_extent_bit(tree, cur, range_end,
2766ff61 8388 EXTENT_DELALLOC |
131e404a 8389 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
ae0f1625 8390 EXTENT_DEFRAG, 1, 0, &cached_state);
3b835840
QW
8391
8392 spin_lock_irq(&inode->ordered_tree.lock);
8393 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8394 ordered->truncated_len = min(ordered->truncated_len,
8395 cur - ordered->file_offset);
8396 spin_unlock_irq(&inode->ordered_tree.lock);
8397
8398 if (btrfs_dec_test_ordered_pending(inode, &ordered,
f41b6ba9 8399 cur, range_end + 1 - cur)) {
3b835840
QW
8400 btrfs_finish_ordered_io(ordered);
8401 /*
8402 * The ordered extent has finished, now we're again
8403 * safe to delete all extent states of the range.
8404 */
8405 delete_states = true;
8406 } else {
8407 /*
8408 * btrfs_finish_ordered_io() will get executed by endio
8409 * of other pages, thus we can't delete extent states
8410 * anymore
8411 */
8412 delete_states = false;
8413 }
8414next:
8415 if (ordered)
8416 btrfs_put_ordered_extent(ordered);
8b62b72b 8417 /*
3b835840
QW
8418 * Qgroup reserved space handler
8419 * Sector(s) here will be either:
266a2586 8420 *
3b835840
QW
8421 * 1) Already written to disk or bio already finished
8422 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8423 * Qgroup will be handled by its qgroup_record then.
8424 * btrfs_qgroup_free_data() call will do nothing here.
8425 *
8426 * 2) Not written to disk yet
8427 * Then btrfs_qgroup_free_data() call will clear the
8428 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8429 * reserved data space.
8430 * Since the IO will never happen for this page.
8b62b72b 8431 */
3b835840 8432 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
131e404a 8433 if (!inode_evicting) {
3b835840
QW
8434 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
8435 EXTENT_DELALLOC | EXTENT_UPTODATE |
8436 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8437 delete_states, &cached_state);
131e404a 8438 }
3b835840 8439 cur = range_end + 1;
131e404a 8440 }
b9d0b389 8441 /*
3b835840 8442 * We have iterated through all ordered extents of the page, the page
f57ad937
QW
8443 * should not have Ordered (Private2) anymore, or the above iteration
8444 * did something wrong.
b9d0b389 8445 */
895586eb
MWO
8446 ASSERT(!folio_test_ordered(folio));
8447 btrfs_page_clear_checked(fs_info, &folio->page, folio_pos(folio), folio_size(folio));
3b835840 8448 if (!inode_evicting)
895586eb
MWO
8449 __btrfs_releasepage(&folio->page, GFP_NOFS);
8450 clear_page_extent_mapped(&folio->page);
39279cc3
CM
8451}
8452
9ebefb18
CM
8453/*
8454 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8455 * called from a page fault handler when a page is first dirtied. Hence we must
8456 * be careful to check for EOF conditions here. We set the page up correctly
8457 * for a written page which means we get ENOSPC checking when writing into
8458 * holes and correct delalloc and unwritten extent mapping on filesystems that
8459 * support these features.
8460 *
8461 * We are not allowed to take the i_mutex here so we have to play games to
8462 * protect against truncate races as the page could now be beyond EOF. Because
d1342aad
OS
8463 * truncate_setsize() writes the inode size before removing pages, once we have
8464 * the page lock we can determine safely if the page is beyond EOF. If it is not
9ebefb18
CM
8465 * beyond EOF, then the page is guaranteed safe against truncation until we
8466 * unlock the page.
8467 */
a528a241 8468vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
9ebefb18 8469{
c2ec175c 8470 struct page *page = vmf->page;
11bac800 8471 struct inode *inode = file_inode(vmf->vma->vm_file);
0b246afa 8472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
e6dcd2dc
CM
8473 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8474 struct btrfs_ordered_extent *ordered;
2ac55d41 8475 struct extent_state *cached_state = NULL;
364ecf36 8476 struct extent_changeset *data_reserved = NULL;
e6dcd2dc 8477 unsigned long zero_start;
9ebefb18 8478 loff_t size;
a528a241
SJ
8479 vm_fault_t ret;
8480 int ret2;
9998eb70 8481 int reserved = 0;
d0b7da88 8482 u64 reserved_space;
a52d9a80 8483 u64 page_start;
e6dcd2dc 8484 u64 page_end;
d0b7da88
CR
8485 u64 end;
8486
09cbfeaf 8487 reserved_space = PAGE_SIZE;
9ebefb18 8488
b2b5ef5c 8489 sb_start_pagefault(inode->i_sb);
df480633 8490 page_start = page_offset(page);
09cbfeaf 8491 page_end = page_start + PAGE_SIZE - 1;
d0b7da88 8492 end = page_end;
df480633 8493
d0b7da88
CR
8494 /*
8495 * Reserving delalloc space after obtaining the page lock can lead to
8496 * deadlock. For example, if a dirty page is locked by this function
8497 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8498 * dirty page write out, then the btrfs_writepage() function could
8499 * end up waiting indefinitely to get a lock on the page currently
8500 * being processed by btrfs_page_mkwrite() function.
8501 */
e5b7231e
NB
8502 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8503 page_start, reserved_space);
a528a241
SJ
8504 if (!ret2) {
8505 ret2 = file_update_time(vmf->vma->vm_file);
9998eb70
CM
8506 reserved = 1;
8507 }
a528a241
SJ
8508 if (ret2) {
8509 ret = vmf_error(ret2);
9998eb70
CM
8510 if (reserved)
8511 goto out;
8512 goto out_noreserve;
56a76f82 8513 }
1832a6d5 8514
56a76f82 8515 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
e6dcd2dc 8516again:
8318ba79 8517 down_read(&BTRFS_I(inode)->i_mmap_lock);
9ebefb18 8518 lock_page(page);
9ebefb18 8519 size = i_size_read(inode);
a52d9a80 8520
9ebefb18 8521 if ((page->mapping != inode->i_mapping) ||
e6dcd2dc 8522 (page_start >= size)) {
9ebefb18
CM
8523 /* page got truncated out from underneath us */
8524 goto out_unlock;
8525 }
e6dcd2dc
CM
8526 wait_on_page_writeback(page);
8527
ff13db41 8528 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
32443de3
QW
8529 ret2 = set_page_extent_mapped(page);
8530 if (ret2 < 0) {
8531 ret = vmf_error(ret2);
8532 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8533 goto out_unlock;
8534 }
e6dcd2dc 8535
eb84ae03
CM
8536 /*
8537 * we can't set the delalloc bits if there are pending ordered
8538 * extents. Drop our locks and wait for them to finish
8539 */
a776c6fa
NB
8540 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8541 PAGE_SIZE);
e6dcd2dc 8542 if (ordered) {
2ac55d41 8543 unlock_extent_cached(io_tree, page_start, page_end,
e43bbe5e 8544 &cached_state);
e6dcd2dc 8545 unlock_page(page);
8318ba79 8546 up_read(&BTRFS_I(inode)->i_mmap_lock);
c0a43603 8547 btrfs_start_ordered_extent(ordered, 1);
e6dcd2dc
CM
8548 btrfs_put_ordered_extent(ordered);
8549 goto again;
8550 }
8551
09cbfeaf 8552 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
da17066c 8553 reserved_space = round_up(size - page_start,
0b246afa 8554 fs_info->sectorsize);
09cbfeaf 8555 if (reserved_space < PAGE_SIZE) {
d0b7da88 8556 end = page_start + reserved_space - 1;
86d52921
NB
8557 btrfs_delalloc_release_space(BTRFS_I(inode),
8558 data_reserved, page_start,
8559 PAGE_SIZE - reserved_space, true);
d0b7da88
CR
8560 }
8561 }
8562
fbf19087 8563 /*
5416034f
LB
8564 * page_mkwrite gets called when the page is firstly dirtied after it's
8565 * faulted in, but write(2) could also dirty a page and set delalloc
8566 * bits, thus in this case for space account reason, we still need to
8567 * clear any delalloc bits within this page range since we have to
8568 * reserve data&meta space before lock_page() (see above comments).
fbf19087 8569 */
d0b7da88 8570 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
e182163d
OS
8571 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8572 EXTENT_DEFRAG, 0, 0, &cached_state);
fbf19087 8573
c2566f22 8574 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
330a5827 8575 &cached_state);
a528a241 8576 if (ret2) {
2ac55d41 8577 unlock_extent_cached(io_tree, page_start, page_end,
e43bbe5e 8578 &cached_state);
9ed74f2d
JB
8579 ret = VM_FAULT_SIGBUS;
8580 goto out_unlock;
8581 }
9ebefb18
CM
8582
8583 /* page is wholly or partially inside EOF */
09cbfeaf 8584 if (page_start + PAGE_SIZE > size)
7073017a 8585 zero_start = offset_in_page(size);
9ebefb18 8586 else
09cbfeaf 8587 zero_start = PAGE_SIZE;
9ebefb18 8588
09cbfeaf 8589 if (zero_start != PAGE_SIZE) {
d048b9c2 8590 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
e6dcd2dc 8591 flush_dcache_page(page);
e6dcd2dc 8592 }
e4f94347 8593 btrfs_page_clear_checked(fs_info, page, page_start, PAGE_SIZE);
2d8ec40e
QW
8594 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8595 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
5a3f23d5 8596
bc0939fc 8597 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
257c62e1 8598
e43bbe5e 8599 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8318ba79 8600 up_read(&BTRFS_I(inode)->i_mmap_lock);
9ebefb18 8601
76de60ed
YY
8602 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8603 sb_end_pagefault(inode->i_sb);
8604 extent_changeset_free(data_reserved);
8605 return VM_FAULT_LOCKED;
717beb96
CM
8606
8607out_unlock:
9ebefb18 8608 unlock_page(page);
8318ba79 8609 up_read(&BTRFS_I(inode)->i_mmap_lock);
1832a6d5 8610out:
8702ba93 8611 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
86d52921 8612 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
43b18595 8613 reserved_space, (ret != 0));
9998eb70 8614out_noreserve:
b2b5ef5c 8615 sb_end_pagefault(inode->i_sb);
364ecf36 8616 extent_changeset_free(data_reserved);
9ebefb18
CM
8617 return ret;
8618}
8619
213e8c55 8620static int btrfs_truncate(struct inode *inode, bool skip_writeback)
39279cc3 8621{
d9ac19c3 8622 struct btrfs_truncate_control control = {
71d18b53 8623 .inode = BTRFS_I(inode),
487e81d2 8624 .ino = btrfs_ino(BTRFS_I(inode)),
d9ac19c3 8625 .min_type = BTRFS_EXTENT_DATA_KEY,
655807b8 8626 .clear_extent_range = true,
d9ac19c3 8627 };
0b246afa 8628 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
39279cc3 8629 struct btrfs_root *root = BTRFS_I(inode)->root;
fcb80c2a 8630 struct btrfs_block_rsv *rsv;
ad7e1a74 8631 int ret;
39279cc3 8632 struct btrfs_trans_handle *trans;
0b246afa 8633 u64 mask = fs_info->sectorsize - 1;
2bd36e7b 8634 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
39279cc3 8635
213e8c55
FM
8636 if (!skip_writeback) {
8637 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8638 (u64)-1);
8639 if (ret)
8640 return ret;
8641 }
39279cc3 8642
fcb80c2a 8643 /*
f7e9e8fc
OS
8644 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8645 * things going on here:
fcb80c2a 8646 *
f7e9e8fc 8647 * 1) We need to reserve space to update our inode.
fcb80c2a 8648 *
f7e9e8fc 8649 * 2) We need to have something to cache all the space that is going to
fcb80c2a
JB
8650 * be free'd up by the truncate operation, but also have some slack
8651 * space reserved in case it uses space during the truncate (thank you
8652 * very much snapshotting).
8653 *
f7e9e8fc 8654 * And we need these to be separate. The fact is we can use a lot of
fcb80c2a 8655 * space doing the truncate, and we have no earthly idea how much space
01327610 8656 * we will use, so we need the truncate reservation to be separate so it
f7e9e8fc
OS
8657 * doesn't end up using space reserved for updating the inode. We also
8658 * need to be able to stop the transaction and start a new one, which
8659 * means we need to be able to update the inode several times, and we
8660 * have no idea of knowing how many times that will be, so we can't just
8661 * reserve 1 item for the entirety of the operation, so that has to be
8662 * done separately as well.
fcb80c2a
JB
8663 *
8664 * So that leaves us with
8665 *
f7e9e8fc 8666 * 1) rsv - for the truncate reservation, which we will steal from the
fcb80c2a 8667 * transaction reservation.
f7e9e8fc 8668 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
fcb80c2a
JB
8669 * updating the inode.
8670 */
2ff7e61e 8671 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
fcb80c2a
JB
8672 if (!rsv)
8673 return -ENOMEM;
4a338542 8674 rsv->size = min_size;
ca7e70f5 8675 rsv->failfast = 1;
f0cd846e 8676
907cbceb 8677 /*
07127184 8678 * 1 for the truncate slack space
907cbceb
JB
8679 * 1 for updating the inode.
8680 */
f3fe820c 8681 trans = btrfs_start_transaction(root, 2);
fcb80c2a 8682 if (IS_ERR(trans)) {
ad7e1a74 8683 ret = PTR_ERR(trans);
fcb80c2a
JB
8684 goto out;
8685 }
f0cd846e 8686
907cbceb 8687 /* Migrate the slack space for the truncate to our reserve */
0b246afa 8688 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
3a584174 8689 min_size, false);
fcb80c2a 8690 BUG_ON(ret);
f0cd846e 8691
ca7e70f5 8692 trans->block_rsv = rsv;
907cbceb 8693
8082510e 8694 while (1) {
9a4a1429
JB
8695 struct extent_state *cached_state = NULL;
8696 const u64 new_size = inode->i_size;
8697 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
8698
d9ac19c3 8699 control.new_size = new_size;
9a4a1429
JB
8700 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
8701 &cached_state);
8702 /*
8703 * We want to drop from the next block forward in case this new
8704 * size is not block aligned since we will be keeping the last
8705 * block of the extent just the way it is.
8706 */
8707 btrfs_drop_extent_cache(BTRFS_I(inode),
8708 ALIGN(new_size, fs_info->sectorsize),
8709 (u64)-1, 0);
8710
71d18b53 8711 ret = btrfs_truncate_inode_items(trans, root, &control);
c2ddb612 8712
462b728e 8713 inode_sub_bytes(inode, control.sub_bytes);
c2ddb612
JB
8714 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), control.last_size);
8715
9a4a1429
JB
8716 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
8717 (u64)-1, &cached_state);
8718
ddfae63c 8719 trans->block_rsv = &fs_info->trans_block_rsv;
ad7e1a74 8720 if (ret != -ENOSPC && ret != -EAGAIN)
8082510e 8721 break;
39279cc3 8722
9a56fcd1 8723 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
ad7e1a74 8724 if (ret)
3893e33b 8725 break;
ca7e70f5 8726
3a45bb20 8727 btrfs_end_transaction(trans);
2ff7e61e 8728 btrfs_btree_balance_dirty(fs_info);
ca7e70f5
JB
8729
8730 trans = btrfs_start_transaction(root, 2);
8731 if (IS_ERR(trans)) {
ad7e1a74 8732 ret = PTR_ERR(trans);
ca7e70f5
JB
8733 trans = NULL;
8734 break;
8735 }
8736
63f018be 8737 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
0b246afa 8738 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
3a584174 8739 rsv, min_size, false);
ca7e70f5
JB
8740 BUG_ON(ret); /* shouldn't happen */
8741 trans->block_rsv = rsv;
8082510e
YZ
8742 }
8743
ddfae63c
JB
8744 /*
8745 * We can't call btrfs_truncate_block inside a trans handle as we could
54f03ab1
JB
8746 * deadlock with freeze, if we got BTRFS_NEED_TRUNCATE_BLOCK then we
8747 * know we've truncated everything except the last little bit, and can
8748 * do btrfs_truncate_block and then update the disk_i_size.
ddfae63c 8749 */
54f03ab1 8750 if (ret == BTRFS_NEED_TRUNCATE_BLOCK) {
ddfae63c
JB
8751 btrfs_end_transaction(trans);
8752 btrfs_btree_balance_dirty(fs_info);
8753
217f42eb 8754 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
ddfae63c
JB
8755 if (ret)
8756 goto out;
8757 trans = btrfs_start_transaction(root, 1);
8758 if (IS_ERR(trans)) {
8759 ret = PTR_ERR(trans);
8760 goto out;
8761 }
76aea537 8762 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
ddfae63c
JB
8763 }
8764
917c16b2 8765 if (trans) {
ad7e1a74
OS
8766 int ret2;
8767
0b246afa 8768 trans->block_rsv = &fs_info->trans_block_rsv;
9a56fcd1 8769 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
ad7e1a74
OS
8770 if (ret2 && !ret)
8771 ret = ret2;
7b128766 8772
ad7e1a74
OS
8773 ret2 = btrfs_end_transaction(trans);
8774 if (ret2 && !ret)
8775 ret = ret2;
2ff7e61e 8776 btrfs_btree_balance_dirty(fs_info);
917c16b2 8777 }
fcb80c2a 8778out:
2ff7e61e 8779 btrfs_free_block_rsv(fs_info, rsv);
0d7d3165
FM
8780 /*
8781 * So if we truncate and then write and fsync we normally would just
8782 * write the extents that changed, which is a problem if we need to
8783 * first truncate that entire inode. So set this flag so we write out
8784 * all of the extents in the inode to the sync log so we're completely
8785 * safe.
8786 *
8787 * If no extents were dropped or trimmed we don't need to force the next
8788 * fsync to truncate all the inode's items from the log and re-log them
8789 * all. This means the truncate operation did not change the file size,
8790 * or changed it to a smaller size but there was only an implicit hole
8791 * between the old i_size and the new i_size, and there were no prealloc
8792 * extents beyond i_size to drop.
8793 */
d9ac19c3 8794 if (control.extents_found > 0)
23e3337f 8795 btrfs_set_inode_full_sync(BTRFS_I(inode));
fcb80c2a 8796
ad7e1a74 8797 return ret;
39279cc3
CM
8798}
8799
a1fd0c35
OS
8800struct inode *btrfs_new_subvol_inode(struct user_namespace *mnt_userns,
8801 struct inode *dir)
8802{
8803 struct inode *inode;
8804
8805 inode = new_inode(dir->i_sb);
8806 if (inode) {
8807 /*
8808 * Subvolumes don't inherit the sgid bit or the parent's gid if
8809 * the parent's sgid bit is set. This is probably a bug.
8810 */
8811 inode_init_owner(mnt_userns, inode, NULL,
8812 S_IFDIR | (~current_umask() & S_IRWXUGO));
8813 inode->i_op = &btrfs_dir_inode_operations;
8814 inode->i_fop = &btrfs_dir_file_operations;
8815 }
8816 return inode;
8817}
8818
39279cc3
CM
8819struct inode *btrfs_alloc_inode(struct super_block *sb)
8820{
69fe2d75 8821 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
39279cc3 8822 struct btrfs_inode *ei;
2ead6ae7 8823 struct inode *inode;
39279cc3 8824
fd60b288 8825 ei = alloc_inode_sb(sb, btrfs_inode_cachep, GFP_KERNEL);
39279cc3
CM
8826 if (!ei)
8827 return NULL;
2ead6ae7
YZ
8828
8829 ei->root = NULL;
2ead6ae7 8830 ei->generation = 0;
15ee9bc7 8831 ei->last_trans = 0;
257c62e1 8832 ei->last_sub_trans = 0;
e02119d5 8833 ei->logged_trans = 0;
2ead6ae7 8834 ei->delalloc_bytes = 0;
a7e3b975 8835 ei->new_delalloc_bytes = 0;
47059d93 8836 ei->defrag_bytes = 0;
2ead6ae7
YZ
8837 ei->disk_i_size = 0;
8838 ei->flags = 0;
77eea05e 8839 ei->ro_flags = 0;
7709cde3 8840 ei->csum_bytes = 0;
2ead6ae7 8841 ei->index_cnt = (u64)-1;
67de1176 8842 ei->dir_index = 0;
2ead6ae7 8843 ei->last_unlink_trans = 0;
3ebac17c 8844 ei->last_reflink_trans = 0;
46d8bc34 8845 ei->last_log_commit = 0;
2ead6ae7 8846
9e0baf60
JB
8847 spin_lock_init(&ei->lock);
8848 ei->outstanding_extents = 0;
69fe2d75
JB
8849 if (sb->s_magic != BTRFS_TEST_MAGIC)
8850 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8851 BTRFS_BLOCK_RSV_DELALLOC);
72ac3c0d 8852 ei->runtime_flags = 0;
b52aa8c9 8853 ei->prop_compress = BTRFS_COMPRESS_NONE;
eec63c65 8854 ei->defrag_compress = BTRFS_COMPRESS_NONE;
2ead6ae7 8855
16cdcec7
MX
8856 ei->delayed_node = NULL;
8857
9cc97d64 8858 ei->i_otime.tv_sec = 0;
8859 ei->i_otime.tv_nsec = 0;
8860
2ead6ae7 8861 inode = &ei->vfs_inode;
a8067e02 8862 extent_map_tree_init(&ei->extent_tree);
43eb5f29
QW
8863 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8864 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8865 IO_TREE_INODE_IO_FAILURE, inode);
41a2ee75
JB
8866 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8867 IO_TREE_INODE_FILE_EXTENT, inode);
7b439738
DS
8868 ei->io_tree.track_uptodate = true;
8869 ei->io_failure_tree.track_uptodate = true;
b812ce28 8870 atomic_set(&ei->sync_writers, 0);
2ead6ae7 8871 mutex_init(&ei->log_mutex);
e6dcd2dc 8872 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
2ead6ae7 8873 INIT_LIST_HEAD(&ei->delalloc_inodes);
8089fe62 8874 INIT_LIST_HEAD(&ei->delayed_iput);
2ead6ae7 8875 RB_CLEAR_NODE(&ei->rb_node);
8318ba79 8876 init_rwsem(&ei->i_mmap_lock);
2ead6ae7
YZ
8877
8878 return inode;
39279cc3
CM
8879}
8880
aaedb55b
JB
8881#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8882void btrfs_test_destroy_inode(struct inode *inode)
8883{
dcdbc059 8884 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
aaedb55b
JB
8885 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8886}
8887#endif
8888
26602cab 8889void btrfs_free_inode(struct inode *inode)
fa0d7e3d 8890{
fa0d7e3d
NP
8891 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8892}
8893
633cc816 8894void btrfs_destroy_inode(struct inode *vfs_inode)
39279cc3 8895{
e6dcd2dc 8896 struct btrfs_ordered_extent *ordered;
633cc816
NB
8897 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8898 struct btrfs_root *root = inode->root;
5a3f23d5 8899
633cc816
NB
8900 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8901 WARN_ON(vfs_inode->i_data.nrpages);
8902 WARN_ON(inode->block_rsv.reserved);
8903 WARN_ON(inode->block_rsv.size);
8904 WARN_ON(inode->outstanding_extents);
dc287224
FM
8905 if (!S_ISDIR(vfs_inode->i_mode)) {
8906 WARN_ON(inode->delalloc_bytes);
8907 WARN_ON(inode->new_delalloc_bytes);
8908 }
633cc816
NB
8909 WARN_ON(inode->csum_bytes);
8910 WARN_ON(inode->defrag_bytes);
39279cc3 8911
a6dbd429
JB
8912 /*
8913 * This can happen where we create an inode, but somebody else also
8914 * created the same inode and we need to destroy the one we already
8915 * created.
8916 */
8917 if (!root)
26602cab 8918 return;
a6dbd429 8919
d397712b 8920 while (1) {
633cc816 8921 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
e6dcd2dc
CM
8922 if (!ordered)
8923 break;
8924 else {
633cc816 8925 btrfs_err(root->fs_info,
5d163e0e 8926 "found ordered extent %llu %llu on inode cleanup",
bffe633e 8927 ordered->file_offset, ordered->num_bytes);
71fe0a55 8928 btrfs_remove_ordered_extent(inode, ordered);
e6dcd2dc
CM
8929 btrfs_put_ordered_extent(ordered);
8930 btrfs_put_ordered_extent(ordered);
8931 }
8932 }
633cc816
NB
8933 btrfs_qgroup_check_reserved_leak(inode);
8934 inode_tree_del(inode);
8935 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8936 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8937 btrfs_put_root(inode->root);
39279cc3
CM
8938}
8939
45321ac5 8940int btrfs_drop_inode(struct inode *inode)
76dda93c
YZ
8941{
8942 struct btrfs_root *root = BTRFS_I(inode)->root;
45321ac5 8943
6379ef9f
NA
8944 if (root == NULL)
8945 return 1;
8946
fa6ac876 8947 /* the snap/subvol tree is on deleting */
69e9c6c6 8948 if (btrfs_root_refs(&root->root_item) == 0)
45321ac5 8949 return 1;
76dda93c 8950 else
45321ac5 8951 return generic_drop_inode(inode);
76dda93c
YZ
8952}
8953
0ee0fda0 8954static void init_once(void *foo)
39279cc3 8955{
0d031dc4 8956 struct btrfs_inode *ei = foo;
39279cc3
CM
8957
8958 inode_init_once(&ei->vfs_inode);
8959}
8960
e67c718b 8961void __cold btrfs_destroy_cachep(void)
39279cc3 8962{
8c0a8537
KS
8963 /*
8964 * Make sure all delayed rcu free inodes are flushed before we
8965 * destroy cache.
8966 */
8967 rcu_barrier();
642c5d34 8968 bioset_exit(&btrfs_dio_bioset);
5598e900
KM
8969 kmem_cache_destroy(btrfs_inode_cachep);
8970 kmem_cache_destroy(btrfs_trans_handle_cachep);
5598e900
KM
8971 kmem_cache_destroy(btrfs_path_cachep);
8972 kmem_cache_destroy(btrfs_free_space_cachep);
3acd4850 8973 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
39279cc3
CM
8974}
8975
f5c29bd9 8976int __init btrfs_init_cachep(void)
39279cc3 8977{
837e1972 8978 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9601e3f6 8979 sizeof(struct btrfs_inode), 0,
5d097056
VD
8980 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8981 init_once);
39279cc3
CM
8982 if (!btrfs_inode_cachep)
8983 goto fail;
9601e3f6 8984
837e1972 8985 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9601e3f6 8986 sizeof(struct btrfs_trans_handle), 0,
fba4b697 8987 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
8988 if (!btrfs_trans_handle_cachep)
8989 goto fail;
9601e3f6 8990
837e1972 8991 btrfs_path_cachep = kmem_cache_create("btrfs_path",
9601e3f6 8992 sizeof(struct btrfs_path), 0,
fba4b697 8993 SLAB_MEM_SPREAD, NULL);
39279cc3
CM
8994 if (!btrfs_path_cachep)
8995 goto fail;
9601e3f6 8996
837e1972 8997 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
dc89e982 8998 sizeof(struct btrfs_free_space), 0,
fba4b697 8999 SLAB_MEM_SPREAD, NULL);
dc89e982
JB
9000 if (!btrfs_free_space_cachep)
9001 goto fail;
9002
3acd4850
CL
9003 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9004 PAGE_SIZE, PAGE_SIZE,
34e49994 9005 SLAB_MEM_SPREAD, NULL);
3acd4850
CL
9006 if (!btrfs_free_space_bitmap_cachep)
9007 goto fail;
9008
642c5d34
CH
9009 if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE,
9010 offsetof(struct btrfs_dio_private, bio),
9011 BIOSET_NEED_BVECS))
9012 goto fail;
9013
39279cc3
CM
9014 return 0;
9015fail:
9016 btrfs_destroy_cachep();
9017 return -ENOMEM;
9018}
9019
549c7297
CB
9020static int btrfs_getattr(struct user_namespace *mnt_userns,
9021 const struct path *path, struct kstat *stat,
a528d35e 9022 u32 request_mask, unsigned int flags)
39279cc3 9023{
df0af1a5 9024 u64 delalloc_bytes;
2766ff61 9025 u64 inode_bytes;
a528d35e 9026 struct inode *inode = d_inode(path->dentry);
fadc0d8b 9027 u32 blocksize = inode->i_sb->s_blocksize;
04a87e34 9028 u32 bi_flags = BTRFS_I(inode)->flags;
14605409 9029 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
04a87e34
YS
9030
9031 stat->result_mask |= STATX_BTIME;
9032 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9033 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9034 if (bi_flags & BTRFS_INODE_APPEND)
9035 stat->attributes |= STATX_ATTR_APPEND;
9036 if (bi_flags & BTRFS_INODE_COMPRESS)
9037 stat->attributes |= STATX_ATTR_COMPRESSED;
9038 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9039 stat->attributes |= STATX_ATTR_IMMUTABLE;
9040 if (bi_flags & BTRFS_INODE_NODUMP)
9041 stat->attributes |= STATX_ATTR_NODUMP;
14605409
BB
9042 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
9043 stat->attributes |= STATX_ATTR_VERITY;
04a87e34
YS
9044
9045 stat->attributes_mask |= (STATX_ATTR_APPEND |
9046 STATX_ATTR_COMPRESSED |
9047 STATX_ATTR_IMMUTABLE |
9048 STATX_ATTR_NODUMP);
fadc0d8b 9049
c020d2ea 9050 generic_fillattr(mnt_userns, inode, stat);
0ee5dc67 9051 stat->dev = BTRFS_I(inode)->root->anon_dev;
df0af1a5
MX
9052
9053 spin_lock(&BTRFS_I(inode)->lock);
a7e3b975 9054 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
2766ff61 9055 inode_bytes = inode_get_bytes(inode);
df0af1a5 9056 spin_unlock(&BTRFS_I(inode)->lock);
2766ff61 9057 stat->blocks = (ALIGN(inode_bytes, blocksize) +
df0af1a5 9058 ALIGN(delalloc_bytes, blocksize)) >> 9;
39279cc3
CM
9059 return 0;
9060}
9061
cdd1fedf
DF
9062static int btrfs_rename_exchange(struct inode *old_dir,
9063 struct dentry *old_dentry,
9064 struct inode *new_dir,
9065 struct dentry *new_dentry)
9066{
0b246afa 9067 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
cdd1fedf 9068 struct btrfs_trans_handle *trans;
c1621871 9069 unsigned int trans_num_items;
cdd1fedf
DF
9070 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9071 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9072 struct inode *new_inode = new_dentry->d_inode;
9073 struct inode *old_inode = old_dentry->d_inode;
95582b00 9074 struct timespec64 ctime = current_time(old_inode);
88d2beec
FM
9075 struct btrfs_rename_ctx old_rename_ctx;
9076 struct btrfs_rename_ctx new_rename_ctx;
4a0cc7ca
NB
9077 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9078 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
cdd1fedf
DF
9079 u64 old_idx = 0;
9080 u64 new_idx = 0;
cdd1fedf 9081 int ret;
75b463d2 9082 int ret2;
dc09ef35 9083 bool need_abort = false;
cdd1fedf 9084
3f79f6f6
N
9085 /*
9086 * For non-subvolumes allow exchange only within one subvolume, in the
9087 * same inode namespace. Two subvolumes (represented as directory) can
9088 * be exchanged as they're a logical link and have a fixed inode number.
9089 */
9090 if (root != dest &&
9091 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9092 new_ino != BTRFS_FIRST_FREE_OBJECTID))
cdd1fedf
DF
9093 return -EXDEV;
9094
9095 /* close the race window with snapshot create/destroy ioctl */
943eb3bf
JB
9096 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9097 new_ino == BTRFS_FIRST_FREE_OBJECTID)
0b246afa 9098 down_read(&fs_info->subvol_sem);
cdd1fedf
DF
9099
9100 /*
c1621871
OS
9101 * For each inode:
9102 * 1 to remove old dir item
9103 * 1 to remove old dir index
9104 * 1 to add new dir item
9105 * 1 to add new dir index
9106 * 1 to update parent inode
9107 *
9108 * If the parents are the same, we only need to account for one
cdd1fedf 9109 */
c1621871
OS
9110 trans_num_items = (old_dir == new_dir ? 9 : 10);
9111 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9112 /*
9113 * 1 to remove old root ref
9114 * 1 to remove old root backref
9115 * 1 to add new root ref
9116 * 1 to add new root backref
9117 */
9118 trans_num_items += 4;
9119 } else {
9120 /*
9121 * 1 to update inode item
9122 * 1 to remove old inode ref
9123 * 1 to add new inode ref
9124 */
9125 trans_num_items += 3;
9126 }
9127 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9128 trans_num_items += 4;
9129 else
9130 trans_num_items += 3;
9131 trans = btrfs_start_transaction(root, trans_num_items);
cdd1fedf
DF
9132 if (IS_ERR(trans)) {
9133 ret = PTR_ERR(trans);
9134 goto out_notrans;
9135 }
9136
00aa8e87
JB
9137 if (dest != root) {
9138 ret = btrfs_record_root_in_trans(trans, dest);
9139 if (ret)
9140 goto out_fail;
9141 }
3e174099 9142
cdd1fedf
DF
9143 /*
9144 * We need to find a free sequence number both in the source and
9145 * in the destination directory for the exchange.
9146 */
877574e2 9147 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
cdd1fedf
DF
9148 if (ret)
9149 goto out_fail;
877574e2 9150 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
cdd1fedf
DF
9151 if (ret)
9152 goto out_fail;
9153
9154 BTRFS_I(old_inode)->dir_index = 0ULL;
9155 BTRFS_I(new_inode)->dir_index = 0ULL;
9156
9157 /* Reference for the source. */
9158 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9159 /* force full log commit if subvolume involved. */
90787766 9160 btrfs_set_log_full_commit(trans);
cdd1fedf
DF
9161 } else {
9162 ret = btrfs_insert_inode_ref(trans, dest,
9163 new_dentry->d_name.name,
9164 new_dentry->d_name.len,
9165 old_ino,
f85b7379
DS
9166 btrfs_ino(BTRFS_I(new_dir)),
9167 old_idx);
cdd1fedf
DF
9168 if (ret)
9169 goto out_fail;
dc09ef35 9170 need_abort = true;
cdd1fedf
DF
9171 }
9172
9173 /* And now for the dest. */
9174 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9175 /* force full log commit if subvolume involved. */
90787766 9176 btrfs_set_log_full_commit(trans);
cdd1fedf
DF
9177 } else {
9178 ret = btrfs_insert_inode_ref(trans, root,
9179 old_dentry->d_name.name,
9180 old_dentry->d_name.len,
9181 new_ino,
f85b7379
DS
9182 btrfs_ino(BTRFS_I(old_dir)),
9183 new_idx);
dc09ef35
JB
9184 if (ret) {
9185 if (need_abort)
9186 btrfs_abort_transaction(trans, ret);
cdd1fedf 9187 goto out_fail;
dc09ef35 9188 }
cdd1fedf
DF
9189 }
9190
9191 /* Update inode version and ctime/mtime. */
9192 inode_inc_iversion(old_dir);
9193 inode_inc_iversion(new_dir);
9194 inode_inc_iversion(old_inode);
9195 inode_inc_iversion(new_inode);
9196 old_dir->i_ctime = old_dir->i_mtime = ctime;
9197 new_dir->i_ctime = new_dir->i_mtime = ctime;
9198 old_inode->i_ctime = ctime;
9199 new_inode->i_ctime = ctime;
9200
9201 if (old_dentry->d_parent != new_dentry->d_parent) {
f85b7379
DS
9202 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9203 BTRFS_I(old_inode), 1);
9204 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9205 BTRFS_I(new_inode), 1);
cdd1fedf
DF
9206 }
9207
9208 /* src is a subvolume */
9209 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
045d3967 9210 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
cdd1fedf 9211 } else { /* src is an inode */
4467af88 9212 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
4ec5934e 9213 BTRFS_I(old_dentry->d_inode),
cdd1fedf 9214 old_dentry->d_name.name,
88d2beec
FM
9215 old_dentry->d_name.len,
9216 &old_rename_ctx);
cdd1fedf 9217 if (!ret)
9a56fcd1 9218 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
cdd1fedf
DF
9219 }
9220 if (ret) {
66642832 9221 btrfs_abort_transaction(trans, ret);
cdd1fedf
DF
9222 goto out_fail;
9223 }
9224
9225 /* dest is a subvolume */
9226 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
045d3967 9227 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
cdd1fedf 9228 } else { /* dest is an inode */
4467af88 9229 ret = __btrfs_unlink_inode(trans, BTRFS_I(new_dir),
4ec5934e 9230 BTRFS_I(new_dentry->d_inode),
cdd1fedf 9231 new_dentry->d_name.name,
88d2beec
FM
9232 new_dentry->d_name.len,
9233 &new_rename_ctx);
cdd1fedf 9234 if (!ret)
9a56fcd1 9235 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
cdd1fedf
DF
9236 }
9237 if (ret) {
66642832 9238 btrfs_abort_transaction(trans, ret);
cdd1fedf
DF
9239 goto out_fail;
9240 }
9241
db0a669f 9242 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
cdd1fedf
DF
9243 new_dentry->d_name.name,
9244 new_dentry->d_name.len, 0, old_idx);
9245 if (ret) {
66642832 9246 btrfs_abort_transaction(trans, ret);
cdd1fedf
DF
9247 goto out_fail;
9248 }
9249
db0a669f 9250 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
cdd1fedf
DF
9251 old_dentry->d_name.name,
9252 old_dentry->d_name.len, 0, new_idx);
9253 if (ret) {
66642832 9254 btrfs_abort_transaction(trans, ret);
cdd1fedf
DF
9255 goto out_fail;
9256 }
9257
9258 if (old_inode->i_nlink == 1)
9259 BTRFS_I(old_inode)->dir_index = old_idx;
9260 if (new_inode->i_nlink == 1)
9261 BTRFS_I(new_inode)->dir_index = new_idx;
9262
259c4b96
FM
9263 /*
9264 * Now pin the logs of the roots. We do it to ensure that no other task
9265 * can sync the logs while we are in progress with the rename, because
9266 * that could result in an inconsistency in case any of the inodes that
9267 * are part of this rename operation were logged before.
9268 */
9269 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
9270 btrfs_pin_log_trans(root);
9271 if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
9272 btrfs_pin_log_trans(dest);
9273
9274 /* Do the log updates for all inodes. */
9275 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
d5f5bd54 9276 btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir),
88d2beec 9277 old_rename_ctx.index, new_dentry->d_parent);
259c4b96 9278 if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
d5f5bd54 9279 btrfs_log_new_name(trans, new_dentry, BTRFS_I(new_dir),
88d2beec 9280 new_rename_ctx.index, old_dentry->d_parent);
259c4b96
FM
9281
9282 /* Now unpin the logs. */
9283 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
9284 btrfs_end_log_trans(root);
9285 if (new_ino != BTRFS_FIRST_FREE_OBJECTID)
cdd1fedf 9286 btrfs_end_log_trans(dest);
cdd1fedf 9287out_fail:
75b463d2
FM
9288 ret2 = btrfs_end_transaction(trans);
9289 ret = ret ? ret : ret2;
cdd1fedf 9290out_notrans:
943eb3bf
JB
9291 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9292 old_ino == BTRFS_FIRST_FREE_OBJECTID)
0b246afa 9293 up_read(&fs_info->subvol_sem);
cdd1fedf
DF
9294
9295 return ret;
9296}
9297
a1fd0c35
OS
9298static struct inode *new_whiteout_inode(struct user_namespace *mnt_userns,
9299 struct inode *dir)
9300{
9301 struct inode *inode;
9302
9303 inode = new_inode(dir->i_sb);
9304 if (inode) {
9305 inode_init_owner(mnt_userns, inode, dir,
9306 S_IFCHR | WHITEOUT_MODE);
9307 inode->i_op = &btrfs_special_inode_operations;
9308 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
9309 }
9310 return inode;
9311}
9312
ca07274c
CB
9313static int btrfs_rename(struct user_namespace *mnt_userns,
9314 struct inode *old_dir, struct dentry *old_dentry,
9315 struct inode *new_dir, struct dentry *new_dentry,
9316 unsigned int flags)
39279cc3 9317{
0b246afa 9318 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
3538d68d
OS
9319 struct btrfs_new_inode_args whiteout_args = {
9320 .dir = old_dir,
9321 .dentry = old_dentry,
9322 };
39279cc3 9323 struct btrfs_trans_handle *trans;
5062af35 9324 unsigned int trans_num_items;
39279cc3 9325 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4df27c4d 9326 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
2b0143b5
DH
9327 struct inode *new_inode = d_inode(new_dentry);
9328 struct inode *old_inode = d_inode(old_dentry);
88d2beec 9329 struct btrfs_rename_ctx rename_ctx;
00e4e6b3 9330 u64 index = 0;
39279cc3 9331 int ret;
75b463d2 9332 int ret2;
4a0cc7ca 9333 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
39279cc3 9334
4a0cc7ca 9335 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
f679a840
YZ
9336 return -EPERM;
9337
4df27c4d 9338 /* we only allow rename subvolume link between subvolumes */
33345d01 9339 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
3394e160
CM
9340 return -EXDEV;
9341
33345d01 9342 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
4a0cc7ca 9343 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
39279cc3 9344 return -ENOTEMPTY;
5f39d397 9345
4df27c4d
YZ
9346 if (S_ISDIR(old_inode->i_mode) && new_inode &&
9347 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
9348 return -ENOTEMPTY;
9c52057c
CM
9349
9350
9351 /* check for collisions, even if the name isn't there */
4871c158 9352 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
9c52057c
CM
9353 new_dentry->d_name.name,
9354 new_dentry->d_name.len);
9355
9356 if (ret) {
9357 if (ret == -EEXIST) {
9358 /* we shouldn't get
9359 * eexist without a new_inode */
fae7f21c 9360 if (WARN_ON(!new_inode)) {
9c52057c
CM
9361 return ret;
9362 }
9363 } else {
9364 /* maybe -EOVERFLOW */
9365 return ret;
9366 }
9367 }
9368 ret = 0;
9369
5a3f23d5 9370 /*
8d875f95
CM
9371 * we're using rename to replace one file with another. Start IO on it
9372 * now so we don't add too much work to the end of the transaction
5a3f23d5 9373 */
8d875f95 9374 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
5a3f23d5
CM
9375 filemap_flush(old_inode->i_mapping);
9376
a1fd0c35 9377 if (flags & RENAME_WHITEOUT) {
3538d68d
OS
9378 whiteout_args.inode = new_whiteout_inode(mnt_userns, old_dir);
9379 if (!whiteout_args.inode)
a1fd0c35 9380 return -ENOMEM;
3538d68d
OS
9381 ret = btrfs_new_inode_prepare(&whiteout_args, &trans_num_items);
9382 if (ret)
9383 goto out_whiteout_inode;
9384 } else {
9385 /* 1 to update the old parent inode. */
9386 trans_num_items = 1;
a1fd0c35
OS
9387 }
9388
c1621871
OS
9389 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9390 /* Close the race window with snapshot create/destroy ioctl */
0b246afa 9391 down_read(&fs_info->subvol_sem);
c1621871
OS
9392 /*
9393 * 1 to remove old root ref
9394 * 1 to remove old root backref
9395 * 1 to add new root ref
9396 * 1 to add new root backref
9397 */
3538d68d 9398 trans_num_items += 4;
c1621871
OS
9399 } else {
9400 /*
9401 * 1 to update inode
9402 * 1 to remove old inode ref
9403 * 1 to add new inode ref
9404 */
3538d68d 9405 trans_num_items += 3;
c1621871 9406 }
a22285a6 9407 /*
c1621871
OS
9408 * 1 to remove old dir item
9409 * 1 to remove old dir index
c1621871
OS
9410 * 1 to add new dir item
9411 * 1 to add new dir index
a22285a6 9412 */
3538d68d
OS
9413 trans_num_items += 4;
9414 /* 1 to update new parent inode if it's not the same as the old parent */
c1621871
OS
9415 if (new_dir != old_dir)
9416 trans_num_items++;
9417 if (new_inode) {
9418 /*
9419 * 1 to update inode
9420 * 1 to remove inode ref
9421 * 1 to remove dir item
9422 * 1 to remove dir index
9423 * 1 to possibly add orphan item
9424 */
9425 trans_num_items += 5;
9426 }
5062af35 9427 trans = btrfs_start_transaction(root, trans_num_items);
b44c59a8 9428 if (IS_ERR(trans)) {
cdd1fedf
DF
9429 ret = PTR_ERR(trans);
9430 goto out_notrans;
9431 }
76dda93c 9432
b0fec6fd
JB
9433 if (dest != root) {
9434 ret = btrfs_record_root_in_trans(trans, dest);
9435 if (ret)
9436 goto out_fail;
9437 }
5f39d397 9438
877574e2 9439 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
a5719521
YZ
9440 if (ret)
9441 goto out_fail;
5a3f23d5 9442
67de1176 9443 BTRFS_I(old_inode)->dir_index = 0ULL;
33345d01 9444 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d 9445 /* force full log commit if subvolume involved. */
90787766 9446 btrfs_set_log_full_commit(trans);
4df27c4d 9447 } else {
a5719521
YZ
9448 ret = btrfs_insert_inode_ref(trans, dest,
9449 new_dentry->d_name.name,
9450 new_dentry->d_name.len,
33345d01 9451 old_ino,
4a0cc7ca 9452 btrfs_ino(BTRFS_I(new_dir)), index);
a5719521
YZ
9453 if (ret)
9454 goto out_fail;
4df27c4d 9455 }
5a3f23d5 9456
0c4d2d95
JB
9457 inode_inc_iversion(old_dir);
9458 inode_inc_iversion(new_dir);
9459 inode_inc_iversion(old_inode);
04b285f3
DD
9460 old_dir->i_ctime = old_dir->i_mtime =
9461 new_dir->i_ctime = new_dir->i_mtime =
c2050a45 9462 old_inode->i_ctime = current_time(old_dir);
5f39d397 9463
12fcfd22 9464 if (old_dentry->d_parent != new_dentry->d_parent)
f85b7379
DS
9465 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9466 BTRFS_I(old_inode), 1);
12fcfd22 9467
33345d01 9468 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
045d3967 9469 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
4df27c4d 9470 } else {
4467af88 9471 ret = __btrfs_unlink_inode(trans, BTRFS_I(old_dir),
4ec5934e 9472 BTRFS_I(d_inode(old_dentry)),
92986796 9473 old_dentry->d_name.name,
88d2beec
FM
9474 old_dentry->d_name.len,
9475 &rename_ctx);
92986796 9476 if (!ret)
9a56fcd1 9477 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
4df27c4d 9478 }
79787eaa 9479 if (ret) {
66642832 9480 btrfs_abort_transaction(trans, ret);
79787eaa
JM
9481 goto out_fail;
9482 }
39279cc3
CM
9483
9484 if (new_inode) {
0c4d2d95 9485 inode_inc_iversion(new_inode);
c2050a45 9486 new_inode->i_ctime = current_time(new_inode);
4a0cc7ca 9487 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
4df27c4d 9488 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
045d3967 9489 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
4df27c4d
YZ
9490 BUG_ON(new_inode->i_nlink == 0);
9491 } else {
4467af88 9492 ret = btrfs_unlink_inode(trans, BTRFS_I(new_dir),
4ec5934e 9493 BTRFS_I(d_inode(new_dentry)),
4df27c4d
YZ
9494 new_dentry->d_name.name,
9495 new_dentry->d_name.len);
9496 }
4ef31a45 9497 if (!ret && new_inode->i_nlink == 0)
73f2e545
NB
9498 ret = btrfs_orphan_add(trans,
9499 BTRFS_I(d_inode(new_dentry)));
79787eaa 9500 if (ret) {
66642832 9501 btrfs_abort_transaction(trans, ret);
79787eaa
JM
9502 goto out_fail;
9503 }
39279cc3 9504 }
aec7477b 9505
db0a669f 9506 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
4df27c4d 9507 new_dentry->d_name.name,
a5719521 9508 new_dentry->d_name.len, 0, index);
79787eaa 9509 if (ret) {
66642832 9510 btrfs_abort_transaction(trans, ret);
79787eaa
JM
9511 goto out_fail;
9512 }
39279cc3 9513
67de1176
MX
9514 if (old_inode->i_nlink == 1)
9515 BTRFS_I(old_inode)->dir_index = index;
9516
259c4b96 9517 if (old_ino != BTRFS_FIRST_FREE_OBJECTID)
d5f5bd54 9518 btrfs_log_new_name(trans, old_dentry, BTRFS_I(old_dir),
88d2beec 9519 rename_ctx.index, new_dentry->d_parent);
cdd1fedf
DF
9520
9521 if (flags & RENAME_WHITEOUT) {
caae78e0 9522 ret = btrfs_create_new_inode(trans, &whiteout_args);
cdd1fedf 9523 if (ret) {
66642832 9524 btrfs_abort_transaction(trans, ret);
cdd1fedf 9525 goto out_fail;
caae78e0
OS
9526 } else {
9527 unlock_new_inode(whiteout_args.inode);
9528 iput(whiteout_args.inode);
9529 whiteout_args.inode = NULL;
cdd1fedf 9530 }
4df27c4d 9531 }
39279cc3 9532out_fail:
75b463d2
FM
9533 ret2 = btrfs_end_transaction(trans);
9534 ret = ret ? ret : ret2;
b44c59a8 9535out_notrans:
33345d01 9536 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
0b246afa 9537 up_read(&fs_info->subvol_sem);
a1fd0c35 9538 if (flags & RENAME_WHITEOUT)
3538d68d
OS
9539 btrfs_new_inode_args_destroy(&whiteout_args);
9540out_whiteout_inode:
9541 if (flags & RENAME_WHITEOUT)
9542 iput(whiteout_args.inode);
39279cc3
CM
9543 return ret;
9544}
9545
549c7297
CB
9546static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9547 struct dentry *old_dentry, struct inode *new_dir,
9548 struct dentry *new_dentry, unsigned int flags)
80ace85c 9549{
cdd1fedf 9550 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
80ace85c
MS
9551 return -EINVAL;
9552
cdd1fedf
DF
9553 if (flags & RENAME_EXCHANGE)
9554 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9555 new_dentry);
9556
ca07274c
CB
9557 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9558 new_dentry, flags);
80ace85c
MS
9559}
9560
3a2f8c07
NB
9561struct btrfs_delalloc_work {
9562 struct inode *inode;
9563 struct completion completion;
9564 struct list_head list;
9565 struct btrfs_work work;
9566};
9567
8ccf6f19
MX
9568static void btrfs_run_delalloc_work(struct btrfs_work *work)
9569{
9570 struct btrfs_delalloc_work *delalloc_work;
9f23e289 9571 struct inode *inode;
8ccf6f19
MX
9572
9573 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9574 work);
9f23e289 9575 inode = delalloc_work->inode;
30424601
DS
9576 filemap_flush(inode->i_mapping);
9577 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9578 &BTRFS_I(inode)->runtime_flags))
9f23e289 9579 filemap_flush(inode->i_mapping);
8ccf6f19 9580
076da91c 9581 iput(inode);
8ccf6f19
MX
9582 complete(&delalloc_work->completion);
9583}
9584
3a2f8c07 9585static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
8ccf6f19
MX
9586{
9587 struct btrfs_delalloc_work *work;
9588
100d5702 9589 work = kmalloc(sizeof(*work), GFP_NOFS);
8ccf6f19
MX
9590 if (!work)
9591 return NULL;
9592
9593 init_completion(&work->completion);
9594 INIT_LIST_HEAD(&work->list);
9595 work->inode = inode;
a0cac0ec 9596 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
8ccf6f19
MX
9597
9598 return work;
9599}
9600
d352ac68
CM
9601/*
9602 * some fairly slow code that needs optimization. This walks the list
9603 * of all the inodes with pending delalloc and forces them to disk.
9604 */
e076ab2a
JB
9605static int start_delalloc_inodes(struct btrfs_root *root,
9606 struct writeback_control *wbc, bool snapshot,
3d45f221 9607 bool in_reclaim_context)
ea8c2819 9608{
ea8c2819 9609 struct btrfs_inode *binode;
5b21f2ed 9610 struct inode *inode;
8ccf6f19
MX
9611 struct btrfs_delalloc_work *work, *next;
9612 struct list_head works;
1eafa6c7 9613 struct list_head splice;
8ccf6f19 9614 int ret = 0;
e076ab2a 9615 bool full_flush = wbc->nr_to_write == LONG_MAX;
ea8c2819 9616
8ccf6f19 9617 INIT_LIST_HEAD(&works);
1eafa6c7 9618 INIT_LIST_HEAD(&splice);
63607cc8 9619
573bfb72 9620 mutex_lock(&root->delalloc_mutex);
eb73c1b7
MX
9621 spin_lock(&root->delalloc_lock);
9622 list_splice_init(&root->delalloc_inodes, &splice);
1eafa6c7
MX
9623 while (!list_empty(&splice)) {
9624 binode = list_entry(splice.next, struct btrfs_inode,
ea8c2819 9625 delalloc_inodes);
1eafa6c7 9626
eb73c1b7
MX
9627 list_move_tail(&binode->delalloc_inodes,
9628 &root->delalloc_inodes);
3d45f221
FM
9629
9630 if (in_reclaim_context &&
9631 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9632 continue;
9633
5b21f2ed 9634 inode = igrab(&binode->vfs_inode);
df0af1a5 9635 if (!inode) {
eb73c1b7 9636 cond_resched_lock(&root->delalloc_lock);
1eafa6c7 9637 continue;
df0af1a5 9638 }
eb73c1b7 9639 spin_unlock(&root->delalloc_lock);
1eafa6c7 9640
3cd24c69
EL
9641 if (snapshot)
9642 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9643 &binode->runtime_flags);
e076ab2a
JB
9644 if (full_flush) {
9645 work = btrfs_alloc_delalloc_work(inode);
9646 if (!work) {
9647 iput(inode);
9648 ret = -ENOMEM;
9649 goto out;
9650 }
9651 list_add_tail(&work->list, &works);
9652 btrfs_queue_work(root->fs_info->flush_workers,
9653 &work->work);
9654 } else {
b3776305 9655 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
e076ab2a
JB
9656 btrfs_add_delayed_iput(inode);
9657 if (ret || wbc->nr_to_write <= 0)
b4912139
JB
9658 goto out;
9659 }
5b21f2ed 9660 cond_resched();
eb73c1b7 9661 spin_lock(&root->delalloc_lock);
ea8c2819 9662 }
eb73c1b7 9663 spin_unlock(&root->delalloc_lock);
8c8bee1d 9664
a1ecaabb 9665out:
eb73c1b7
MX
9666 list_for_each_entry_safe(work, next, &works, list) {
9667 list_del_init(&work->list);
40012f96
NB
9668 wait_for_completion(&work->completion);
9669 kfree(work);
eb73c1b7
MX
9670 }
9671
81f1d390 9672 if (!list_empty(&splice)) {
eb73c1b7
MX
9673 spin_lock(&root->delalloc_lock);
9674 list_splice_tail(&splice, &root->delalloc_inodes);
9675 spin_unlock(&root->delalloc_lock);
9676 }
573bfb72 9677 mutex_unlock(&root->delalloc_mutex);
eb73c1b7
MX
9678 return ret;
9679}
1eafa6c7 9680
f9baa501 9681int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
eb73c1b7 9682{
e076ab2a
JB
9683 struct writeback_control wbc = {
9684 .nr_to_write = LONG_MAX,
9685 .sync_mode = WB_SYNC_NONE,
9686 .range_start = 0,
9687 .range_end = LLONG_MAX,
9688 };
0b246afa 9689 struct btrfs_fs_info *fs_info = root->fs_info;
1eafa6c7 9690
84961539 9691 if (BTRFS_FS_ERROR(fs_info))
eb73c1b7
MX
9692 return -EROFS;
9693
f9baa501 9694 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
eb73c1b7
MX
9695}
9696
9db4dc24 9697int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
3d45f221 9698 bool in_reclaim_context)
eb73c1b7 9699{
e076ab2a 9700 struct writeback_control wbc = {
9db4dc24 9701 .nr_to_write = nr,
e076ab2a
JB
9702 .sync_mode = WB_SYNC_NONE,
9703 .range_start = 0,
9704 .range_end = LLONG_MAX,
9705 };
eb73c1b7
MX
9706 struct btrfs_root *root;
9707 struct list_head splice;
9708 int ret;
9709
84961539 9710 if (BTRFS_FS_ERROR(fs_info))
eb73c1b7
MX
9711 return -EROFS;
9712
9713 INIT_LIST_HEAD(&splice);
9714
573bfb72 9715 mutex_lock(&fs_info->delalloc_root_mutex);
eb73c1b7
MX
9716 spin_lock(&fs_info->delalloc_root_lock);
9717 list_splice_init(&fs_info->delalloc_roots, &splice);
d7830b71 9718 while (!list_empty(&splice)) {
e076ab2a
JB
9719 /*
9720 * Reset nr_to_write here so we know that we're doing a full
9721 * flush.
9722 */
9db4dc24 9723 if (nr == LONG_MAX)
e076ab2a
JB
9724 wbc.nr_to_write = LONG_MAX;
9725
eb73c1b7
MX
9726 root = list_first_entry(&splice, struct btrfs_root,
9727 delalloc_root);
00246528 9728 root = btrfs_grab_root(root);
eb73c1b7
MX
9729 BUG_ON(!root);
9730 list_move_tail(&root->delalloc_root,
9731 &fs_info->delalloc_roots);
9732 spin_unlock(&fs_info->delalloc_root_lock);
9733
e076ab2a 9734 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
00246528 9735 btrfs_put_root(root);
e076ab2a 9736 if (ret < 0 || wbc.nr_to_write <= 0)
eb73c1b7 9737 goto out;
eb73c1b7 9738 spin_lock(&fs_info->delalloc_root_lock);
8ccf6f19 9739 }
eb73c1b7 9740 spin_unlock(&fs_info->delalloc_root_lock);
1eafa6c7 9741
6c255e67 9742 ret = 0;
eb73c1b7 9743out:
81f1d390 9744 if (!list_empty(&splice)) {
eb73c1b7
MX
9745 spin_lock(&fs_info->delalloc_root_lock);
9746 list_splice_tail(&splice, &fs_info->delalloc_roots);
9747 spin_unlock(&fs_info->delalloc_root_lock);
1eafa6c7 9748 }
573bfb72 9749 mutex_unlock(&fs_info->delalloc_root_mutex);
8ccf6f19 9750 return ret;
ea8c2819
CM
9751}
9752
549c7297
CB
9753static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
9754 struct dentry *dentry, const char *symname)
39279cc3 9755{
0b246afa 9756 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
39279cc3
CM
9757 struct btrfs_trans_handle *trans;
9758 struct btrfs_root *root = BTRFS_I(dir)->root;
9759 struct btrfs_path *path;
9760 struct btrfs_key key;
a1fd0c35 9761 struct inode *inode;
3538d68d
OS
9762 struct btrfs_new_inode_args new_inode_args = {
9763 .dir = dir,
9764 .dentry = dentry,
9765 };
9766 unsigned int trans_num_items;
39279cc3 9767 int err;
39279cc3
CM
9768 int name_len;
9769 int datasize;
5f39d397 9770 unsigned long ptr;
39279cc3 9771 struct btrfs_file_extent_item *ei;
5f39d397 9772 struct extent_buffer *leaf;
39279cc3 9773
f06becc4 9774 name_len = strlen(symname);
0b246afa 9775 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
39279cc3 9776 return -ENAMETOOLONG;
1832a6d5 9777
a1fd0c35
OS
9778 inode = new_inode(dir->i_sb);
9779 if (!inode)
9780 return -ENOMEM;
9781 inode_init_owner(mnt_userns, inode, dir, S_IFLNK | S_IRWXUGO);
9782 inode->i_op = &btrfs_symlink_inode_operations;
9783 inode_nohighmem(inode);
9784 inode->i_mapping->a_ops = &btrfs_aops;
caae78e0
OS
9785 btrfs_i_size_write(BTRFS_I(inode), name_len);
9786 inode_set_bytes(inode, name_len);
a1fd0c35 9787
3538d68d
OS
9788 new_inode_args.inode = inode;
9789 err = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
caae78e0
OS
9790 if (err)
9791 goto out_inode;
3538d68d
OS
9792 /* 1 additional item for the inline extent */
9793 trans_num_items++;
9794
9795 trans = btrfs_start_transaction(root, trans_num_items);
a1fd0c35 9796 if (IS_ERR(trans)) {
3538d68d
OS
9797 err = PTR_ERR(trans);
9798 goto out_new_inode_args;
a1fd0c35 9799 }
1832a6d5 9800
caae78e0 9801 err = btrfs_create_new_inode(trans, &new_inode_args);
b0d5d10f 9802 if (err)
caae78e0 9803 goto out;
ad19db71 9804
39279cc3 9805 path = btrfs_alloc_path();
d8926bb3
MF
9806 if (!path) {
9807 err = -ENOMEM;
caae78e0
OS
9808 btrfs_abort_transaction(trans, err);
9809 discard_new_inode(inode);
9810 inode = NULL;
9811 goto out;
d8926bb3 9812 }
4a0cc7ca 9813 key.objectid = btrfs_ino(BTRFS_I(inode));
39279cc3 9814 key.offset = 0;
962a298f 9815 key.type = BTRFS_EXTENT_DATA_KEY;
39279cc3
CM
9816 datasize = btrfs_file_extent_calc_inline_size(name_len);
9817 err = btrfs_insert_empty_item(trans, root, path, &key,
9818 datasize);
54aa1f4d 9819 if (err) {
caae78e0 9820 btrfs_abort_transaction(trans, err);
b0839166 9821 btrfs_free_path(path);
caae78e0
OS
9822 discard_new_inode(inode);
9823 inode = NULL;
9824 goto out;
54aa1f4d 9825 }
5f39d397
CM
9826 leaf = path->nodes[0];
9827 ei = btrfs_item_ptr(leaf, path->slots[0],
9828 struct btrfs_file_extent_item);
9829 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9830 btrfs_set_file_extent_type(leaf, ei,
39279cc3 9831 BTRFS_FILE_EXTENT_INLINE);
c8b97818
CM
9832 btrfs_set_file_extent_encryption(leaf, ei, 0);
9833 btrfs_set_file_extent_compression(leaf, ei, 0);
9834 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9835 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9836
39279cc3 9837 ptr = btrfs_file_extent_inline_start(ei);
5f39d397
CM
9838 write_extent_buffer(leaf, symname, ptr, name_len);
9839 btrfs_mark_buffer_dirty(leaf);
39279cc3 9840 btrfs_free_path(path);
5f39d397 9841
1e2e547a 9842 d_instantiate_new(dentry, inode);
caae78e0
OS
9843 err = 0;
9844out:
3a45bb20 9845 btrfs_end_transaction(trans);
2ff7e61e 9846 btrfs_btree_balance_dirty(fs_info);
3538d68d
OS
9847out_new_inode_args:
9848 btrfs_new_inode_args_destroy(&new_inode_args);
caae78e0
OS
9849out_inode:
9850 if (err)
9851 iput(inode);
39279cc3
CM
9852 return err;
9853}
16432985 9854
8fccebfa
FM
9855static struct btrfs_trans_handle *insert_prealloc_file_extent(
9856 struct btrfs_trans_handle *trans_in,
90dffd0c
NB
9857 struct btrfs_inode *inode,
9858 struct btrfs_key *ins,
203f44c5
QW
9859 u64 file_offset)
9860{
9861 struct btrfs_file_extent_item stack_fi;
bf385648 9862 struct btrfs_replace_extent_info extent_info;
8fccebfa
FM
9863 struct btrfs_trans_handle *trans = trans_in;
9864 struct btrfs_path *path;
203f44c5
QW
9865 u64 start = ins->objectid;
9866 u64 len = ins->offset;
fbf48bb0 9867 int qgroup_released;
9729f10a 9868 int ret;
203f44c5
QW
9869
9870 memset(&stack_fi, 0, sizeof(stack_fi));
9871
9872 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9873 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9874 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9875 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9876 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9877 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9878 /* Encryption and other encoding is reserved and all 0 */
9879
fbf48bb0
QW
9880 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
9881 if (qgroup_released < 0)
9882 return ERR_PTR(qgroup_released);
8fccebfa
FM
9883
9884 if (trans) {
90dffd0c 9885 ret = insert_reserved_file_extent(trans, inode,
2766ff61 9886 file_offset, &stack_fi,
fbf48bb0 9887 true, qgroup_released);
8fccebfa 9888 if (ret)
a3ee79bd 9889 goto free_qgroup;
8fccebfa
FM
9890 return trans;
9891 }
9892
9893 extent_info.disk_offset = start;
9894 extent_info.disk_len = len;
9895 extent_info.data_offset = 0;
9896 extent_info.data_len = len;
9897 extent_info.file_offset = file_offset;
9898 extent_info.extent_buf = (char *)&stack_fi;
8fccebfa 9899 extent_info.is_new_extent = true;
983d8209 9900 extent_info.update_times = true;
fbf48bb0 9901 extent_info.qgroup_reserved = qgroup_released;
8fccebfa
FM
9902 extent_info.insertions = 0;
9903
9904 path = btrfs_alloc_path();
a3ee79bd
QW
9905 if (!path) {
9906 ret = -ENOMEM;
9907 goto free_qgroup;
9908 }
8fccebfa 9909
bfc78479 9910 ret = btrfs_replace_file_extents(inode, path, file_offset,
8fccebfa
FM
9911 file_offset + len - 1, &extent_info,
9912 &trans);
9913 btrfs_free_path(path);
9914 if (ret)
a3ee79bd 9915 goto free_qgroup;
8fccebfa 9916 return trans;
a3ee79bd
QW
9917
9918free_qgroup:
9919 /*
9920 * We have released qgroup data range at the beginning of the function,
9921 * and normally qgroup_released bytes will be freed when committing
9922 * transaction.
9923 * But if we error out early, we have to free what we have released
9924 * or we leak qgroup data reservation.
9925 */
9926 btrfs_qgroup_free_refroot(inode->root->fs_info,
9927 inode->root->root_key.objectid, qgroup_released,
9928 BTRFS_QGROUP_RSV_DATA);
9929 return ERR_PTR(ret);
203f44c5 9930}
8fccebfa 9931
0af3d00b
JB
9932static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9933 u64 start, u64 num_bytes, u64 min_size,
9934 loff_t actual_len, u64 *alloc_hint,
9935 struct btrfs_trans_handle *trans)
d899e052 9936{
0b246afa 9937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5dc562c5
JB
9938 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9939 struct extent_map *em;
d899e052
YZ
9940 struct btrfs_root *root = BTRFS_I(inode)->root;
9941 struct btrfs_key ins;
d899e052 9942 u64 cur_offset = start;
b778cf96 9943 u64 clear_offset = start;
55a61d1d 9944 u64 i_size;
154ea289 9945 u64 cur_bytes;
0b670dc4 9946 u64 last_alloc = (u64)-1;
d899e052 9947 int ret = 0;
0af3d00b 9948 bool own_trans = true;
18513091 9949 u64 end = start + num_bytes - 1;
d899e052 9950
0af3d00b
JB
9951 if (trans)
9952 own_trans = false;
d899e052 9953 while (num_bytes > 0) {
ee22184b 9954 cur_bytes = min_t(u64, num_bytes, SZ_256M);
154ea289 9955 cur_bytes = max(cur_bytes, min_size);
0b670dc4
JB
9956 /*
9957 * If we are severely fragmented we could end up with really
9958 * small allocations, so if the allocator is returning small
9959 * chunks lets make its job easier by only searching for those
9960 * sized chunks.
9961 */
9962 cur_bytes = min(cur_bytes, last_alloc);
18513091
WX
9963 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9964 min_size, 0, *alloc_hint, &ins, 1, 0);
8fccebfa 9965 if (ret)
a22285a6 9966 break;
b778cf96
JB
9967
9968 /*
9969 * We've reserved this space, and thus converted it from
9970 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9971 * from here on out we will only need to clear our reservation
9972 * for the remaining unreserved area, so advance our
9973 * clear_offset by our extent size.
9974 */
9975 clear_offset += ins.offset;
5a303d5d 9976
0b670dc4 9977 last_alloc = ins.offset;
90dffd0c
NB
9978 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9979 &ins, cur_offset);
1afc708d
FM
9980 /*
9981 * Now that we inserted the prealloc extent we can finally
9982 * decrement the number of reservations in the block group.
9983 * If we did it before, we could race with relocation and have
9984 * relocation miss the reserved extent, making it fail later.
9985 */
9986 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
8fccebfa
FM
9987 if (IS_ERR(trans)) {
9988 ret = PTR_ERR(trans);
2ff7e61e 9989 btrfs_free_reserved_extent(fs_info, ins.objectid,
e570fd27 9990 ins.offset, 0);
79787eaa
JM
9991 break;
9992 }
31193213 9993
dcdbc059 9994 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
a1ed835e 9995 cur_offset + ins.offset -1, 0);
5a303d5d 9996
5dc562c5
JB
9997 em = alloc_extent_map();
9998 if (!em) {
23e3337f 9999 btrfs_set_inode_full_sync(BTRFS_I(inode));
5dc562c5
JB
10000 goto next;
10001 }
10002
10003 em->start = cur_offset;
10004 em->orig_start = cur_offset;
10005 em->len = ins.offset;
10006 em->block_start = ins.objectid;
10007 em->block_len = ins.offset;
b4939680 10008 em->orig_block_len = ins.offset;
cc95bef6 10009 em->ram_bytes = ins.offset;
5dc562c5
JB
10010 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10011 em->generation = trans->transid;
10012
10013 while (1) {
10014 write_lock(&em_tree->lock);
09a2a8f9 10015 ret = add_extent_mapping(em_tree, em, 1);
5dc562c5
JB
10016 write_unlock(&em_tree->lock);
10017 if (ret != -EEXIST)
10018 break;
dcdbc059 10019 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
5dc562c5
JB
10020 cur_offset + ins.offset - 1,
10021 0);
10022 }
10023 free_extent_map(em);
10024next:
d899e052
YZ
10025 num_bytes -= ins.offset;
10026 cur_offset += ins.offset;
efa56464 10027 *alloc_hint = ins.objectid + ins.offset;
5a303d5d 10028
0c4d2d95 10029 inode_inc_iversion(inode);
c2050a45 10030 inode->i_ctime = current_time(inode);
6cbff00f 10031 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
d899e052 10032 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
efa56464
YZ
10033 (actual_len > inode->i_size) &&
10034 (cur_offset > inode->i_size)) {
d1ea6a61 10035 if (cur_offset > actual_len)
55a61d1d 10036 i_size = actual_len;
d1ea6a61 10037 else
55a61d1d
JB
10038 i_size = cur_offset;
10039 i_size_write(inode, i_size);
76aea537 10040 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
5a303d5d
YZ
10041 }
10042
9a56fcd1 10043 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
79787eaa
JM
10044
10045 if (ret) {
66642832 10046 btrfs_abort_transaction(trans, ret);
79787eaa 10047 if (own_trans)
3a45bb20 10048 btrfs_end_transaction(trans);
79787eaa
JM
10049 break;
10050 }
d899e052 10051
8fccebfa 10052 if (own_trans) {
3a45bb20 10053 btrfs_end_transaction(trans);
8fccebfa
FM
10054 trans = NULL;
10055 }
5a303d5d 10056 }
b778cf96 10057 if (clear_offset < end)
25ce28ca 10058 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
b778cf96 10059 end - clear_offset + 1);
d899e052
YZ
10060 return ret;
10061}
10062
0af3d00b
JB
10063int btrfs_prealloc_file_range(struct inode *inode, int mode,
10064 u64 start, u64 num_bytes, u64 min_size,
10065 loff_t actual_len, u64 *alloc_hint)
10066{
10067 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10068 min_size, actual_len, alloc_hint,
10069 NULL);
10070}
10071
10072int btrfs_prealloc_file_range_trans(struct inode *inode,
10073 struct btrfs_trans_handle *trans, int mode,
10074 u64 start, u64 num_bytes, u64 min_size,
10075 loff_t actual_len, u64 *alloc_hint)
10076{
10077 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10078 min_size, actual_len, alloc_hint, trans);
10079}
10080
549c7297
CB
10081static int btrfs_permission(struct user_namespace *mnt_userns,
10082 struct inode *inode, int mask)
fdebe2bd 10083{
b83cc969 10084 struct btrfs_root *root = BTRFS_I(inode)->root;
cb6db4e5 10085 umode_t mode = inode->i_mode;
b83cc969 10086
cb6db4e5
JM
10087 if (mask & MAY_WRITE &&
10088 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10089 if (btrfs_root_readonly(root))
10090 return -EROFS;
10091 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10092 return -EACCES;
10093 }
3bc71ba0 10094 return generic_permission(mnt_userns, inode, mask);
fdebe2bd 10095}
39279cc3 10096
549c7297
CB
10097static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10098 struct dentry *dentry, umode_t mode)
ef3b9af5 10099{
2ff7e61e 10100 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
ef3b9af5
FM
10101 struct btrfs_trans_handle *trans;
10102 struct btrfs_root *root = BTRFS_I(dir)->root;
a1fd0c35 10103 struct inode *inode;
3538d68d
OS
10104 struct btrfs_new_inode_args new_inode_args = {
10105 .dir = dir,
10106 .dentry = dentry,
10107 .orphan = true,
10108 };
10109 unsigned int trans_num_items;
a1fd0c35
OS
10110 int ret;
10111
10112 inode = new_inode(dir->i_sb);
10113 if (!inode)
10114 return -ENOMEM;
10115 inode_init_owner(mnt_userns, inode, dir, mode);
10116 inode->i_fop = &btrfs_file_operations;
10117 inode->i_op = &btrfs_file_inode_operations;
10118 inode->i_mapping->a_ops = &btrfs_aops;
ef3b9af5 10119
3538d68d
OS
10120 new_inode_args.inode = inode;
10121 ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
caae78e0
OS
10122 if (ret)
10123 goto out_inode;
3538d68d
OS
10124
10125 trans = btrfs_start_transaction(root, trans_num_items);
a1fd0c35 10126 if (IS_ERR(trans)) {
3538d68d
OS
10127 ret = PTR_ERR(trans);
10128 goto out_new_inode_args;
a1fd0c35 10129 }
ef3b9af5 10130
caae78e0 10131 ret = btrfs_create_new_inode(trans, &new_inode_args);
ef3b9af5 10132
5762b5c9 10133 /*
3538d68d
OS
10134 * We set number of links to 0 in btrfs_create_new_inode(), and here we
10135 * set it to 1 because d_tmpfile() will issue a warning if the count is
10136 * 0, through:
5762b5c9
FM
10137 *
10138 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10139 */
10140 set_nlink(inode, 1);
caae78e0
OS
10141
10142 if (!ret) {
10143 d_tmpfile(dentry, inode);
10144 unlock_new_inode(inode);
10145 mark_inode_dirty(inode);
10146 }
10147
3a45bb20 10148 btrfs_end_transaction(trans);
2ff7e61e 10149 btrfs_btree_balance_dirty(fs_info);
3538d68d
OS
10150out_new_inode_args:
10151 btrfs_new_inode_args_destroy(&new_inode_args);
caae78e0
OS
10152out_inode:
10153 if (ret)
10154 iput(inode);
ef3b9af5
FM
10155 return ret;
10156}
10157
d2a91064 10158void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
c6100a4b 10159{
d2a91064 10160 struct btrfs_fs_info *fs_info = inode->root->fs_info;
c6100a4b
JB
10161 unsigned long index = start >> PAGE_SHIFT;
10162 unsigned long end_index = end >> PAGE_SHIFT;
10163 struct page *page;
d2a91064 10164 u32 len;
c6100a4b 10165
d2a91064
QW
10166 ASSERT(end + 1 - start <= U32_MAX);
10167 len = end + 1 - start;
c6100a4b 10168 while (index <= end_index) {
d2a91064 10169 page = find_get_page(inode->vfs_inode.i_mapping, index);
c6100a4b 10170 ASSERT(page); /* Pages should be in the extent_io_tree */
d2a91064
QW
10171
10172 btrfs_page_set_writeback(fs_info, page, start, len);
c6100a4b
JB
10173 put_page(page);
10174 index++;
10175 }
10176}
10177
1881fba8
OS
10178static int btrfs_encoded_io_compression_from_extent(
10179 struct btrfs_fs_info *fs_info,
10180 int compress_type)
10181{
10182 switch (compress_type) {
10183 case BTRFS_COMPRESS_NONE:
10184 return BTRFS_ENCODED_IO_COMPRESSION_NONE;
10185 case BTRFS_COMPRESS_ZLIB:
10186 return BTRFS_ENCODED_IO_COMPRESSION_ZLIB;
10187 case BTRFS_COMPRESS_LZO:
10188 /*
10189 * The LZO format depends on the sector size. 64K is the maximum
10190 * sector size that we support.
10191 */
10192 if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K)
10193 return -EINVAL;
10194 return BTRFS_ENCODED_IO_COMPRESSION_LZO_4K +
10195 (fs_info->sectorsize_bits - 12);
10196 case BTRFS_COMPRESS_ZSTD:
10197 return BTRFS_ENCODED_IO_COMPRESSION_ZSTD;
10198 default:
10199 return -EUCLEAN;
10200 }
10201}
10202
10203static ssize_t btrfs_encoded_read_inline(
10204 struct kiocb *iocb,
10205 struct iov_iter *iter, u64 start,
10206 u64 lockend,
10207 struct extent_state **cached_state,
10208 u64 extent_start, size_t count,
10209 struct btrfs_ioctl_encoded_io_args *encoded,
10210 bool *unlocked)
10211{
10212 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10213 struct btrfs_root *root = inode->root;
10214 struct btrfs_fs_info *fs_info = root->fs_info;
10215 struct extent_io_tree *io_tree = &inode->io_tree;
10216 struct btrfs_path *path;
10217 struct extent_buffer *leaf;
10218 struct btrfs_file_extent_item *item;
10219 u64 ram_bytes;
10220 unsigned long ptr;
10221 void *tmp;
10222 ssize_t ret;
10223
10224 path = btrfs_alloc_path();
10225 if (!path) {
10226 ret = -ENOMEM;
10227 goto out;
10228 }
10229 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
10230 extent_start, 0);
10231 if (ret) {
10232 if (ret > 0) {
10233 /* The extent item disappeared? */
10234 ret = -EIO;
10235 }
10236 goto out;
10237 }
10238 leaf = path->nodes[0];
10239 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
10240
10241 ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
10242 ptr = btrfs_file_extent_inline_start(item);
10243
10244 encoded->len = min_t(u64, extent_start + ram_bytes,
10245 inode->vfs_inode.i_size) - iocb->ki_pos;
10246 ret = btrfs_encoded_io_compression_from_extent(fs_info,
10247 btrfs_file_extent_compression(leaf, item));
10248 if (ret < 0)
10249 goto out;
10250 encoded->compression = ret;
10251 if (encoded->compression) {
10252 size_t inline_size;
10253
10254 inline_size = btrfs_file_extent_inline_item_len(leaf,
10255 path->slots[0]);
10256 if (inline_size > count) {
10257 ret = -ENOBUFS;
10258 goto out;
10259 }
10260 count = inline_size;
10261 encoded->unencoded_len = ram_bytes;
10262 encoded->unencoded_offset = iocb->ki_pos - extent_start;
10263 } else {
10264 count = min_t(u64, count, encoded->len);
10265 encoded->len = count;
10266 encoded->unencoded_len = count;
10267 ptr += iocb->ki_pos - extent_start;
10268 }
10269
10270 tmp = kmalloc(count, GFP_NOFS);
10271 if (!tmp) {
10272 ret = -ENOMEM;
10273 goto out;
10274 }
10275 read_extent_buffer(leaf, tmp, ptr, count);
10276 btrfs_release_path(path);
10277 unlock_extent_cached(io_tree, start, lockend, cached_state);
10278 btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED);
10279 *unlocked = true;
10280
10281 ret = copy_to_iter(tmp, count, iter);
10282 if (ret != count)
10283 ret = -EFAULT;
10284 kfree(tmp);
10285out:
10286 btrfs_free_path(path);
10287 return ret;
10288}
10289
10290struct btrfs_encoded_read_private {
10291 struct btrfs_inode *inode;
10292 u64 file_offset;
10293 wait_queue_head_t wait;
10294 atomic_t pending;
10295 blk_status_t status;
10296 bool skip_csum;
10297};
10298
10299static blk_status_t submit_encoded_read_bio(struct btrfs_inode *inode,
10300 struct bio *bio, int mirror_num)
10301{
10302 struct btrfs_encoded_read_private *priv = bio->bi_private;
10303 struct btrfs_bio *bbio = btrfs_bio(bio);
10304 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10305 blk_status_t ret;
10306
10307 if (!priv->skip_csum) {
10308 ret = btrfs_lookup_bio_sums(&inode->vfs_inode, bio, NULL);
10309 if (ret)
10310 return ret;
10311 }
10312
10313 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
10314 if (ret) {
10315 btrfs_bio_free_csum(bbio);
10316 return ret;
10317 }
10318
10319 atomic_inc(&priv->pending);
10320 ret = btrfs_map_bio(fs_info, bio, mirror_num);
10321 if (ret) {
10322 atomic_dec(&priv->pending);
10323 btrfs_bio_free_csum(bbio);
10324 }
10325 return ret;
10326}
10327
10328static blk_status_t btrfs_encoded_read_verify_csum(struct btrfs_bio *bbio)
10329{
10330 const bool uptodate = (bbio->bio.bi_status == BLK_STS_OK);
10331 struct btrfs_encoded_read_private *priv = bbio->bio.bi_private;
10332 struct btrfs_inode *inode = priv->inode;
10333 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10334 u32 sectorsize = fs_info->sectorsize;
10335 struct bio_vec *bvec;
10336 struct bvec_iter_all iter_all;
10337 u64 start = priv->file_offset;
10338 u32 bio_offset = 0;
10339
10340 if (priv->skip_csum || !uptodate)
10341 return bbio->bio.bi_status;
10342
10343 bio_for_each_segment_all(bvec, &bbio->bio, iter_all) {
10344 unsigned int i, nr_sectors, pgoff;
10345
10346 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
10347 pgoff = bvec->bv_offset;
10348 for (i = 0; i < nr_sectors; i++) {
10349 ASSERT(pgoff < PAGE_SIZE);
10350 if (check_data_csum(&inode->vfs_inode, bbio, bio_offset,
10351 bvec->bv_page, pgoff, start))
10352 return BLK_STS_IOERR;
10353 start += sectorsize;
10354 bio_offset += sectorsize;
10355 pgoff += sectorsize;
10356 }
10357 }
10358 return BLK_STS_OK;
10359}
10360
10361static void btrfs_encoded_read_endio(struct bio *bio)
10362{
10363 struct btrfs_encoded_read_private *priv = bio->bi_private;
10364 struct btrfs_bio *bbio = btrfs_bio(bio);
10365 blk_status_t status;
10366
10367 status = btrfs_encoded_read_verify_csum(bbio);
10368 if (status) {
10369 /*
10370 * The memory barrier implied by the atomic_dec_return() here
10371 * pairs with the memory barrier implied by the
10372 * atomic_dec_return() or io_wait_event() in
10373 * btrfs_encoded_read_regular_fill_pages() to ensure that this
10374 * write is observed before the load of status in
10375 * btrfs_encoded_read_regular_fill_pages().
10376 */
10377 WRITE_ONCE(priv->status, status);
10378 }
10379 if (!atomic_dec_return(&priv->pending))
10380 wake_up(&priv->wait);
10381 btrfs_bio_free_csum(bbio);
10382 bio_put(bio);
10383}
10384
10385static int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
10386 u64 file_offset,
10387 u64 disk_bytenr,
10388 u64 disk_io_size,
10389 struct page **pages)
10390{
10391 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10392 struct btrfs_encoded_read_private priv = {
10393 .inode = inode,
10394 .file_offset = file_offset,
10395 .pending = ATOMIC_INIT(1),
10396 .skip_csum = (inode->flags & BTRFS_INODE_NODATASUM),
10397 };
10398 unsigned long i = 0;
10399 u64 cur = 0;
10400 int ret;
10401
10402 init_waitqueue_head(&priv.wait);
10403 /*
10404 * Submit bios for the extent, splitting due to bio or stripe limits as
10405 * necessary.
10406 */
10407 while (cur < disk_io_size) {
10408 struct extent_map *em;
10409 struct btrfs_io_geometry geom;
10410 struct bio *bio = NULL;
10411 u64 remaining;
10412
10413 em = btrfs_get_chunk_map(fs_info, disk_bytenr + cur,
10414 disk_io_size - cur);
10415 if (IS_ERR(em)) {
10416 ret = PTR_ERR(em);
10417 } else {
10418 ret = btrfs_get_io_geometry(fs_info, em, BTRFS_MAP_READ,
10419 disk_bytenr + cur, &geom);
10420 free_extent_map(em);
10421 }
10422 if (ret) {
10423 WRITE_ONCE(priv.status, errno_to_blk_status(ret));
10424 break;
10425 }
10426 remaining = min(geom.len, disk_io_size - cur);
10427 while (bio || remaining) {
10428 size_t bytes = min_t(u64, remaining, PAGE_SIZE);
10429
10430 if (!bio) {
10431 bio = btrfs_bio_alloc(BIO_MAX_VECS);
10432 bio->bi_iter.bi_sector =
10433 (disk_bytenr + cur) >> SECTOR_SHIFT;
10434 bio->bi_end_io = btrfs_encoded_read_endio;
10435 bio->bi_private = &priv;
10436 bio->bi_opf = REQ_OP_READ;
10437 }
10438
10439 if (!bytes ||
10440 bio_add_page(bio, pages[i], bytes, 0) < bytes) {
10441 blk_status_t status;
10442
10443 status = submit_encoded_read_bio(inode, bio, 0);
10444 if (status) {
10445 WRITE_ONCE(priv.status, status);
10446 bio_put(bio);
10447 goto out;
10448 }
10449 bio = NULL;
10450 continue;
10451 }
10452
10453 i++;
10454 cur += bytes;
10455 remaining -= bytes;
10456 }
10457 }
10458
10459out:
10460 if (atomic_dec_return(&priv.pending))
10461 io_wait_event(priv.wait, !atomic_read(&priv.pending));
10462 /* See btrfs_encoded_read_endio() for ordering. */
10463 return blk_status_to_errno(READ_ONCE(priv.status));
10464}
10465
10466static ssize_t btrfs_encoded_read_regular(struct kiocb *iocb,
10467 struct iov_iter *iter,
10468 u64 start, u64 lockend,
10469 struct extent_state **cached_state,
10470 u64 disk_bytenr, u64 disk_io_size,
10471 size_t count, bool compressed,
10472 bool *unlocked)
10473{
10474 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10475 struct extent_io_tree *io_tree = &inode->io_tree;
10476 struct page **pages;
10477 unsigned long nr_pages, i;
10478 u64 cur;
10479 size_t page_offset;
10480 ssize_t ret;
10481
10482 nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
10483 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
10484 if (!pages)
10485 return -ENOMEM;
dd137dd1
STD
10486 ret = btrfs_alloc_page_array(nr_pages, pages);
10487 if (ret) {
10488 ret = -ENOMEM;
10489 goto out;
1881fba8 10490 }
1881fba8
OS
10491
10492 ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr,
10493 disk_io_size, pages);
10494 if (ret)
10495 goto out;
10496
10497 unlock_extent_cached(io_tree, start, lockend, cached_state);
10498 btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED);
10499 *unlocked = true;
10500
10501 if (compressed) {
10502 i = 0;
10503 page_offset = 0;
10504 } else {
10505 i = (iocb->ki_pos - start) >> PAGE_SHIFT;
10506 page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1);
10507 }
10508 cur = 0;
10509 while (cur < count) {
10510 size_t bytes = min_t(size_t, count - cur,
10511 PAGE_SIZE - page_offset);
10512
10513 if (copy_page_to_iter(pages[i], page_offset, bytes,
10514 iter) != bytes) {
10515 ret = -EFAULT;
10516 goto out;
10517 }
10518 i++;
10519 cur += bytes;
10520 page_offset = 0;
10521 }
10522 ret = count;
10523out:
10524 for (i = 0; i < nr_pages; i++) {
10525 if (pages[i])
10526 __free_page(pages[i]);
10527 }
10528 kfree(pages);
10529 return ret;
10530}
10531
10532ssize_t btrfs_encoded_read(struct kiocb *iocb, struct iov_iter *iter,
10533 struct btrfs_ioctl_encoded_io_args *encoded)
10534{
10535 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10536 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10537 struct extent_io_tree *io_tree = &inode->io_tree;
10538 ssize_t ret;
10539 size_t count = iov_iter_count(iter);
10540 u64 start, lockend, disk_bytenr, disk_io_size;
10541 struct extent_state *cached_state = NULL;
10542 struct extent_map *em;
10543 bool unlocked = false;
10544
10545 file_accessed(iocb->ki_filp);
10546
10547 btrfs_inode_lock(&inode->vfs_inode, BTRFS_ILOCK_SHARED);
10548
10549 if (iocb->ki_pos >= inode->vfs_inode.i_size) {
10550 btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED);
10551 return 0;
10552 }
10553 start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize);
10554 /*
10555 * We don't know how long the extent containing iocb->ki_pos is, but if
10556 * it's compressed we know that it won't be longer than this.
10557 */
10558 lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
10559
10560 for (;;) {
10561 struct btrfs_ordered_extent *ordered;
10562
10563 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start,
10564 lockend - start + 1);
10565 if (ret)
10566 goto out_unlock_inode;
10567 lock_extent_bits(io_tree, start, lockend, &cached_state);
10568 ordered = btrfs_lookup_ordered_range(inode, start,
10569 lockend - start + 1);
10570 if (!ordered)
10571 break;
10572 btrfs_put_ordered_extent(ordered);
10573 unlock_extent_cached(io_tree, start, lockend, &cached_state);
10574 cond_resched();
10575 }
10576
10577 em = btrfs_get_extent(inode, NULL, 0, start, lockend - start + 1);
10578 if (IS_ERR(em)) {
10579 ret = PTR_ERR(em);
10580 goto out_unlock_extent;
10581 }
10582
10583 if (em->block_start == EXTENT_MAP_INLINE) {
10584 u64 extent_start = em->start;
10585
10586 /*
10587 * For inline extents we get everything we need out of the
10588 * extent item.
10589 */
10590 free_extent_map(em);
10591 em = NULL;
10592 ret = btrfs_encoded_read_inline(iocb, iter, start, lockend,
10593 &cached_state, extent_start,
10594 count, encoded, &unlocked);
10595 goto out;
10596 }
10597
10598 /*
10599 * We only want to return up to EOF even if the extent extends beyond
10600 * that.
10601 */
10602 encoded->len = min_t(u64, extent_map_end(em),
10603 inode->vfs_inode.i_size) - iocb->ki_pos;
10604 if (em->block_start == EXTENT_MAP_HOLE ||
10605 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
10606 disk_bytenr = EXTENT_MAP_HOLE;
10607 count = min_t(u64, count, encoded->len);
10608 encoded->len = count;
10609 encoded->unencoded_len = count;
10610 } else if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10611 disk_bytenr = em->block_start;
10612 /*
10613 * Bail if the buffer isn't large enough to return the whole
10614 * compressed extent.
10615 */
10616 if (em->block_len > count) {
10617 ret = -ENOBUFS;
10618 goto out_em;
10619 }
10620 disk_io_size = count = em->block_len;
10621 encoded->unencoded_len = em->ram_bytes;
10622 encoded->unencoded_offset = iocb->ki_pos - em->orig_start;
10623 ret = btrfs_encoded_io_compression_from_extent(fs_info,
10624 em->compress_type);
10625 if (ret < 0)
10626 goto out_em;
10627 encoded->compression = ret;
10628 } else {
10629 disk_bytenr = em->block_start + (start - em->start);
10630 if (encoded->len > count)
10631 encoded->len = count;
10632 /*
10633 * Don't read beyond what we locked. This also limits the page
10634 * allocations that we'll do.
10635 */
10636 disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start;
10637 count = start + disk_io_size - iocb->ki_pos;
10638 encoded->len = count;
10639 encoded->unencoded_len = count;
10640 disk_io_size = ALIGN(disk_io_size, fs_info->sectorsize);
10641 }
10642 free_extent_map(em);
10643 em = NULL;
10644
10645 if (disk_bytenr == EXTENT_MAP_HOLE) {
10646 unlock_extent_cached(io_tree, start, lockend, &cached_state);
10647 btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED);
10648 unlocked = true;
10649 ret = iov_iter_zero(count, iter);
10650 if (ret != count)
10651 ret = -EFAULT;
10652 } else {
10653 ret = btrfs_encoded_read_regular(iocb, iter, start, lockend,
10654 &cached_state, disk_bytenr,
10655 disk_io_size, count,
10656 encoded->compression,
10657 &unlocked);
10658 }
10659
10660out:
10661 if (ret >= 0)
10662 iocb->ki_pos += encoded->len;
10663out_em:
10664 free_extent_map(em);
10665out_unlock_extent:
10666 if (!unlocked)
10667 unlock_extent_cached(io_tree, start, lockend, &cached_state);
10668out_unlock_inode:
10669 if (!unlocked)
10670 btrfs_inode_unlock(&inode->vfs_inode, BTRFS_ILOCK_SHARED);
10671 return ret;
10672}
10673
7c0c7269
OS
10674ssize_t btrfs_do_encoded_write(struct kiocb *iocb, struct iov_iter *from,
10675 const struct btrfs_ioctl_encoded_io_args *encoded)
10676{
10677 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10678 struct btrfs_root *root = inode->root;
10679 struct btrfs_fs_info *fs_info = root->fs_info;
10680 struct extent_io_tree *io_tree = &inode->io_tree;
10681 struct extent_changeset *data_reserved = NULL;
10682 struct extent_state *cached_state = NULL;
10683 int compression;
10684 size_t orig_count;
10685 u64 start, end;
10686 u64 num_bytes, ram_bytes, disk_num_bytes;
10687 unsigned long nr_pages, i;
10688 struct page **pages;
10689 struct btrfs_key ins;
10690 bool extent_reserved = false;
10691 struct extent_map *em;
10692 ssize_t ret;
10693
10694 switch (encoded->compression) {
10695 case BTRFS_ENCODED_IO_COMPRESSION_ZLIB:
10696 compression = BTRFS_COMPRESS_ZLIB;
10697 break;
10698 case BTRFS_ENCODED_IO_COMPRESSION_ZSTD:
10699 compression = BTRFS_COMPRESS_ZSTD;
10700 break;
10701 case BTRFS_ENCODED_IO_COMPRESSION_LZO_4K:
10702 case BTRFS_ENCODED_IO_COMPRESSION_LZO_8K:
10703 case BTRFS_ENCODED_IO_COMPRESSION_LZO_16K:
10704 case BTRFS_ENCODED_IO_COMPRESSION_LZO_32K:
10705 case BTRFS_ENCODED_IO_COMPRESSION_LZO_64K:
10706 /* The sector size must match for LZO. */
10707 if (encoded->compression -
10708 BTRFS_ENCODED_IO_COMPRESSION_LZO_4K + 12 !=
10709 fs_info->sectorsize_bits)
10710 return -EINVAL;
10711 compression = BTRFS_COMPRESS_LZO;
10712 break;
10713 default:
10714 return -EINVAL;
10715 }
10716 if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE)
10717 return -EINVAL;
10718
10719 orig_count = iov_iter_count(from);
10720
10721 /* The extent size must be sane. */
10722 if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED ||
10723 orig_count > BTRFS_MAX_COMPRESSED || orig_count == 0)
10724 return -EINVAL;
10725
10726 /*
10727 * The compressed data must be smaller than the decompressed data.
10728 *
10729 * It's of course possible for data to compress to larger or the same
10730 * size, but the buffered I/O path falls back to no compression for such
10731 * data, and we don't want to break any assumptions by creating these
10732 * extents.
10733 *
10734 * Note that this is less strict than the current check we have that the
10735 * compressed data must be at least one sector smaller than the
10736 * decompressed data. We only want to enforce the weaker requirement
10737 * from old kernels that it is at least one byte smaller.
10738 */
10739 if (orig_count >= encoded->unencoded_len)
10740 return -EINVAL;
10741
10742 /* The extent must start on a sector boundary. */
10743 start = iocb->ki_pos;
10744 if (!IS_ALIGNED(start, fs_info->sectorsize))
10745 return -EINVAL;
10746
10747 /*
10748 * The extent must end on a sector boundary. However, we allow a write
10749 * which ends at or extends i_size to have an unaligned length; we round
10750 * up the extent size and set i_size to the unaligned end.
10751 */
10752 if (start + encoded->len < inode->vfs_inode.i_size &&
10753 !IS_ALIGNED(start + encoded->len, fs_info->sectorsize))
10754 return -EINVAL;
10755
10756 /* Finally, the offset in the unencoded data must be sector-aligned. */
10757 if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize))
10758 return -EINVAL;
10759
10760 num_bytes = ALIGN(encoded->len, fs_info->sectorsize);
10761 ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize);
10762 end = start + num_bytes - 1;
10763
10764 /*
10765 * If the extent cannot be inline, the compressed data on disk must be
10766 * sector-aligned. For convenience, we extend it with zeroes if it
10767 * isn't.
10768 */
10769 disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize);
10770 nr_pages = DIV_ROUND_UP(disk_num_bytes, PAGE_SIZE);
10771 pages = kvcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL_ACCOUNT);
10772 if (!pages)
10773 return -ENOMEM;
10774 for (i = 0; i < nr_pages; i++) {
10775 size_t bytes = min_t(size_t, PAGE_SIZE, iov_iter_count(from));
10776 char *kaddr;
10777
10778 pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);
10779 if (!pages[i]) {
10780 ret = -ENOMEM;
10781 goto out_pages;
10782 }
10783 kaddr = kmap(pages[i]);
10784 if (copy_from_iter(kaddr, bytes, from) != bytes) {
10785 kunmap(pages[i]);
10786 ret = -EFAULT;
10787 goto out_pages;
10788 }
10789 if (bytes < PAGE_SIZE)
10790 memset(kaddr + bytes, 0, PAGE_SIZE - bytes);
10791 kunmap(pages[i]);
10792 }
10793
10794 for (;;) {
10795 struct btrfs_ordered_extent *ordered;
10796
10797 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes);
10798 if (ret)
10799 goto out_pages;
10800 ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping,
10801 start >> PAGE_SHIFT,
10802 end >> PAGE_SHIFT);
10803 if (ret)
10804 goto out_pages;
10805 lock_extent_bits(io_tree, start, end, &cached_state);
10806 ordered = btrfs_lookup_ordered_range(inode, start, num_bytes);
10807 if (!ordered &&
10808 !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end))
10809 break;
10810 if (ordered)
10811 btrfs_put_ordered_extent(ordered);
10812 unlock_extent_cached(io_tree, start, end, &cached_state);
10813 cond_resched();
10814 }
10815
10816 /*
10817 * We don't use the higher-level delalloc space functions because our
10818 * num_bytes and disk_num_bytes are different.
10819 */
10820 ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes);
10821 if (ret)
10822 goto out_unlock;
10823 ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes);
10824 if (ret)
10825 goto out_free_data_space;
d4135134
FM
10826 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes,
10827 false);
7c0c7269
OS
10828 if (ret)
10829 goto out_qgroup_free_data;
10830
10831 /* Try an inline extent first. */
10832 if (start == 0 && encoded->unencoded_len == encoded->len &&
10833 encoded->unencoded_offset == 0) {
10834 ret = cow_file_range_inline(inode, encoded->len, orig_count,
10835 compression, pages, true);
10836 if (ret <= 0) {
10837 if (ret == 0)
10838 ret = orig_count;
10839 goto out_delalloc_release;
10840 }
10841 }
10842
10843 ret = btrfs_reserve_extent(root, disk_num_bytes, disk_num_bytes,
10844 disk_num_bytes, 0, 0, &ins, 1, 1);
10845 if (ret)
10846 goto out_delalloc_release;
10847 extent_reserved = true;
10848
10849 em = create_io_em(inode, start, num_bytes,
10850 start - encoded->unencoded_offset, ins.objectid,
10851 ins.offset, ins.offset, ram_bytes, compression,
10852 BTRFS_ORDERED_COMPRESSED);
10853 if (IS_ERR(em)) {
10854 ret = PTR_ERR(em);
10855 goto out_free_reserved;
10856 }
10857 free_extent_map(em);
10858
10859 ret = btrfs_add_ordered_extent(inode, start, num_bytes, ram_bytes,
10860 ins.objectid, ins.offset,
10861 encoded->unencoded_offset,
10862 (1 << BTRFS_ORDERED_ENCODED) |
10863 (1 << BTRFS_ORDERED_COMPRESSED),
10864 compression);
10865 if (ret) {
10866 btrfs_drop_extent_cache(inode, start, end, 0);
10867 goto out_free_reserved;
10868 }
10869 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10870
10871 if (start + encoded->len > inode->vfs_inode.i_size)
10872 i_size_write(&inode->vfs_inode, start + encoded->len);
10873
10874 unlock_extent_cached(io_tree, start, end, &cached_state);
10875
10876 btrfs_delalloc_release_extents(inode, num_bytes);
10877
10878 if (btrfs_submit_compressed_write(inode, start, num_bytes, ins.objectid,
10879 ins.offset, pages, nr_pages, 0, NULL,
10880 false)) {
10881 btrfs_writepage_endio_finish_ordered(inode, pages[0], start, end, 0);
10882 ret = -EIO;
10883 goto out_pages;
10884 }
10885 ret = orig_count;
10886 goto out;
10887
10888out_free_reserved:
10889 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10890 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
10891out_delalloc_release:
10892 btrfs_delalloc_release_extents(inode, num_bytes);
10893 btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0);
10894out_qgroup_free_data:
10895 if (ret < 0)
10896 btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes);
10897out_free_data_space:
10898 /*
10899 * If btrfs_reserve_extent() succeeded, then we already decremented
10900 * bytes_may_use.
10901 */
10902 if (!extent_reserved)
10903 btrfs_free_reserved_data_space_noquota(fs_info, disk_num_bytes);
10904out_unlock:
10905 unlock_extent_cached(io_tree, start, end, &cached_state);
10906out_pages:
10907 for (i = 0; i < nr_pages; i++) {
10908 if (pages[i])
10909 __free_page(pages[i]);
10910 }
10911 kvfree(pages);
10912out:
10913 if (ret >= 0)
10914 iocb->ki_pos += encoded->len;
10915 return ret;
10916}
10917
ed46ff3d
OS
10918#ifdef CONFIG_SWAP
10919/*
10920 * Add an entry indicating a block group or device which is pinned by a
10921 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10922 * negative errno on failure.
10923 */
10924static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10925 bool is_block_group)
10926{
10927 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10928 struct btrfs_swapfile_pin *sp, *entry;
10929 struct rb_node **p;
10930 struct rb_node *parent = NULL;
10931
10932 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10933 if (!sp)
10934 return -ENOMEM;
10935 sp->ptr = ptr;
10936 sp->inode = inode;
10937 sp->is_block_group = is_block_group;
195a49ea 10938 sp->bg_extent_count = 1;
ed46ff3d
OS
10939
10940 spin_lock(&fs_info->swapfile_pins_lock);
10941 p = &fs_info->swapfile_pins.rb_node;
10942 while (*p) {
10943 parent = *p;
10944 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10945 if (sp->ptr < entry->ptr ||
10946 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10947 p = &(*p)->rb_left;
10948 } else if (sp->ptr > entry->ptr ||
10949 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10950 p = &(*p)->rb_right;
10951 } else {
195a49ea
FM
10952 if (is_block_group)
10953 entry->bg_extent_count++;
ed46ff3d
OS
10954 spin_unlock(&fs_info->swapfile_pins_lock);
10955 kfree(sp);
10956 return 1;
10957 }
10958 }
10959 rb_link_node(&sp->node, parent, p);
10960 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10961 spin_unlock(&fs_info->swapfile_pins_lock);
10962 return 0;
10963}
10964
10965/* Free all of the entries pinned by this swapfile. */
10966static void btrfs_free_swapfile_pins(struct inode *inode)
10967{
10968 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10969 struct btrfs_swapfile_pin *sp;
10970 struct rb_node *node, *next;
10971
10972 spin_lock(&fs_info->swapfile_pins_lock);
10973 node = rb_first(&fs_info->swapfile_pins);
10974 while (node) {
10975 next = rb_next(node);
10976 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10977 if (sp->inode == inode) {
10978 rb_erase(&sp->node, &fs_info->swapfile_pins);
195a49ea
FM
10979 if (sp->is_block_group) {
10980 btrfs_dec_block_group_swap_extents(sp->ptr,
10981 sp->bg_extent_count);
ed46ff3d 10982 btrfs_put_block_group(sp->ptr);
195a49ea 10983 }
ed46ff3d
OS
10984 kfree(sp);
10985 }
10986 node = next;
10987 }
10988 spin_unlock(&fs_info->swapfile_pins_lock);
10989}
10990
10991struct btrfs_swap_info {
10992 u64 start;
10993 u64 block_start;
10994 u64 block_len;
10995 u64 lowest_ppage;
10996 u64 highest_ppage;
10997 unsigned long nr_pages;
10998 int nr_extents;
10999};
11000
11001static int btrfs_add_swap_extent(struct swap_info_struct *sis,
11002 struct btrfs_swap_info *bsi)
11003{
11004 unsigned long nr_pages;
c2f82263 11005 unsigned long max_pages;
ed46ff3d
OS
11006 u64 first_ppage, first_ppage_reported, next_ppage;
11007 int ret;
11008
c2f82263
FM
11009 /*
11010 * Our swapfile may have had its size extended after the swap header was
11011 * written. In that case activating the swapfile should not go beyond
11012 * the max size set in the swap header.
11013 */
11014 if (bsi->nr_pages >= sis->max)
11015 return 0;
11016
11017 max_pages = sis->max - bsi->nr_pages;
ed46ff3d
OS
11018 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
11019 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
11020 PAGE_SIZE) >> PAGE_SHIFT;
11021
11022 if (first_ppage >= next_ppage)
11023 return 0;
11024 nr_pages = next_ppage - first_ppage;
c2f82263 11025 nr_pages = min(nr_pages, max_pages);
ed46ff3d
OS
11026
11027 first_ppage_reported = first_ppage;
11028 if (bsi->start == 0)
11029 first_ppage_reported++;
11030 if (bsi->lowest_ppage > first_ppage_reported)
11031 bsi->lowest_ppage = first_ppage_reported;
11032 if (bsi->highest_ppage < (next_ppage - 1))
11033 bsi->highest_ppage = next_ppage - 1;
11034
11035 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
11036 if (ret < 0)
11037 return ret;
11038 bsi->nr_extents += ret;
11039 bsi->nr_pages += nr_pages;
11040 return 0;
11041}
11042
11043static void btrfs_swap_deactivate(struct file *file)
11044{
11045 struct inode *inode = file_inode(file);
11046
11047 btrfs_free_swapfile_pins(inode);
11048 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
11049}
11050
11051static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
11052 sector_t *span)
11053{
11054 struct inode *inode = file_inode(file);
dd0734f2
FM
11055 struct btrfs_root *root = BTRFS_I(inode)->root;
11056 struct btrfs_fs_info *fs_info = root->fs_info;
ed46ff3d
OS
11057 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
11058 struct extent_state *cached_state = NULL;
11059 struct extent_map *em = NULL;
11060 struct btrfs_device *device = NULL;
11061 struct btrfs_swap_info bsi = {
11062 .lowest_ppage = (sector_t)-1ULL,
11063 };
11064 int ret = 0;
11065 u64 isize;
11066 u64 start;
11067
11068 /*
11069 * If the swap file was just created, make sure delalloc is done. If the
11070 * file changes again after this, the user is doing something stupid and
11071 * we don't really care.
11072 */
11073 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
11074 if (ret)
11075 return ret;
11076
11077 /*
11078 * The inode is locked, so these flags won't change after we check them.
11079 */
11080 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
11081 btrfs_warn(fs_info, "swapfile must not be compressed");
11082 return -EINVAL;
11083 }
11084 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
11085 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
11086 return -EINVAL;
11087 }
11088 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
11089 btrfs_warn(fs_info, "swapfile must not be checksummed");
11090 return -EINVAL;
11091 }
11092
11093 /*
11094 * Balance or device remove/replace/resize can move stuff around from
c3e1f96c
GR
11095 * under us. The exclop protection makes sure they aren't running/won't
11096 * run concurrently while we are mapping the swap extents, and
11097 * fs_info->swapfile_pins prevents them from running while the swap
11098 * file is active and moving the extents. Note that this also prevents
11099 * a concurrent device add which isn't actually necessary, but it's not
ed46ff3d
OS
11100 * really worth the trouble to allow it.
11101 */
c3e1f96c 11102 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
ed46ff3d
OS
11103 btrfs_warn(fs_info,
11104 "cannot activate swapfile while exclusive operation is running");
11105 return -EBUSY;
11106 }
dd0734f2
FM
11107
11108 /*
11109 * Prevent snapshot creation while we are activating the swap file.
11110 * We do not want to race with snapshot creation. If snapshot creation
11111 * already started before we bumped nr_swapfiles from 0 to 1 and
11112 * completes before the first write into the swap file after it is
11113 * activated, than that write would fallback to COW.
11114 */
11115 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
11116 btrfs_exclop_finish(fs_info);
11117 btrfs_warn(fs_info,
11118 "cannot activate swapfile because snapshot creation is in progress");
11119 return -EINVAL;
11120 }
ed46ff3d
OS
11121 /*
11122 * Snapshots can create extents which require COW even if NODATACOW is
11123 * set. We use this counter to prevent snapshots. We must increment it
11124 * before walking the extents because we don't want a concurrent
11125 * snapshot to run after we've already checked the extents.
60021bd7
KH
11126 *
11127 * It is possible that subvolume is marked for deletion but still not
11128 * removed yet. To prevent this race, we check the root status before
11129 * activating the swapfile.
ed46ff3d 11130 */
60021bd7
KH
11131 spin_lock(&root->root_item_lock);
11132 if (btrfs_root_dead(root)) {
11133 spin_unlock(&root->root_item_lock);
11134
11135 btrfs_exclop_finish(fs_info);
11136 btrfs_warn(fs_info,
11137 "cannot activate swapfile because subvolume %llu is being deleted",
11138 root->root_key.objectid);
11139 return -EPERM;
11140 }
dd0734f2 11141 atomic_inc(&root->nr_swapfiles);
60021bd7 11142 spin_unlock(&root->root_item_lock);
ed46ff3d
OS
11143
11144 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
11145
11146 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
11147 start = 0;
11148 while (start < isize) {
11149 u64 logical_block_start, physical_block_start;
32da5386 11150 struct btrfs_block_group *bg;
ed46ff3d
OS
11151 u64 len = isize - start;
11152
39b07b5d 11153 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
ed46ff3d
OS
11154 if (IS_ERR(em)) {
11155 ret = PTR_ERR(em);
11156 goto out;
11157 }
11158
11159 if (em->block_start == EXTENT_MAP_HOLE) {
11160 btrfs_warn(fs_info, "swapfile must not have holes");
11161 ret = -EINVAL;
11162 goto out;
11163 }
11164 if (em->block_start == EXTENT_MAP_INLINE) {
11165 /*
11166 * It's unlikely we'll ever actually find ourselves
11167 * here, as a file small enough to fit inline won't be
11168 * big enough to store more than the swap header, but in
11169 * case something changes in the future, let's catch it
11170 * here rather than later.
11171 */
11172 btrfs_warn(fs_info, "swapfile must not be inline");
11173 ret = -EINVAL;
11174 goto out;
11175 }
11176 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
11177 btrfs_warn(fs_info, "swapfile must not be compressed");
11178 ret = -EINVAL;
11179 goto out;
11180 }
11181
11182 logical_block_start = em->block_start + (start - em->start);
11183 len = min(len, em->len - (start - em->start));
11184 free_extent_map(em);
11185 em = NULL;
11186
a84d5d42 11187 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
ed46ff3d
OS
11188 if (ret < 0) {
11189 goto out;
11190 } else if (ret) {
11191 ret = 0;
11192 } else {
11193 btrfs_warn(fs_info,
11194 "swapfile must not be copy-on-write");
11195 ret = -EINVAL;
11196 goto out;
11197 }
11198
11199 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
11200 if (IS_ERR(em)) {
11201 ret = PTR_ERR(em);
11202 goto out;
11203 }
11204
11205 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
11206 btrfs_warn(fs_info,
11207 "swapfile must have single data profile");
11208 ret = -EINVAL;
11209 goto out;
11210 }
11211
11212 if (device == NULL) {
11213 device = em->map_lookup->stripes[0].dev;
11214 ret = btrfs_add_swapfile_pin(inode, device, false);
11215 if (ret == 1)
11216 ret = 0;
11217 else if (ret)
11218 goto out;
11219 } else if (device != em->map_lookup->stripes[0].dev) {
11220 btrfs_warn(fs_info, "swapfile must be on one device");
11221 ret = -EINVAL;
11222 goto out;
11223 }
11224
11225 physical_block_start = (em->map_lookup->stripes[0].physical +
11226 (logical_block_start - em->start));
11227 len = min(len, em->len - (logical_block_start - em->start));
11228 free_extent_map(em);
11229 em = NULL;
11230
11231 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
11232 if (!bg) {
11233 btrfs_warn(fs_info,
11234 "could not find block group containing swapfile");
11235 ret = -EINVAL;
11236 goto out;
11237 }
11238
195a49ea
FM
11239 if (!btrfs_inc_block_group_swap_extents(bg)) {
11240 btrfs_warn(fs_info,
11241 "block group for swapfile at %llu is read-only%s",
11242 bg->start,
11243 atomic_read(&fs_info->scrubs_running) ?
11244 " (scrub running)" : "");
11245 btrfs_put_block_group(bg);
11246 ret = -EINVAL;
11247 goto out;
11248 }
11249
ed46ff3d
OS
11250 ret = btrfs_add_swapfile_pin(inode, bg, true);
11251 if (ret) {
11252 btrfs_put_block_group(bg);
11253 if (ret == 1)
11254 ret = 0;
11255 else
11256 goto out;
11257 }
11258
11259 if (bsi.block_len &&
11260 bsi.block_start + bsi.block_len == physical_block_start) {
11261 bsi.block_len += len;
11262 } else {
11263 if (bsi.block_len) {
11264 ret = btrfs_add_swap_extent(sis, &bsi);
11265 if (ret)
11266 goto out;
11267 }
11268 bsi.start = start;
11269 bsi.block_start = physical_block_start;
11270 bsi.block_len = len;
11271 }
11272
11273 start += len;
11274 }
11275
11276 if (bsi.block_len)
11277 ret = btrfs_add_swap_extent(sis, &bsi);
11278
11279out:
11280 if (!IS_ERR_OR_NULL(em))
11281 free_extent_map(em);
11282
11283 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
11284
11285 if (ret)
11286 btrfs_swap_deactivate(file);
11287
dd0734f2
FM
11288 btrfs_drew_write_unlock(&root->snapshot_lock);
11289
c3e1f96c 11290 btrfs_exclop_finish(fs_info);
ed46ff3d
OS
11291
11292 if (ret)
11293 return ret;
11294
11295 if (device)
11296 sis->bdev = device->bdev;
11297 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
11298 sis->max = bsi.nr_pages;
11299 sis->pages = bsi.nr_pages - 1;
11300 sis->highest_bit = bsi.nr_pages - 1;
11301 return bsi.nr_extents;
11302}
11303#else
11304static void btrfs_swap_deactivate(struct file *file)
11305{
11306}
11307
11308static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
11309 sector_t *span)
11310{
11311 return -EOPNOTSUPP;
11312}
11313#endif
11314
2766ff61
FM
11315/*
11316 * Update the number of bytes used in the VFS' inode. When we replace extents in
11317 * a range (clone, dedupe, fallocate's zero range), we must update the number of
11318 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
11319 * always get a correct value.
11320 */
11321void btrfs_update_inode_bytes(struct btrfs_inode *inode,
11322 const u64 add_bytes,
11323 const u64 del_bytes)
11324{
11325 if (add_bytes == del_bytes)
11326 return;
11327
11328 spin_lock(&inode->lock);
11329 if (del_bytes > 0)
11330 inode_sub_bytes(&inode->vfs_inode, del_bytes);
11331 if (add_bytes > 0)
11332 inode_add_bytes(&inode->vfs_inode, add_bytes);
11333 spin_unlock(&inode->lock);
11334}
11335
63c34cb4
FM
11336/**
11337 * Verify that there are no ordered extents for a given file range.
11338 *
11339 * @inode: The target inode.
11340 * @start: Start offset of the file range, should be sector size aligned.
11341 * @end: End offset (inclusive) of the file range, its value +1 should be
11342 * sector size aligned.
11343 *
11344 * This should typically be used for cases where we locked an inode's VFS lock in
11345 * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode,
11346 * we have flushed all delalloc in the range, we have waited for all ordered
11347 * extents in the range to complete and finally we have locked the file range in
11348 * the inode's io_tree.
11349 */
11350void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end)
11351{
11352 struct btrfs_root *root = inode->root;
11353 struct btrfs_ordered_extent *ordered;
11354
11355 if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
11356 return;
11357
11358 ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start);
11359 if (ordered) {
11360 btrfs_err(root->fs_info,
11361"found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])",
11362 start, end, btrfs_ino(inode), root->root_key.objectid,
11363 ordered->file_offset,
11364 ordered->file_offset + ordered->num_bytes - 1);
11365 btrfs_put_ordered_extent(ordered);
11366 }
11367
11368 ASSERT(ordered == NULL);
11369}
11370
6e1d5dcc 11371static const struct inode_operations btrfs_dir_inode_operations = {
3394e160 11372 .getattr = btrfs_getattr,
39279cc3
CM
11373 .lookup = btrfs_lookup,
11374 .create = btrfs_create,
11375 .unlink = btrfs_unlink,
11376 .link = btrfs_link,
11377 .mkdir = btrfs_mkdir,
11378 .rmdir = btrfs_rmdir,
2773bf00 11379 .rename = btrfs_rename2,
39279cc3
CM
11380 .symlink = btrfs_symlink,
11381 .setattr = btrfs_setattr,
618e21d5 11382 .mknod = btrfs_mknod,
5103e947 11383 .listxattr = btrfs_listxattr,
fdebe2bd 11384 .permission = btrfs_permission,
4e34e719 11385 .get_acl = btrfs_get_acl,
996a710d 11386 .set_acl = btrfs_set_acl,
93fd63c2 11387 .update_time = btrfs_update_time,
ef3b9af5 11388 .tmpfile = btrfs_tmpfile,
97fc2977
MS
11389 .fileattr_get = btrfs_fileattr_get,
11390 .fileattr_set = btrfs_fileattr_set,
39279cc3 11391};
76dda93c 11392
828c0950 11393static const struct file_operations btrfs_dir_file_operations = {
39279cc3
CM
11394 .llseek = generic_file_llseek,
11395 .read = generic_read_dir,
02dbfc99 11396 .iterate_shared = btrfs_real_readdir,
23b5ec74 11397 .open = btrfs_opendir,
34287aa3 11398 .unlocked_ioctl = btrfs_ioctl,
39279cc3 11399#ifdef CONFIG_COMPAT
4c63c245 11400 .compat_ioctl = btrfs_compat_ioctl,
39279cc3 11401#endif
6bf13c0c 11402 .release = btrfs_release_file,
e02119d5 11403 .fsync = btrfs_sync_file,
39279cc3
CM
11404};
11405
35054394
CM
11406/*
11407 * btrfs doesn't support the bmap operation because swapfiles
11408 * use bmap to make a mapping of extents in the file. They assume
11409 * these extents won't change over the life of the file and they
11410 * use the bmap result to do IO directly to the drive.
11411 *
11412 * the btrfs bmap call would return logical addresses that aren't
11413 * suitable for IO and they also will change frequently as COW
11414 * operations happen. So, swapfile + btrfs == corruption.
11415 *
11416 * For now we're avoiding this by dropping bmap.
11417 */
7f09410b 11418static const struct address_space_operations btrfs_aops = {
39279cc3
CM
11419 .readpage = btrfs_readpage,
11420 .writepage = btrfs_writepage,
b293f02e 11421 .writepages = btrfs_writepages,
ba206a02 11422 .readahead = btrfs_readahead,
f85781fb 11423 .direct_IO = noop_direct_IO,
895586eb 11424 .invalidate_folio = btrfs_invalidate_folio,
a52d9a80 11425 .releasepage = btrfs_releasepage,
f8e66081
RG
11426#ifdef CONFIG_MIGRATION
11427 .migratepage = btrfs_migratepage,
11428#endif
187c82cb 11429 .dirty_folio = filemap_dirty_folio,
465fdd97 11430 .error_remove_page = generic_error_remove_page,
ed46ff3d
OS
11431 .swap_activate = btrfs_swap_activate,
11432 .swap_deactivate = btrfs_swap_deactivate,
39279cc3
CM
11433};
11434
6e1d5dcc 11435static const struct inode_operations btrfs_file_inode_operations = {
39279cc3
CM
11436 .getattr = btrfs_getattr,
11437 .setattr = btrfs_setattr,
5103e947 11438 .listxattr = btrfs_listxattr,
fdebe2bd 11439 .permission = btrfs_permission,
1506fcc8 11440 .fiemap = btrfs_fiemap,
4e34e719 11441 .get_acl = btrfs_get_acl,
996a710d 11442 .set_acl = btrfs_set_acl,
e41f941a 11443 .update_time = btrfs_update_time,
97fc2977
MS
11444 .fileattr_get = btrfs_fileattr_get,
11445 .fileattr_set = btrfs_fileattr_set,
39279cc3 11446};
6e1d5dcc 11447static const struct inode_operations btrfs_special_inode_operations = {
618e21d5
JB
11448 .getattr = btrfs_getattr,
11449 .setattr = btrfs_setattr,
fdebe2bd 11450 .permission = btrfs_permission,
33268eaf 11451 .listxattr = btrfs_listxattr,
4e34e719 11452 .get_acl = btrfs_get_acl,
996a710d 11453 .set_acl = btrfs_set_acl,
e41f941a 11454 .update_time = btrfs_update_time,
618e21d5 11455};
6e1d5dcc 11456static const struct inode_operations btrfs_symlink_inode_operations = {
6b255391 11457 .get_link = page_get_link,
f209561a 11458 .getattr = btrfs_getattr,
22c44fe6 11459 .setattr = btrfs_setattr,
fdebe2bd 11460 .permission = btrfs_permission,
0279b4cd 11461 .listxattr = btrfs_listxattr,
e41f941a 11462 .update_time = btrfs_update_time,
39279cc3 11463};
76dda93c 11464
82d339d9 11465const struct dentry_operations btrfs_dentry_operations = {
76dda93c
YZ
11466 .d_delete = btrfs_dentry_delete,
11467};