blob: 46e04247193abbeea5024f583a9280091f6dc22d [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
David Sterba55e20bd2020-06-09 19:56:06 +02008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020033#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020034#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040040#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040041#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040042#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020043#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040044#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050045#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050046#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080047#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010052#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020055 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040056 struct btrfs_root *root;
57};
58
Filipe Mananaf28a4922015-12-08 19:23:20 +000059struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000060 u64 reserve;
David Sterba55e20bd2020-06-09 19:56:06 +020061 u64 unsubmitted_oe_range_start;
62 u64 unsubmitted_oe_range_end;
63 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000064};
65
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_dir_inode_operations;
67static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010072static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030083static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050084 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030087static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
88 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080089 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030093static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080094 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
98 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010099 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800100 *
101 * NOTE: caller must ensure that when an error happens, it can not call
102 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
103 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
104 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200105 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800106 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300107static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200108 struct page *locked_page,
109 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800110{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900111 unsigned long index = offset >> PAGE_SHIFT;
112 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200113 u64 page_start = page_offset(locked_page);
114 u64 page_end = page_start + PAGE_SIZE - 1;
115
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900116 struct page *page;
117
118 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300119 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900120 index++;
121 if (!page)
122 continue;
123 ClearPagePrivate2(page);
124 put_page(page);
125 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200126
127 /*
128 * In case this page belongs to the delalloc range being instantiated
129 * then skip it, since the first page of a range is going to be
130 * properly cleaned up by the caller of run_delalloc_range
131 */
132 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
133 offset += PAGE_SIZE;
134 bytes -= PAGE_SIZE;
135 }
136
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300137 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800138}
139
Eric Sandeen48a3b632013-04-25 20:41:01 +0000140static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400141
Josef Bacik6a3891c2015-03-16 17:38:52 -0400142#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
143void btrfs_test_inode_set_ops(struct inode *inode)
144{
145 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
146}
147#endif
148
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000149static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500150 struct inode *inode, struct inode *dir,
151 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500152{
153 int err;
154
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000155 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500157 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 return err;
159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/*
Chris Masonc8b97812008-10-29 14:49:59 -0400162 * this does all the hard work for inserting an inline extent into
163 * the btree. The caller should have done a btrfs_drop_extents so that
164 * no overlapping inline items exist in the btree
165 */
Chris Mason40f76582014-05-21 13:35:51 -0700166static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000167 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct btrfs_root *root, struct inode *inode,
169 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000170 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page **compressed_pages)
172{
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct extent_buffer *leaf;
174 struct page *page = NULL;
175 char *kaddr;
176 unsigned long ptr;
177 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 int ret;
179 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400181
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800182 ASSERT((compressed_size > 0 && compressed_pages) ||
183 (compressed_size == 0 && !compressed_pages));
184
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400186 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400187
Chris Masonc8b97812008-10-29 14:49:59 -0400188 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189
190 if (!extent_inserted) {
191 struct btrfs_key key;
192 size_t datasize;
193
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200194 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200196 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197
198 datasize = btrfs_file_extent_calc_inline_size(cur_size);
199 path->leave_spinning = 1;
200 ret = btrfs_insert_empty_item(trans, root, path, &key,
201 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200202 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000203 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400204 }
205 leaf = path->nodes[0];
206 ei = btrfs_item_ptr(leaf, path->slots[0],
207 struct btrfs_file_extent_item);
208 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
209 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
210 btrfs_set_file_extent_encryption(leaf, ei, 0);
211 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
212 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
213 ptr = btrfs_file_extent_inline_start(ei);
214
Li Zefan261507a02010-12-17 14:21:50 +0800215 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400216 struct page *cpage;
217 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500218 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400219 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500220 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300221 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400224 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400226
227 i++;
228 ptr += cur_size;
229 compressed_size -= cur_size;
230 }
231 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800232 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400233 } else {
234 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300235 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100238 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400239 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800240 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 }
243 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400245
Yan, Zhengc2167752009-11-12 09:34:21 +0000246 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500247 * We align size to sectorsize for inline extents just for simplicity
248 * sake.
249 */
250 size = ALIGN(size, root->fs_info->sectorsize);
251 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
252 if (ret)
253 goto fail;
254
255 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000256 * we're an inline extent, so nobody can
257 * extend the file past i_size without locking
258 * a page we already have locked.
259 *
260 * We must do any isize and inode updates
261 * before we unlock the pages. Otherwise we
262 * could end up racing with unlink.
263 */
Chris Masonc8b97812008-10-29 14:49:59 -0400264 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000266
Chris Masonc8b97812008-10-29 14:49:59 -0400267fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200268 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400269}
270
271
272/*
273 * conditionally insert an inline extent into the file. This
274 * does the checks required to make sure the data is small enough
275 * to fit as an inline extent.
276 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300277static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400278 u64 end, size_t compressed_size,
279 int compress_type,
280 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400281{
Nikolay Borisova0349402020-06-03 08:55:12 +0300282 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400283 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400284 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300285 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400286 u64 actual_end = min(end + 1, isize);
287 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400289 u64 data_len = inline_len;
290 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000291 struct btrfs_path *path;
292 int extent_inserted = 0;
293 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400294
295 if (compressed_size)
296 data_len = compressed_size;
297
298 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400299 actual_end > fs_info->sectorsize ||
300 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400301 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400305 return 1;
306 }
307
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 path = btrfs_alloc_path();
309 if (!path)
310 return -ENOMEM;
311
Josef Bacik00361582013-08-14 14:02:47 -0400312 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 if (IS_ERR(trans)) {
314 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400315 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000316 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300317 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400318
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000319 if (compressed_size && compressed_pages)
320 extent_item_size = btrfs_file_extent_calc_inline_size(
321 compressed_size);
322 else
323 extent_item_size = btrfs_file_extent_calc_inline_size(
324 inline_len);
325
Nikolay Borisova0349402020-06-03 08:55:12 +0300326 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
327 NULL, 1, 1, extent_item_size,
328 &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400329 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400330 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400331 goto out;
332 }
Chris Masonc8b97812008-10-29 14:49:59 -0400333
334 if (isize > actual_end)
335 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000336 ret = insert_inline_extent(trans, path, extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300337 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400338 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000339 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400340 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400341 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400342 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400343 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400344 ret = 1;
345 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100346 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400347
Nikolay Borisova0349402020-06-03 08:55:12 +0300348 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
349 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400350out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800351 /*
352 * Don't forget to free the reserved space, as for inlined extent
353 * it won't count as data extent, free them directly here.
354 * And at reserve time, it's always aligned to page size, so
355 * just free one page here.
356 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300357 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000358 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400359 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400360 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400361}
362
Chris Mason771ed682008-11-06 22:02:51 -0500363struct async_extent {
364 u64 start;
365 u64 ram_size;
366 u64 compressed_size;
367 struct page **pages;
368 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 struct list_head list;
371};
372
Nikolay Borisov97db1202019-03-12 17:20:24 +0200373struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500374 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500375 struct page *locked_page;
376 u64 start;
377 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600378 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500379 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700380 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500381 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200382 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500383};
384
Nikolay Borisov97db1202019-03-12 17:20:24 +0200385struct async_cow {
386 /* Number of chunks in flight; must be first in the structure */
387 atomic_t num_chunks;
388 struct async_chunk chunks[];
389};
390
391static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500392 u64 start, u64 ram_size,
393 u64 compressed_size,
394 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800395 unsigned long nr_pages,
396 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500397{
398 struct async_extent *async_extent;
399
400 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100401 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500402 async_extent->start = start;
403 async_extent->ram_size = ram_size;
404 async_extent->compressed_size = compressed_size;
405 async_extent->pages = pages;
406 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800407 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500408 list_add_tail(&async_extent->list, &cow->extents);
409 return 0;
410}
411
Qu Wenruo42c16da2019-07-01 05:12:46 +0000412/*
413 * Check if the inode has flags compatible with compression
414 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300415static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000416{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300417 if (inode->flags & BTRFS_INODE_NODATACOW ||
418 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000419 return false;
420 return true;
421}
422
423/*
424 * Check if the inode needs to be submitted to compression, based on mount
425 * options, defragmentation, properties or heuristics.
426 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300427static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
428 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800429{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300430 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800431
Nikolay Borisov808a1292020-06-03 08:55:27 +0300432 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000433 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
434 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300435 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000436 return 0;
437 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400439 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200441 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300442 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200443 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800444 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300445 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800446 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300448 inode->flags & BTRFS_INODE_COMPRESS ||
449 inode->prop_compress)
450 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800451 return 0;
452}
453
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200454static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800455 u64 start, u64 end, u64 num_bytes, u64 small_write)
456{
457 /* If this is a small write inside eof, kick off a defrag */
458 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200459 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800460 btrfs_add_inode_defrag(NULL, inode);
461}
462
Chris Masonc8b97812008-10-29 14:49:59 -0400463/*
Chris Mason771ed682008-11-06 22:02:51 -0500464 * we create compressed extents in two phases. The first
465 * phase compresses a range of pages that have already been
466 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400467 *
Chris Mason771ed682008-11-06 22:02:51 -0500468 * This is done inside an ordered work queue, and the compression
469 * is spread across many cpus. The actual IO submission is step
470 * two, and the ordered work queue takes care of making sure that
471 * happens in the same order things were put onto the queue by
472 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 *
Chris Mason771ed682008-11-06 22:02:51 -0500474 * If this code finds it can't get good compression, it puts an
475 * entry onto the work queue to write the uncompressed bytes. This
476 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300477 * are written in the same order that the flusher thread sent them
478 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400479 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300480static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400481{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200482 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200485 u64 start = async_chunk->start;
486 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400487 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400488 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400489 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400490 struct page **pages = NULL;
491 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 unsigned long total_compressed = 0;
493 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400494 int i;
495 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400496 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300497 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400498 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400499
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200500 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
501 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400502
Josef Bacikd98da492019-10-11 09:03:54 -0400503 /*
504 * We need to save i_size before now because it could change in between
505 * us evaluating the size and assigning it. This is because we lock and
506 * unlock the page in truncate and fallocate, and then modify the i_size
507 * later on.
508 *
509 * The barriers are to emulate READ_ONCE, remove that once i_size_read
510 * does that for us.
511 */
512 barrier();
513 i_size = i_size_read(inode);
514 barrier();
515 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400516again:
517 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100519 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
520 nr_pages = min_t(unsigned long, nr_pages,
521 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400522
Chris Masonf03d9301f2009-02-04 09:31:06 -0500523 /*
524 * we don't want to send crud past the end of i_size through
525 * compression, that's just a waste of CPU time. So, if the
526 * end of the file is before the start of our current
527 * requested range of bytes, we bail out to the uncompressed
528 * cleanup code that can deal with all of this.
529 *
530 * It isn't really the fastest way to fix things, but this is a
531 * very uncommon corner.
532 */
533 if (actual_end <= start)
534 goto cleanup_and_bail_uncompressed;
535
Chris Masonc8b97812008-10-29 14:49:59 -0400536 total_compressed = actual_end - start;
537
Shilong Wang4bcbb332014-10-07 18:44:35 -0400538 /*
539 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400540 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400541 */
542 if (total_compressed <= blocksize &&
543 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
544 goto cleanup_and_bail_uncompressed;
545
David Sterba069eac72017-02-14 19:36:54 +0100546 total_compressed = min_t(unsigned long, total_compressed,
547 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400548 total_in = 0;
549 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400550
Chris Mason771ed682008-11-06 22:02:51 -0500551 /*
552 * we do compression for mount -o compress and when the
553 * inode has not been flagged as nocompress. This flag can
554 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400555 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300556 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400557 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100558 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800559 if (!pages) {
560 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100561 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800562 goto cont;
563 }
Chris Mason179e29e2007-11-01 11:28:41 -0400564
David Sterbaeec63c62017-07-17 19:41:31 +0200565 if (BTRFS_I(inode)->defrag_compress)
566 compress_type = BTRFS_I(inode)->defrag_compress;
567 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200568 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800569
Chris Mason4adaa612013-03-26 13:07:00 -0400570 /*
571 * we need to call clear_page_dirty_for_io on each
572 * page in the range. Otherwise applications with the file
573 * mmap'd can wander in and change the page contents while
574 * we are compressing them.
575 *
576 * If the compression fails for any reason, we set the pages
577 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300578 *
579 * Note that the remaining part is redirtied, the start pointer
580 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400581 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300582 if (!redirty) {
583 extent_range_clear_dirty_for_io(inode, start, end);
584 redirty = 1;
585 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200586
587 /* Compression level is applied here and only here */
588 ret = btrfs_compress_pages(
589 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800590 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100591 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100592 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800593 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100594 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400595
596 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100597 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100598 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400599 char *kaddr;
600
601 /* zero the tail end of the last page, we might be
602 * sending it down to disk
603 */
604 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800605 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400606 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300607 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800608 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400609 }
610 will_compress = 1;
611 }
612 }
Li Zefan560f7d72011-09-08 10:22:01 +0800613cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400614 if (start == 0) {
615 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300616 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400617 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500618 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400619 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300620 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
621 0, BTRFS_COMPRESS_NONE,
622 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400623 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500624 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300625 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000626 total_compressed,
627 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400628 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100629 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400630 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400631 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
632 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100633 unsigned long page_error_op;
634
Filipe Mananae6eb4312014-10-10 10:45:12 +0100635 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400636
Chris Mason771ed682008-11-06 22:02:51 -0500637 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100638 * inline extent creation worked or returned error,
639 * we don't need to create any more async work items.
640 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400641 *
642 * We use DO_ACCOUNTING here because we need the
643 * delalloc_release_metadata to be done _after_ we drop
644 * our outstanding extent for clearing delalloc for this
645 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500646 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300647 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
648 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300649 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800650 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400651 PAGE_CLEAR_DIRTY |
652 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100653 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400654 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300655
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800656 /*
657 * Ensure we only free the compressed pages if we have
658 * them allocated, as we can still reach here with
659 * inode_need_compress() == false.
660 */
661 if (pages) {
662 for (i = 0; i < nr_pages; i++) {
663 WARN_ON(pages[i]->mapping);
664 put_page(pages[i]);
665 }
666 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300667 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300668 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400669 }
670 }
671
672 if (will_compress) {
673 /*
674 * we aren't doing an inline extent round the compressed size
675 * up to a block size boundary so the allocator does sane
676 * things
677 */
Qu Wenruofda28322013-02-26 08:10:22 +0000678 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400679
680 /*
681 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300682 * win, compare the page count read with the blocks on disk,
683 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400684 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300685 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300686 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300687 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700688
689 /*
690 * The async work queues will take care of doing actual
691 * allocation on disk for these compressed pages, and
692 * will submit them to the elevator.
693 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200694 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100695 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700696 compress_type);
697
Timofey Titovets11708622017-10-03 18:06:01 +0300698 if (start + total_in < end) {
699 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700700 pages = NULL;
701 cond_resched();
702 goto again;
703 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300704 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400705 }
706 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700707 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400708 /*
709 * the compression code ran but failed to make things smaller,
710 * free any pages it allocated and our page pointer array
711 */
David Sterba4d3a8002017-02-14 19:04:07 +0100712 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400713 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300714 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400715 }
716 kfree(pages);
717 pages = NULL;
718 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100719 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400720
721 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400722 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200723 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500724 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500725 }
Chris Masonc8b97812008-10-29 14:49:59 -0400726 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500727cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700728 /*
729 * No compression, but we still need to write the pages in the file
730 * we've been given so far. redirty the locked page if it corresponds
731 * to our extent and set things up for the async work queue to run
732 * cow_file_range to do the normal delalloc dance.
733 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700734 if (async_chunk->locked_page &&
735 (page_offset(async_chunk->locked_page) >= start &&
736 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200737 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700738 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700739 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740
741 if (redirty)
742 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200743 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700744 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300745 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500746
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300747 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500748}
Chris Mason771ed682008-11-06 22:02:51 -0500749
Filipe Manana40ae8372014-10-06 22:14:24 +0100750static void free_async_extent_pages(struct async_extent *async_extent)
751{
752 int i;
753
754 if (!async_extent->pages)
755 return;
756
757 for (i = 0; i < async_extent->nr_pages; i++) {
758 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300759 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100760 }
761 kfree(async_extent->pages);
762 async_extent->nr_pages = 0;
763 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500764}
765
766/*
767 * phase two of compressed writeback. This is the ordered portion
768 * of the code, which only gets called in the order the work was
769 * queued. We walk all the async extents created by compress_file_range
770 * and send them down to the disk.
771 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200772static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500773{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300774 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
775 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500776 struct async_extent *async_extent;
777 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500778 struct btrfs_key ins;
779 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300780 struct btrfs_root *root = inode->root;
781 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500783
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500784again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200785 while (!list_empty(&async_chunk->extents)) {
786 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500787 struct async_extent, list);
788 list_del(&async_extent->list);
789
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500790retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200791 lock_extent(io_tree, async_extent->start,
792 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500793 /* did the compression code fall back to uncompressed IO? */
794 if (!async_extent->pages) {
795 int page_started = 0;
796 unsigned long nr_written = 0;
797
Chris Mason771ed682008-11-06 22:02:51 -0500798 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300799 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500800 async_extent->start,
801 async_extent->start +
802 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300803 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500804
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100805 /* JDM XXX */
806
Chris Mason771ed682008-11-06 22:02:51 -0500807 /*
808 * if page_started, cow_file_range inserted an
809 * inline extent and took care of all the unlocking
810 * and IO for us. Otherwise, we need to submit
811 * all those pages down to the drive.
812 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500813 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300814 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200815 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500816 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500817 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500818 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700819 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200820 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500821 kfree(async_extent);
822 cond_resched();
823 continue;
824 }
825
Wang Xiaoguang18513092016-07-25 15:51:40 +0800826 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500827 async_extent->compressed_size,
828 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800829 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500830 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100831 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400833 if (ret == -ENOSPC) {
834 unlock_extent(io_tree, async_extent->start,
835 async_extent->start +
836 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800837
838 /*
839 * we need to redirty the pages if we decide to
840 * fallback to uncompressed IO, otherwise we
841 * will not submit these pages down to lower
842 * layers.
843 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300844 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800845 async_extent->start,
846 async_extent->start +
847 async_extent->ram_size - 1);
848
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100849 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400850 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500851 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500852 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000853 /*
854 * here we're doing allocation and writeback of the
855 * compressed pages
856 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300857 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800858 async_extent->ram_size, /* len */
859 async_extent->start, /* orig_start */
860 ins.objectid, /* block_start */
861 ins.offset, /* block_len */
862 ins.offset, /* orig_block_len */
863 async_extent->ram_size, /* ram_bytes */
864 async_extent->compress_type,
865 BTRFS_ORDERED_COMPRESSED);
866 if (IS_ERR(em))
867 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500868 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800869 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300871 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800872 async_extent->start,
873 ins.objectid,
874 async_extent->ram_size,
875 ins.offset,
876 BTRFS_ORDERED_COMPRESSED,
877 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100878 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300879 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100880 async_extent->start +
881 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500882 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100883 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400884 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500885
Chris Mason771ed682008-11-06 22:02:51 -0500886 /*
887 * clear dirty, set writeback and unlock the pages.
888 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300889 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400890 async_extent->start +
891 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400892 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
893 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400894 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300895 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500896 async_extent->ram_size,
897 ins.objectid,
898 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600899 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700900 async_chunk->write_flags,
901 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100902 struct page *p = async_extent->pages[0];
903 const u64 start = async_extent->start;
904 const u64 end = start + async_extent->ram_size - 1;
905
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300906 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200907 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200908
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100909 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300910 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100911 PAGE_END_WRITEBACK |
912 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100913 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100914 }
Chris Mason771ed682008-11-06 22:02:51 -0500915 alloc_hint = ins.objectid + ins.offset;
916 kfree(async_extent);
917 cond_resched();
918 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100919 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500920out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400921 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400922 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100923out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300924 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500925 async_extent->start +
926 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400927 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100928 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400929 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
930 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100931 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
932 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100933 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500935 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500936}
937
Nikolay Borisov43c69842020-06-03 08:55:02 +0300938static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400939 u64 num_bytes)
940{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300941 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400942 struct extent_map *em;
943 u64 alloc_hint = 0;
944
945 read_lock(&em_tree->lock);
946 em = search_extent_mapping(em_tree, start, num_bytes);
947 if (em) {
948 /*
949 * if block start isn't an actual block number then find the
950 * first block in this inode and use that as a hint. If that
951 * block is also bogus then just don't worry about it.
952 */
953 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
954 free_extent_map(em);
955 em = search_extent_mapping(em_tree, 0, 0);
956 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
957 alloc_hint = em->block_start;
958 if (em)
959 free_extent_map(em);
960 } else {
961 alloc_hint = em->block_start;
962 free_extent_map(em);
963 }
964 }
965 read_unlock(&em_tree->lock);
966
967 return alloc_hint;
968}
969
Chris Mason771ed682008-11-06 22:02:51 -0500970/*
971 * when extent_io.c finds a delayed allocation range in the file,
972 * the call backs end up in this code. The basic idea is to
973 * allocate extents on disk for the range, and create ordered data structs
974 * in ram to track those extents.
975 *
976 * locked_page is the page that writepage had locked already. We use
977 * it to make sure we don't do extra locks or unlocks.
978 *
979 * *page_started is set to one if we unlock locked_page and do everything
980 * required to start IO on it. It may be clean and already done with
981 * IO when we return.
982 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300983static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400984 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300985 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300986 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500987{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300988 struct btrfs_root *root = inode->root;
989 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500990 u64 alloc_hint = 0;
991 u64 num_bytes;
992 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000993 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100994 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400995 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500996 struct btrfs_key ins;
997 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000998 unsigned clear_bits;
999 unsigned long page_ops;
1000 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001001 int ret = 0;
1002
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001003 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001004 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001005 ret = -EINVAL;
1006 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001007 }
Chris Mason771ed682008-11-06 22:02:51 -05001008
Qu Wenruofda28322013-02-26 08:10:22 +00001009 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001010 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001011 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001012
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001013 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001014
Chris Mason771ed682008-11-06 22:02:51 -05001015 if (start == 0) {
1016 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001017 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001018 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001019 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001020 /*
1021 * We use DO_ACCOUNTING here because we need the
1022 * delalloc_release_metadata to be run _after_ we drop
1023 * our outstanding extent for clearing delalloc for this
1024 * range.
1025 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001026 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001027 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001028 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1029 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001030 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1031 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001032 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001033 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001034 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001035 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001036 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001037 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001038 }
1039 }
Chris Masonc8b97812008-10-29 14:49:59 -04001040
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001041 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1042 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001043
Filipe Manana432cd2a2020-06-08 13:32:55 +01001044 /*
1045 * Relocation relies on the relocated extents to have exactly the same
1046 * size as the original extents. Normally writeback for relocation data
1047 * extents follows a NOCOW path because relocation preallocates the
1048 * extents. However, due to an operation such as scrub turning a block
1049 * group to RO mode, it may fallback to COW mode, so we must make sure
1050 * an extent allocated during COW has exactly the requested size and can
1051 * not be split into smaller extents, otherwise relocation breaks and
1052 * fails during the stage where it updates the bytenr of file extent
1053 * items.
1054 */
1055 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1056 min_alloc_size = num_bytes;
1057 else
1058 min_alloc_size = fs_info->sectorsize;
1059
Anand Jain3752d222018-02-15 12:29:38 +08001060 while (num_bytes > 0) {
1061 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001062 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001063 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001064 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001065 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001066 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001067 cur_alloc_size = ins.offset;
1068 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001069
Chris Mason771ed682008-11-06 22:02:51 -05001070 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001071 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001072 start, /* orig_start */
1073 ins.objectid, /* block_start */
1074 ins.offset, /* block_len */
1075 ins.offset, /* orig_block_len */
1076 ram_size, /* ram_bytes */
1077 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001078 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001079 if (IS_ERR(em)) {
1080 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001081 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001082 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001083 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001084
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001085 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1086 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001087 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001088 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001089
Yan Zheng17d217f2008-12-12 10:03:38 -05001090 if (root->root_key.objectid ==
1091 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001092 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001093 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001094 /*
1095 * Only drop cache here, and process as normal.
1096 *
1097 * We must not allow extent_clear_unlock_delalloc()
1098 * at out_unlock label to free meta of this ordered
1099 * extent, as its meta should be freed by
1100 * btrfs_finish_ordered_io().
1101 *
1102 * So we must continue until @start is increased to
1103 * skip current ordered extent.
1104 */
Josef Bacik00361582013-08-14 14:02:47 -04001105 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001106 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001107 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001108 }
1109
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001110 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001111
Chris Masonc8b97812008-10-29 14:49:59 -04001112 /* we're not doing compressed IO, don't unlock the first
1113 * page (which the caller expects to stay locked), don't
1114 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001115 *
1116 * Do set the Private2 bit so we know this page was properly
1117 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001118 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001119 page_ops = unlock ? PAGE_UNLOCK : 0;
1120 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001121
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001122 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001123 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001124 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001125 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001126 if (num_bytes < cur_alloc_size)
1127 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001128 else
Anand Jain3752d222018-02-15 12:29:38 +08001129 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001130 alloc_hint = ins.objectid + ins.offset;
1131 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001132 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001133
1134 /*
1135 * btrfs_reloc_clone_csums() error, since start is increased
1136 * extent_clear_unlock_delalloc() at out_unlock label won't
1137 * free metadata of current ordered extent, we're OK to exit.
1138 */
1139 if (ret)
1140 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001141 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001142out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001144
Filipe Mananad9f85962014-08-25 10:43:00 +01001145out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001146 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001147out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001148 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001149 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001150out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001151 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1152 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001153 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1154 PAGE_END_WRITEBACK;
1155 /*
1156 * If we reserved an extent for our delalloc range (or a subrange) and
1157 * failed to create the respective ordered extent, then it means that
1158 * when we reserved the extent we decremented the extent's size from
1159 * the data space_info's bytes_may_use counter and incremented the
1160 * space_info's bytes_reserved counter by the same amount. We must make
1161 * sure extent_clear_unlock_delalloc() does not try to decrement again
1162 * the data space_info's bytes_may_use counter, therefore we do not pass
1163 * it the flag EXTENT_CLEAR_DATA_RESV.
1164 */
1165 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001166 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001167 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001168 locked_page,
1169 clear_bits,
1170 page_ops);
1171 start += cur_alloc_size;
1172 if (start >= end)
1173 goto out;
1174 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001175 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001176 clear_bits | EXTENT_CLEAR_DATA_RESV,
1177 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001178 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001179}
Chris Masonc8b97812008-10-29 14:49:59 -04001180
Chris Mason771ed682008-11-06 22:02:51 -05001181/*
1182 * work queue call back to started compression on a file and pages
1183 */
1184static noinline void async_cow_start(struct btrfs_work *work)
1185{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001186 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001187 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001188
Nikolay Borisovb5326272019-03-12 17:20:25 +02001189 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001190
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001191 compressed_extents = compress_file_range(async_chunk);
1192 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001193 btrfs_add_delayed_iput(async_chunk->inode);
1194 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001195 }
Chris Mason771ed682008-11-06 22:02:51 -05001196}
1197
1198/*
1199 * work queue call back to submit previously compressed pages
1200 */
1201static noinline void async_cow_submit(struct btrfs_work *work)
1202{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001203 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1204 work);
1205 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001206 unsigned long nr_pages;
1207
Nikolay Borisovb5326272019-03-12 17:20:25 +02001208 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001209 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001210
David Sterba093258e2018-02-26 16:15:17 +01001211 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001212 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001213 5 * SZ_1M)
1214 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001215
Nikolay Borisov4546d172019-01-03 10:50:03 +02001216 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001217 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001218 * in which case we don't have anything to submit, yet we need to
1219 * always adjust ->async_delalloc_pages as its paired with the init
1220 * happening in cow_file_range_async
1221 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001222 if (async_chunk->inode)
1223 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001224}
Chris Masonc8b97812008-10-29 14:49:59 -04001225
Chris Mason771ed682008-11-06 22:02:51 -05001226static noinline void async_cow_free(struct btrfs_work *work)
1227{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001228 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001229
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 async_chunk = container_of(work, struct async_chunk, work);
1231 if (async_chunk->inode)
1232 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001233 if (async_chunk->blkcg_css)
1234 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001235 /*
1236 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001237 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001238 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001239 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001240 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001241}
1242
Nikolay Borisov751b6432020-06-03 08:55:22 +03001243static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001244 struct writeback_control *wbc,
1245 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001246 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001247 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001248{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001249 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001250 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001251 struct async_cow *ctx;
1252 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001253 unsigned long nr_pages;
1254 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001255 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1256 int i;
1257 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001258 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001259 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001260
Nikolay Borisov751b6432020-06-03 08:55:22 +03001261 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001262
Nikolay Borisov751b6432020-06-03 08:55:22 +03001263 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001264 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1265 num_chunks = 1;
1266 should_compress = false;
1267 } else {
1268 should_compress = true;
1269 }
1270
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001271 nofs_flag = memalloc_nofs_save();
1272 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1273 memalloc_nofs_restore(nofs_flag);
1274
Nikolay Borisov97db1202019-03-12 17:20:24 +02001275 if (!ctx) {
1276 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1277 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1278 EXTENT_DO_ACCOUNTING;
1279 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1280 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1281 PAGE_SET_ERROR;
1282
Nikolay Borisov751b6432020-06-03 08:55:22 +03001283 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1284 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001285 return -ENOMEM;
1286 }
1287
1288 async_chunk = ctx->chunks;
1289 atomic_set(&ctx->num_chunks, num_chunks);
1290
1291 for (i = 0; i < num_chunks; i++) {
1292 if (should_compress)
1293 cur_end = min(end, start + SZ_512K - 1);
1294 else
1295 cur_end = end;
1296
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001297 /*
1298 * igrab is called higher up in the call chain, take only the
1299 * lightweight reference for the callback lifetime
1300 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001301 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001302 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001303 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001304 async_chunk[i].start = start;
1305 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001306 async_chunk[i].write_flags = write_flags;
1307 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001308
Chris Mason1d53c9e2019-07-10 12:28:16 -07001309 /*
1310 * The locked_page comes all the way from writepage and its
1311 * the original page we were actually given. As we spread
1312 * this large delalloc region across multiple async_chunk
1313 * structs, only the first struct needs a pointer to locked_page
1314 *
1315 * This way we don't need racey decisions about who is supposed
1316 * to unlock it.
1317 */
1318 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001319 /*
1320 * Depending on the compressibility, the pages might or
1321 * might not go through async. We want all of them to
1322 * be accounted against wbc once. Let's do it here
1323 * before the paths diverge. wbc accounting is used
1324 * only for foreign writeback detection and doesn't
1325 * need full accuracy. Just account the whole thing
1326 * against the first page.
1327 */
1328 wbc_account_cgroup_owner(wbc, locked_page,
1329 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001330 async_chunk[i].locked_page = locked_page;
1331 locked_page = NULL;
1332 } else {
1333 async_chunk[i].locked_page = NULL;
1334 }
1335
Chris Masonec39f762019-07-10 12:28:17 -07001336 if (blkcg_css != blkcg_root_css) {
1337 css_get(blkcg_css);
1338 async_chunk[i].blkcg_css = blkcg_css;
1339 } else {
1340 async_chunk[i].blkcg_css = NULL;
1341 }
1342
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001343 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1344 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001345
Nikolay Borisov97db1202019-03-12 17:20:24 +02001346 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001347 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001348
Nikolay Borisov97db1202019-03-12 17:20:24 +02001349 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001350
Chris Mason771ed682008-11-06 22:02:51 -05001351 *nr_written += nr_pages;
1352 start = cur_end + 1;
1353 }
1354 *page_started = 1;
1355 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001356}
1357
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001358static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001359 u64 bytenr, u64 num_bytes)
1360{
1361 int ret;
1362 struct btrfs_ordered_sum *sums;
1363 LIST_HEAD(list);
1364
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001365 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001366 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001367 if (ret == 0 && list_empty(&list))
1368 return 0;
1369
1370 while (!list_empty(&list)) {
1371 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1372 list_del(&sums->list);
1373 kfree(sums);
1374 }
Liu Bo58113752018-01-31 17:09:13 -07001375 if (ret < 0)
1376 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001377 return 1;
1378}
1379
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001380static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001381 const u64 start, const u64 end,
1382 int *page_started, unsigned long *nr_written)
1383{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001384 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1385 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001386 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001387 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001388 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001389 u64 range_start = start;
1390 u64 count;
1391
1392 /*
1393 * If EXTENT_NORESERVE is set it means that when the buffered write was
1394 * made we had not enough available data space and therefore we did not
1395 * reserve data space for it, since we though we could do NOCOW for the
1396 * respective file range (either there is prealloc extent or the inode
1397 * has the NOCOW bit set).
1398 *
1399 * However when we need to fallback to COW mode (because for example the
1400 * block group for the corresponding extent was turned to RO mode by a
1401 * scrub or relocation) we need to do the following:
1402 *
1403 * 1) We increment the bytes_may_use counter of the data space info.
1404 * If COW succeeds, it allocates a new data extent and after doing
1405 * that it decrements the space info's bytes_may_use counter and
1406 * increments its bytes_reserved counter by the same amount (we do
1407 * this at btrfs_add_reserved_bytes()). So we need to increment the
1408 * bytes_may_use counter to compensate (when space is reserved at
1409 * buffered write time, the bytes_may_use counter is incremented);
1410 *
1411 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1412 * that if the COW path fails for any reason, it decrements (through
1413 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1414 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001415 *
1416 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001417 * space cache inode or an inode of the data relocation tree, we must
1418 * also increment bytes_may_use of the data space_info for the same
1419 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001420 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001421 * group that contains that extent to RO mode and therefore force COW
1422 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001423 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001424 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001425 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001426 if (count > 0 || is_space_ino || is_reloc_ino) {
1427 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001428 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001429 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1430
Filipe Manana6bd335b2020-06-08 13:33:05 +01001431 if (is_space_ino || is_reloc_ino)
1432 bytes = range_bytes;
1433
Filipe Manana467dc472020-05-27 11:16:07 +01001434 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001435 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001436 spin_unlock(&sinfo->lock);
1437
Filipe Manana2166e5e2020-05-27 11:16:19 +01001438 if (count > 0)
1439 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1440 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001441 }
1442
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001443 return cow_file_range(inode, locked_page, start, end, page_started,
1444 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * when nowcow writeback call back. This checks for snapshots or COW copies
1449 * of the extents that exist in the file, and COWs the file as required.
1450 *
1451 * If no cow copies or snapshots exist, we write directly to the existing
1452 * blocks on disk
1453 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001454static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001455 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001456 const u64 start, const u64 end,
1457 int *page_started, int force,
1458 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001459{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001460 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1461 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001462 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001463 u64 cow_start = (u64)-1;
1464 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001465 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001466 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001467 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1468 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001469 bool nocow = false;
1470 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001471
1472 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001473 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001474 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001475 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001476 EXTENT_DO_ACCOUNTING |
1477 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001478 PAGE_CLEAR_DIRTY |
1479 PAGE_SET_WRITEBACK |
1480 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001481 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001482 }
Li Zefan82d59022011-04-20 10:33:24 +08001483
Yan Zheng80ff3852008-10-30 14:20:02 -04001484 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001485 struct btrfs_key found_key;
1486 struct btrfs_file_extent_item *fi;
1487 struct extent_buffer *leaf;
1488 u64 extent_end;
1489 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001490 u64 num_bytes = 0;
1491 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001492 u64 ram_bytes;
1493 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001494
1495 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001496
Liu Boe4c3b2d2017-01-30 12:25:28 -08001497 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001498 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001499 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001500 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001501
1502 /*
1503 * If there is no extent for our range when doing the initial
1504 * search, then go back to the previous slot as it will be the
1505 * one containing the search offset
1506 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001507 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1508 leaf = path->nodes[0];
1509 btrfs_item_key_to_cpu(leaf, &found_key,
1510 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001511 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 found_key.type == BTRFS_EXTENT_DATA_KEY)
1513 path->slots[0]--;
1514 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001515 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001516next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001517 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001518 leaf = path->nodes[0];
1519 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1520 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001521 if (ret < 0) {
1522 if (cow_start != (u64)-1)
1523 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001524 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001525 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 if (ret > 0)
1527 break;
1528 leaf = path->nodes[0];
1529 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001530
Yan Zheng80ff3852008-10-30 14:20:02 -04001531 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001532
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001533 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001534 if (found_key.objectid > ino)
1535 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001536 /*
1537 * Keep searching until we find an EXTENT_ITEM or there are no
1538 * more extents for this inode
1539 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001540 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1541 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1542 path->slots[0]++;
1543 goto next_slot;
1544 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001545
1546 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001547 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 found_key.offset > end)
1549 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001550
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001551 /*
1552 * If the found extent starts after requested offset, then
1553 * adjust extent_end to be right before this extent begins
1554 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001555 if (found_key.offset > cur_offset) {
1556 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001557 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001558 goto out_check;
1559 }
Chris Masonc31f8832008-01-08 15:46:31 -05001560
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001561 /*
1562 * Found extent which begins before our range and potentially
1563 * intersect it
1564 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001565 fi = btrfs_item_ptr(leaf, path->slots[0],
1566 struct btrfs_file_extent_item);
1567 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001568
Josef Bacikcc95bef2013-04-04 14:31:27 -04001569 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001570 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1571 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001572 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001573 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001574 extent_end = found_key.offset +
1575 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001576 disk_num_bytes =
1577 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001578 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001579 * If the extent we got ends before our current offset,
1580 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001581 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001582 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001583 path->slots[0]++;
1584 goto next_slot;
1585 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001586 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001587 if (disk_bytenr == 0)
1588 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001589 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001590 if (btrfs_file_extent_compression(leaf, fi) ||
1591 btrfs_file_extent_encryption(leaf, fi) ||
1592 btrfs_file_extent_other_encoding(leaf, fi))
1593 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001594 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001595 * If extent is created before the last volume's snapshot
1596 * this implies the extent is shared, hence we can't do
1597 * nocow. This is the same check as in
1598 * btrfs_cross_ref_exist but without calling
1599 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001600 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001601 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001602 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001603 btrfs_root_last_snapshot(&root->root_item))
1604 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001605 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1606 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001607 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001608 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001609 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001610 ret = btrfs_cross_ref_exist(root, ino,
1611 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001612 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001613 if (ret) {
1614 /*
1615 * ret could be -EIO if the above fails to read
1616 * metadata.
1617 */
1618 if (ret < 0) {
1619 if (cow_start != (u64)-1)
1620 cur_offset = cow_start;
1621 goto error;
1622 }
1623
Nikolay Borisov3e024842019-08-21 10:42:03 +03001624 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001625 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001626 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001627 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001628 disk_bytenr += cur_offset - found_key.offset;
1629 num_bytes = min(end + 1, extent_end) - cur_offset;
1630 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001631 * If there are pending snapshots for this root, we
1632 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001633 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001634 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001635 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001636 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001637 * force cow if csum exists in the range.
1638 * this ensure that csum for a given extent are
1639 * either valid or do not exist.
1640 */
Liu Bo58113752018-01-31 17:09:13 -07001641 ret = csum_exist_in_range(fs_info, disk_bytenr,
1642 num_bytes);
1643 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001644 /*
1645 * ret could be -EIO if the above fails to read
1646 * metadata.
1647 */
1648 if (ret < 0) {
1649 if (cow_start != (u64)-1)
1650 cur_offset = cow_start;
1651 goto error;
1652 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001653 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001654 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001655 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001656 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001657 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001658 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001659 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001660 extent_end = found_key.offset + ram_bytes;
1661 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001662 /* Skip extents outside of our requested range */
1663 if (extent_end <= start) {
1664 path->slots[0]++;
1665 goto next_slot;
1666 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001667 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001668 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001669 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001670 }
1671out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001672 /*
1673 * If nocow is false then record the beginning of the range
1674 * that needs to be COWed
1675 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001676 if (!nocow) {
1677 if (cow_start == (u64)-1)
1678 cow_start = cur_offset;
1679 cur_offset = extent_end;
1680 if (cur_offset > end)
1681 break;
1682 path->slots[0]++;
1683 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001684 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001685
David Sterbab3b4aa72011-04-21 01:20:15 +02001686 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001687
1688 /*
1689 * COW range from cow_start to found_key.offset - 1. As the key
1690 * will contain the beginning of the first extent that can be
1691 * NOCOW, following one which needs to be COW'ed
1692 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001693 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001694 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001695 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001696 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001697 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001698 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001699 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001700 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001701
Yan Zhengd899e052008-10-30 14:25:28 -04001702 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001703 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001704 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001705
Nikolay Borisov968322c2020-06-03 08:55:21 +03001706 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001707 orig_start,
1708 disk_bytenr, /* block_start */
1709 num_bytes, /* block_len */
1710 disk_num_bytes, /* orig_block_len */
1711 ram_bytes, BTRFS_COMPRESS_NONE,
1712 BTRFS_ORDERED_PREALLOC);
1713 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001714 ret = PTR_ERR(em);
1715 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001716 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001717 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001718 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001719 disk_bytenr, num_bytes,
1720 num_bytes,
1721 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001722 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001723 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001724 cur_offset + num_bytes - 1,
1725 0);
1726 goto error;
1727 }
Yan Zhengd899e052008-10-30 14:25:28 -04001728 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001729 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001730 disk_bytenr, num_bytes,
1731 num_bytes,
1732 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001733 if (ret)
1734 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001735 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001736
Filipe Mananaf78c4362016-05-09 13:15:41 +01001737 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001738 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001739 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001740
Yan, Zhengefa56462010-05-16 10:49:59 -04001741 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001742 BTRFS_DATA_RELOC_TREE_OBJECTID)
1743 /*
1744 * Error handled later, as we must prevent
1745 * extent_clear_unlock_delalloc() in error handler
1746 * from freeing metadata of created ordered extent.
1747 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001748 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001749 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001750
Nikolay Borisov968322c2020-06-03 08:55:21 +03001751 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001752 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001753 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001754 EXTENT_DELALLOC |
1755 EXTENT_CLEAR_DATA_RESV,
1756 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1757
Yan Zheng80ff3852008-10-30 14:20:02 -04001758 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001759
1760 /*
1761 * btrfs_reloc_clone_csums() error, now we're OK to call error
1762 * handler, as metadata for created ordered extent will only
1763 * be freed by btrfs_finish_ordered_io().
1764 */
1765 if (ret)
1766 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001767 if (cur_offset > end)
1768 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001769 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001770 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001771
Robbie Ko506481b2018-10-30 18:04:04 +08001772 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001773 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001774
Yan Zheng80ff3852008-10-30 14:20:02 -04001775 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001776 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001777 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1778 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001779 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001780 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001781 }
1782
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001783error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001784 if (nocow)
1785 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1786
Josef Bacik17ca04a2012-05-31 15:58:55 -04001787 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001788 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001789 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001790 EXTENT_DELALLOC | EXTENT_DEFRAG |
1791 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1792 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001793 PAGE_SET_WRITEBACK |
1794 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001795 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001796 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001797}
1798
Nikolay Borisov0c494222020-06-03 08:55:28 +03001799static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001800{
1801
Nikolay Borisov0c494222020-06-03 08:55:28 +03001802 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1803 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001804 return 0;
1805
1806 /*
1807 * @defrag_bytes is a hint value, no spinlock held here,
1808 * if is not zero, it means the file is defragging.
1809 * Force cow if given extent needs to be defragged.
1810 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001811 if (inode->defrag_bytes &&
1812 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001813 return 1;
1814
1815 return 0;
1816}
1817
Chris Masond352ac62008-09-29 15:18:18 -04001818/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001819 * Function to process delayed allocation (create CoW) for ranges which are
1820 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001821 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001822int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001823 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1824 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001825{
Chris Masonbe20aa92007-12-17 20:14:01 -05001826 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001827 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001828
Nikolay Borisov98456b92020-06-03 08:55:29 +03001829 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1830 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001831 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001832 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1833 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001834 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001835 } else if (!inode_can_compress(inode) ||
1836 !inode_need_compress(inode, start, end)) {
1837 ret = cow_file_range(inode, locked_page, start, end,
1838 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001839 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001840 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1841 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001842 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001843 }
Qu Wenruo524272602017-03-08 10:25:52 +08001844 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001845 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001846 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001847 return ret;
1848}
1849
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001850void btrfs_split_delalloc_extent(struct inode *inode,
1851 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001852{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001853 u64 size;
1854
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001855 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001856 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001857 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858
Josef Bacikdcab6a32015-02-11 15:08:59 -05001859 size = orig->end - orig->start + 1;
1860 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001861 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001862 u64 new_size;
1863
1864 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001865 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001866 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001867 */
1868 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001869 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001870 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001871 num_extents += count_max_extents(new_size);
1872 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001873 return;
1874 }
1875
Josef Bacik9e0baf62011-07-15 15:16:44 +00001876 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001877 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001878 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001879}
1880
1881/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001882 * Handle merged delayed allocation extents so we can keep track of new extents
1883 * that are just merged onto old extents, such as when we are doing sequential
1884 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001885 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001886void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1887 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001888{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001889 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001890 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001891
Josef Bacik9ed74f22009-09-11 16:12:44 -04001892 /* not delalloc, ignore it */
1893 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001894 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001895
Josef Bacik8461a3d2015-03-13 15:12:08 -04001896 if (new->start > other->start)
1897 new_size = new->end - other->start + 1;
1898 else
1899 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001900
1901 /* we're not bigger than the max, unreserve the space and go */
1902 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1903 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001904 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001905 spin_unlock(&BTRFS_I(inode)->lock);
1906 return;
1907 }
1908
1909 /*
Josef Bacikba117212015-03-13 15:01:24 -04001910 * We have to add up either side to figure out how many extents were
1911 * accounted for before we merged into one big extent. If the number of
1912 * extents we accounted for is <= the amount we need for the new range
1913 * then we can return, otherwise drop. Think of it like this
1914 *
1915 * [ 4k][MAX_SIZE]
1916 *
1917 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1918 * need 2 outstanding extents, on one side we have 1 and the other side
1919 * we have 1 so they are == and we can return. But in this case
1920 *
1921 * [MAX_SIZE+4k][MAX_SIZE+4k]
1922 *
1923 * Each range on their own accounts for 2 extents, but merged together
1924 * they are only 3 extents worth of accounting, so we need to drop in
1925 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001926 */
Josef Bacikba117212015-03-13 15:01:24 -04001927 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001928 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001929 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001930 num_extents += count_max_extents(old_size);
1931 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001932 return;
1933
Josef Bacik9e0baf62011-07-15 15:16:44 +00001934 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001935 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001936 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001937}
1938
Miao Xieeb73c1b2013-05-15 07:48:22 +00001939static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1940 struct inode *inode)
1941{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001942 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1943
Miao Xieeb73c1b2013-05-15 07:48:22 +00001944 spin_lock(&root->delalloc_lock);
1945 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1946 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1947 &root->delalloc_inodes);
1948 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1949 &BTRFS_I(inode)->runtime_flags);
1950 root->nr_delalloc_inodes++;
1951 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001952 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001953 BUG_ON(!list_empty(&root->delalloc_root));
1954 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001955 &fs_info->delalloc_roots);
1956 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001957 }
1958 }
1959 spin_unlock(&root->delalloc_lock);
1960}
1961
Nikolay Borisov2b877332018-04-27 12:21:51 +03001962
1963void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1964 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001965{
David Sterba3ffbd682018-06-29 10:56:42 +02001966 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001967
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001968 if (!list_empty(&inode->delalloc_inodes)) {
1969 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001970 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001971 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001972 root->nr_delalloc_inodes--;
1973 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001974 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001975 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001976 BUG_ON(list_empty(&root->delalloc_root));
1977 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001978 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001979 }
1980 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001981}
1982
1983static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1984 struct btrfs_inode *inode)
1985{
1986 spin_lock(&root->delalloc_lock);
1987 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001988 spin_unlock(&root->delalloc_lock);
1989}
1990
Chris Masond352ac62008-09-29 15:18:18 -04001991/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001992 * Properly track delayed allocation bytes in the inode and to maintain the
1993 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001994 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001995void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1996 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001997{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1999
Wang Shilong47059d92014-07-03 18:22:07 +08002000 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2001 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002002 /*
2003 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002004 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002005 * bit, which is only set or cleared with irqs on
2006 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002007 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002008 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002009 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002010 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002011 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002012
Josef Bacik8b62f872017-10-19 14:15:55 -04002013 spin_lock(&BTRFS_I(inode)->lock);
2014 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2015 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002016
Josef Bacik6a3891c2015-03-16 17:38:52 -04002017 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002018 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002019 return;
2020
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002021 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2022 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002023 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002024 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002025 if (*bits & EXTENT_DEFRAG)
2026 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002027 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002028 &BTRFS_I(inode)->runtime_flags))
2029 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002030 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002031 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002032
2033 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2034 (*bits & EXTENT_DELALLOC_NEW)) {
2035 spin_lock(&BTRFS_I(inode)->lock);
2036 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2037 state->start;
2038 spin_unlock(&BTRFS_I(inode)->lock);
2039 }
Chris Mason291d6732008-01-29 15:55:23 -05002040}
2041
Chris Masond352ac62008-09-29 15:18:18 -04002042/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002043 * Once a range is no longer delalloc this function ensures that proper
2044 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002045 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002046void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2047 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002048{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002049 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2050 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002051 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002052 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002053
Filipe Manana4a4b9642017-07-27 19:52:55 +01002054 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2055 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002056 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002057 spin_unlock(&inode->lock);
2058 }
Wang Shilong47059d92014-07-03 18:22:07 +08002059
Chris Mason75eff682008-12-15 15:54:40 -05002060 /*
2061 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002062 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002063 * bit, which is only set or cleared with irqs on
2064 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002065 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002066 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002067 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002068
Josef Bacik8b62f872017-10-19 14:15:55 -04002069 spin_lock(&inode->lock);
2070 btrfs_mod_outstanding_extents(inode, -num_extents);
2071 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002072
Josef Bacikb6d08f02013-09-27 14:57:43 -04002073 /*
2074 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002075 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002076 * error.
2077 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002078 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002079 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002080 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002081
Josef Bacik6a3891c2015-03-16 17:38:52 -04002082 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002083 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002084 return;
2085
Filipe Mananaa315e682017-03-06 23:04:20 +00002086 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2087 do_list && !(state->state & EXTENT_NORESERVE) &&
2088 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002089 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002090
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002091 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2092 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002093 spin_lock(&inode->lock);
2094 inode->delalloc_bytes -= len;
2095 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002096 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002097 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002098 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002099 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002100 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002101
2102 if ((state->state & EXTENT_DELALLOC_NEW) &&
2103 (*bits & EXTENT_DELALLOC_NEW)) {
2104 spin_lock(&inode->lock);
2105 ASSERT(inode->new_delalloc_bytes >= len);
2106 inode->new_delalloc_bytes -= len;
2107 spin_unlock(&inode->lock);
2108 }
Chris Mason291d6732008-01-29 15:55:23 -05002109}
2110
Chris Masond352ac62008-09-29 15:18:18 -04002111/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002112 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2113 * in a chunk's stripe. This function ensures that bios do not span a
2114 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002115 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002116 * @page - The page we are about to add to the bio
2117 * @size - size we want to add to the bio
2118 * @bio - bio we want to ensure is smaller than a stripe
2119 * @bio_flags - flags of the bio
2120 *
2121 * return 1 if page cannot be added to the bio
2122 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002123 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002124 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002125int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2126 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002128 struct inode *inode = page->mapping->host;
2129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002130 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002131 u64 length = 0;
2132 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002133 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002134 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002135
Chris Mason771ed682008-11-06 22:02:51 -05002136 if (bio_flags & EXTENT_BIO_COMPRESSED)
2137 return 0;
2138
Kent Overstreet4f024f32013-10-11 15:44:27 -07002139 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002140 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002141 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2142 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002143 if (ret < 0)
2144 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002145
2146 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002147 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002148 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002149}
2150
Chris Masond352ac62008-09-29 15:18:18 -04002151/*
2152 * in order to insert checksums into the metadata in large chunks,
2153 * we wait until bio submission time. All the pages in the bio are
2154 * checksummed and sums are attached onto the ordered extent record.
2155 *
2156 * At IO completion time the cums attached on the ordered extent record
2157 * are inserted into the btree
2158 */
David Sterbad0ee3932018-03-08 14:35:48 +01002159static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002160 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002161{
Josef Bacikc6100a42017-05-05 11:57:13 -04002162 struct inode *inode = private_data;
Chris Masone0156402008-04-16 11:15:20 -04002163
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002164 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002165}
Chris Masone0156402008-04-16 11:15:20 -04002166
Chris Mason4a69a412008-11-06 22:03:00 -05002167/*
Chris Masoncad321a2008-12-17 14:51:42 -05002168 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002169 * on write, or reading the csums from the tree before a read.
2170 *
2171 * Rules about async/sync submit,
2172 * a) read: sync submit
2173 *
2174 * b) write without checksum: sync submit
2175 *
2176 * c) write with checksum:
2177 * c-1) if bio is issued by fsync: sync submit
2178 * (sync_writers != 0)
2179 *
2180 * c-2) if root is reloc root: sync submit
2181 * (only in case of buffered IO)
2182 *
2183 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002184 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002185static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002186 int mirror_num,
2187 unsigned long bio_flags)
2188
Chris Mason44b8bd72008-04-16 11:14:51 -04002189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002191 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302192 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002193 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002194 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002195 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002196
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002197 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002198
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002199 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302200 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002201
Mike Christie37226b22016-06-05 14:31:52 -05002202 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002203 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002204 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002205 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002206
Chris Masond20f7042008-12-08 16:58:54 -05002207 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002208 ret = btrfs_submit_compressed_read(inode, bio,
2209 mirror_num,
2210 bio_flags);
2211 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002212 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002213 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002214 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002215 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002216 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002217 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002218 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002219 /* csum items have already been cloned */
2220 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2221 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002222 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002223 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002224 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002225 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002226 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002227 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002228 if (ret)
2229 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002230 }
2231
Chris Mason0b86a832008-03-24 15:01:56 -04002232mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002233 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002234
2235out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002236 if (ret) {
2237 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002238 bio_endio(bio);
2239 }
Stefan Behrens61891922012-11-05 18:51:52 +01002240 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002241}
Chris Mason6885f302008-02-20 16:11:05 -05002242
Chris Masond352ac62008-09-29 15:18:18 -04002243/*
2244 * given a list of ordered sums record them in the inode. This happens
2245 * at IO completion time based on sums calculated at bio submission time.
2246 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002247static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002248 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002249{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002250 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002251 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002252
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002253 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002254 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002255 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002256 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002257 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002258 if (ret)
2259 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002260 }
2261 return 0;
2262}
2263
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002264int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002265 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002266 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002267{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002268 WARN_ON(PAGE_ALIGNED(end));
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002269 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2270 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002271}
2272
Chris Masond352ac62008-09-29 15:18:18 -04002273/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002274struct btrfs_writepage_fixup {
2275 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002276 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002277 struct btrfs_work work;
2278};
2279
Christoph Hellwigb2950862008-12-02 09:54:17 -05002280static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002281{
2282 struct btrfs_writepage_fixup *fixup;
2283 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002284 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002285 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002286 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002287 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002288 u64 page_start;
2289 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002290 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002291 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002292
2293 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2294 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002295 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002296 page_start = page_offset(page);
2297 page_end = page_offset(page) + PAGE_SIZE - 1;
2298
2299 /*
2300 * This is similar to page_mkwrite, we need to reserve the space before
2301 * we take the page lock.
2302 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002303 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2304 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002305again:
Chris Mason247e7432008-07-17 12:53:51 -04002306 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002307
Chris Mason25f3c502020-01-21 11:51:42 -05002308 /*
2309 * Before we queued this fixup, we took a reference on the page.
2310 * page->mapping may go NULL, but it shouldn't be moved to a different
2311 * address space.
2312 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002313 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2314 /*
2315 * Unfortunately this is a little tricky, either
2316 *
2317 * 1) We got here and our page had already been dealt with and
2318 * we reserved our space, thus ret == 0, so we need to just
2319 * drop our space reservation and bail. This can happen the
2320 * first time we come into the fixup worker, or could happen
2321 * while waiting for the ordered extent.
2322 * 2) Our page was already dealt with, but we happened to get an
2323 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2324 * this case we obviously don't have anything to release, but
2325 * because the page was already dealt with we don't want to
2326 * mark the page with an error, so make sure we're resetting
2327 * ret to 0. This is why we have this check _before_ the ret
2328 * check, because we do not want to have a surprise ENOSPC
2329 * when the page was already properly dealt with.
2330 */
2331 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002332 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2333 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002334 page_start, PAGE_SIZE,
2335 true);
2336 }
2337 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002338 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002339 }
Chris Mason25f3c502020-01-21 11:51:42 -05002340
2341 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002342 * We can't mess with the page state unless it is locked, so now that
2343 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002344 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002345 if (ret)
2346 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002347
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002348 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002349
2350 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002351 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002352 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002353
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002354 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002355 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002356 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2357 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002358 unlock_page(page);
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002359 btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002360 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002361 goto again;
2362 }
Chris Mason247e7432008-07-17 12:53:51 -04002363
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002364 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002365 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002366 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002367 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002368
Chris Mason25f3c502020-01-21 11:51:42 -05002369 /*
2370 * Everything went as planned, we're now the owner of a dirty page with
2371 * delayed allocation bits set and space reserved for our COW
2372 * destination.
2373 *
2374 * The page was dirty when we started, nothing should have cleaned it.
2375 */
2376 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002377 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002378out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002379 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002380 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002381 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2382 PAGE_SIZE, true);
2383 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002384 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002385out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002386 if (ret) {
2387 /*
2388 * We hit ENOSPC or other errors. Update the mapping and page
2389 * to reflect the errors and clean the page.
2390 */
2391 mapping_set_error(page->mapping, ret);
2392 end_extent_writepage(page, ret, page_start, page_end);
2393 clear_page_dirty_for_io(page);
2394 SetPageError(page);
2395 }
2396 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002397 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002398 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002399 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002400 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002401 /*
2402 * As a precaution, do a delayed iput in case it would be the last iput
2403 * that could need flushing space. Recursing back to fixup worker would
2404 * deadlock.
2405 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002406 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002407}
2408
2409/*
2410 * There are a few paths in the higher layers of the kernel that directly
2411 * set the page dirty bit without asking the filesystem if it is a
2412 * good idea. This causes problems because we want to make sure COW
2413 * properly happens and the data=ordered rules are followed.
2414 *
Chris Masonc8b97812008-10-29 14:49:59 -04002415 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002416 * hasn't been properly setup for IO. We kick off an async process
2417 * to fix it up. The async helper will wait for ordered extents, set
2418 * the delalloc bit and make it safe to write the page.
2419 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002420int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002421{
2422 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002424 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002425
Chris Mason8b62b722009-09-02 16:53:46 -04002426 /* this page is properly in the ordered list */
2427 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002428 return 0;
2429
Chris Mason25f3c502020-01-21 11:51:42 -05002430 /*
2431 * PageChecked is set below when we create a fixup worker for this page,
2432 * don't try to create another one if we're already PageChecked()
2433 *
2434 * The extent_io writepage code will redirty the page if we send back
2435 * EAGAIN.
2436 */
Chris Mason247e7432008-07-17 12:53:51 -04002437 if (PageChecked(page))
2438 return -EAGAIN;
2439
2440 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2441 if (!fixup)
2442 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002443
Josef Bacikf4b13632020-01-21 14:34:52 -05002444 /*
2445 * We are already holding a reference to this inode from
2446 * write_cache_pages. We need to hold it because the space reservation
2447 * takes place outside of the page lock, and we can't trust
2448 * page->mapping outside of the page lock.
2449 */
2450 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002451 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002452 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002453 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002454 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002455 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002457
2458 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002459}
2460
Yan Zhengd899e052008-10-30 14:25:28 -04002461static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002462 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002463 struct btrfs_file_extent_item *stack_fi,
2464 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002465{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002466 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002467 struct btrfs_path *path;
2468 struct extent_buffer *leaf;
2469 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002470 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2471 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2472 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2473 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002474 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002475 int ret;
2476
2477 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002478 if (!path)
2479 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002480
Chris Masona1ed8352009-09-11 12:27:37 -04002481 /*
2482 * we may be replacing one extent in the tree with another.
2483 * The new extent is pinned in the extent map, and we don't want
2484 * to drop it from the cache until it is completely in the btree.
2485 *
2486 * So, tell btrfs_drop_extents to leave this extent in the cache.
2487 * the caller is expected to unpin it and allow it to be merged
2488 * with the others.
2489 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002490 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002491 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002492 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002493 if (ret)
2494 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002495
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002496 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002497 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002498 ins.offset = file_pos;
2499 ins.type = BTRFS_EXTENT_DATA_KEY;
2500
2501 path->leave_spinning = 1;
2502 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002503 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002504 if (ret)
2505 goto out;
2506 }
Yan Zhengd899e052008-10-30 14:25:28 -04002507 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002508 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2509 write_extent_buffer(leaf, stack_fi,
2510 btrfs_item_ptr_offset(leaf, path->slots[0]),
2511 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002512
Yan Zhengd899e052008-10-30 14:25:28 -04002513 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002514 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002515
Nikolay Borisovc553f942020-06-03 08:55:19 +03002516 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002517
2518 ins.objectid = disk_bytenr;
2519 ins.offset = disk_num_bytes;
2520 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002521
Nikolay Borisovc553f942020-06-03 08:55:19 +03002522 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002523 if (ret)
2524 goto out;
2525
Nikolay Borisovc553f942020-06-03 08:55:19 +03002526 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002527 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002528out:
Yan Zhengd899e052008-10-30 14:25:28 -04002529 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002530
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002531 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002532}
2533
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002534static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002535 u64 start, u64 len)
2536{
David Sterba32da53862019-10-29 19:20:18 +01002537 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002538
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002539 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002540 ASSERT(cache);
2541
2542 spin_lock(&cache->lock);
2543 cache->delalloc_bytes -= len;
2544 spin_unlock(&cache->lock);
2545
2546 btrfs_put_block_group(cache);
2547}
2548
Qu Wenruo203f44c52020-06-10 09:04:40 +08002549static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2550 struct inode *inode,
2551 struct btrfs_ordered_extent *oe)
2552{
2553 struct btrfs_file_extent_item stack_fi;
2554 u64 logical_len;
2555
2556 memset(&stack_fi, 0, sizeof(stack_fi));
2557 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2558 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2559 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2560 oe->disk_num_bytes);
2561 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2562 logical_len = oe->truncated_len;
2563 else
2564 logical_len = oe->num_bytes;
2565 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2566 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2567 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2568 /* Encryption and other encoding is reserved and all 0 */
2569
Nikolay Borisovc553f942020-06-03 08:55:19 +03002570 return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002571 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002572}
2573
2574/*
2575 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002576 * an ordered extent if the range of bytes in the file it covers are
2577 * fully written.
2578 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002579static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002580{
Josef Bacik5fd02042012-05-02 14:00:54 -04002581 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002584 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002585 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002586 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002587 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002588 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002589 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002590 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002591 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002592 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002593 bool range_locked = false;
2594 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002595 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002596 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002597
Omar Sandovalbffe6332019-12-02 17:34:19 -08002598 start = ordered_extent->file_offset;
2599 end = start + ordered_extent->num_bytes - 1;
2600
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002601 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2602 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2603 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2604 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605
Nikolay Borisov8d510122019-10-08 20:43:06 +03002606 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002607
Josef Bacik5fd02042012-05-02 14:00:54 -04002608 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2609 ret = -EIO;
2610 goto out;
2611 }
2612
Omar Sandovalbffe6332019-12-02 17:34:19 -08002613 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002614
Josef Bacik77cef2e2013-08-29 13:57:21 -04002615 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2616 truncated = true;
2617 logical_len = ordered_extent->truncated_len;
2618 /* Truncated the entire extent, don't bother adding */
2619 if (!logical_len)
2620 goto out;
2621 }
2622
Yan, Zhengc2167752009-11-12 09:34:21 +00002623 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002624 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002625
Josef Bacikd923afe2020-01-17 09:02:23 -05002626 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002627 if (freespace_inode)
2628 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002629 else
2630 trans = btrfs_join_transaction(root);
2631 if (IS_ERR(trans)) {
2632 ret = PTR_ERR(trans);
2633 trans = NULL;
2634 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002635 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002636 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002637 ret = btrfs_update_inode_fallback(trans, root, inode);
2638 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002639 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002640 goto out;
2641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002643 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002644 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Nikolay Borisov8d510122019-10-08 20:43:06 +03002646 if (freespace_inode)
2647 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002648 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002649 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002650 if (IS_ERR(trans)) {
2651 ret = PTR_ERR(trans);
2652 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002653 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 }
Chris Masona79b7d42014-05-22 16:18:52 -07002655
Josef Bacik69fe2d72017-10-19 14:15:57 -04002656 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002657
Chris Masonc8b97812008-10-29 14:49:59 -04002658 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002659 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002660 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002661 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002662 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002663 ordered_extent->file_offset,
2664 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002665 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002666 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002668 ret = insert_ordered_extent_file_extent(trans, inode,
2669 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002670 if (!ret) {
2671 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002672 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002673 ordered_extent->disk_bytenr,
2674 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002675 }
Yan Zhengd899e052008-10-30 14:25:28 -04002676 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002677 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002678 ordered_extent->file_offset,
2679 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002681 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002682 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002684
Nikolay Borisovac01f262018-01-08 10:59:43 +02002685 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2686 if (ret) {
2687 btrfs_abort_transaction(trans, ret);
2688 goto out;
2689 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690
Josef Bacikd923afe2020-01-17 09:02:23 -05002691 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002692 ret = btrfs_update_inode_fallback(trans, root, inode);
2693 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002694 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002695 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002696 }
2697 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002698out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002699 clear_bits = EXTENT_DEFRAG;
2700 if (range_locked)
2701 clear_bits |= EXTENT_LOCKED;
2702 if (clear_new_delalloc_bytes)
2703 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002704 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2705 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002706 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002707
Miao Xiea698d0752012-09-20 01:51:59 -06002708 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002709 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002710
Josef Bacik77cef2e2013-08-29 13:57:21 -04002711 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002712 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002713
2714 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002715 unwritten_start += logical_len;
2716 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002717
2718 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002719 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002720
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002721 /*
2722 * If the ordered extent had an IOERR or something else went
2723 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002724 * back to the allocator. We only free the extent in the
2725 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002726 *
2727 * If we made it past insert_reserved_file_extent before we
2728 * errored out then we don't need to do this as the accounting
2729 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002730 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002731 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002732 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002734 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2735 /*
2736 * Discard the range before returning it back to the
2737 * free space pool
2738 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002739 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002740 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002741 ordered_extent->disk_bytenr,
2742 ordered_extent->disk_num_bytes,
2743 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002744 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002745 ordered_extent->disk_bytenr,
2746 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002747 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002748 }
2749
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002751 * This needs to be done to make sure anybody waiting knows we are done
2752 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002753 */
2754 btrfs_remove_ordered_extent(inode, ordered_extent);
2755
Chris Masone6dcd2d2008-07-17 12:53:50 -04002756 /* once for us */
2757 btrfs_put_ordered_extent(ordered_extent);
2758 /* once for the tree */
2759 btrfs_put_ordered_extent(ordered_extent);
2760
Josef Bacik5fd02042012-05-02 14:00:54 -04002761 return ret;
2762}
2763
2764static void finish_ordered_fn(struct btrfs_work *work)
2765{
2766 struct btrfs_ordered_extent *ordered_extent;
2767 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2768 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769}
2770
Nikolay Borisovc6297322018-11-08 10:18:08 +02002771void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2772 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002773{
Josef Bacik5fd02042012-05-02 14:00:54 -04002774 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002775 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002777 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002778
liubo1abe9b82011-03-24 11:18:59 +00002779 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2780
Chris Mason8b62b722009-09-02 16:53:46 -04002781 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002782 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2783 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002784 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002785
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002786 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002787 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002788 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002790
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002791 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002792 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002793}
2794
Omar Sandoval47df7762020-04-16 14:46:17 -07002795static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2796 int icsum, struct page *page, int pgoff, u64 start,
2797 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002798{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2800 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002801 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002802 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2803 u8 *csum_expected;
2804 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002805
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002806 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002807
2808 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002809 shash->tfm = fs_info->csum_shash;
2810
Eric Biggersfd080012020-04-30 23:51:59 -07002811 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002812
2813 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002814 goto zeroit;
2815
2816 kunmap_atomic(kaddr);
2817 return 0;
2818zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002819 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2820 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002821 if (io_bio->device)
2822 btrfs_dev_stat_inc_and_print(io_bio->device,
2823 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002824 memset(kaddr + pgoff, 1, len);
2825 flush_dcache_page(page);
2826 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002827 return -EIO;
2828}
2829
Chris Masond352ac62008-09-29 15:18:18 -04002830/*
Chris Masond352ac62008-09-29 15:18:18 -04002831 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002832 * if there's a match, we allow the bio to finish. If not, the code in
2833 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002834 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002835static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2836 u64 phy_offset, struct page *page,
2837 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002838{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002839 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002840 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002841 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002843
Chris Masond20f7042008-12-08 16:58:54 -05002844 if (PageChecked(page)) {
2845 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002846 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002847 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002848
2849 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002850 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002851
Yan Zheng17d217f2008-12-12 10:03:38 -05002852 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002853 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002854 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002855 return 0;
2856 }
2857
Miao Xiefacc8a222013-07-25 19:22:34 +08002858 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002859 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2860 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002861}
Chris Masonb888db22007-08-27 16:49:44 -04002862
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002863/*
2864 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2865 *
2866 * @inode: The inode we want to perform iput on
2867 *
2868 * This function uses the generic vfs_inode::i_count to track whether we should
2869 * just decrement it (in case it's > 1) or if this is the last iput then link
2870 * the inode to the delayed iput machinery. Delayed iputs are processed at
2871 * transaction commit time/superblock commit/cleaner kthread.
2872 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002873void btrfs_add_delayed_iput(struct inode *inode)
2874{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002876 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002877
2878 if (atomic_add_unless(&inode->i_count, -1, 1))
2879 return;
2880
Josef Bacik034f7842018-12-03 11:06:52 -05002881 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002882 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002883 ASSERT(list_empty(&binode->delayed_iput));
2884 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002885 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002886 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2887 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888}
2889
Josef Bacik63611e72019-06-18 10:59:18 -04002890static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2891 struct btrfs_inode *inode)
2892{
2893 list_del_init(&inode->delayed_iput);
2894 spin_unlock(&fs_info->delayed_iput_lock);
2895 iput(&inode->vfs_inode);
2896 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2897 wake_up(&fs_info->delayed_iputs_wait);
2898 spin_lock(&fs_info->delayed_iput_lock);
2899}
2900
2901static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2902 struct btrfs_inode *inode)
2903{
2904 if (!list_empty(&inode->delayed_iput)) {
2905 spin_lock(&fs_info->delayed_iput_lock);
2906 if (!list_empty(&inode->delayed_iput))
2907 run_delayed_iput_locked(fs_info, inode);
2908 spin_unlock(&fs_info->delayed_iput_lock);
2909 }
2910}
2911
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002912void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002914
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002915 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002916 while (!list_empty(&fs_info->delayed_iputs)) {
2917 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002918
David Sterba8089fe62015-11-19 14:15:51 +01002919 inode = list_first_entry(&fs_info->delayed_iputs,
2920 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002921 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002922 }
David Sterba8089fe62015-11-19 14:15:51 +01002923 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002924}
2925
Josef Bacik034f7842018-12-03 11:06:52 -05002926/**
2927 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2928 * @fs_info - the fs_info for this fs
2929 * @return - EINTR if we were killed, 0 if nothing's pending
2930 *
2931 * This will wait on any delayed iputs that are currently running with KILLABLE
2932 * set. Once they are all done running we will return, unless we are killed in
2933 * which case we return EINTR. This helps in user operations like fallocate etc
2934 * that might get blocked on the iputs.
2935 */
2936int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2937{
2938 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2939 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2940 if (ret)
2941 return -EINTR;
2942 return 0;
2943}
2944
Yan, Zhengd68fc572010-05-16 10:49:58 -04002945/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002946 * This creates an orphan entry for the given inode in case something goes wrong
2947 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002948 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002949int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002950 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002951{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002953
Omar Sandoval27919062018-05-11 13:13:37 -07002954 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2955 if (ret && ret != -EEXIST) {
2956 btrfs_abort_transaction(trans, ret);
2957 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 }
2959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002961}
2962
2963/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002964 * We have done the delete so we can go ahead and remove the orphan item for
2965 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002966 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002967static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002968 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002969{
Omar Sandoval27919062018-05-11 13:13:37 -07002970 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002971}
2972
2973/*
2974 * this cleans up any orphans that may be left on the list from the last use
2975 * of this root.
2976 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002977int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002978{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002979 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002980 struct btrfs_path *path;
2981 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002982 struct btrfs_key key, found_key;
2983 struct btrfs_trans_handle *trans;
2984 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002985 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002986 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002989 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002990
2991 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002992 if (!path) {
2993 ret = -ENOMEM;
2994 goto out;
2995 }
David Sterbae4058b52015-11-27 16:31:35 +01002996 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002997
2998 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002999 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003000 key.offset = (u64)-1;
3001
Josef Bacik7b128762008-07-24 12:17:14 -04003002 while (1) {
3003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003004 if (ret < 0)
3005 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003006
3007 /*
3008 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003009 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003010 * find the key and see if we have stuff that matches
3011 */
3012 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003013 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003014 if (path->slots[0] == 0)
3015 break;
3016 path->slots[0]--;
3017 }
3018
3019 /* pull out the item */
3020 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3022
3023 /* make sure the item matches what we want */
3024 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3025 break;
David Sterba962a2982014-06-04 18:41:45 +02003026 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003027 break;
3028
3029 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003030 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003031
3032 /*
3033 * this is where we are basically btrfs_lookup, without the
3034 * crossing root thing. we store the inode number in the
3035 * offset of the orphan item.
3036 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003037
3038 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003039 btrfs_err(fs_info,
3040 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003041 ret = -EINVAL;
3042 goto out;
3043 }
3044
3045 last_objectid = found_key.offset;
3046
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003047 found_key.objectid = found_key.offset;
3048 found_key.type = BTRFS_INODE_ITEM_KEY;
3049 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003050 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303051 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003052 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003053 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003056 struct btrfs_root *dead_root;
3057 struct btrfs_fs_info *fs_info = root->fs_info;
3058 int is_dead_root = 0;
3059
3060 /*
3061 * this is an orphan in the tree root. Currently these
3062 * could come from 2 sources:
3063 * a) a snapshot deletion in progress
3064 * b) a free space cache inode
3065 * We need to distinguish those two, as the snapshot
3066 * orphan must not get deleted.
3067 * find_dead_roots already ran before us, so if this
3068 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003069 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003070 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003071
3072 spin_lock(&fs_info->fs_roots_radix_lock);
3073 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3074 (unsigned long)found_key.objectid);
3075 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3076 is_dead_root = 1;
3077 spin_unlock(&fs_info->fs_roots_radix_lock);
3078
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003079 if (is_dead_root) {
3080 /* prevent this orphan from being found again */
3081 key.offset = found_key.objectid - 1;
3082 continue;
3083 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003084
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003085 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003086
Josef Bacika8c9e572011-09-21 16:55:59 -04003087 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003088 * If we have an inode with links, there are a couple of
3089 * possibilities. Old kernels (before v3.12) used to create an
3090 * orphan item for truncate indicating that there were possibly
3091 * extent items past i_size that needed to be deleted. In v3.12,
3092 * truncate was changed to update i_size in sync with the extent
3093 * items, but the (useless) orphan item was still created. Since
3094 * v4.18, we don't create the orphan item for truncate at all.
3095 *
3096 * So, this item could mean that we need to do a truncate, but
3097 * only if this filesystem was last used on a pre-v3.12 kernel
3098 * and was not cleanly unmounted. The odds of that are quite
3099 * slim, and it's a pain to do the truncate now, so just delete
3100 * the orphan item.
3101 *
3102 * It's also possible that this orphan item was supposed to be
3103 * deleted but wasn't. The inode number may have been reused,
3104 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003105 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003106 if (ret == -ENOENT || inode->i_nlink) {
3107 if (!ret)
3108 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003109 trans = btrfs_start_transaction(root, 1);
3110 if (IS_ERR(trans)) {
3111 ret = PTR_ERR(trans);
3112 goto out;
3113 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003114 btrfs_debug(fs_info, "auto deleting %Lu",
3115 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003116 ret = btrfs_del_orphan_item(trans, root,
3117 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003118 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003119 if (ret)
3120 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003121 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003122 }
Josef Bacik7b128762008-07-24 12:17:14 -04003123
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003124 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003125
3126 /* this will do delete_inode and everything for us */
3127 iput(inode);
3128 }
Miao Xie3254c872011-11-10 20:45:05 -05003129 /* release the path since we're done with it */
3130 btrfs_release_path(path);
3131
Yan, Zhengd68fc572010-05-16 10:49:58 -04003132 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3133
Omar Sandovala575cee2018-05-11 13:13:38 -07003134 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003135 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003136 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003137 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 }
Josef Bacik7b128762008-07-24 12:17:14 -04003139
3140 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003142
3143out:
3144 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003145 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003146 btrfs_free_path(path);
3147 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003148}
3149
Chris Masond352ac62008-09-29 15:18:18 -04003150/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003151 * very simple check to peek ahead in the leaf looking for xattrs. If we
3152 * don't find any xattrs, we know there can't be any acls.
3153 *
3154 * slot is the slot the inode is in, objectid is the objectid of the inode
3155 */
3156static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003157 int slot, u64 objectid,
3158 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003159{
3160 u32 nritems = btrfs_header_nritems(leaf);
3161 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003162 static u64 xattr_access = 0;
3163 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003164 int scanned = 0;
3165
Josef Bacikf23b5a52013-06-19 10:16:26 -04003166 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003167 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3168 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3169 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3170 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003171 }
3172
Chris Mason46a53cc2009-04-27 11:47:50 -04003173 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003174 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003175 while (slot < nritems) {
3176 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3177
3178 /* we found a different objectid, there must not be acls */
3179 if (found_key.objectid != objectid)
3180 return 0;
3181
3182 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003183 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003184 if (*first_xattr_slot == -1)
3185 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003186 if (found_key.offset == xattr_access ||
3187 found_key.offset == xattr_default)
3188 return 1;
3189 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003190
3191 /*
3192 * we found a key greater than an xattr key, there can't
3193 * be any acls later on
3194 */
3195 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3196 return 0;
3197
3198 slot++;
3199 scanned++;
3200
3201 /*
3202 * it goes inode, inode backrefs, xattrs, extents,
3203 * so if there are a ton of hard links to an inode there can
3204 * be a lot of backrefs. Don't waste time searching too hard,
3205 * this is just an optimization
3206 */
3207 if (scanned >= 8)
3208 break;
3209 }
3210 /* we hit the end of the leaf before we found an xattr or
3211 * something larger than an xattr. We have to assume the inode
3212 * has acls
3213 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003214 if (*first_xattr_slot == -1)
3215 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003216 return 1;
3217}
3218
3219/*
Chris Masond352ac62008-09-29 15:18:18 -04003220 * read an inode from the btree into the in-memory inode
3221 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003222static int btrfs_read_locked_inode(struct inode *inode,
3223 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003226 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003227 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003228 struct btrfs_inode_item *inode_item;
3229 struct btrfs_root *root = BTRFS_I(inode)->root;
3230 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003231 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003232 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003233 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003234 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003235 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003236 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003237
3238 ret = btrfs_fill_inode(inode, &rdev);
3239 if (!ret)
3240 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003241
Filipe Manana4222ea72018-10-24 10:13:03 +01003242 if (!path) {
3243 path = btrfs_alloc_path();
3244 if (!path)
3245 return -ENOMEM;
3246 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003247
Chris Mason39279cc2007-06-12 06:35:45 -04003248 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003249
Chris Mason39279cc2007-06-12 06:35:45 -04003250 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003251 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003252 if (path != in_path)
3253 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003254 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003255 }
Chris Mason39279cc2007-06-12 06:35:45 -04003256
Chris Mason5f39d392007-10-15 16:14:19 -04003257 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003258
3259 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003260 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003261
Chris Mason5f39d392007-10-15 16:14:19 -04003262 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3263 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003264 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003265 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003266 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3267 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003268 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003269 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3270 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003271
David Sterbaa937b972014-12-12 17:39:12 +01003272 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3273 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003274
David Sterbaa937b972014-12-12 17:39:12 +01003275 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3276 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003277
David Sterbaa937b972014-12-12 17:39:12 +01003278 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3279 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003280
chandan r9cc97d62012-07-04 12:48:07 +05303281 BTRFS_I(inode)->i_otime.tv_sec =
3282 btrfs_timespec_sec(leaf, &inode_item->otime);
3283 BTRFS_I(inode)->i_otime.tv_nsec =
3284 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003285
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003286 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003287 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003288 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3289
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003290 inode_set_iversion_queried(inode,
3291 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003292 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003293 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003294 rdev = btrfs_inode_rdev(leaf, inode_item);
3295
Josef Bacikaec74772008-07-24 12:12:38 -04003296 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003297 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003298
3299cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003300 /*
3301 * If we were modified in the current generation and evicted from memory
3302 * and then re-read we need to do a full sync since we don't have any
3303 * idea about which extents were modified before we were evicted from
3304 * cache.
3305 *
3306 * This is required for both inode re-read from disk and delayed inode
3307 * in delayed_nodes_tree.
3308 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003309 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003310 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3311 &BTRFS_I(inode)->runtime_flags);
3312
Filipe Mananabde6c242015-07-24 00:00:19 +01003313 /*
3314 * We don't persist the id of the transaction where an unlink operation
3315 * against the inode was last made. So here we assume the inode might
3316 * have been evicted, and therefore the exact value of last_unlink_trans
3317 * lost, and set it to last_trans to avoid metadata inconsistencies
3318 * between the inode and its parent if the inode is fsync'ed and the log
3319 * replayed. For example, in the scenario:
3320 *
3321 * touch mydir/foo
3322 * ln mydir/foo mydir/bar
3323 * sync
3324 * unlink mydir/bar
3325 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3326 * xfs_io -c fsync mydir/foo
3327 * <power failure>
3328 * mount fs, triggers fsync log replay
3329 *
3330 * We must make sure that when we fsync our inode foo we also log its
3331 * parent inode, otherwise after log replay the parent still has the
3332 * dentry with the "bar" name but our inode foo has a link count of 1
3333 * and doesn't have an inode ref with the name "bar" anymore.
3334 *
3335 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003336 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003337 * transaction commits on fsync if our inode is a directory, or if our
3338 * inode is not a directory, logging its parent unnecessarily.
3339 */
3340 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3341
Filipe Manana3ebac172020-07-15 12:30:43 +01003342 /*
3343 * Same logic as for last_unlink_trans. We don't persist the generation
3344 * of the last transaction where this inode was used for a reflink
3345 * operation, so after eviction and reloading the inode we must be
3346 * pessimistic and assume the last transaction that modified the inode.
3347 */
3348 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3349
Miao Xie67de1172013-12-26 13:07:06 +08003350 path->slots[0]++;
3351 if (inode->i_nlink != 1 ||
3352 path->slots[0] >= btrfs_header_nritems(leaf))
3353 goto cache_acl;
3354
3355 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003356 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003357 goto cache_acl;
3358
3359 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3360 if (location.type == BTRFS_INODE_REF_KEY) {
3361 struct btrfs_inode_ref *ref;
3362
3363 ref = (struct btrfs_inode_ref *)ptr;
3364 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3365 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3366 struct btrfs_inode_extref *extref;
3367
3368 extref = (struct btrfs_inode_extref *)ptr;
3369 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3370 extref);
3371 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003372cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003373 /*
3374 * try to precache a NULL acl entry for files that don't have
3375 * any xattrs or acls
3376 */
Li Zefan33345d012011-04-20 10:31:50 +08003377 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003378 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003379 if (first_xattr_slot != -1) {
3380 path->slots[0] = first_xattr_slot;
3381 ret = btrfs_load_inode_props(inode, path);
3382 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003383 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003384 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003385 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003386 root->root_key.objectid, ret);
3387 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003388 if (path != in_path)
3389 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003390
Al Viro72c04902009-06-24 16:58:48 -04003391 if (!maybe_acls)
3392 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003393
Chris Mason39279cc2007-06-12 06:35:45 -04003394 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003395 case S_IFREG:
3396 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003397 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003398 inode->i_fop = &btrfs_file_operations;
3399 inode->i_op = &btrfs_file_inode_operations;
3400 break;
3401 case S_IFDIR:
3402 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003403 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003404 break;
3405 case S_IFLNK:
3406 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003407 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003408 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003409 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003410 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003411 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003412 init_special_inode(inode, inode->i_mode, rdev);
3413 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003414 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003415
David Sterba7b6a2212018-03-26 18:40:21 +02003416 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003417 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003418}
3419
Chris Masond352ac62008-09-29 15:18:18 -04003420/*
3421 * given a leaf and an inode, copy the inode fields into the leaf
3422 */
Chris Masone02119d2008-09-05 16:13:11 -04003423static void fill_inode_item(struct btrfs_trans_handle *trans,
3424 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003425 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003426 struct inode *inode)
3427{
Liu Bo51fab692012-12-27 09:01:21 +00003428 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003429
David Sterbac82f8232019-08-09 17:48:21 +02003430 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003431
David Sterbacc4c13d2020-04-29 02:15:56 +02003432 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3433 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3434 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3435 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3436 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003437
David Sterbacc4c13d2020-04-29 02:15:56 +02003438 btrfs_set_token_timespec_sec(&token, &item->atime,
3439 inode->i_atime.tv_sec);
3440 btrfs_set_token_timespec_nsec(&token, &item->atime,
3441 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003442
David Sterbacc4c13d2020-04-29 02:15:56 +02003443 btrfs_set_token_timespec_sec(&token, &item->mtime,
3444 inode->i_mtime.tv_sec);
3445 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3446 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003447
David Sterbacc4c13d2020-04-29 02:15:56 +02003448 btrfs_set_token_timespec_sec(&token, &item->ctime,
3449 inode->i_ctime.tv_sec);
3450 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3451 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003452
David Sterbacc4c13d2020-04-29 02:15:56 +02003453 btrfs_set_token_timespec_sec(&token, &item->otime,
3454 BTRFS_I(inode)->i_otime.tv_sec);
3455 btrfs_set_token_timespec_nsec(&token, &item->otime,
3456 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303457
David Sterbacc4c13d2020-04-29 02:15:56 +02003458 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3459 btrfs_set_token_inode_generation(&token, item,
3460 BTRFS_I(inode)->generation);
3461 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3462 btrfs_set_token_inode_transid(&token, item, trans->transid);
3463 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3464 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3465 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003466}
3467
Chris Masond352ac62008-09-29 15:18:18 -04003468/*
3469 * copy everything in the in-memory inode into the btree.
3470 */
Chris Mason21151332011-11-10 20:39:08 -05003471static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003472 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003473{
3474 struct btrfs_inode_item *inode_item;
3475 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003476 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003477 int ret;
3478
3479 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003480 if (!path)
3481 return -ENOMEM;
3482
Chris Masonb9473432009-03-13 11:00:37 -04003483 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003484 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3485 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003486 if (ret) {
3487 if (ret > 0)
3488 ret = -ENOENT;
3489 goto failed;
3490 }
3491
Chris Mason5f39d392007-10-15 16:14:19 -04003492 leaf = path->nodes[0];
3493 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003494 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003495
Chris Masone02119d2008-09-05 16:13:11 -04003496 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003497 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003498 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003499 ret = 0;
3500failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003501 btrfs_free_path(path);
3502 return ret;
3503}
3504
Chris Masond352ac62008-09-29 15:18:18 -04003505/*
Chris Mason21151332011-11-10 20:39:08 -05003506 * copy everything in the in-memory inode into the btree.
3507 */
3508noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3509 struct btrfs_root *root, struct inode *inode)
3510{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003511 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003512 int ret;
3513
3514 /*
3515 * If the inode is a free space inode, we can deadlock during commit
3516 * if we put it into the delayed code.
3517 *
3518 * The data relocation inode should also be directly updated
3519 * without delay
3520 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003521 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003522 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003523 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003524 btrfs_update_root_times(trans, root);
3525
Chris Mason21151332011-11-10 20:39:08 -05003526 ret = btrfs_delayed_update_inode(trans, root, inode);
3527 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003528 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003529 return ret;
3530 }
3531
3532 return btrfs_update_inode_item(trans, root, inode);
3533}
3534
Josef Bacikbe6aef62012-10-22 15:43:12 -04003535noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3536 struct btrfs_root *root,
3537 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003538{
3539 int ret;
3540
3541 ret = btrfs_update_inode(trans, root, inode);
3542 if (ret == -ENOSPC)
3543 return btrfs_update_inode_item(trans, root, inode);
3544 return ret;
3545}
3546
3547/*
Chris Masond352ac62008-09-29 15:18:18 -04003548 * unlink helper that gets used here in inode.c and in the tree logging
3549 * recovery code. It remove a link in a directory with a given name, and
3550 * also drops the back refs in the inode to the directory
3551 */
Al Viro92986792011-03-04 17:14:37 +00003552static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3553 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003554 struct btrfs_inode *dir,
3555 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003556 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003558 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003559 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003560 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003561 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003562 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003563 u64 ino = btrfs_ino(inode);
3564 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003565
3566 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003567 if (!path) {
3568 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003569 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003570 }
3571
Chris Masonb9473432009-03-13 11:00:37 -04003572 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003573 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003574 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003575 if (IS_ERR_OR_NULL(di)) {
3576 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003577 goto err;
3578 }
Chris Mason39279cc2007-06-12 06:35:45 -04003579 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003580 if (ret)
3581 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003582 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003583
Miao Xie67de1172013-12-26 13:07:06 +08003584 /*
3585 * If we don't have dir index, we have to get it by looking up
3586 * the inode ref, since we get the inode ref, remove it directly,
3587 * it is unnecessary to do delayed deletion.
3588 *
3589 * But if we have dir index, needn't search inode ref to get it.
3590 * Since the inode ref is close to the inode item, it is better
3591 * that we delay to delete it, and just do this deletion when
3592 * we update the inode item.
3593 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003594 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003595 ret = btrfs_delayed_delete_inode_ref(inode);
3596 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003597 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003598 goto skip_backref;
3599 }
3600 }
3601
Li Zefan33345d012011-04-20 10:31:50 +08003602 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3603 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003604 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003605 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003606 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003607 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003608 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003609 goto err;
3610 }
Miao Xie67de1172013-12-26 13:07:06 +08003611skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003612 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003613 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003614 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003615 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 }
Chris Mason39279cc2007-06-12 06:35:45 -04003617
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003618 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3619 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003621 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003622 goto err;
3623 }
Chris Masone02119d2008-09-05 16:13:11 -04003624
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003625 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3626 index);
Chris Mason6418c962010-10-30 07:34:24 -04003627 if (ret == -ENOENT)
3628 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003629 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003630 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003631
3632 /*
3633 * If we have a pending delayed iput we could end up with the final iput
3634 * being run in btrfs-cleaner context. If we have enough of these built
3635 * up we can end up burning a lot of time in btrfs-cleaner without any
3636 * way to throttle the unlinks. Since we're currently holding a ref on
3637 * the inode we can run the delayed iput here without any issues as the
3638 * final iput won't be done until after we drop the ref we're currently
3639 * holding.
3640 */
3641 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003642err:
3643 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003644 if (ret)
3645 goto out;
3646
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003647 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003648 inode_inc_iversion(&inode->vfs_inode);
3649 inode_inc_iversion(&dir->vfs_inode);
3650 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3651 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3652 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003653out:
Chris Mason39279cc2007-06-12 06:35:45 -04003654 return ret;
3655}
3656
Al Viro92986792011-03-04 17:14:37 +00003657int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3658 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003659 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003660 const char *name, int name_len)
3661{
3662 int ret;
3663 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3664 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003665 drop_nlink(&inode->vfs_inode);
3666 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003667 }
3668 return ret;
3669}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670
3671/*
3672 * helper to start transaction for unlink and rmdir.
3673 *
Josef Bacikd52be812013-05-29 14:54:47 -04003674 * unlink and rmdir are special in btrfs, they do not always free space, so
3675 * if we cannot make our reservations the normal way try and see if there is
3676 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3677 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678 */
Josef Bacikd52be812013-05-29 14:54:47 -04003679static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682
Josef Bacike70bea52011-10-11 14:18:24 -04003683 /*
3684 * 1 for the possible orphan item
3685 * 1 for the dir item
3686 * 1 for the dir index
3687 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003688 * 1 for the inode
3689 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003690 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691}
3692
Chris Mason39279cc2007-06-12 06:35:45 -04003693static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3694{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003696 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003697 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003698 int ret;
3699
Josef Bacikd52be812013-05-29 14:54:47 -04003700 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003701 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003702 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003703
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003704 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3705 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003706
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003707 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3708 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3709 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003710 if (ret)
3711 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003712
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003714 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003715 if (ret)
3716 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003717 }
Josef Bacik7b128762008-07-24 12:17:14 -04003718
Tsutomu Itohb5324022011-07-19 07:27:20 +00003719out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003720 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003721 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003722 return ret;
3723}
3724
Misono Tomohirof60a2362018-04-18 11:34:52 +09003725static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003726 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003727{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003728 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003729 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003730 struct btrfs_path *path;
3731 struct extent_buffer *leaf;
3732 struct btrfs_dir_item *di;
3733 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003734 const char *name = dentry->d_name.name;
3735 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003736 u64 index;
3737 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003738 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003739 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003740
Josef Bacik045d3962019-12-18 17:20:27 -05003741 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3742 objectid = inode->root->root_key.objectid;
3743 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3744 objectid = inode->location.objectid;
3745 } else {
3746 WARN_ON(1);
3747 return -EINVAL;
3748 }
3749
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003750 path = btrfs_alloc_path();
3751 if (!path)
3752 return -ENOMEM;
3753
Li Zefan33345d012011-04-20 10:31:50 +08003754 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003755 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003757 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003758 goto out;
3759 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003760
3761 leaf = path->nodes[0];
3762 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3763 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3764 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003765 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003766 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003767 goto out;
3768 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003769 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003770
Josef Bacikd49d3282019-12-18 17:20:28 -05003771 /*
3772 * This is a placeholder inode for a subvolume we didn't have a
3773 * reference to at the time of the snapshot creation. In the meantime
3774 * we could have renamed the real subvol link into our snapshot, so
3775 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3776 * Instead simply lookup the dir_index_item for this entry so we can
3777 * remove it. Otherwise we know we have a ref to the root and we can
3778 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3779 */
3780 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003781 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003782 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003783 if (IS_ERR_OR_NULL(di)) {
3784 if (!di)
3785 ret = -ENOENT;
3786 else
3787 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003788 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003789 goto out;
3790 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003791
3792 leaf = path->nodes[0];
3793 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003794 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003795 btrfs_release_path(path);
3796 } else {
3797 ret = btrfs_del_root_ref(trans, objectid,
3798 root->root_key.objectid, dir_ino,
3799 &index, name, name_len);
3800 if (ret) {
3801 btrfs_abort_transaction(trans, ret);
3802 goto out;
3803 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003804 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805
Lu Fengqi9add2942018-08-01 11:32:26 +08003806 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003808 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003809 goto out;
3810 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003811
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003812 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003813 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003814 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003815 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003817 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003819 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003820 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003821}
3822
Misono Tomohiroec42f162018-04-18 11:34:13 +09003823/*
3824 * Helper to check if the subvolume references other subvolumes or if it's
3825 * default.
3826 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003827static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003828{
3829 struct btrfs_fs_info *fs_info = root->fs_info;
3830 struct btrfs_path *path;
3831 struct btrfs_dir_item *di;
3832 struct btrfs_key key;
3833 u64 dir_id;
3834 int ret;
3835
3836 path = btrfs_alloc_path();
3837 if (!path)
3838 return -ENOMEM;
3839
3840 /* Make sure this root isn't set as the default subvol */
3841 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3842 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3843 dir_id, "default", 7, 0);
3844 if (di && !IS_ERR(di)) {
3845 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3846 if (key.objectid == root->root_key.objectid) {
3847 ret = -EPERM;
3848 btrfs_err(fs_info,
3849 "deleting default subvolume %llu is not allowed",
3850 key.objectid);
3851 goto out;
3852 }
3853 btrfs_release_path(path);
3854 }
3855
3856 key.objectid = root->root_key.objectid;
3857 key.type = BTRFS_ROOT_REF_KEY;
3858 key.offset = (u64)-1;
3859
3860 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3861 if (ret < 0)
3862 goto out;
3863 BUG_ON(ret == 0);
3864
3865 ret = 0;
3866 if (path->slots[0] > 0) {
3867 path->slots[0]--;
3868 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3869 if (key.objectid == root->root_key.objectid &&
3870 key.type == BTRFS_ROOT_REF_KEY)
3871 ret = -ENOTEMPTY;
3872 }
3873out:
3874 btrfs_free_path(path);
3875 return ret;
3876}
3877
Nikolay Borisov20a68002018-04-27 14:36:24 +03003878/* Delete all dentries for inodes belonging to the root */
3879static void btrfs_prune_dentries(struct btrfs_root *root)
3880{
3881 struct btrfs_fs_info *fs_info = root->fs_info;
3882 struct rb_node *node;
3883 struct rb_node *prev;
3884 struct btrfs_inode *entry;
3885 struct inode *inode;
3886 u64 objectid = 0;
3887
3888 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3889 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3890
3891 spin_lock(&root->inode_lock);
3892again:
3893 node = root->inode_tree.rb_node;
3894 prev = NULL;
3895 while (node) {
3896 prev = node;
3897 entry = rb_entry(node, struct btrfs_inode, rb_node);
3898
David Sterba37508512018-06-29 10:56:40 +02003899 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003900 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003901 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003902 node = node->rb_right;
3903 else
3904 break;
3905 }
3906 if (!node) {
3907 while (prev) {
3908 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003909 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003910 node = prev;
3911 break;
3912 }
3913 prev = rb_next(prev);
3914 }
3915 }
3916 while (node) {
3917 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003918 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003919 inode = igrab(&entry->vfs_inode);
3920 if (inode) {
3921 spin_unlock(&root->inode_lock);
3922 if (atomic_read(&inode->i_count) > 1)
3923 d_prune_aliases(inode);
3924 /*
3925 * btrfs_drop_inode will have it removed from the inode
3926 * cache when its usage count hits zero.
3927 */
3928 iput(inode);
3929 cond_resched();
3930 spin_lock(&root->inode_lock);
3931 goto again;
3932 }
3933
3934 if (cond_resched_lock(&root->inode_lock))
3935 goto again;
3936
3937 node = rb_next(node);
3938 }
3939 spin_unlock(&root->inode_lock);
3940}
3941
Misono Tomohirof60a2362018-04-18 11:34:52 +09003942int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3943{
3944 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3945 struct btrfs_root *root = BTRFS_I(dir)->root;
3946 struct inode *inode = d_inode(dentry);
3947 struct btrfs_root *dest = BTRFS_I(inode)->root;
3948 struct btrfs_trans_handle *trans;
3949 struct btrfs_block_rsv block_rsv;
3950 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003951 int ret;
3952 int err;
3953
3954 /*
3955 * Don't allow to delete a subvolume with send in progress. This is
3956 * inside the inode lock so the error handling that has to drop the bit
3957 * again is not run concurrently.
3958 */
3959 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003960 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003961 spin_unlock(&dest->root_item_lock);
3962 btrfs_warn(fs_info,
3963 "attempt to delete subvolume %llu during send",
3964 dest->root_key.objectid);
3965 return -EPERM;
3966 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003967 root_flags = btrfs_root_flags(&dest->root_item);
3968 btrfs_set_root_flags(&dest->root_item,
3969 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3970 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003971
3972 down_write(&fs_info->subvol_sem);
3973
3974 err = may_destroy_subvol(dest);
3975 if (err)
3976 goto out_up_write;
3977
3978 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3979 /*
3980 * One for dir inode,
3981 * two for dir entries,
3982 * two for root ref/backref.
3983 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003984 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003985 if (err)
3986 goto out_up_write;
3987
3988 trans = btrfs_start_transaction(root, 0);
3989 if (IS_ERR(trans)) {
3990 err = PTR_ERR(trans);
3991 goto out_release;
3992 }
3993 trans->block_rsv = &block_rsv;
3994 trans->bytes_reserved = block_rsv.size;
3995
3996 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3997
Josef Bacik045d3962019-12-18 17:20:27 -05003998 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003999 if (ret) {
4000 err = ret;
4001 btrfs_abort_transaction(trans, ret);
4002 goto out_end_trans;
4003 }
4004
4005 btrfs_record_root_in_trans(trans, dest);
4006
4007 memset(&dest->root_item.drop_progress, 0,
4008 sizeof(dest->root_item.drop_progress));
4009 dest->root_item.drop_level = 0;
4010 btrfs_set_root_refs(&dest->root_item, 0);
4011
4012 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4013 ret = btrfs_insert_orphan_item(trans,
4014 fs_info->tree_root,
4015 dest->root_key.objectid);
4016 if (ret) {
4017 btrfs_abort_transaction(trans, ret);
4018 err = ret;
4019 goto out_end_trans;
4020 }
4021 }
4022
Lu Fengqid1957792018-05-29 15:01:54 +08004023 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004024 BTRFS_UUID_KEY_SUBVOL,
4025 dest->root_key.objectid);
4026 if (ret && ret != -ENOENT) {
4027 btrfs_abort_transaction(trans, ret);
4028 err = ret;
4029 goto out_end_trans;
4030 }
4031 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004032 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004033 dest->root_item.received_uuid,
4034 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4035 dest->root_key.objectid);
4036 if (ret && ret != -ENOENT) {
4037 btrfs_abort_transaction(trans, ret);
4038 err = ret;
4039 goto out_end_trans;
4040 }
4041 }
4042
Qu Wenruo082b6c92020-06-16 10:17:37 +08004043 free_anon_bdev(dest->anon_dev);
4044 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004045out_end_trans:
4046 trans->block_rsv = NULL;
4047 trans->bytes_reserved = 0;
4048 ret = btrfs_end_transaction(trans);
4049 if (ret && !err)
4050 err = ret;
4051 inode->i_flags |= S_DEAD;
4052out_release:
4053 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4054out_up_write:
4055 up_write(&fs_info->subvol_sem);
4056 if (err) {
4057 spin_lock(&dest->root_item_lock);
4058 root_flags = btrfs_root_flags(&dest->root_item);
4059 btrfs_set_root_flags(&dest->root_item,
4060 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4061 spin_unlock(&dest->root_item_lock);
4062 } else {
4063 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004064 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004065 ASSERT(dest->send_in_progress == 0);
4066
4067 /* the last ref */
4068 if (dest->ino_cache_inode) {
4069 iput(dest->ino_cache_inode);
4070 dest->ino_cache_inode = NULL;
4071 }
4072 }
4073
4074 return err;
4075}
4076
Chris Mason39279cc2007-06-12 06:35:45 -04004077static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4078{
David Howells2b0143b2015-03-17 22:25:59 +00004079 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004080 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004081 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004083 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004084
David Sterbab3ae2442012-09-13 16:04:34 -06004085 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004086 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004087 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004088 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004089
Josef Bacikd52be812013-05-29 14:54:47 -04004090 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004091 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004092 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004093
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004094 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004095 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004096 goto out;
4097 }
4098
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004099 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004100 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004101 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004102
Filipe Manana44f714d2016-06-06 16:11:13 +01004103 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4104
Chris Mason39279cc2007-06-12 06:35:45 -04004105 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004106 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4107 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4108 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004109 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004110 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004111 /*
4112 * Propagate the last_unlink_trans value of the deleted dir to
4113 * its parent directory. This is to prevent an unrecoverable
4114 * log tree in the case we do something like this:
4115 * 1) create dir foo
4116 * 2) create snapshot under dir foo
4117 * 3) delete the snapshot
4118 * 4) rmdir foo
4119 * 5) mkdir foo
4120 * 6) fsync foo or some file inside foo
4121 */
4122 if (last_unlink_trans >= trans->transid)
4123 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4124 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004126 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004127 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004128
Chris Mason39279cc2007-06-12 06:35:45 -04004129 return err;
4130}
4131
Josef Bacikddfae632017-10-19 14:16:02 -04004132/*
4133 * Return this if we need to call truncate_block for the last bit of the
4134 * truncate.
4135 */
4136#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004137
Chris Mason323ac952008-10-01 19:05:46 -04004138/*
Chris Mason39279cc2007-06-12 06:35:45 -04004139 * this can truncate away extent items, csum items and directory items.
4140 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004141 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004142 *
4143 * csum items that cross the new i_size are truncated to the new size
4144 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004145 *
4146 * min_type is the minimum key type to truncate down to. If set to 0, this
4147 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004148 */
Yan, Zheng80825102009-11-12 09:35:36 +00004149int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4150 struct btrfs_root *root,
4151 struct inode *inode,
4152 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004153{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004154 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004156 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004157 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004158 struct btrfs_key key;
4159 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004161 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004163 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004164 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004165 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 int found_extent;
4167 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004168 int pending_del_nr = 0;
4169 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004170 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004171 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004172 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004173 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004174 bool be_nice = false;
4175 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004176 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4177 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004178
4179 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004180
Chris Mason28ed1342014-12-17 09:41:04 -08004181 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004182 * For non-free space inodes and non-shareable roots, we want to back
4183 * off from time to time. This means all inodes in subvolume roots,
4184 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004185 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004186 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004187 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004188 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004189
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004190 path = btrfs_alloc_path();
4191 if (!path)
4192 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004193 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004194
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004195 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004196 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4197 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004198
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004199 /*
4200 * We want to drop from the next block forward in case this
4201 * new size is not block aligned since we will be keeping the
4202 * last block of the extent just the way it is.
4203 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004204 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004205 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004206 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004207 }
Yan, Zheng80825102009-11-12 09:35:36 +00004208
Miao Xie16cdcec2011-04-22 18:12:22 +08004209 /*
4210 * This function is also used to drop the items in the log tree before
4211 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004212 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004213 * items.
4214 */
4215 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004216 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004217
Li Zefan33345d012011-04-20 10:31:50 +08004218 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004219 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004220 key.type = (u8)-1;
4221
Chris Mason85e21ba2008-01-29 15:11:36 -05004222search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004223 /*
4224 * with a 16K leaf size and 128MB extents, you can actually queue
4225 * up a huge file in a single leaf. Most of the time that
4226 * bytes_deleted is > 0, it will be huge by the time we get here
4227 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004228 if (be_nice && bytes_deleted > SZ_32M &&
4229 btrfs_should_end_transaction(trans)) {
4230 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004231 goto out;
4232 }
Chris Masond3977122009-01-05 21:25:51 -05004233
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004234 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4235 if (ret < 0)
4236 goto out;
4237
Chris Mason85e21ba2008-01-29 15:11:36 -05004238 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004239 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004240 /* there are no items in the tree for us to truncate, we're
4241 * done
4242 */
Yan, Zheng80825102009-11-12 09:35:36 +00004243 if (path->slots[0] == 0)
4244 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004245 path->slots[0]--;
4246 }
4247
Chris Masond3977122009-01-05 21:25:51 -05004248 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004249 u64 clear_start = 0, clear_len = 0;
4250
Chris Mason39279cc2007-06-12 06:35:45 -04004251 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004252 leaf = path->nodes[0];
4253 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004254 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004255
Li Zefan33345d012011-04-20 10:31:50 +08004256 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004257 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004258
Chris Mason85e21ba2008-01-29 15:11:36 -05004259 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004260 break;
4261
Chris Mason5f39d392007-10-15 16:14:19 -04004262 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004263 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004264 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004265 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004266 extent_type = btrfs_file_extent_type(leaf, fi);
4267 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004268 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004269 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004270
4271 trace_btrfs_truncate_show_fi_regular(
4272 BTRFS_I(inode), leaf, fi,
4273 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004274 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004275 item_end += btrfs_file_extent_ram_bytes(leaf,
4276 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004277
4278 trace_btrfs_truncate_show_fi_inline(
4279 BTRFS_I(inode), leaf, fi, path->slots[0],
4280 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004281 }
Yan008630c2007-11-07 13:31:09 -05004282 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004283 }
Yan, Zheng80825102009-11-12 09:35:36 +00004284 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004285 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004286 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004287 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004288 break;
4289 if (found_key.offset >= new_size)
4290 del_item = 1;
4291 else
4292 del_item = 0;
4293 }
Chris Mason39279cc2007-06-12 06:35:45 -04004294 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004296 if (found_type != BTRFS_EXTENT_DATA_KEY)
4297 goto delete;
4298
4299 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004300 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004301
4302 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004303 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004304 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004305 u64 orig_num_bytes =
4306 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004307 extent_num_bytes = ALIGN(new_size -
4308 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004309 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004310 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004311 btrfs_set_file_extent_num_bytes(leaf, fi,
4312 extent_num_bytes);
4313 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004314 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004315 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004316 &root->state) &&
4317 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004318 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004319 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004320 } else {
Chris Masondb945352007-10-15 16:15:53 -04004321 extent_num_bytes =
4322 btrfs_file_extent_disk_num_bytes(leaf,
4323 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004324 extent_offset = found_key.offset -
4325 btrfs_file_extent_offset(leaf, fi);
4326
Chris Mason39279cc2007-06-12 06:35:45 -04004327 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004328 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004329 if (extent_start != 0) {
4330 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004331 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004332 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004333 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004334 }
4335 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004336 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004337 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004338 /*
4339 * we can't truncate inline items that have had
4340 * special encodings
4341 */
4342 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004343 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004344 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4345 btrfs_file_extent_compression(leaf, fi) == 0) {
4346 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004347
Josef Bacikddfae632017-10-19 14:16:02 -04004348 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4349 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004350 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004351 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004352 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004353 * We have to bail so the last_size is set to
4354 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004355 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004356 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004357 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004358 } else {
4359 /*
4360 * Inline extents are special, we just treat
4361 * them as a full sector worth in the file
4362 * extent tree just for simplicity sake.
4363 */
4364 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004365 }
Josef Bacikddfae632017-10-19 14:16:02 -04004366
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004367 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004368 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004369 }
Chris Mason179e29e2007-11-01 11:28:41 -04004370delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004371 /*
4372 * We use btrfs_truncate_inode_items() to clean up log trees for
4373 * multiple fsyncs, and in this case we don't want to clear the
4374 * file extent range because it's just the log.
4375 */
4376 if (root == BTRFS_I(inode)->root) {
4377 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4378 clear_start, clear_len);
4379 if (ret) {
4380 btrfs_abort_transaction(trans, ret);
4381 break;
4382 }
4383 }
4384
Josef Bacikddfae632017-10-19 14:16:02 -04004385 if (del_item)
4386 last_size = found_key.offset;
4387 else
4388 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004389 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004390 if (!pending_del_nr) {
4391 /* no pending yet, add ourselves */
4392 pending_del_slot = path->slots[0];
4393 pending_del_nr = 1;
4394 } else if (pending_del_nr &&
4395 path->slots[0] + 1 == pending_del_slot) {
4396 /* hop on the pending chunk */
4397 pending_del_nr++;
4398 pending_del_slot = path->slots[0];
4399 } else {
Chris Masond3977122009-01-05 21:25:51 -05004400 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004401 }
Chris Mason39279cc2007-06-12 06:35:45 -04004402 } else {
4403 break;
4404 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004405 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004406
Miao Xie27cdeb72014-04-02 19:51:05 +08004407 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004408 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004409 struct btrfs_ref ref = { 0 };
4410
Chris Mason28ed1342014-12-17 09:41:04 -08004411 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004412
4413 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4414 extent_start, extent_num_bytes, 0);
4415 ref.real_root = root->root_key.objectid;
4416 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4417 ino, extent_offset);
4418 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004419 if (ret) {
4420 btrfs_abort_transaction(trans, ret);
4421 break;
4422 }
Chris Mason28f75a02015-02-04 06:59:29 -08004423 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004424 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004425 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004426 }
Chris Mason39279cc2007-06-12 06:35:45 -04004427 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004428
Yan, Zheng80825102009-11-12 09:35:36 +00004429 if (found_type == BTRFS_INODE_ITEM_KEY)
4430 break;
4431
4432 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004433 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004434 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004435 if (pending_del_nr) {
4436 ret = btrfs_del_items(trans, root, path,
4437 pending_del_slot,
4438 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004439 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004440 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004441 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004442 }
Yan, Zheng80825102009-11-12 09:35:36 +00004443 pending_del_nr = 0;
4444 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004445 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004446
Chris Mason28f75a02015-02-04 06:59:29 -08004447 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004448 * We can generate a lot of delayed refs, so we need to
4449 * throttle every once and a while and make sure we're
4450 * adding enough space to keep up with the work we are
4451 * generating. Since we hold a transaction here we
4452 * can't flush, and we don't want to FLUSH_LIMIT because
4453 * we could have generated too many delayed refs to
4454 * actually allocate, so just bail if we're short and
4455 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004456 */
Josef Bacik28bad212018-12-03 10:20:38 -05004457 if (should_throttle) {
4458 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4459 BTRFS_RESERVE_NO_FLUSH);
4460 if (ret) {
4461 ret = -EAGAIN;
4462 break;
4463 }
Chris Mason28f75a02015-02-04 06:59:29 -08004464 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004465 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004466 } else {
4467 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004468 }
Chris Mason39279cc2007-06-12 06:35:45 -04004469 }
Yan, Zheng80825102009-11-12 09:35:36 +00004470out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004471 if (ret >= 0 && pending_del_nr) {
4472 int err;
4473
4474 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004475 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004476 if (err) {
4477 btrfs_abort_transaction(trans, err);
4478 ret = err;
4479 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004480 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004481 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4482 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004483 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004484 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004485 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004486 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4487 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004488 }
Chris Mason28ed1342014-12-17 09:41:04 -08004489
Chris Mason39279cc2007-06-12 06:35:45 -04004490 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004491 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004492}
4493
Chris Masona52d9a82007-08-27 16:49:44 -04004494/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304495 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004496 * @inode - inode that we're zeroing
4497 * @from - the offset to start zeroing
4498 * @len - the length to zero, 0 to zero the entire range respective to the
4499 * offset
4500 * @front - zero up to the offset instead of from the offset on
4501 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304502 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004503 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004504 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304505int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004506 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004508 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004509 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004510 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4511 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004512 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004513 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004514 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004515 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004516 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004517 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304518 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004519 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004520 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004521 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004522 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304523 u64 block_start;
4524 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004525
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004526 if (IS_ALIGNED(offset, blocksize) &&
4527 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004528 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304529
Josef Bacik8b62f872017-10-19 14:15:55 -04004530 block_start = round_down(from, blocksize);
4531 block_end = block_start + blocksize - 1;
4532
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004533 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4534 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004535 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004536 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4537 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004538 /* For nocow case, no need to reserve data space */
4539 only_release_metadata = true;
4540 } else {
4541 goto out;
4542 }
4543 }
4544 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4545 if (ret < 0) {
4546 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004547 btrfs_free_reserved_data_space(BTRFS_I(inode),
4548 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004549 goto out;
4550 }
Chris Mason211c17f2008-05-15 09:13:45 -04004551again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004552 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004553 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004554 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004555 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004556 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004557 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004558 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004559 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004560
Chris Masona52d9a82007-08-27 16:49:44 -04004561 if (!PageUptodate(page)) {
4562 ret = btrfs_readpage(NULL, page);
4563 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004564 if (page->mapping != mapping) {
4565 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004566 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004567 goto again;
4568 }
Chris Masona52d9a82007-08-27 16:49:44 -04004569 if (!PageUptodate(page)) {
4570 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004571 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004572 }
4573 }
Chris Mason211c17f2008-05-15 09:13:45 -04004574 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304576 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004577 set_page_extent_mapped(page);
4578
Nikolay Borisovc3504372020-06-03 08:55:03 +03004579 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304581 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004582 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004583 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004584 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004585 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004586 btrfs_put_ordered_extent(ordered);
4587 goto again;
4588 }
4589
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304590 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004591 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4592 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004593
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004594 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004595 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004596 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304597 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004598 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004599 goto out_unlock;
4600 }
4601
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304602 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004603 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304604 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004605 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004606 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304607 memset(kaddr + (block_start - page_offset(page)),
4608 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004609 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304610 memset(kaddr + (block_start - page_offset(page)) + offset,
4611 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004612 flush_dcache_page(page);
4613 kunmap(page);
4614 }
Chris Mason247e7432008-07-17 12:53:51 -04004615 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004616 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004617 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004618
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004619 if (only_release_metadata)
4620 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4621 block_end, EXTENT_NORESERVE, NULL, NULL,
4622 GFP_NOFS);
4623
Chris Mason89642222008-07-24 09:41:53 -04004624out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004625 if (ret) {
4626 if (only_release_metadata)
4627 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4628 blocksize, true);
4629 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004630 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004631 block_start, blocksize, true);
4632 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004633 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004634 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004635 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004636out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004637 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004638 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004639 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004640 return ret;
4641}
4642
Josef Bacik16e75492013-10-22 12:18:51 -04004643static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4644 u64 offset, u64 len)
4645{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004646 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004647 struct btrfs_trans_handle *trans;
4648 int ret;
4649
4650 /*
4651 * Still need to make sure the inode looks like it's been updated so
4652 * that any holes get logged if we fsync.
4653 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4655 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004656 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4657 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4658 return 0;
4659 }
4660
4661 /*
4662 * 1 - for the one we're dropping
4663 * 1 - for the one we're adding
4664 * 1 - for updating the inode.
4665 */
4666 trans = btrfs_start_transaction(root, 3);
4667 if (IS_ERR(trans))
4668 return PTR_ERR(trans);
4669
4670 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004672 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004673 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004674 return ret;
4675 }
4676
David Sterbaf85b7372017-01-20 14:54:07 +01004677 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4678 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004679 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004680 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004681 else
4682 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004683 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004684 return ret;
4685}
4686
Josef Bacik695a0d02011-03-04 15:46:53 -05004687/*
4688 * This function puts in dummy file extents for the area we're creating a hole
4689 * for. So if we are truncating this file to a larger size we need to insert
4690 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4691 * the range between oldsize and size
4692 */
Josef Bacika41ad392011-01-31 15:30:16 -05004693int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004694{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004695 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004696 struct btrfs_root *root = BTRFS_I(inode)->root;
4697 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004698 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004699 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004700 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004701 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4702 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004703 u64 last_byte;
4704 u64 cur_offset;
4705 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004706 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004707
Josef Bacika71754f2013-06-17 17:14:39 -04004708 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304709 * If our size started in the middle of a block we need to zero out the
4710 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004711 * expose stale data.
4712 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304713 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004714 if (err)
4715 return err;
4716
Yan Zheng9036c102008-10-30 14:19:41 -04004717 if (size <= hole_start)
4718 return 0;
4719
David Sterbab272ae22020-02-05 19:09:33 +01004720 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004721 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004722 cur_offset = hole_start;
4723 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004724 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004725 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 if (IS_ERR(em)) {
4727 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004728 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004729 break;
4730 }
Yan Zheng9036c102008-10-30 14:19:41 -04004731 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004732 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004733 hole_size = last_byte - cur_offset;
4734
Yan, Zheng80825102009-11-12 09:35:36 +00004735 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004736 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004737
Josef Bacik16e75492013-10-22 12:18:51 -04004738 err = maybe_insert_hole(root, inode, cur_offset,
4739 hole_size);
4740 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004741 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004742
4743 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4744 cur_offset, hole_size);
4745 if (err)
4746 break;
4747
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004748 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004749 cur_offset + hole_size - 1, 0);
4750 hole_em = alloc_extent_map();
4751 if (!hole_em) {
4752 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4753 &BTRFS_I(inode)->runtime_flags);
4754 goto next;
4755 }
4756 hole_em->start = cur_offset;
4757 hole_em->len = hole_size;
4758 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004759
Josef Bacik5dc562c2012-08-17 13:14:17 -04004760 hole_em->block_start = EXTENT_MAP_HOLE;
4761 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004762 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004763 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004764 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004765 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004766
4767 while (1) {
4768 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004769 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004770 write_unlock(&em_tree->lock);
4771 if (err != -EEXIST)
4772 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004773 btrfs_drop_extent_cache(BTRFS_I(inode),
4774 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004775 cur_offset +
4776 hole_size - 1, 0);
4777 }
4778 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004779 } else {
4780 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4781 cur_offset, hole_size);
4782 if (err)
4783 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004784 }
Josef Bacik16e75492013-10-22 12:18:51 -04004785next:
Yan Zheng9036c102008-10-30 14:19:41 -04004786 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004787 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004788 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004789 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004790 break;
4791 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004792 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004793 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004794 return err;
4795}
4796
Eric Sandeen3972f262013-01-12 02:57:22 +00004797static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004798{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004799 struct btrfs_root *root = BTRFS_I(inode)->root;
4800 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004801 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004802 loff_t newsize = attr->ia_size;
4803 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004804 int ret;
4805
Eric Sandeen3972f262013-01-12 02:57:22 +00004806 /*
4807 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4808 * special case where we need to update the times despite not having
4809 * these flags set. For all other operations the VFS set these flags
4810 * explicitly if it wants a timestamp update.
4811 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004812 if (newsize != oldsize) {
4813 inode_inc_iversion(inode);
4814 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4815 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004816 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004817 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004818
Josef Bacika41ad392011-01-31 15:30:16 -05004819 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004821 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004822 * This is to ensure the snapshot captures a fully consistent
4823 * state of this file - if the snapshot captures this expanding
4824 * truncation, it must capture all writes that happened before
4825 * this truncation.
4826 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004827 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004828 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004829 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004830 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004831 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004832 }
Yan, Zheng80825102009-11-12 09:35:36 +00004833
Miao Xief4a2f4c2011-12-14 20:12:01 -05004834 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004835 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004836 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004837 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004838 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004839
4840 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004841 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304842 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004843 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004844 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004845 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004846 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004847
Josef Bacika41ad392011-01-31 15:30:16 -05004848 /*
4849 * We're truncating a file that used to have good data down to
4850 * zero. Make sure it gets into the ordered flush list so that
4851 * any new writes get down to disk quickly.
4852 */
4853 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004854 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4855 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004856
Josef Bacika41ad392011-01-31 15:30:16 -05004857 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004858
David Sterba8e0fa5d2020-06-09 19:21:48 +02004859 /* Disable nonlocked read DIO to avoid the endless truncate */
4860 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004861 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004862 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004863
Filipe Manana213e8c52018-02-06 20:40:31 +00004864 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004865 if (ret && inode->i_nlink) {
4866 int err;
4867
4868 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004869 * Truncate failed, so fix up the in-memory size. We
4870 * adjusted disk_i_size down as we removed extents, so
4871 * wait for disk_i_size to be stable and then update the
4872 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004873 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004874 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004875 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004876 return err;
4877 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004878 }
Yan, Zheng80825102009-11-12 09:35:36 +00004879 }
4880
Josef Bacika41ad392011-01-31 15:30:16 -05004881 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004882}
4883
Chris Mason39279cc2007-06-12 06:35:45 -04004884static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4885{
David Howells2b0143b2015-03-17 22:25:59 +00004886 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004887 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004888 int err;
4889
Li Zefanb83cc962010-12-20 16:04:08 +08004890 if (btrfs_root_readonly(root))
4891 return -EROFS;
4892
Jan Kara31051c82016-05-26 16:55:18 +02004893 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004894 if (err)
4895 return err;
4896
Chris Mason5a3f23d2009-03-31 13:27:11 -04004897 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004898 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004899 if (err)
4900 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004901 }
Yan Zheng9036c102008-10-30 14:19:41 -04004902
Christoph Hellwig10257742010-06-04 11:30:02 +02004903 if (attr->ia_valid) {
4904 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004905 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004906 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004907
Josef Bacik22c44fe2011-11-30 10:45:38 -05004908 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004909 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004910 }
4911
Chris Mason39279cc2007-06-12 06:35:45 -04004912 return err;
4913}
Chris Mason61295eb2008-01-14 16:24:38 -05004914
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004915/*
4916 * While truncating the inode pages during eviction, we get the VFS calling
4917 * btrfs_invalidatepage() against each page of the inode. This is slow because
4918 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4919 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4920 * extent_state structures over and over, wasting lots of time.
4921 *
4922 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4923 * those expensive operations on a per page basis and do only the ordered io
4924 * finishing, while we release here the extent_map and extent_state structures,
4925 * without the excessive merging and splitting.
4926 */
4927static void evict_inode_truncate_pages(struct inode *inode)
4928{
4929 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4930 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4931 struct rb_node *node;
4932
4933 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004934 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004935
4936 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004937 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004938 struct extent_map *em;
4939
Liu Bo07e1ce02018-08-23 03:51:52 +08004940 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004941 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004942 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4943 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004944 remove_extent_mapping(map_tree, em);
4945 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004946 if (need_resched()) {
4947 write_unlock(&map_tree->lock);
4948 cond_resched();
4949 write_lock(&map_tree->lock);
4950 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004951 }
4952 write_unlock(&map_tree->lock);
4953
Filipe Manana6ca07092015-05-26 00:55:42 +01004954 /*
4955 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004956 * We can have ongoing bios started by readahead that have
4957 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004958 * still in progress (unlocked the pages in the bio but did not yet
4959 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004960 * ranges can still be locked and eviction started because before
4961 * submitting those bios, which are executed by a separate task (work
4962 * queue kthread), inode references (inode->i_count) were not taken
4963 * (which would be dropped in the end io callback of each bio).
4964 * Therefore here we effectively end up waiting for those bios and
4965 * anyone else holding locked ranges without having bumped the inode's
4966 * reference count - if we don't do it, when they access the inode's
4967 * io_tree to unlock a range it may be too late, leading to an
4968 * use-after-free issue.
4969 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004970 spin_lock(&io_tree->lock);
4971 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4972 struct extent_state *state;
4973 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004974 u64 start;
4975 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004976 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004977
4978 node = rb_first(&io_tree->state);
4979 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004980 start = state->start;
4981 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004982 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004983 spin_unlock(&io_tree->lock);
4984
David Sterbaff13db42015-12-03 14:30:40 +01004985 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004986
4987 /*
4988 * If still has DELALLOC flag, the extent didn't reach disk,
4989 * and its reserved space won't be freed by delayed_ref.
4990 * So we need to free its reserved space here.
4991 * (Refer to comment in btrfs_invalidatepage, case 2)
4992 *
4993 * Note, end is the bytenr of last byte, so we need + 1 here.
4994 */
Filipe Manana421f0922018-10-12 13:02:48 +01004995 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004996 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4997 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004998
Filipe Manana6ca07092015-05-26 00:55:42 +01004999 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005000 EXTENT_LOCKED | EXTENT_DELALLOC |
5001 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5002 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005003
Filipe Manana7064dd52014-08-08 02:47:05 +01005004 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005005 spin_lock(&io_tree->lock);
5006 }
5007 spin_unlock(&io_tree->lock);
5008}
5009
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005010static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005011 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005012{
5013 struct btrfs_fs_info *fs_info = root->fs_info;
5014 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005015 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005016 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005017 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005018
Josef Bacikd3984c92019-08-01 18:19:37 -04005019 /*
5020 * Eviction should be taking place at some place safe because of our
5021 * delayed iputs. However the normal flushing code will run delayed
5022 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5023 *
5024 * We reserve the delayed_refs_extra here again because we can't use
5025 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5026 * above. We reserve our extra bit here because we generate a ton of
5027 * delayed refs activity by truncating.
5028 *
5029 * If we cannot make our reservation we'll attempt to steal from the
5030 * global reserve, because we really want to be able to free up space.
5031 */
5032 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5033 BTRFS_RESERVE_FLUSH_EVICT);
5034 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005035 /*
5036 * Try to steal from the global reserve if there is space for
5037 * it.
5038 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005039 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5040 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5041 btrfs_warn(fs_info,
5042 "could not allocate space for delete; will truncate on mount");
5043 return ERR_PTR(-ENOSPC);
5044 }
5045 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005046 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005047
5048 trans = btrfs_join_transaction(root);
5049 if (IS_ERR(trans))
5050 return trans;
5051
5052 if (delayed_refs_extra) {
5053 trans->block_rsv = &fs_info->trans_block_rsv;
5054 trans->bytes_reserved = delayed_refs_extra;
5055 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5056 delayed_refs_extra, 1);
5057 }
5058 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005059}
5060
Al Virobd555972010-06-07 11:35:40 -04005061void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005062{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005064 struct btrfs_trans_handle *trans;
5065 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005066 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005067 int ret;
5068
liubo1abe9b82011-03-24 11:18:59 +00005069 trace_btrfs_inode_evict(inode);
5070
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005071 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005072 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005073 return;
5074 }
5075
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005076 evict_inode_truncate_pages(inode);
5077
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005078 if (inode->i_nlink &&
5079 ((btrfs_root_refs(&root->root_item) != 0 &&
5080 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005081 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005082 goto no_delete;
5083
Omar Sandoval27919062018-05-11 13:13:37 -07005084 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005085 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005086
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005087 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005088
Omar Sandoval7b40b692018-05-11 13:13:33 -07005089 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005090 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005091
Yan, Zheng76dda932009-09-21 16:00:26 -04005092 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005093 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5094 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005095 goto no_delete;
5096 }
5097
Nikolay Borisovaa790212017-01-10 20:35:40 +02005098 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005099 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005100 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005101
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005102 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005103 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005104 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005105 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005106 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005107
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005108 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005109
Yan, Zheng80825102009-11-12 09:35:36 +00005110 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005111 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005112 if (IS_ERR(trans))
5113 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005114
5115 trans->block_rsv = rsv;
5116
Yan, Zhengd68fc572010-05-16 10:49:58 -04005117 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005118 trans->block_rsv = &fs_info->trans_block_rsv;
5119 btrfs_end_transaction(trans);
5120 btrfs_btree_balance_dirty(fs_info);
5121 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5122 goto free_rsv;
5123 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005124 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005125 }
5126
Josef Bacik4ef31a42013-08-13 14:10:08 -04005127 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005128 * Errors here aren't a big deal, it just means we leave orphan items in
5129 * the tree. They will be cleaned up on the next mount. If the inode
5130 * number gets reused, cleanup deletes the orphan item without doing
5131 * anything, and unlink reuses the existing orphan item.
5132 *
5133 * If it turns out that we are dropping too many of these, we might want
5134 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005135 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005136 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005137 if (!IS_ERR(trans)) {
5138 trans->block_rsv = rsv;
5139 btrfs_orphan_del(trans, BTRFS_I(inode));
5140 trans->block_rsv = &fs_info->trans_block_rsv;
5141 btrfs_end_transaction(trans);
5142 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005143
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005144 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005145 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005146 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005147
Omar Sandoval27919062018-05-11 13:13:37 -07005148free_rsv:
5149 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005150no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005151 /*
5152 * If we didn't successfully delete, the orphan item will still be in
5153 * the tree and we'll retry on the next mount. Again, we might also want
5154 * to retry these periodically in the future.
5155 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005156 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005157 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005158}
5159
5160/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005161 * Return the key found in the dir entry in the location pointer, fill @type
5162 * with BTRFS_FT_*, and return 0.
5163 *
Su Yue005d6712018-03-05 17:13:37 +08005164 * If no dir entries were found, returns -ENOENT.
5165 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005166 */
5167static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005168 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005169{
5170 const char *name = dentry->d_name.name;
5171 int namelen = dentry->d_name.len;
5172 struct btrfs_dir_item *di;
5173 struct btrfs_path *path;
5174 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005175 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005176
5177 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005178 if (!path)
5179 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005180
David Sterbaf85b7372017-01-20 14:54:07 +01005181 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5182 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005183 if (IS_ERR_OR_NULL(di)) {
5184 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005185 goto out;
5186 }
Chris Masond3977122009-01-05 21:25:51 -05005187
Chris Mason5f39d392007-10-15 16:14:19 -04005188 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005189 if (location->type != BTRFS_INODE_ITEM_KEY &&
5190 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005191 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005192 btrfs_warn(root->fs_info,
5193"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5194 __func__, name, btrfs_ino(BTRFS_I(dir)),
5195 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005196 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005197 if (!ret)
5198 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005199out:
Chris Mason39279cc2007-06-12 06:35:45 -04005200 btrfs_free_path(path);
5201 return ret;
5202}
5203
5204/*
5205 * when we hit a tree root in a directory, the btrfs part of the inode
5206 * needs to be changed to reflect the root directory of the tree root. This
5207 * is kind of like crossing a mount point.
5208 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005209static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005210 struct inode *dir,
5211 struct dentry *dentry,
5212 struct btrfs_key *location,
5213 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005214{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005215 struct btrfs_path *path;
5216 struct btrfs_root *new_root;
5217 struct btrfs_root_ref *ref;
5218 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005219 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005220 int ret;
5221 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005222
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005223 path = btrfs_alloc_path();
5224 if (!path) {
5225 err = -ENOMEM;
5226 goto out;
5227 }
Chris Mason39279cc2007-06-12 06:35:45 -04005228
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005229 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005230 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5231 key.type = BTRFS_ROOT_REF_KEY;
5232 key.offset = location->objectid;
5233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005234 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005235 if (ret) {
5236 if (ret < 0)
5237 err = ret;
5238 goto out;
5239 }
Chris Mason39279cc2007-06-12 06:35:45 -04005240
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005241 leaf = path->nodes[0];
5242 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005243 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005244 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5245 goto out;
5246
5247 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5248 (unsigned long)(ref + 1),
5249 dentry->d_name.len);
5250 if (ret)
5251 goto out;
5252
David Sterbab3b4aa72011-04-21 01:20:15 +02005253 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005254
David Sterba56e93572020-05-15 19:35:55 +02005255 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005256 if (IS_ERR(new_root)) {
5257 err = PTR_ERR(new_root);
5258 goto out;
5259 }
5260
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005261 *sub_root = new_root;
5262 location->objectid = btrfs_root_dirid(&new_root->root_item);
5263 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005264 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005265 err = 0;
5266out:
5267 btrfs_free_path(path);
5268 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005269}
5270
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005271static void inode_tree_add(struct inode *inode)
5272{
5273 struct btrfs_root *root = BTRFS_I(inode)->root;
5274 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005275 struct rb_node **p;
5276 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005277 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005278 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005279
Al Viro1d3382cb2010-10-23 15:19:20 -04005280 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005281 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005282 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005283 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005284 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005285 while (*p) {
5286 parent = *p;
5287 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5288
David Sterba37508512018-06-29 10:56:40 +02005289 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005290 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005291 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005292 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005293 else {
5294 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005295 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005296 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005297 RB_CLEAR_NODE(parent);
5298 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005299 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005300 }
5301 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005302 rb_link_node(new, parent, p);
5303 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005304 spin_unlock(&root->inode_lock);
5305}
5306
5307static void inode_tree_del(struct inode *inode)
5308{
5309 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005310 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005311
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005312 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005313 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005314 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005315 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005316 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005317 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005318 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005319
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005320 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005321 spin_lock(&root->inode_lock);
5322 empty = RB_EMPTY_ROOT(&root->inode_tree);
5323 spin_unlock(&root->inode_lock);
5324 if (empty)
5325 btrfs_add_dead_root(root);
5326 }
5327}
5328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005329
Chris Masone02119d2008-09-05 16:13:11 -04005330static int btrfs_init_locked_inode(struct inode *inode, void *p)
5331{
5332 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005333
5334 inode->i_ino = args->ino;
5335 BTRFS_I(inode)->location.objectid = args->ino;
5336 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5337 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005338 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5339 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005340 return 0;
5341}
5342
5343static int btrfs_find_actor(struct inode *inode, void *opaque)
5344{
5345 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005346
5347 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005348 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005349}
5350
David Sterba0202e832020-05-15 19:35:59 +02005351static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005352 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005353{
5354 struct inode *inode;
5355 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005356 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005357
David Sterba0202e832020-05-15 19:35:59 +02005358 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005359 args.root = root;
5360
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005361 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005362 btrfs_init_locked_inode,
5363 (void *)&args);
5364 return inode;
5365}
5366
David Sterba4c66e0d2019-10-03 19:09:35 +02005367/*
David Sterba0202e832020-05-15 19:35:59 +02005368 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005369 * Path can be preallocated to prevent recursing back to iget through
5370 * allocator. NULL is also valid but may require an additional allocation
5371 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305372 */
David Sterba0202e832020-05-15 19:35:59 +02005373struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005374 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305375{
5376 struct inode *inode;
5377
David Sterba0202e832020-05-15 19:35:59 +02005378 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305379 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005380 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305381
5382 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005383 int ret;
5384
Filipe Manana4222ea72018-10-24 10:13:03 +01005385 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005386 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005387 inode_tree_add(inode);
5388 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005389 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005390 iget_failed(inode);
5391 /*
5392 * ret > 0 can come from btrfs_search_slot called by
5393 * btrfs_read_locked_inode, this means the inode item
5394 * was not found.
5395 */
5396 if (ret > 0)
5397 ret = -ENOENT;
5398 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005399 }
5400 }
5401
Balaji Rao1a54ef82008-07-21 02:01:04 +05305402 return inode;
5403}
5404
David Sterba0202e832020-05-15 19:35:59 +02005405struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005406{
David Sterba0202e832020-05-15 19:35:59 +02005407 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005408}
5409
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005410static struct inode *new_simple_dir(struct super_block *s,
5411 struct btrfs_key *key,
5412 struct btrfs_root *root)
5413{
5414 struct inode *inode = new_inode(s);
5415
5416 if (!inode)
5417 return ERR_PTR(-ENOMEM);
5418
Josef Bacik5c8fd992020-02-14 16:11:43 -05005419 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005420 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005421 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422
5423 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005424 /*
5425 * We only need lookup, the rest is read-only and there's no inode
5426 * associated with the dentry
5427 */
5428 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005429 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 inode->i_fop = &simple_dir_operations;
5431 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005432 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305433 inode->i_atime = inode->i_mtime;
5434 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005435 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005436
5437 return inode;
5438}
5439
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005440static inline u8 btrfs_inode_type(struct inode *inode)
5441{
5442 /*
5443 * Compile-time asserts that generic FT_* types still match
5444 * BTRFS_FT_* types
5445 */
5446 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5447 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5448 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5449 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5450 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5451 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5452 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5453 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5454
5455 return fs_umode_to_ftype(inode->i_mode);
5456}
5457
Chris Mason3de45862008-11-17 21:02:50 -05005458struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005460 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005461 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005462 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005463 struct btrfs_root *sub_root = root;
5464 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005465 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005466 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005467
5468 if (dentry->d_name.len > BTRFS_NAME_LEN)
5469 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005470
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005471 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005472 if (ret < 0)
5473 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005474
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005475 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005476 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005477 if (IS_ERR(inode))
5478 return inode;
5479
5480 /* Do extra check against inode mode with di_type */
5481 if (btrfs_inode_type(inode) != di_type) {
5482 btrfs_crit(fs_info,
5483"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5484 inode->i_mode, btrfs_inode_type(inode),
5485 di_type);
5486 iput(inode);
5487 return ERR_PTR(-EUCLEAN);
5488 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005490 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005491
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005492 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 &location, &sub_root);
5494 if (ret < 0) {
5495 if (ret != -ENOENT)
5496 inode = ERR_PTR(ret);
5497 else
5498 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5499 } else {
David Sterba0202e832020-05-15 19:35:59 +02005500 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 }
Josef Bacik87270022020-01-24 09:32:31 -05005502 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005503 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005504
Julia Lawall34d19ba2011-01-24 19:55:19 +00005505 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005506 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005507 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005508 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005509 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005510 if (ret) {
5511 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005512 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005513 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005514 }
5515
Chris Mason3de45862008-11-17 21:02:50 -05005516 return inode;
5517}
5518
Nick Pigginfe15ce42011-01-07 17:49:23 +11005519static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005520{
5521 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005522 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005523
Li Zefan848cce02012-02-21 17:04:28 +08005524 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005525 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005526
Li Zefan848cce02012-02-21 17:04:28 +08005527 if (inode) {
5528 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005529 if (btrfs_root_refs(&root->root_item) == 0)
5530 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005531
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005532 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005533 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005534 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005535 return 0;
5536}
5537
Chris Mason3de45862008-11-17 21:02:50 -05005538static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005539 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005540{
Al Viro3837d202018-10-10 16:38:27 -04005541 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005542
Al Viro3837d202018-10-10 16:38:27 -04005543 if (inode == ERR_PTR(-ENOENT))
5544 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005545 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005546}
5547
Josef Bacik23b5ec72017-07-24 15:14:25 -04005548/*
5549 * All this infrastructure exists because dir_emit can fault, and we are holding
5550 * the tree lock when doing readdir. For now just allocate a buffer and copy
5551 * our information into that, and then dir_emit from the buffer. This is
5552 * similar to what NFS does, only we don't keep the buffer around in pagecache
5553 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5554 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5555 * tree lock.
5556 */
5557static int btrfs_opendir(struct inode *inode, struct file *file)
5558{
5559 struct btrfs_file_private *private;
5560
5561 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5562 if (!private)
5563 return -ENOMEM;
5564 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5565 if (!private->filldir_buf) {
5566 kfree(private);
5567 return -ENOMEM;
5568 }
5569 file->private_data = private;
5570 return 0;
5571}
5572
5573struct dir_entry {
5574 u64 ino;
5575 u64 offset;
5576 unsigned type;
5577 int name_len;
5578};
5579
5580static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5581{
5582 while (entries--) {
5583 struct dir_entry *entry = addr;
5584 char *name = (char *)(entry + 1);
5585
David Sterba92d32172018-04-16 21:10:14 +02005586 ctx->pos = get_unaligned(&entry->offset);
5587 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5588 get_unaligned(&entry->ino),
5589 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005590 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005591 addr += sizeof(struct dir_entry) +
5592 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005593 ctx->pos++;
5594 }
5595 return 0;
5596}
5597
Al Viro9cdda8d2013-05-22 16:48:09 -04005598static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005599{
Al Viro9cdda8d2013-05-22 16:48:09 -04005600 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005601 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005602 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005603 struct btrfs_dir_item *di;
5604 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005605 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005606 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005607 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005608 struct list_head ins_list;
5609 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005611 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005612 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005613 char *name_ptr;
5614 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005615 int entries = 0;
5616 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005617 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005618 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005619
Al Viro9cdda8d2013-05-22 16:48:09 -04005620 if (!dir_emit_dots(file, ctx))
5621 return 0;
5622
David Woodhouse49593bf2008-08-17 17:08:36 +01005623 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005624 if (!path)
5625 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005626
Josef Bacik23b5ec72017-07-24 15:14:25 -04005627 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005628 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005629
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005630 INIT_LIST_HEAD(&ins_list);
5631 INIT_LIST_HEAD(&del_list);
5632 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005633
Josef Bacik23b5ec72017-07-24 15:14:25 -04005634again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005635 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005636 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005637 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005638
Chris Mason39279cc2007-06-12 06:35:45 -04005639 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5640 if (ret < 0)
5641 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005642
5643 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005644 struct dir_entry *entry;
5645
Chris Mason5f39d392007-10-15 16:14:19 -04005646 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005647 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005648 if (slot >= btrfs_header_nritems(leaf)) {
5649 ret = btrfs_next_leaf(root, path);
5650 if (ret < 0)
5651 goto err;
5652 else if (ret > 0)
5653 break;
5654 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005655 }
Chris Mason3de45862008-11-17 21:02:50 -05005656
Chris Mason5f39d392007-10-15 16:14:19 -04005657 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5658
5659 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005660 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005661 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005662 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005663 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005664 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005665 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005666 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005667 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005668 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005669 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5670 PAGE_SIZE) {
5671 btrfs_release_path(path);
5672 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5673 if (ret)
5674 goto nopos;
5675 addr = private->filldir_buf;
5676 entries = 0;
5677 total_len = 0;
5678 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005679 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005680
5681 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005682 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005683 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005684 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5685 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005686 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005687 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005688 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005689 put_unaligned(location.objectid, &entry->ino);
5690 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005691 entries++;
5692 addr += sizeof(struct dir_entry) + name_len;
5693 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005694next:
5695 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005696 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005697 btrfs_release_path(path);
5698
5699 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5700 if (ret)
5701 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005702
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005703 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005704 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005705 goto nopos;
5706
Zach Browndb62efb2013-07-11 16:19:42 -07005707 /*
5708 * Stop new entries from being returned after we return the last
5709 * entry.
5710 *
5711 * New directory entries are assigned a strictly increasing
5712 * offset. This means that new entries created during readdir
5713 * are *guaranteed* to be seen in the future by that readdir.
5714 * This has broken buggy programs which operate on names as
5715 * they're returned by readdir. Until we re-use freed offsets
5716 * we have this hack to stop new entries from being returned
5717 * under the assumption that they'll never reach this huge
5718 * offset.
5719 *
5720 * This is being careful not to overflow 32bit loff_t unless the
5721 * last entry requires it because doing so has broken 32bit apps
5722 * in the past.
5723 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005724 if (ctx->pos >= INT_MAX)
5725 ctx->pos = LLONG_MAX;
5726 else
5727 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005728nopos:
5729 ret = 0;
5730err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005731 if (put)
5732 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005733 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005734 return ret;
5735}
5736
Chris Mason39279cc2007-06-12 06:35:45 -04005737/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005738 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005739 * inode changes. But, it is most likely to find the inode in cache.
5740 * FIXME, needs more benchmarking...there are no reasons other than performance
5741 * to keep or drop this code.
5742 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005743static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005744{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005746 struct btrfs_root *root = BTRFS_I(inode)->root;
5747 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005748 int ret;
5749
Josef Bacik72ac3c02012-05-23 14:13:11 -04005750 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005751 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005752
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005753 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005754 if (IS_ERR(trans))
5755 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005756
5757 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005758 if (ret && ret == -ENOSPC) {
5759 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005760 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005761 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005762 if (IS_ERR(trans))
5763 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005764
Chris Mason94b60442010-05-26 11:02:00 -04005765 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005766 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005767 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005768 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005769 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005770
5771 return ret;
5772}
5773
5774/*
5775 * This is a copy of file_update_time. We need this so we can return error on
5776 * ENOSPC for updating the inode in the case of file write and mmap writes.
5777 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005778static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005779 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005780{
Alexander Block2bc5565282012-06-15 09:49:33 +02005781 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005782 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005783
5784 if (btrfs_root_readonly(root))
5785 return -EROFS;
5786
Josef Bacike41f9412012-03-26 09:46:47 -04005787 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005788 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005789 if (flags & S_CTIME)
5790 inode->i_ctime = *now;
5791 if (flags & S_MTIME)
5792 inode->i_mtime = *now;
5793 if (flags & S_ATIME)
5794 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005795 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005796}
5797
Chris Masond352ac62008-09-29 15:18:18 -04005798/*
5799 * find the highest existing sequence number in a directory
5800 * and then set the in-memory index_cnt variable to reflect
5801 * free sequence numbers
5802 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005803static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005804{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005805 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005806 struct btrfs_key key, found_key;
5807 struct btrfs_path *path;
5808 struct extent_buffer *leaf;
5809 int ret;
5810
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005811 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005812 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005813 key.offset = (u64)-1;
5814
5815 path = btrfs_alloc_path();
5816 if (!path)
5817 return -ENOMEM;
5818
5819 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5820 if (ret < 0)
5821 goto out;
5822 /* FIXME: we should be able to handle this */
5823 if (ret == 0)
5824 goto out;
5825 ret = 0;
5826
5827 /*
5828 * MAGIC NUMBER EXPLANATION:
5829 * since we search a directory based on f_pos we have to start at 2
5830 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5831 * else has to start at 2
5832 */
5833 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005834 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005835 goto out;
5836 }
5837
5838 path->slots[0]--;
5839
5840 leaf = path->nodes[0];
5841 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5842
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005843 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005844 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005845 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005846 goto out;
5847 }
5848
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005849 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005850out:
5851 btrfs_free_path(path);
5852 return ret;
5853}
5854
Chris Masond352ac62008-09-29 15:18:18 -04005855/*
5856 * helper to find a free sequence number in a given directory. This current
5857 * code is very simple, later versions will do smarter things in the btree
5858 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005859int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005860{
5861 int ret = 0;
5862
Nikolay Borisov877574e2017-02-20 13:50:33 +02005863 if (dir->index_cnt == (u64)-1) {
5864 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005865 if (ret) {
5866 ret = btrfs_set_inode_index_count(dir);
5867 if (ret)
5868 return ret;
5869 }
Josef Bacikaec74772008-07-24 12:12:38 -04005870 }
5871
Nikolay Borisov877574e2017-02-20 13:50:33 +02005872 *index = dir->index_cnt;
5873 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005874
5875 return ret;
5876}
5877
Chris Masonb0d5d102014-09-08 13:08:51 -07005878static int btrfs_insert_inode_locked(struct inode *inode)
5879{
5880 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005881
5882 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005883 args.root = BTRFS_I(inode)->root;
5884
5885 return insert_inode_locked4(inode,
5886 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5887 btrfs_find_actor, &args);
5888}
5889
Anand Jain19aee8d2017-07-18 17:37:05 +08005890/*
5891 * Inherit flags from the parent inode.
5892 *
5893 * Currently only the compression flags and the cow flags are inherited.
5894 */
5895static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5896{
5897 unsigned int flags;
5898
5899 if (!dir)
5900 return;
5901
5902 flags = BTRFS_I(dir)->flags;
5903
5904 if (flags & BTRFS_INODE_NOCOMPRESS) {
5905 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5906 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5907 } else if (flags & BTRFS_INODE_COMPRESS) {
5908 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5909 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5910 }
5911
5912 if (flags & BTRFS_INODE_NODATACOW) {
5913 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5914 if (S_ISREG(inode->i_mode))
5915 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5916 }
5917
David Sterba7b6a2212018-03-26 18:40:21 +02005918 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005919}
5920
Chris Mason39279cc2007-06-12 06:35:45 -04005921static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5922 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005923 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005924 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005925 u64 ref_objectid, u64 objectid,
5926 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005927{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005928 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005929 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005930 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005931 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005932 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005933 struct btrfs_inode_ref *ref;
5934 struct btrfs_key key[2];
5935 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005936 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005937 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005938 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005939 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005940
Chris Mason5f39d392007-10-15 16:14:19 -04005941 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005942 if (!path)
5943 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005944
Josef Bacik11a19a92019-09-09 10:12:04 -04005945 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005946 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005947 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005948 if (!inode) {
5949 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005950 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005951 }
Chris Mason39279cc2007-06-12 06:35:45 -04005952
Li Zefan581bb052011-04-20 10:06:11 +08005953 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005954 * O_TMPFILE, set link count to 0, so that after this point,
5955 * we fill in an inode item with the correct link count.
5956 */
5957 if (!name)
5958 set_nlink(inode, 0);
5959
5960 /*
Li Zefan581bb052011-04-20 10:06:11 +08005961 * we have to initialize this early, so we can reclaim the inode
5962 * number if we fail afterwards in this function.
5963 */
5964 inode->i_ino = objectid;
5965
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005966 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005967 trace_btrfs_inode_request(dir);
5968
Nikolay Borisov877574e2017-02-20 13:50:33 +02005969 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005970 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005971 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005972 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005973 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005974 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005975 } else if (dir) {
5976 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005977 }
5978 /*
5979 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005980 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005981 * number
5982 */
5983 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005984 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005985 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005986 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005987 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005988
Josef Bacik5dc562c2012-08-17 13:14:17 -04005989 /*
5990 * We could have gotten an inode number from somebody who was fsynced
5991 * and then removed in this same transaction, so let's just set full
5992 * sync since it will be a full sync anyway and this will blow away the
5993 * old info in the log.
5994 */
5995 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5996
Chris Mason9c583092008-01-29 15:15:18 -05005997 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005998 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005999 key[0].offset = 0;
6000
Chris Mason9c583092008-01-29 15:15:18 -05006001 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006002
6003 if (name) {
6004 /*
6005 * Start new inodes with an inode_ref. This is slightly more
6006 * efficient for small numbers of hard links since they will
6007 * be packed into one item. Extended refs will kick in if we
6008 * add more hard links than can fit in the ref item.
6009 */
6010 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006011 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006012 key[1].offset = ref_objectid;
6013
6014 sizes[1] = name_len + sizeof(*ref);
6015 }
Chris Mason9c583092008-01-29 15:15:18 -05006016
Chris Masonb0d5d102014-09-08 13:08:51 -07006017 location = &BTRFS_I(inode)->location;
6018 location->objectid = objectid;
6019 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006020 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006021
6022 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006023 if (ret < 0) {
6024 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006025 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006026 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006027
Chris Masonb9473432009-03-13 11:00:37 -04006028 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006029 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006030 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006031 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006032
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006033 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006034 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306035
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006036 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306037 inode->i_atime = inode->i_mtime;
6038 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006039 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306040
Chris Mason5f39d392007-10-15 16:14:19 -04006041 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6042 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006043 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006044 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006045 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006046
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006047 if (name) {
6048 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6049 struct btrfs_inode_ref);
6050 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6051 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6052 ptr = (unsigned long)(ref + 1);
6053 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6054 }
Chris Mason9c583092008-01-29 15:15:18 -05006055
Chris Mason5f39d392007-10-15 16:14:19 -04006056 btrfs_mark_buffer_dirty(path->nodes[0]);
6057 btrfs_free_path(path);
6058
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006059 btrfs_inherit_iflags(inode, dir);
6060
Al Viro569254b2011-07-24 17:08:40 -04006061 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006062 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006063 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006064 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006065 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6066 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006067 }
6068
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006069 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006070
6071 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006072 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006073
Alexander Block8ea05e32012-07-25 17:35:53 +02006074 btrfs_update_root_times(trans, root);
6075
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006076 ret = btrfs_inode_inherit_props(trans, inode, dir);
6077 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006078 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006079 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006080 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006081
Chris Mason39279cc2007-06-12 06:35:45 -04006082 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006083
6084fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006085 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006086fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006087 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006088 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006089 btrfs_free_path(path);
6090 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006091}
6092
Chris Masond352ac62008-09-29 15:18:18 -04006093/*
6094 * utility function to add 'inode' into 'parent_inode' with
6095 * a give name and a given sequence number.
6096 * if 'add_backref' is true, also insert a backref from the
6097 * inode to the parent directory.
6098 */
Chris Masone02119d2008-09-05 16:13:11 -04006099int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006100 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006101 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006102{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006103 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006104 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006105 struct btrfs_root *root = parent_inode->root;
6106 u64 ino = btrfs_ino(inode);
6107 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006108
Li Zefan33345d012011-04-20 10:31:50 +08006109 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006110 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006111 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006112 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006113 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006114 key.offset = 0;
6115 }
Chris Mason39279cc2007-06-12 06:35:45 -04006116
Li Zefan33345d012011-04-20 10:31:50 +08006117 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006118 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006119 root->root_key.objectid, parent_ino,
6120 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006121 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006122 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6123 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006124 }
6125
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006126 /* Nothing to clean up yet */
6127 if (ret)
6128 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006129
Lu Fengqi684572d2018-08-04 21:10:57 +08006130 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006131 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006132 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006133 goto fail_dir_item;
6134 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006135 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006136 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006137 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006138
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006139 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006140 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006141 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006142 /*
6143 * If we are replaying a log tree, we do not want to update the mtime
6144 * and ctime of the parent directory with the current time, since the
6145 * log replay procedure is responsible for setting them to their correct
6146 * values (the ones it had when the fsync was done).
6147 */
6148 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6149 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6150
6151 parent_inode->vfs_inode.i_mtime = now;
6152 parent_inode->vfs_inode.i_ctime = now;
6153 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006154 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006155 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006156 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006157 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006158
6159fail_dir_item:
6160 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6161 u64 local_index;
6162 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006163 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006164 root->root_key.objectid, parent_ino,
6165 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006166 if (err)
6167 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006168 } else if (add_backref) {
6169 u64 local_index;
6170 int err;
6171
6172 err = btrfs_del_inode_ref(trans, root, name, name_len,
6173 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006174 if (err)
6175 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006176 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006177
6178 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006179 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006180}
6181
6182static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006183 struct btrfs_inode *dir, struct dentry *dentry,
6184 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006185{
Josef Bacika1b075d2010-11-19 20:36:11 +00006186 int err = btrfs_add_link(trans, dir, inode,
6187 dentry->d_name.name, dentry->d_name.len,
6188 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006189 if (err > 0)
6190 err = -EEXIST;
6191 return err;
6192}
6193
Josef Bacik618e21d2007-07-11 10:18:17 -04006194static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006195 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006196{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006197 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006198 struct btrfs_trans_handle *trans;
6199 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006200 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006201 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006202 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006203 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006204
Josef Bacik9ed74f22009-09-11 16:12:44 -04006205 /*
6206 * 2 for inode item and ref
6207 * 2 for dir items
6208 * 1 for xattr if selinux is on
6209 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006210 trans = btrfs_start_transaction(root, 5);
6211 if (IS_ERR(trans))
6212 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006213
Li Zefan581bb052011-04-20 10:06:11 +08006214 err = btrfs_find_free_ino(root, &objectid);
6215 if (err)
6216 goto out_unlock;
6217
Josef Bacikaec74772008-07-24 12:12:38 -04006218 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006219 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6220 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006221 if (IS_ERR(inode)) {
6222 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006223 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006224 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006225 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006226
Casey Schauflerad19db72011-12-15 10:09:07 -05006227 /*
6228 * If the active LSM wants to access the inode during
6229 * d_instantiate it needs these. Smack checks to see
6230 * if the filesystem supports xattrs by looking at the
6231 * ops vector.
6232 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006233 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006234 init_special_inode(inode, inode->i_mode, rdev);
6235
6236 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006237 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006238 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006239
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006240 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6241 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006242 if (err)
6243 goto out_unlock;
6244
6245 btrfs_update_inode(trans, root, inode);
6246 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006247
Josef Bacik618e21d2007-07-11 10:18:17 -04006248out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006249 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006250 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006251 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006252 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006253 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006254 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006255 return err;
6256}
6257
Chris Mason39279cc2007-06-12 06:35:45 -04006258static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006259 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006260{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006261 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006262 struct btrfs_trans_handle *trans;
6263 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006264 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006265 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006266 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006267 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006268
Josef Bacik9ed74f22009-09-11 16:12:44 -04006269 /*
6270 * 2 for inode item and ref
6271 * 2 for dir items
6272 * 1 for xattr if selinux is on
6273 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006274 trans = btrfs_start_transaction(root, 5);
6275 if (IS_ERR(trans))
6276 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006277
Li Zefan581bb052011-04-20 10:06:11 +08006278 err = btrfs_find_free_ino(root, &objectid);
6279 if (err)
6280 goto out_unlock;
6281
Josef Bacikaec74772008-07-24 12:12:38 -04006282 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006283 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6284 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006285 if (IS_ERR(inode)) {
6286 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006287 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006289 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006290 /*
6291 * If the active LSM wants to access the inode during
6292 * d_instantiate it needs these. Smack checks to see
6293 * if the filesystem supports xattrs by looking at the
6294 * ops vector.
6295 */
6296 inode->i_fop = &btrfs_file_operations;
6297 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006298 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006299
6300 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6301 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006302 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006303
6304 err = btrfs_update_inode(trans, root, inode);
6305 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006306 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006307
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006308 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6309 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006310 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006311 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006312
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006313 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006314 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006315
Chris Mason39279cc2007-06-12 06:35:45 -04006316out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006317 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006318 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006319 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006320 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006321 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006322 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006323 return err;
6324}
6325
6326static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6327 struct dentry *dentry)
6328{
Filipe Manana271dba452016-01-05 16:24:05 +00006329 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006330 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006331 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006333 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006334 int err;
6335 int drop_inode = 0;
6336
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006337 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006338 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006339 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006340
Mark Fashehf1863732012-08-08 11:32:27 -07006341 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006342 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006343
Nikolay Borisov877574e2017-02-20 13:50:33 +02006344 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006345 if (err)
6346 goto fail;
6347
Yan, Zhenga22285a2010-05-16 10:48:46 -04006348 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006349 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006350 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006351 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006352 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006353 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006354 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006355 if (IS_ERR(trans)) {
6356 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006357 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006358 goto fail;
6359 }
Chris Mason5f39d392007-10-15 16:14:19 -04006360
Miao Xie67de1172013-12-26 13:07:06 +08006361 /* There are several dir indexes for this inode, clear the cache. */
6362 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006363 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006364 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006365 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006366 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006367 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006368
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006369 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6370 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006371
Yan, Zhenga5719522009-09-24 09:17:31 -04006372 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006373 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006374 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006375 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006376 int ret;
6377
Yan, Zhenga5719522009-09-24 09:17:31 -04006378 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006379 if (err)
6380 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006381 if (inode->i_nlink == 1) {
6382 /*
6383 * If new hard link count is 1, it's a file created
6384 * with open(2) O_TMPFILE flag.
6385 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006386 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006387 if (err)
6388 goto fail;
6389 }
Al Viro08c422c2011-12-23 07:58:13 -05006390 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006391 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6392 true, NULL);
6393 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6394 err = btrfs_commit_transaction(trans);
6395 trans = NULL;
6396 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006397 }
Chris Mason39279cc2007-06-12 06:35:45 -04006398
Chris Mason1832a6d2007-12-21 16:27:21 -05006399fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006400 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006401 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006402 if (drop_inode) {
6403 inode_dec_link_count(inode);
6404 iput(inode);
6405 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006406 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006407 return err;
6408}
6409
Al Viro18bb1db2011-07-26 01:41:39 -04006410static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006411{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006412 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006413 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006414 struct btrfs_trans_handle *trans;
6415 struct btrfs_root *root = BTRFS_I(dir)->root;
6416 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006417 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006418 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Josef Bacik9ed74f22009-09-11 16:12:44 -04006420 /*
6421 * 2 items for inode and ref
6422 * 2 items for dir items
6423 * 1 for xattr if selinux is on
6424 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006425 trans = btrfs_start_transaction(root, 5);
6426 if (IS_ERR(trans))
6427 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006428
Li Zefan581bb052011-04-20 10:06:11 +08006429 err = btrfs_find_free_ino(root, &objectid);
6430 if (err)
6431 goto out_fail;
6432
Josef Bacikaec74772008-07-24 12:12:38 -04006433 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006434 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6435 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006436 if (IS_ERR(inode)) {
6437 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006438 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006439 goto out_fail;
6440 }
Chris Mason5f39d392007-10-15 16:14:19 -04006441
Chris Masonb0d5d102014-09-08 13:08:51 -07006442 /* these must be set before we unlock the inode */
6443 inode->i_op = &btrfs_dir_inode_operations;
6444 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006445
Eric Paris2a7dba32011-02-01 11:05:39 -05006446 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006447 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006448 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006449
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006450 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 err = btrfs_update_inode(trans, root, inode);
6452 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006453 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006454
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006455 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6456 dentry->d_name.name,
6457 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006458 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006459 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006460
Al Viro1e2e5472018-05-04 08:23:01 -04006461 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006462
6463out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006464 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006465 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006466 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006467 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006468 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006469 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006470 return err;
6471}
6472
Chris Masonc8b97812008-10-29 14:49:59 -04006473static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006474 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006475 size_t pg_offset, u64 extent_offset,
6476 struct btrfs_file_extent_item *item)
6477{
6478 int ret;
6479 struct extent_buffer *leaf = path->nodes[0];
6480 char *tmp;
6481 size_t max_size;
6482 unsigned long inline_size;
6483 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006484 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006485
6486 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006487 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006488 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6489 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006490 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006491 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006492 if (!tmp)
6493 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006494 ptr = btrfs_file_extent_inline_start(item);
6495
6496 read_extent_buffer(leaf, tmp, ptr, inline_size);
6497
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006498 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006499 ret = btrfs_decompress(compress_type, tmp, page,
6500 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006501
6502 /*
6503 * decompression code contains a memset to fill in any space between the end
6504 * of the uncompressed data and the end of max_size in case the decompressed
6505 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6506 * the end of an inline extent and the beginning of the next block, so we
6507 * cover that region here.
6508 */
6509
6510 if (max_size + pg_offset < PAGE_SIZE) {
6511 char *map = kmap(page);
6512 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6513 kunmap(page);
6514 }
Chris Masonc8b97812008-10-29 14:49:59 -04006515 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006516 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006517}
6518
Omar Sandoval39b07b52019-12-02 17:34:23 -08006519/**
6520 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6521 * @inode: file to search in
6522 * @page: page to read extent data into if the extent is inline
6523 * @pg_offset: offset into @page to copy to
6524 * @start: file offset
6525 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006526 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006527 * This returns the first &struct extent_map which overlaps with the given
6528 * range, reading it from the B-tree and caching it if necessary. Note that
6529 * there may be more extents which overlap the given range after the returned
6530 * extent_map.
6531 *
6532 * If @page is not NULL and the extent is inline, this also reads the extent
6533 * data directly into the page and marks the extent up to date in the io_tree.
6534 *
6535 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006536 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006537struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006538 struct page *page, size_t pg_offset,
6539 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006540{
David Sterba3ffbd682018-06-29 10:56:42 +02006541 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006542 int ret;
6543 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006544 u64 extent_start = 0;
6545 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006546 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006547 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006548 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006549 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006550 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006551 struct extent_buffer *leaf;
6552 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006553 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006554 struct extent_map_tree *em_tree = &inode->extent_tree;
6555 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006556
Chris Mason890871b2009-09-02 16:24:52 -04006557 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006558 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006559 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006560
Chris Masona52d9a82007-08-27 16:49:44 -04006561 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006562 if (em->start > start || em->start + em->len <= start)
6563 free_extent_map(em);
6564 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006565 free_extent_map(em);
6566 else
6567 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006568 }
David Sterba172ddd62011-04-21 00:48:27 +02006569 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006570 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006571 err = -ENOMEM;
6572 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006573 }
Chris Masond1310b22008-01-24 16:13:08 -05006574 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006575 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006576 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006577 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006578
Liu Bobee6ec82018-08-17 05:05:28 +08006579 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006580 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006581 err = -ENOMEM;
6582 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006583 }
6584
Liu Bobee6ec82018-08-17 05:05:28 +08006585 /* Chances are we'll be called again, so go ahead and do readahead */
6586 path->reada = READA_FORWARD;
6587
Liu Boe49aabd2018-08-25 13:47:09 +08006588 /*
6589 * Unless we're going to uncompress the inline extent, no sleep would
6590 * happen.
6591 */
6592 path->leave_spinning = 1;
6593
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006594 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006595 if (ret < 0) {
6596 err = ret;
6597 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006598 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006599 if (path->slots[0] == 0)
6600 goto not_found;
6601 path->slots[0]--;
6602 }
6603
Chris Mason5f39d392007-10-15 16:14:19 -04006604 leaf = path->nodes[0];
6605 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006606 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006607 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006608 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006609 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006610 /*
6611 * If we backup past the first extent we want to move forward
6612 * and see if there is an extent in front of us, otherwise we'll
6613 * say there is a hole for our whole search range which can
6614 * cause problems.
6615 */
6616 extent_end = start;
6617 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006618 }
6619
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006620 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006621 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006622 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006623 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6624 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006625 /* Only regular file could have regular/prealloc extent */
6626 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Pavel Machek881a3a12020-08-03 11:35:06 +02006627 err = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006628 btrfs_crit(fs_info,
6629 "regular/prealloc extent found for non-regular inode %llu",
6630 btrfs_ino(inode));
6631 goto out;
6632 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006633 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6634 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006635 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006636 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6637 path->slots[0],
6638 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006639 }
Josef Bacik25a50342013-10-14 12:08:38 -04006640next:
Yan Zheng9036c102008-10-30 14:19:41 -04006641 if (start >= extent_end) {
6642 path->slots[0]++;
6643 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6644 ret = btrfs_next_leaf(root, path);
6645 if (ret < 0) {
6646 err = ret;
6647 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006648 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006649 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006650 }
Yan Zheng9036c102008-10-30 14:19:41 -04006651 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006652 }
Yan Zheng9036c102008-10-30 14:19:41 -04006653 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6654 if (found_key.objectid != objectid ||
6655 found_key.type != BTRFS_EXTENT_DATA_KEY)
6656 goto not_found;
6657 if (start + len <= found_key.offset)
6658 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006659 if (start > found_key.offset)
6660 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006661
6662 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006663 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006664 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006665 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006666 em->block_start = EXTENT_MAP_HOLE;
6667 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006668 }
6669
Omar Sandoval39b07b52019-12-02 17:34:23 -08006670 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006671
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006672 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6673 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006674 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006675 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006676 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006677 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006678 size_t size;
6679 size_t extent_offset;
6680 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006681
Omar Sandoval39b07b52019-12-02 17:34:23 -08006682 if (!page)
Yan689f9342007-10-29 11:41:07 -04006683 goto out;
Yan689f9342007-10-29 11:41:07 -04006684
Qu Wenruoe41ca582018-06-06 15:41:49 +08006685 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006686 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006687 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6688 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006689 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006690 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006691 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006692 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006693 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006694
6695 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006696 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006697 if (btrfs_file_extent_compression(leaf, item) !=
6698 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006699 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006700 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006701 if (ret) {
6702 err = ret;
6703 goto out;
6704 }
Chris Masonc8b97812008-10-29 14:49:59 -04006705 } else {
6706 map = kmap(page);
6707 read_extent_buffer(leaf, map + pg_offset, ptr,
6708 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006709 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006710 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006711 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006712 copy_size);
6713 }
Chris Masonc8b97812008-10-29 14:49:59 -04006714 kunmap(page);
6715 }
Chris Mason179e29e2007-11-01 11:28:41 -04006716 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006717 }
Chris Masond1310b22008-01-24 16:13:08 -05006718 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006719 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006720 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006721 }
6722not_found:
6723 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006724 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006725 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006726 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006727insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006728 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006729 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006730 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006731 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6732 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006733 err = -EIO;
6734 goto out;
6735 }
Chris Masond1310b22008-01-24 16:13:08 -05006736
6737 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006738 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006739 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006740 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006741out:
Liu Boc6414282018-08-23 07:36:17 +08006742 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006743
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006744 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006745
Chris Masona52d9a82007-08-27 16:49:44 -04006746 if (err) {
6747 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006748 return ERR_PTR(err);
6749 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006750 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006751 return em;
6752}
6753
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006754struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006755 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006756{
6757 struct extent_map *em;
6758 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006759 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006760 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006761 u64 delalloc_len;
6762 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006763 int err = 0;
6764
Omar Sandoval39b07b52019-12-02 17:34:23 -08006765 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006766 if (IS_ERR(em))
6767 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006768 /*
6769 * If our em maps to:
6770 * - a hole or
6771 * - a pre-alloc extent,
6772 * there might actually be delalloc bytes behind it.
6773 */
6774 if (em->block_start != EXTENT_MAP_HOLE &&
6775 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6776 return em;
6777 else
6778 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006779
6780 /* check to see if we've wrapped (len == -1 or similar) */
6781 end = start + len;
6782 if (end < start)
6783 end = (u64)-1;
6784 else
6785 end -= 1;
6786
6787 em = NULL;
6788
6789 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006790 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006791 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006792 delalloc_end = delalloc_start + delalloc_len;
6793 if (delalloc_end < delalloc_start)
6794 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006795
6796 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006797 * We didn't find anything useful, return the original results from
6798 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006799 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006800 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006801 em = hole_em;
6802 hole_em = NULL;
6803 goto out;
6804 }
6805
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006806 /*
6807 * Adjust the delalloc_start to make sure it doesn't go backwards from
6808 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006809 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006810 delalloc_start = max(start, delalloc_start);
6811 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006812
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006813 if (delalloc_len > 0) {
6814 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006815 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006816 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006817
David Sterba172ddd62011-04-21 00:48:27 +02006818 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006819 if (!em) {
6820 err = -ENOMEM;
6821 goto out;
6822 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006823
6824 ASSERT(hole_em);
6825 /*
6826 * When btrfs_get_extent can't find anything it returns one
6827 * huge hole
6828 *
6829 * Make sure what it found really fits our range, and adjust to
6830 * make sure it is based on the start from the caller
6831 */
6832 if (hole_end <= start || hole_em->start > end) {
6833 free_extent_map(hole_em);
6834 hole_em = NULL;
6835 } else {
6836 hole_start = max(hole_em->start, start);
6837 hole_len = hole_end - hole_start;
6838 }
6839
6840 if (hole_em && delalloc_start > hole_start) {
6841 /*
6842 * Our hole starts before our delalloc, so we have to
6843 * return just the parts of the hole that go until the
6844 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006845 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006846 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006847 em->start = hole_start;
6848 em->orig_start = hole_start;
6849 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006850 * Don't adjust block start at all, it is fixed at
6851 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006852 */
6853 em->block_start = hole_em->block_start;
6854 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006855 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6856 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006857 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006858 /*
6859 * Hole is out of passed range or it starts after
6860 * delalloc range
6861 */
6862 em->start = delalloc_start;
6863 em->len = delalloc_len;
6864 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006865 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006866 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006867 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006868 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006869 return hole_em;
6870 }
6871out:
6872
6873 free_extent_map(hole_em);
6874 if (err) {
6875 free_extent_map(em);
6876 return ERR_PTR(err);
6877 }
6878 return em;
6879}
6880
Nikolay Borisov64f54182020-06-03 08:55:31 +03006881static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006882 const u64 start,
6883 const u64 len,
6884 const u64 orig_start,
6885 const u64 block_start,
6886 const u64 block_len,
6887 const u64 orig_block_len,
6888 const u64 ram_bytes,
6889 const int type)
6890{
6891 struct extent_map *em = NULL;
6892 int ret;
6893
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006894 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006895 em = create_io_em(inode, start, len, orig_start, block_start,
6896 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006897 BTRFS_COMPRESS_NONE, /* compress_type */
6898 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006899 if (IS_ERR(em))
6900 goto out;
6901 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006902 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6903 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006904 if (ret) {
6905 if (em) {
6906 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006907 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006908 }
6909 em = ERR_PTR(ret);
6910 }
6911 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006912
6913 return em;
6914}
6915
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006916static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006917 u64 start, u64 len)
6918{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006919 struct btrfs_root *root = inode->root;
6920 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006921 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922 struct btrfs_key ins;
6923 u64 alloc_hint;
6924 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006925
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006926 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006927 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006928 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006929 if (ret)
6930 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006931
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006932 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006933 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006934 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006935 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006936 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006937 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6938 1);
Josef Bacik00361582013-08-14 14:02:47 -04006939
Josef Bacik4b46fce2010-05-23 11:00:55 -04006940 return em;
6941}
6942
Chris Mason46bfbb52010-05-26 11:04:10 -04006943/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006944 * Check if we can do nocow write into the range [@offset, @offset + @len)
6945 *
6946 * @offset: File offset
6947 * @len: The length to write, will be updated to the nocow writeable
6948 * range
6949 * @orig_start: (optional) Return the original file offset of the file extent
6950 * @orig_len: (optional) Return the original on-disk length of the file extent
6951 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07006952 * @strict: if true, omit optimizations that might force us into unnecessary
6953 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006954 *
6955 * This function will flush ordered extents in the range to ensure proper
6956 * nocow checks for (nowait == false) case.
6957 *
6958 * Return:
6959 * >0 and update @len if we can do nocow write
6960 * 0 if we can't do nocow write
6961 * <0 if error happened
6962 *
6963 * NOTE: This only checks the file extents, caller is responsible to wait for
6964 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006965 */
Josef Bacik00361582013-08-14 14:02:47 -04006966noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006967 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07006968 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04006969{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006971 struct btrfs_path *path;
6972 int ret;
6973 struct extent_buffer *leaf;
6974 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006975 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006976 struct btrfs_file_extent_item *fi;
6977 struct btrfs_key key;
6978 u64 disk_bytenr;
6979 u64 backref_offset;
6980 u64 extent_end;
6981 u64 num_bytes;
6982 int slot;
6983 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006984 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006985
Chris Mason46bfbb52010-05-26 11:04:10 -04006986 path = btrfs_alloc_path();
6987 if (!path)
6988 return -ENOMEM;
6989
David Sterbaf85b7372017-01-20 14:54:07 +01006990 ret = btrfs_lookup_file_extent(NULL, root, path,
6991 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006992 if (ret < 0)
6993 goto out;
6994
6995 slot = path->slots[0];
6996 if (ret == 1) {
6997 if (slot == 0) {
6998 /* can't find the item, must cow */
6999 ret = 0;
7000 goto out;
7001 }
7002 slot--;
7003 }
7004 ret = 0;
7005 leaf = path->nodes[0];
7006 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007007 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007008 key.type != BTRFS_EXTENT_DATA_KEY) {
7009 /* not our file or wrong item type, must cow */
7010 goto out;
7011 }
7012
7013 if (key.offset > offset) {
7014 /* Wrong offset, must cow */
7015 goto out;
7016 }
7017
7018 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7019 found_type = btrfs_file_extent_type(leaf, fi);
7020 if (found_type != BTRFS_FILE_EXTENT_REG &&
7021 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7022 /* not a regular extent, must cow */
7023 goto out;
7024 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007025
7026 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7027 goto out;
7028
Miao Xiee77751a2013-12-27 21:11:50 +08007029 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7030 if (extent_end <= offset)
7031 goto out;
7032
Chris Mason46bfbb52010-05-26 11:04:10 -04007033 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007034 if (disk_bytenr == 0)
7035 goto out;
7036
7037 if (btrfs_file_extent_compression(leaf, fi) ||
7038 btrfs_file_extent_encryption(leaf, fi) ||
7039 btrfs_file_extent_other_encoding(leaf, fi))
7040 goto out;
7041
Ethan Lien78d42952018-05-17 14:58:29 +08007042 /*
7043 * Do the same check as in btrfs_cross_ref_exist but without the
7044 * unnecessary search.
7045 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007046 if (!strict &&
7047 (btrfs_file_extent_generation(leaf, fi) <=
7048 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007049 goto out;
7050
Chris Mason46bfbb52010-05-26 11:04:10 -04007051 backref_offset = btrfs_file_extent_offset(leaf, fi);
7052
Josef Bacik7ee9e442013-06-21 16:37:03 -04007053 if (orig_start) {
7054 *orig_start = key.offset - backref_offset;
7055 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7056 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7057 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007058
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007059 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007060 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007061
7062 num_bytes = min(offset + *len, extent_end) - offset;
7063 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7064 u64 range_end;
7065
Jeff Mahoneyda170662016-06-15 09:22:56 -04007066 range_end = round_up(offset + num_bytes,
7067 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007068 ret = test_range_bit(io_tree, offset, range_end,
7069 EXTENT_DELALLOC, 0, NULL);
7070 if (ret) {
7071 ret = -EAGAIN;
7072 goto out;
7073 }
7074 }
7075
Josef Bacik1bda19e2013-10-18 12:10:36 -04007076 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007077
7078 /*
7079 * look for other files referencing this extent, if we
7080 * find any we must cow
7081 */
Josef Bacik00361582013-08-14 14:02:47 -04007082
Liu Boe4c3b2d2017-01-30 12:25:28 -08007083 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007084 key.offset - backref_offset, disk_bytenr,
7085 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007086 if (ret) {
7087 ret = 0;
7088 goto out;
7089 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007090
7091 /*
7092 * adjust disk_bytenr and num_bytes to cover just the bytes
7093 * in this extent we are about to write. If there
7094 * are any csums in that range we have to cow in order
7095 * to keep the csums correct
7096 */
7097 disk_bytenr += backref_offset;
7098 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007099 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7100 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007101 /*
7102 * all of the above have passed, it is safe to overwrite this extent
7103 * without cow
7104 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007105 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007106 ret = 1;
7107out:
7108 btrfs_free_path(path);
7109 return ret;
7110}
7111
Josef Bacikeb838e72012-07-31 16:28:48 -04007112static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007113 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007114{
7115 struct btrfs_ordered_extent *ordered;
7116 int ret = 0;
7117
7118 while (1) {
7119 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007120 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007121 /*
7122 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007123 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007124 * extents in this range.
7125 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007126 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007127 lockend - lockstart + 1);
7128
7129 /*
7130 * We need to make sure there are no buffered pages in this
7131 * range either, we could have raced between the invalidate in
7132 * generic_file_direct_write and locking the extent. The
7133 * invalidate needs to happen so that reads after a write do not
7134 * get stale data.
7135 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007136 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007137 (!writing || !filemap_range_has_page(inode->i_mapping,
7138 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007139 break;
7140
7141 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007142 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007143
7144 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007145 /*
7146 * If we are doing a DIO read and the ordered extent we
7147 * found is for a buffered write, we can not wait for it
7148 * to complete and retry, because if we do so we can
7149 * deadlock with concurrent buffered writes on page
7150 * locks. This happens only if our DIO read covers more
7151 * than one extent map, if at this point has already
7152 * created an ordered extent for a previous extent map
7153 * and locked its range in the inode's io tree, and a
7154 * concurrent write against that previous extent map's
7155 * range and this range started (we unlock the ranges
7156 * in the io tree only when the bios complete and
7157 * buffered writes always lock pages before attempting
7158 * to lock range in the io tree).
7159 */
7160 if (writing ||
7161 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7162 btrfs_start_ordered_extent(inode, ordered, 1);
7163 else
7164 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007165 btrfs_put_ordered_extent(ordered);
7166 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007167 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007168 * We could trigger writeback for this range (and wait
7169 * for it to complete) and then invalidate the pages for
7170 * this range (through invalidate_inode_pages2_range()),
7171 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007172 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007173 * triggered a readahead) on a page lock due to an
7174 * ordered dio extent we created before but did not have
7175 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007176 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007177 * ordered extent to complete while holding a lock on
7178 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007179 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007180 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007181 }
7182
Filipe Mananaade77022016-02-18 14:28:55 +00007183 if (ret)
7184 break;
7185
Josef Bacikeb838e72012-07-31 16:28:48 -04007186 cond_resched();
7187 }
7188
7189 return ret;
7190}
7191
Liu Bo6f9994d2017-01-31 07:50:22 -08007192/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007193static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7194 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007195 u64 block_len, u64 orig_block_len,
7196 u64 ram_bytes, int compress_type,
7197 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007198{
7199 struct extent_map_tree *em_tree;
7200 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007201 int ret;
7202
Liu Bo6f9994d2017-01-31 07:50:22 -08007203 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7204 type == BTRFS_ORDERED_COMPRESSED ||
7205 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007206 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007207
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007208 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007209 em = alloc_extent_map();
7210 if (!em)
7211 return ERR_PTR(-ENOMEM);
7212
7213 em->start = start;
7214 em->orig_start = orig_start;
7215 em->len = len;
7216 em->block_len = block_len;
7217 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007218 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007219 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007220 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007221 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007222 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007223 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007224 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007225 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7226 em->compress_type = compress_type;
7227 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007228
7229 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007230 btrfs_drop_extent_cache(inode, em->start,
7231 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007232 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007233 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007234 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007235 /*
7236 * The caller has taken lock_extent(), who could race with us
7237 * to add em?
7238 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007239 } while (ret == -EEXIST);
7240
7241 if (ret) {
7242 free_extent_map(em);
7243 return ERR_PTR(ret);
7244 }
7245
Liu Bo6f9994d2017-01-31 07:50:22 -08007246 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007247 return em;
7248}
7249
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007250
David Sterba55e20bd2020-06-09 19:56:06 +02007251static int btrfs_get_blocks_direct_read(struct extent_map *em,
7252 struct buffer_head *bh_result,
7253 struct inode *inode,
7254 u64 start, u64 len)
7255{
7256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7257
7258 if (em->block_start == EXTENT_MAP_HOLE ||
7259 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7260 return -ENOENT;
7261
7262 len = min(len, em->len - (start - em->start));
7263
7264 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7265 inode->i_blkbits;
7266 bh_result->b_size = len;
7267 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7268 set_buffer_mapped(bh_result);
7269
7270 return 0;
7271}
7272
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007273static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007274 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007275 struct inode *inode,
7276 struct btrfs_dio_data *dio_data,
7277 u64 start, u64 len)
7278{
7279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7280 struct extent_map *em = *map;
7281 int ret = 0;
7282
7283 /*
7284 * We don't allocate a new extent in the following cases
7285 *
7286 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7287 * existing extent.
7288 * 2) The extent is marked as PREALLOC. We're good to go here and can
7289 * just use the extent.
7290 *
7291 */
7292 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7293 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7294 em->block_start != EXTENT_MAP_HOLE)) {
7295 int type;
7296 u64 block_start, orig_start, orig_block_len, ram_bytes;
7297
7298 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7299 type = BTRFS_ORDERED_PREALLOC;
7300 else
7301 type = BTRFS_ORDERED_NOCOW;
7302 len = min(len, em->len - (start - em->start));
7303 block_start = em->block_start + (start - em->start);
7304
7305 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007306 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007307 btrfs_inc_nocow_writers(fs_info, block_start)) {
7308 struct extent_map *em2;
7309
Nikolay Borisov64f54182020-06-03 08:55:31 +03007310 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007311 orig_start, block_start,
7312 len, orig_block_len,
7313 ram_bytes, type);
7314 btrfs_dec_nocow_writers(fs_info, block_start);
7315 if (type == BTRFS_ORDERED_PREALLOC) {
7316 free_extent_map(em);
7317 *map = em = em2;
7318 }
7319
7320 if (em2 && IS_ERR(em2)) {
7321 ret = PTR_ERR(em2);
7322 goto out;
7323 }
7324 /*
7325 * For inode marked NODATACOW or extent marked PREALLOC,
7326 * use the existing or preallocated extent, so does not
7327 * need to adjust btrfs_space_info's bytes_may_use.
7328 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007329 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007330 goto skip_cow;
7331 }
7332 }
7333
7334 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007335 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007336 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007337 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007338 if (IS_ERR(em)) {
7339 ret = PTR_ERR(em);
7340 goto out;
7341 }
7342
7343 len = min(len, em->len - (start - em->start));
7344
7345skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007346 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7347 inode->i_blkbits;
7348 bh_result->b_size = len;
7349 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7350 set_buffer_mapped(bh_result);
7351
7352 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7353 set_buffer_new(bh_result);
7354
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007355 /*
7356 * Need to update the i_size under the extent lock so buffered
7357 * readers will get the updated i_size when we unlock.
7358 */
David Sterba55e20bd2020-06-09 19:56:06 +02007359 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007360 i_size_write(inode, start + len);
7361
David Sterba55e20bd2020-06-09 19:56:06 +02007362 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007363 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007364 dio_data->unsubmitted_oe_range_end = start + len;
7365 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007366out:
7367 return ret;
7368}
7369
David Sterba55e20bd2020-06-09 19:56:06 +02007370static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7371 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007372{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007373 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007374 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007375 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307376 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007377 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007378 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007379 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007380 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007381
David Sterba55e20bd2020-06-09 19:56:06 +02007382 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007383 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007384
Josef Bacikc3298612012-08-03 16:49:19 -04007385 lockstart = start;
7386 lockend = start + len - 1;
7387
David Sterba55e20bd2020-06-09 19:56:06 +02007388 if (current->journal_info) {
7389 /*
7390 * Need to pull our outstanding extents and set journal_info to NULL so
7391 * that anything that needs to check if there's a transaction doesn't get
7392 * confused.
7393 */
7394 dio_data = current->journal_info;
7395 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007396 }
7397
Josef Bacikeb838e72012-07-31 16:28:48 -04007398 /*
7399 * If this errors out it's because we couldn't invalidate pagecache for
7400 * this range and we need to fallback to buffered.
7401 */
David Sterba55e20bd2020-06-09 19:56:06 +02007402 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7403 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007404 ret = -ENOTBLK;
7405 goto err;
7406 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007407
Omar Sandoval39b07b52019-12-02 17:34:23 -08007408 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007409 if (IS_ERR(em)) {
7410 ret = PTR_ERR(em);
7411 goto unlock_err;
7412 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007413
7414 /*
7415 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7416 * io. INLINE is special, and we could probably kludge it in here, but
7417 * it's still buffered so for safety lets just fall back to the generic
7418 * buffered path.
7419 *
7420 * For COMPRESSED we _have_ to read the entire extent in so we can
7421 * decompress it, so there will be buffering required no matter what we
7422 * do, so go ahead and fallback to buffered.
7423 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007424 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007425 * to buffered IO. Don't blame me, this is the price we pay for using
7426 * the generic code.
7427 */
7428 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7429 em->block_start == EXTENT_MAP_INLINE) {
7430 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007431 ret = -ENOTBLK;
7432 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007433 }
7434
David Sterba55e20bd2020-06-09 19:56:06 +02007435 if (create) {
7436 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7437 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007438 if (ret < 0)
7439 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007440
7441 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7442 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007443 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007444 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7445 start, len);
7446 /* Can be negative only if we read from a hole */
7447 if (ret < 0) {
7448 ret = 0;
7449 free_extent_map(em);
7450 goto unlock_err;
7451 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007452 /*
7453 * We need to unlock only the end area that we aren't using.
7454 * The rest is going to be unlocked by the endio routine.
7455 */
David Sterba55e20bd2020-06-09 19:56:06 +02007456 lockstart = start + bh_result->b_size;
7457 if (lockstart < lockend) {
7458 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7459 lockstart, lockend, &cached_state);
7460 } else {
7461 free_extent_state(cached_state);
7462 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007463 }
7464
Josef Bacik4b46fce2010-05-23 11:00:55 -04007465 free_extent_map(em);
7466
7467 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007468
7469unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007470 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7471 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007472err:
David Sterba55e20bd2020-06-09 19:56:06 +02007473 if (dio_data)
7474 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007475 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007476}
7477
Omar Sandoval769b4f22020-04-16 14:46:22 -07007478static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7479{
7480 /*
7481 * This implies a barrier so that stores to dio_bio->bi_status before
7482 * this and loads of dio_bio->bi_status after this are fully ordered.
7483 */
7484 if (!refcount_dec_and_test(&dip->refs))
7485 return;
7486
7487 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007488 __endio_write_update_ordered(BTRFS_I(dip->inode),
7489 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007490 dip->bytes,
7491 !dip->dio_bio->bi_status);
7492 } else {
7493 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7494 dip->logical_offset,
7495 dip->logical_offset + dip->bytes - 1);
7496 }
7497
David Sterba55e20bd2020-06-09 19:56:06 +02007498 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007499 kfree(dip);
7500}
7501
Omar Sandoval77d5d682020-04-16 14:46:25 -07007502static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7503 int mirror_num,
7504 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007505{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007506 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007507 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007508 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007509
Mike Christie37226b22016-06-05 14:31:52 -05007510 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007511
Omar Sandoval5c047a62020-04-16 14:46:24 -07007512 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007513 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007514 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007515
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007516 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007517 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007518 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007519 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007520 return ret;
7521}
7522
Omar Sandoval769b4f22020-04-16 14:46:22 -07007523static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7524 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007525 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007526{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007527 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7528 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007529 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007530 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7531 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007532 struct bio_vec bvec;
7533 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007534 u64 start = io_bio->logical;
7535 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007536 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007537
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007538 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7539 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307540
Liu Bo17347ce2017-05-15 15:33:27 -07007541 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7542 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007543 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007544 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007545 if (uptodate &&
7546 (!csum || !check_data_csum(inode, io_bio, icsum,
7547 bvec.bv_page, pgoff,
7548 start, sectorsize))) {
7549 clean_io_failure(fs_info, failure_tree, io_tree,
7550 start, bvec.bv_page,
7551 btrfs_ino(BTRFS_I(inode)),
7552 pgoff);
7553 } else {
7554 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007555
Omar Sandoval77d5d682020-04-16 14:46:25 -07007556 status = btrfs_submit_read_repair(inode,
7557 &io_bio->bio,
7558 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007559 bvec.bv_page, pgoff,
7560 start,
7561 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007562 io_bio->mirror_num,
7563 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007564 if (status)
7565 err = status;
7566 }
7567 start += sectorsize;
7568 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307569 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307570 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007571 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007572 return err;
7573}
7574
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007575static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007576 const u64 offset, const u64 bytes,
7577 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007578{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007579 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007581 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007582 u64 ordered_offset = offset;
7583 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007584 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007585
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007586 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007587 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007588 else
Qu Wenruo524272602017-03-08 10:25:52 +08007589 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007590
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007591 while (ordered_offset < offset + bytes) {
7592 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007593 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007594 &ordered_offset,
7595 ordered_bytes,
7596 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007597 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7598 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007599 btrfs_queue_work(wq, &ordered->work);
7600 }
7601 /*
7602 * If btrfs_dec_test_ordered_pending does not find any ordered
7603 * extent in the range, we can exit.
7604 */
7605 if (ordered_offset == last_offset)
7606 return;
7607 /*
7608 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007609 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007610 */
7611 if (ordered_offset < offset + bytes) {
7612 ordered_bytes = offset + bytes - ordered_offset;
7613 ordered = NULL;
7614 }
Chris Mason163cf092010-11-28 19:56:33 -05007615 }
Filipe Manana14543772015-11-24 16:23:54 +00007616}
7617
David Sterbad0ee3932018-03-08 14:35:48 +01007618static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007619 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007620{
Josef Bacikc6100a42017-05-05 11:57:13 -04007621 struct inode *inode = private_data;
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007622
7623 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007624}
7625
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007626static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007627{
7628 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007629 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007630
Miao Xie8b110e32014-09-12 18:44:03 +08007631 if (err)
7632 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007633 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007634 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7635 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007636 (unsigned long long)bio->bi_iter.bi_sector,
7637 bio->bi_iter.bi_size, err);
7638
Omar Sandoval769b4f22020-04-16 14:46:22 -07007639 if (bio_op(bio) == REQ_OP_READ) {
7640 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007641 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007642 }
7643
Omar Sandoval769b4f22020-04-16 14:46:22 -07007644 if (err)
7645 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007646
Miao Xiee65e1532010-11-22 03:04:43 +00007647 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007648 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007649}
7650
David Sterbad0ee3932018-03-08 14:35:48 +01007651static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7652 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007655 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007656 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007657 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007658
Liu Bo4c274bc2017-11-01 17:19:27 -06007659 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007660 if (async_submit)
7661 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7662
Josef Bacik5fd02042012-05-02 14:00:54 -04007663 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007664 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007665 if (ret)
7666 goto err;
7667 }
Miao Xiee65e1532010-11-22 03:04:43 +00007668
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007669 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007670 goto map;
7671
7672 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007673 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7674 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007675 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007676 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007677 } else if (write) {
7678 /*
7679 * If we aren't doing async submit, calculate the csum of the
7680 * bio now.
7681 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007682 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007683 if (ret)
7684 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007685 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007686 u64 csum_offset;
7687
7688 csum_offset = file_offset - dip->logical_offset;
7689 csum_offset >>= inode->i_sb->s_blocksize_bits;
7690 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7691 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007692 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007693map:
Chris Mason08635ba2019-07-10 12:28:14 -07007694 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007695err:
Miao Xiee65e1532010-11-22 03:04:43 +00007696 return ret;
7697}
7698
Omar Sandovalc36cac22020-04-16 14:46:13 -07007699/*
7700 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7701 * or ordered extents whether or not we submit any bios.
7702 */
7703static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7704 struct inode *inode,
7705 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007706{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007707 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007708 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7709 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007710 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007711
Omar Sandoval85879572020-04-16 14:46:21 -07007712 dip_size = sizeof(*dip);
7713 if (!write && csum) {
7714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7715 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7716 size_t nblocks;
7717
7718 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7719 dip_size += csum_size * nblocks;
7720 }
7721
7722 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007723 if (!dip)
7724 return NULL;
7725
Omar Sandovalc36cac22020-04-16 14:46:13 -07007726 dip->inode = inode;
7727 dip->logical_offset = file_offset;
7728 dip->bytes = dio_bio->bi_iter.bi_size;
7729 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007730 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007731 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007732
7733 if (write) {
7734 struct btrfs_dio_data *dio_data = current->journal_info;
7735
7736 /*
7737 * Setting range start and end to the same value means that
7738 * no cleanup will happen in btrfs_direct_IO
7739 */
7740 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7741 dip->bytes;
7742 dio_data->unsubmitted_oe_range_start =
7743 dio_data->unsubmitted_oe_range_end;
7744 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007745 return dip;
7746}
7747
David Sterba55e20bd2020-06-09 19:56:06 +02007748static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7749 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007750{
7751 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007752 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007754 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7755 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007756 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007757 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007758 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007759 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007760 u64 submit_len;
7761 int clone_offset = 0;
7762 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307763 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007764 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007765 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007766
Omar Sandovalc36cac22020-04-16 14:46:13 -07007767 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7768 if (!dip) {
7769 if (!write) {
7770 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7771 file_offset + dio_bio->bi_iter.bi_size - 1);
7772 }
7773 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007774 dio_end_io(dio_bio);
7775 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007776 }
7777
Omar Sandoval85879572020-04-16 14:46:21 -07007778 if (!write && csum) {
7779 /*
7780 * Load the csums up front to reduce csum tree searches and
7781 * contention when submitting bios.
7782 */
7783 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7784 dip->csums);
7785 if (status != BLK_STS_OK)
7786 goto out_err;
7787 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007788
Omar Sandoval769b4f22020-04-16 14:46:22 -07007789 start_sector = dio_bio->bi_iter.bi_sector;
7790 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007791
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007792 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007793 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7794 start_sector << 9, submit_len,
7795 &geom);
7796 if (ret) {
7797 status = errno_to_blk_status(ret);
7798 goto out_err;
7799 }
7800 ASSERT(geom.len <= INT_MAX);
7801
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007802 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007803
Liu Bo725130b2017-05-16 09:51:39 -07007804 /*
7805 * This will never fail as it's passing GPF_NOFS and
7806 * the allocation is backed by btrfs_bioset.
7807 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007808 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007809 bio->bi_private = dip;
7810 bio->bi_end_io = btrfs_end_dio_bio;
7811 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007812
Liu Bo725130b2017-05-16 09:51:39 -07007813 ASSERT(submit_len >= clone_len);
7814 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007815
Liu Bo725130b2017-05-16 09:51:39 -07007816 /*
7817 * Increase the count before we submit the bio so we know
7818 * the end IO handler won't happen before we increase the
7819 * count. Otherwise, the dip might get freed before we're
7820 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007821 *
7822 * We transfer the initial reference to the last bio, so we
7823 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007824 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007825 if (submit_len > 0) {
7826 refcount_inc(&dip->refs);
7827 /*
7828 * If we are submitting more than one bio, submit them
7829 * all asynchronously. The exception is RAID 5 or 6, as
7830 * asynchronous checksums make it difficult to collect
7831 * full stripe writes.
7832 */
7833 if (!raid56)
7834 async_submit = 1;
7835 }
Miao Xiee65e1532010-11-22 03:04:43 +00007836
David Sterbad0ee3932018-03-08 14:35:48 +01007837 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007838 async_submit);
7839 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007840 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007841 if (submit_len > 0)
7842 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007843 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007844 }
Liu Bo725130b2017-05-16 09:51:39 -07007845
7846 clone_offset += clone_len;
7847 start_sector += clone_len >> 9;
7848 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007849 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007850 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007851
Miao Xiee65e1532010-11-22 03:04:43 +00007852out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007853 dip->dio_bio->bi_status = status;
7854 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007855}
7856
David Sterbaf4c48b42020-06-09 19:19:27 +02007857static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7858 const struct iov_iter *iter, loff_t offset)
7859{
7860 int seg;
7861 int i;
7862 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7863 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864
David Sterbaf4c48b42020-06-09 19:19:27 +02007865 if (offset & blocksize_mask)
7866 goto out;
7867
7868 if (iov_iter_alignment(iter) & blocksize_mask)
7869 goto out;
7870
7871 /* If this is a write we don't need to check anymore */
7872 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7873 return 0;
7874 /*
7875 * Check to make sure we don't have duplicate iov_base's in this
7876 * iovec, if so return EINVAL, otherwise we'll get csum errors
7877 * when reading back.
7878 */
7879 for (seg = 0; seg < iter->nr_segs; seg++) {
7880 for (i = seg + 1; i < iter->nr_segs; i++) {
7881 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7882 goto out;
7883 }
7884 }
7885 retval = 0;
7886out:
7887 return retval;
7888}
7889
David Sterba55e20bd2020-06-09 19:56:06 +02007890static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007891{
7892 struct file *file = iocb->ki_filp;
7893 struct inode *inode = file->f_mapping->host;
7894 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007895 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007896 struct extent_changeset *data_reserved = NULL;
7897 loff_t offset = iocb->ki_pos;
7898 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007899 int flags = 0;
7900 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007901 bool relock = false;
7902 ssize_t ret;
7903
7904 if (check_direct_IO(fs_info, iter, offset))
7905 return 0;
7906
David Sterba55e20bd2020-06-09 19:56:06 +02007907 inode_dio_begin(inode);
7908
7909 /*
7910 * The generic stuff only does filemap_write_and_wait_range, which
7911 * isn't enough if we've written compressed pages to this area, so
7912 * we need to flush the dirty pages again to make absolutely sure
7913 * that any outstanding dirty pages are on disk.
7914 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007915 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007916 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7917 &BTRFS_I(inode)->runtime_flags))
7918 filemap_fdatawrite_range(inode->i_mapping, offset,
7919 offset + count - 1);
7920
David Sterbaf4c48b42020-06-09 19:19:27 +02007921 if (iov_iter_rw(iter) == WRITE) {
7922 /*
7923 * If the write DIO is beyond the EOF, we need update
7924 * the isize, but it is protected by i_mutex. So we can
7925 * not unlock the i_mutex at this case.
7926 */
7927 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007928 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007929 inode_unlock(inode);
7930 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007931 }
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03007932 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
David Sterba55e20bd2020-06-09 19:56:06 +02007933 offset, count);
7934 if (ret)
7935 goto out;
7936
7937 /*
7938 * We need to know how many extents we reserved so that we can
7939 * do the accounting properly if we go over the number we
7940 * originally calculated. Abuse current->journal_info for this.
7941 */
7942 dio_data.reserve = round_up(count,
7943 fs_info->sectorsize);
7944 dio_data.unsubmitted_oe_range_start = (u64)offset;
7945 dio_data.unsubmitted_oe_range_end = (u64)offset;
7946 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007947 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007948 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7949 &BTRFS_I(inode)->runtime_flags)) {
7950 inode_dio_end(inode);
7951 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7952 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007953 }
7954
David Sterba55e20bd2020-06-09 19:56:06 +02007955 ret = __blockdev_direct_IO(iocb, inode,
7956 fs_info->fs_devices->latest_bdev,
7957 iter, btrfs_get_blocks_direct, NULL,
7958 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007959 if (iov_iter_rw(iter) == WRITE) {
7960 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007961 current->journal_info = NULL;
7962 if (ret < 0 && ret != -EIOCBQUEUED) {
7963 if (dio_data.reserve)
Nikolay Borisov86d52922020-06-03 08:55:40 +03007964 btrfs_delalloc_release_space(BTRFS_I(inode),
7965 data_reserved, offset, dio_data.reserve,
7966 true);
David Sterba55e20bd2020-06-09 19:56:06 +02007967 /*
7968 * On error we might have left some ordered extents
7969 * without submitting corresponding bios for them, so
7970 * cleanup them up to avoid other tasks getting them
7971 * and waiting for them to complete forever.
7972 */
7973 if (dio_data.unsubmitted_oe_range_start <
7974 dio_data.unsubmitted_oe_range_end)
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007975 __endio_write_update_ordered(BTRFS_I(inode),
David Sterba55e20bd2020-06-09 19:56:06 +02007976 dio_data.unsubmitted_oe_range_start,
7977 dio_data.unsubmitted_oe_range_end -
7978 dio_data.unsubmitted_oe_range_start,
7979 false);
7980 } else if (ret >= 0 && (size_t)ret < count)
Nikolay Borisov86d52922020-06-03 08:55:40 +03007981 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
David Sterba55e20bd2020-06-09 19:56:06 +02007982 offset, count - (size_t)ret, true);
7983 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007984 }
7985out:
David Sterba55e20bd2020-06-09 19:56:06 +02007986 if (wakeup)
7987 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007988 if (relock)
7989 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007990
David Sterbaf4c48b42020-06-09 19:19:27 +02007991 extent_changeset_free(data_reserved);
7992 return ret;
7993}
Chris Mason16432982008-04-10 10:23:21 -04007994
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007995static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02007996 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007997{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007998 int ret;
7999
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008000 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008001 if (ret)
8002 return ret;
8003
David Sterba2135fb92017-06-23 04:09:57 +02008004 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008005}
8006
Chris Mason9ebefb182007-06-15 13:50:00 -04008007int btrfs_readpage(struct file *file, struct page *page)
8008{
David Sterba71ad38b2020-02-05 19:09:35 +01008009 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008010}
Chris Mason1832a6d2007-12-21 16:27:21 -05008011
Chris Mason39279cc2007-06-12 06:35:45 -04008012static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8013{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008014 struct inode *inode = page->mapping->host;
8015 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008016
8017 if (current->flags & PF_MEMALLOC) {
8018 redirty_page_for_writepage(wbc, page);
8019 unlock_page(page);
8020 return 0;
8021 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008022
8023 /*
8024 * If we are under memory pressure we will call this directly from the
8025 * VM, we need to make sure we have the inode referenced for the ordered
8026 * extent. If not just return like we didn't do anything.
8027 */
8028 if (!igrab(inode)) {
8029 redirty_page_for_writepage(wbc, page);
8030 return AOP_WRITEPAGE_ACTIVATE;
8031 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008032 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008033 btrfs_add_delayed_iput(inode);
8034 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008035}
Chris Mason39279cc2007-06-12 06:35:45 -04008036
Eric Sandeen48a3b632013-04-25 20:41:01 +00008037static int btrfs_writepages(struct address_space *mapping,
8038 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008039{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008040 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008041}
8042
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008043static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008044{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008045 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008046}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008047
Chris Masone6dcd2d2008-07-17 12:53:50 -04008048static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008049{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008050 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008051 if (ret == 1)
8052 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008053 return ret;
8054}
Chris Mason39279cc2007-06-12 06:35:45 -04008055
Chris Masone6dcd2d2008-07-17 12:53:50 -04008056static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8057{
Chris Mason98509cf2008-09-11 15:51:43 -04008058 if (PageWriteback(page) || PageDirty(page))
8059 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008060 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008061}
8062
Roman Gushchinf8e66082020-03-04 16:57:35 -08008063#ifdef CONFIG_MIGRATION
8064static int btrfs_migratepage(struct address_space *mapping,
8065 struct page *newpage, struct page *page,
8066 enum migrate_mode mode)
8067{
8068 int ret;
8069
8070 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8071 if (ret != MIGRATEPAGE_SUCCESS)
8072 return ret;
8073
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008074 if (page_has_private(page))
8075 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008076
8077 if (PagePrivate2(page)) {
8078 ClearPagePrivate2(page);
8079 SetPagePrivate2(newpage);
8080 }
8081
8082 if (mode != MIGRATE_SYNC_NO_COPY)
8083 migrate_page_copy(newpage, page);
8084 else
8085 migrate_page_states(newpage, page);
8086 return MIGRATEPAGE_SUCCESS;
8087}
8088#endif
8089
Lukas Czernerd47992f2013-05-21 23:17:23 -04008090static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8091 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008092{
Josef Bacik5fd02042012-05-02 14:00:54 -04008093 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008094 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008095 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008096 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008097 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008098 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308099 u64 start;
8100 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008101 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008102
Chris Mason8b62b722009-09-02 16:53:46 -04008103 /*
8104 * we have the page locked, so new writeback can't start,
8105 * and the dirty bit won't be cleared while we are here.
8106 *
8107 * Wait for IO on this page so that we can safely clear
8108 * the PagePrivate2 bit and do ordered accounting
8109 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008110 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008111
Josef Bacik5fd02042012-05-02 14:00:54 -04008112 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008113 if (offset) {
8114 btrfs_releasepage(page, GFP_NOFS);
8115 return;
8116 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008117
8118 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008119 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308120again:
8121 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008122 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308123 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008124 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008125 end = min(page_end,
8126 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008127 /*
8128 * IO on this page will never be started, so we need
8129 * to account for any ordered extents now
8130 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008131 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308132 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008133 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008134 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008135 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008136 /*
8137 * whoever cleared the private bit is responsible
8138 * for the finish_ordered_io
8139 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008140 if (TestClearPagePrivate2(page)) {
8141 struct btrfs_ordered_inode_tree *tree;
8142 u64 new_len;
8143
8144 tree = &BTRFS_I(inode)->ordered_tree;
8145
8146 spin_lock_irq(&tree->lock);
8147 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308148 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008149 if (new_len < ordered->truncated_len)
8150 ordered->truncated_len = new_len;
8151 spin_unlock_irq(&tree->lock);
8152
8153 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308154 start,
8155 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008156 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008157 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008158 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008159 if (!inode_evicting) {
8160 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308161 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008162 &cached_state);
8163 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308164
8165 start = end + 1;
8166 if (start < page_end)
8167 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008168 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008169
Qu Wenruob9d0b382015-09-29 10:35:16 +08008170 /*
8171 * Qgroup reserved space handler
8172 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008173 * 1) Already written to disk or ordered extent already submitted
8174 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8175 * Qgroup will be handled by its qgroup_record then.
8176 * btrfs_qgroup_free_data() call will do nothing here.
8177 *
8178 * 2) Not written to disk yet
8179 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8180 * bit of its io_tree, and free the qgroup reserved data space.
8181 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008182 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03008183 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008184 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008185 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008186 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8187 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008188 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008189
8190 __btrfs_releasepage(page, GFP_NOFS);
8191 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008192
Chris Mason4a096752008-07-21 10:29:44 -04008193 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008194 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008195}
8196
Chris Mason9ebefb182007-06-15 13:50:00 -04008197/*
8198 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8199 * called from a page fault handler when a page is first dirtied. Hence we must
8200 * be careful to check for EOF conditions here. We set the page up correctly
8201 * for a written page which means we get ENOSPC checking when writing into
8202 * holes and correct delalloc and unwritten extent mapping on filesystems that
8203 * support these features.
8204 *
8205 * We are not allowed to take the i_mutex here so we have to play games to
8206 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008207 * truncate_setsize() writes the inode size before removing pages, once we have
8208 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008209 * beyond EOF, then the page is guaranteed safe against truncation until we
8210 * unlock the page.
8211 */
Souptick Joardera528a242018-06-06 19:54:44 +05308212vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008213{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008214 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008215 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8218 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008219 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008220 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008221 char *kaddr;
8222 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008223 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308224 vm_fault_t ret;
8225 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008226 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308227 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008228 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008229 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308230 u64 end;
8231
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008232 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008233
Jan Karab2b5ef52012-06-12 16:20:45 +02008234 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008235 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008236 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308237 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008238
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308239 /*
8240 * Reserving delalloc space after obtaining the page lock can lead to
8241 * deadlock. For example, if a dirty page is locked by this function
8242 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8243 * dirty page write out, then the btrfs_writepage() function could
8244 * end up waiting indefinitely to get a lock on the page currently
8245 * being processed by btrfs_page_mkwrite() function.
8246 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008247 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8248 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308249 if (!ret2) {
8250 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008251 reserved = 1;
8252 }
Souptick Joardera528a242018-06-06 19:54:44 +05308253 if (ret2) {
8254 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008255 if (reserved)
8256 goto out;
8257 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008258 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008259
Nick Piggin56a76f82009-03-31 15:23:23 -07008260 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008261again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008262 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008263 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008264
Chris Mason9ebefb182007-06-15 13:50:00 -04008265 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008266 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008267 /* page got truncated out from underneath us */
8268 goto out_unlock;
8269 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008270 wait_on_page_writeback(page);
8271
David Sterbaff13db42015-12-03 14:30:40 +01008272 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273 set_page_extent_mapped(page);
8274
Chris Masoneb84ae02008-07-17 13:53:27 -04008275 /*
8276 * we can't set the delalloc bits if there are pending ordered
8277 * extents. Drop our locks and wait for them to finish
8278 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008279 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8280 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008281 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008282 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008283 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008284 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008285 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008286 btrfs_put_ordered_extent(ordered);
8287 goto again;
8288 }
8289
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008290 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008291 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008292 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008293 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308294 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008295 btrfs_delalloc_release_space(BTRFS_I(inode),
8296 data_reserved, page_start,
8297 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308298 }
8299 }
8300
Josef Bacikfbf19082009-10-01 17:10:23 -04008301 /*
Liu Bo54160342016-12-13 12:15:19 -08008302 * page_mkwrite gets called when the page is firstly dirtied after it's
8303 * faulted in, but write(2) could also dirty a page and set delalloc
8304 * bits, thus in this case for space account reason, we still need to
8305 * clear any delalloc bits within this page range since we have to
8306 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008307 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308308 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008309 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8310 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008311
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008312 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008313 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308314 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008315 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008316 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008317 ret = VM_FAULT_SIGBUS;
8318 goto out_unlock;
8319 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008320
8321 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008322 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008323 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008324 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008325 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008326
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008327 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008328 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008329 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008330 flush_dcache_page(page);
8331 kunmap(page);
8332 }
Chris Mason247e7432008-07-17 12:53:51 -04008333 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008334 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008335 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008336
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008337 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008338 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008339 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008340
David Sterbae43bbe52017-12-12 21:43:52 +01008341 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008342
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008343 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8344 sb_end_pagefault(inode->i_sb);
8345 extent_changeset_free(data_reserved);
8346 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008347
8348out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008349 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008350out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008351 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008352 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008353 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008354out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008355 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008356 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008357 return ret;
8358}
8359
Filipe Manana213e8c52018-02-06 20:40:31 +00008360static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008361{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008362 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008363 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008364 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008365 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008366 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008367 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008368 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008369
Filipe Manana213e8c52018-02-06 20:40:31 +00008370 if (!skip_writeback) {
8371 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8372 (u64)-1);
8373 if (ret)
8374 return ret;
8375 }
Chris Mason39279cc2007-06-12 06:35:45 -04008376
Josef Bacikfcb80c22011-05-03 10:40:22 -04008377 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008378 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8379 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008380 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008381 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008382 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008383 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008384 * be free'd up by the truncate operation, but also have some slack
8385 * space reserved in case it uses space during the truncate (thank you
8386 * very much snapshotting).
8387 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008388 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008389 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008390 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008391 * doesn't end up using space reserved for updating the inode. We also
8392 * need to be able to stop the transaction and start a new one, which
8393 * means we need to be able to update the inode several times, and we
8394 * have no idea of knowing how many times that will be, so we can't just
8395 * reserve 1 item for the entirety of the operation, so that has to be
8396 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008397 *
8398 * So that leaves us with
8399 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008400 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008401 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008402 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008403 * updating the inode.
8404 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008405 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008406 if (!rsv)
8407 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008408 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008409 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008410
Josef Bacik907cbce2011-08-08 13:46:15 -04008411 /*
Josef Bacik07127182011-08-19 10:29:59 -04008412 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008413 * 1 for updating the inode.
8414 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008415 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008416 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008417 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008418 goto out;
8419 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008420
Josef Bacik907cbce2011-08-08 13:46:15 -04008421 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008422 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008423 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008424 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008425
Chris Mason5a3f23d2009-03-31 13:27:11 -04008426 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008427 * So if we truncate and then write and fsync we normally would just
8428 * write the extents that changed, which is a problem if we need to
8429 * first truncate that entire inode. So set this flag so we write out
8430 * all of the extents in the inode to the sync log so we're completely
8431 * safe.
8432 */
8433 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008434 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008435
Yan, Zheng80825102009-11-12 09:35:36 +00008436 while (1) {
8437 ret = btrfs_truncate_inode_items(trans, root, inode,
8438 inode->i_size,
8439 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008440 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008441 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008442 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008443
Yan, Zheng80825102009-11-12 09:35:36 +00008444 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008445 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008446 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008447
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008448 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008449 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008450
8451 trans = btrfs_start_transaction(root, 2);
8452 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008453 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008454 trans = NULL;
8455 break;
8456 }
8457
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008458 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008459 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008460 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008461 BUG_ON(ret); /* shouldn't happen */
8462 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008463 }
8464
Josef Bacikddfae632017-10-19 14:16:02 -04008465 /*
8466 * We can't call btrfs_truncate_block inside a trans handle as we could
8467 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8468 * we've truncated everything except the last little bit, and can do
8469 * btrfs_truncate_block and then update the disk_i_size.
8470 */
8471 if (ret == NEED_TRUNCATE_BLOCK) {
8472 btrfs_end_transaction(trans);
8473 btrfs_btree_balance_dirty(fs_info);
8474
8475 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8476 if (ret)
8477 goto out;
8478 trans = btrfs_start_transaction(root, 1);
8479 if (IS_ERR(trans)) {
8480 ret = PTR_ERR(trans);
8481 goto out;
8482 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008483 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008484 }
8485
Chris Mason917c16b2011-11-08 14:49:59 -05008486 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008487 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008488
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008489 trans->block_rsv = &fs_info->trans_block_rsv;
8490 ret2 = btrfs_update_inode(trans, root, inode);
8491 if (ret2 && !ret)
8492 ret = ret2;
8493
8494 ret2 = btrfs_end_transaction(trans);
8495 if (ret2 && !ret)
8496 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008497 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008498 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008499out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008500 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008501
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008502 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008503}
8504
Sven Wegener3b963622008-06-09 21:57:42 -04008505/*
Chris Masond352ac62008-09-29 15:18:18 -04008506 * create a new subvolume directory/inode (helper for the ioctl).
8507 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008508int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008509 struct btrfs_root *new_root,
8510 struct btrfs_root *parent_root,
8511 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008512{
Chris Mason39279cc2007-06-12 06:35:45 -04008513 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008514 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008515 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008516
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008517 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8518 new_dirid, new_dirid,
8519 S_IFDIR | (~current_umask() & S_IRWXUGO),
8520 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008521 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008522 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008523 inode->i_op = &btrfs_dir_inode_operations;
8524 inode->i_fop = &btrfs_dir_file_operations;
8525
Miklos Szeredibfe86842011-10-28 14:13:29 +02008526 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008527 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008528 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008529
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008530 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8531 if (err)
8532 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008533 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008534 new_root->root_key.objectid, err);
8535
Yan, Zheng76dda932009-09-21 16:00:26 -04008536 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008537
Yan, Zheng76dda932009-09-21 16:00:26 -04008538 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008539 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008540}
8541
Chris Mason39279cc2007-06-12 06:35:45 -04008542struct inode *btrfs_alloc_inode(struct super_block *sb)
8543{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008544 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008545 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008546 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008547
David Sterba712e36c2017-10-31 17:08:27 +01008548 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008549 if (!ei)
8550 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008551
8552 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008554 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008555 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008556 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008557 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008558 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008559 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008560 ei->disk_i_size = 0;
8561 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008562 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008563 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008564 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008565 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008566 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008567 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008568
Josef Bacik9e0baf62011-07-15 15:16:44 +00008569 spin_lock_init(&ei->lock);
8570 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008571 if (sb->s_magic != BTRFS_TEST_MAGIC)
8572 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8573 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008574 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008575 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008576 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008577
Miao Xie16cdcec2011-04-22 18:12:22 +08008578 ei->delayed_node = NULL;
8579
chandan r9cc97d62012-07-04 12:48:07 +05308580 ei->i_otime.tv_sec = 0;
8581 ei->i_otime.tv_nsec = 0;
8582
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008583 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008584 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008585 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8586 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8587 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008588 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8589 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008590 ei->io_tree.track_uptodate = true;
8591 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008592 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008593 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008594 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008595 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008596 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008597 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008598 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008599
8600 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008601}
8602
Josef Bacikaaedb552013-10-11 14:44:09 -04008603#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8604void btrfs_test_destroy_inode(struct inode *inode)
8605{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008606 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008607 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8608}
8609#endif
8610
Al Viro26602ca2019-04-10 15:14:41 -04008611void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008612{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008613 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8614}
8615
Chris Mason39279cc2007-06-12 06:35:45 -04008616void btrfs_destroy_inode(struct inode *inode)
8617{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008618 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008619 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008620 struct btrfs_root *root = BTRFS_I(inode)->root;
8621
Al Virob3d9b7a2012-06-09 13:51:19 -04008622 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008623 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008624 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8625 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008626 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008627 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008628 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008629 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008630 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008631
Chris Mason5a3f23d2009-03-31 13:27:11 -04008632 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008633 * This can happen where we create an inode, but somebody else also
8634 * created the same inode and we need to destroy the one we already
8635 * created.
8636 */
8637 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008638 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008639
Chris Masond3977122009-01-05 21:25:51 -05008640 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008641 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8642 if (!ordered)
8643 break;
8644 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008645 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008646 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008647 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008648 btrfs_remove_ordered_extent(inode, ordered);
8649 btrfs_put_ordered_extent(ordered);
8650 btrfs_put_ordered_extent(ordered);
8651 }
8652 }
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03008653 btrfs_qgroup_check_reserved_leak(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008654 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008655 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008656 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008657 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008658}
8659
Al Viro45321ac2010-06-07 13:43:19 -04008660int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008661{
8662 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008663
Naohiro Aota6379ef92013-06-06 09:56:34 +00008664 if (root == NULL)
8665 return 1;
8666
Liu Bofa6ac872013-02-20 14:10:23 +00008667 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008668 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008669 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008670 else
Al Viro45321ac2010-06-07 13:43:19 -04008671 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008672}
8673
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008674static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008675{
8676 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8677
8678 inode_init_once(&ei->vfs_inode);
8679}
8680
David Sterbae67c7182018-02-19 17:24:18 +01008681void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008682{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008683 /*
8684 * Make sure all delayed rcu free inodes are flushed before we
8685 * destroy cache.
8686 */
8687 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008688 kmem_cache_destroy(btrfs_inode_cachep);
8689 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008690 kmem_cache_destroy(btrfs_path_cachep);
8691 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008692 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008693}
8694
Liu Bof5c29bd2017-11-02 17:21:50 -06008695int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008696{
David Sterba837e1972012-09-07 03:00:48 -06008697 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008698 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008699 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8700 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008701 if (!btrfs_inode_cachep)
8702 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008703
David Sterba837e1972012-09-07 03:00:48 -06008704 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008705 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008706 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008707 if (!btrfs_trans_handle_cachep)
8708 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008709
David Sterba837e1972012-09-07 03:00:48 -06008710 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008711 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008712 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008713 if (!btrfs_path_cachep)
8714 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008715
David Sterba837e1972012-09-07 03:00:48 -06008716 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008717 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008718 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008719 if (!btrfs_free_space_cachep)
8720 goto fail;
8721
Christophe Leroy3acd4852019-08-21 15:05:55 +00008722 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8723 PAGE_SIZE, PAGE_SIZE,
8724 SLAB_RED_ZONE, NULL);
8725 if (!btrfs_free_space_bitmap_cachep)
8726 goto fail;
8727
Chris Mason39279cc2007-06-12 06:35:45 -04008728 return 0;
8729fail:
8730 btrfs_destroy_cachep();
8731 return -ENOMEM;
8732}
8733
David Howellsa528d352017-01-31 16:46:22 +00008734static int btrfs_getattr(const struct path *path, struct kstat *stat,
8735 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008736{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008737 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008738 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008739 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008740 u32 bi_flags = BTRFS_I(inode)->flags;
8741
8742 stat->result_mask |= STATX_BTIME;
8743 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8744 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8745 if (bi_flags & BTRFS_INODE_APPEND)
8746 stat->attributes |= STATX_ATTR_APPEND;
8747 if (bi_flags & BTRFS_INODE_COMPRESS)
8748 stat->attributes |= STATX_ATTR_COMPRESSED;
8749 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8750 stat->attributes |= STATX_ATTR_IMMUTABLE;
8751 if (bi_flags & BTRFS_INODE_NODUMP)
8752 stat->attributes |= STATX_ATTR_NODUMP;
8753
8754 stat->attributes_mask |= (STATX_ATTR_APPEND |
8755 STATX_ATTR_COMPRESSED |
8756 STATX_ATTR_IMMUTABLE |
8757 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008758
Chris Mason39279cc2007-06-12 06:35:45 -04008759 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008760 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008761
8762 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008763 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008764 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008765 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008766 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008767 return 0;
8768}
8769
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008770static int btrfs_rename_exchange(struct inode *old_dir,
8771 struct dentry *old_dentry,
8772 struct inode *new_dir,
8773 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008774{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008775 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008776 struct btrfs_trans_handle *trans;
8777 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008778 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008779 struct inode *new_inode = new_dentry->d_inode;
8780 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008781 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008782 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008783 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8784 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008785 u64 old_idx = 0;
8786 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008787 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008788 bool root_log_pinned = false;
8789 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008790 struct btrfs_log_ctx ctx_root;
8791 struct btrfs_log_ctx ctx_dest;
8792 bool sync_log_root = false;
8793 bool sync_log_dest = false;
8794 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008795
8796 /* we only allow rename subvolume link between subvolumes */
8797 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8798 return -EXDEV;
8799
Filipe Mananad4682ba2018-06-11 19:24:28 +01008800 btrfs_init_log_ctx(&ctx_root, old_inode);
8801 btrfs_init_log_ctx(&ctx_dest, new_inode);
8802
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008803 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008804 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8805 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008806 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008807
8808 /*
8809 * We want to reserve the absolute worst case amount of items. So if
8810 * both inodes are subvols and we need to unlink them then that would
8811 * require 4 item modifications, but if they are both normal inodes it
8812 * would require 5 item modifications, so we'll assume their normal
8813 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8814 * should cover the worst case number of items we'll modify.
8815 */
8816 trans = btrfs_start_transaction(root, 12);
8817 if (IS_ERR(trans)) {
8818 ret = PTR_ERR(trans);
8819 goto out_notrans;
8820 }
8821
Josef Bacik3e174092019-11-15 15:43:06 -05008822 if (dest != root)
8823 btrfs_record_root_in_trans(trans, dest);
8824
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008825 /*
8826 * We need to find a free sequence number both in the source and
8827 * in the destination directory for the exchange.
8828 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008829 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008830 if (ret)
8831 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008832 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008833 if (ret)
8834 goto out_fail;
8835
8836 BTRFS_I(old_inode)->dir_index = 0ULL;
8837 BTRFS_I(new_inode)->dir_index = 0ULL;
8838
8839 /* Reference for the source. */
8840 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8841 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008842 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008843 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008844 btrfs_pin_log_trans(root);
8845 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008846 ret = btrfs_insert_inode_ref(trans, dest,
8847 new_dentry->d_name.name,
8848 new_dentry->d_name.len,
8849 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008850 btrfs_ino(BTRFS_I(new_dir)),
8851 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008852 if (ret)
8853 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008854 }
8855
8856 /* And now for the dest. */
8857 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8858 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008859 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008860 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008861 btrfs_pin_log_trans(dest);
8862 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008863 ret = btrfs_insert_inode_ref(trans, root,
8864 old_dentry->d_name.name,
8865 old_dentry->d_name.len,
8866 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008867 btrfs_ino(BTRFS_I(old_dir)),
8868 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008869 if (ret)
8870 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008871 }
8872
8873 /* Update inode version and ctime/mtime. */
8874 inode_inc_iversion(old_dir);
8875 inode_inc_iversion(new_dir);
8876 inode_inc_iversion(old_inode);
8877 inode_inc_iversion(new_inode);
8878 old_dir->i_ctime = old_dir->i_mtime = ctime;
8879 new_dir->i_ctime = new_dir->i_mtime = ctime;
8880 old_inode->i_ctime = ctime;
8881 new_inode->i_ctime = ctime;
8882
8883 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008884 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8885 BTRFS_I(old_inode), 1);
8886 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8887 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008888 }
8889
8890 /* src is a subvolume */
8891 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008892 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008893 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008894 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8895 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008896 old_dentry->d_name.name,
8897 old_dentry->d_name.len);
8898 if (!ret)
8899 ret = btrfs_update_inode(trans, root, old_inode);
8900 }
8901 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008902 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008903 goto out_fail;
8904 }
8905
8906 /* dest is a subvolume */
8907 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008908 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008909 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008910 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8911 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008912 new_dentry->d_name.name,
8913 new_dentry->d_name.len);
8914 if (!ret)
8915 ret = btrfs_update_inode(trans, dest, new_inode);
8916 }
8917 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008918 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008919 goto out_fail;
8920 }
8921
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008922 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008923 new_dentry->d_name.name,
8924 new_dentry->d_name.len, 0, old_idx);
8925 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008926 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008927 goto out_fail;
8928 }
8929
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008930 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008931 old_dentry->d_name.name,
8932 old_dentry->d_name.len, 0, new_idx);
8933 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008934 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008935 goto out_fail;
8936 }
8937
8938 if (old_inode->i_nlink == 1)
8939 BTRFS_I(old_inode)->dir_index = old_idx;
8940 if (new_inode->i_nlink == 1)
8941 BTRFS_I(new_inode)->dir_index = new_idx;
8942
Filipe Manana86e8aa02016-05-05 02:02:27 +01008943 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008944 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008945 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8946 BTRFS_I(old_dir), parent,
8947 false, &ctx_root);
8948 if (ret == BTRFS_NEED_LOG_SYNC)
8949 sync_log_root = true;
8950 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8951 commit_transaction = true;
8952 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008953 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008954 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008955 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008956 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008957 if (!commit_transaction) {
8958 parent = old_dentry->d_parent;
8959 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8960 BTRFS_I(new_dir), parent,
8961 false, &ctx_dest);
8962 if (ret == BTRFS_NEED_LOG_SYNC)
8963 sync_log_dest = true;
8964 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8965 commit_transaction = true;
8966 ret = 0;
8967 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008968 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008969 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008970 }
8971out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008972 /*
8973 * If we have pinned a log and an error happened, we unpin tasks
8974 * trying to sync the log and force them to fallback to a transaction
8975 * commit if the log currently contains any of the inodes involved in
8976 * this rename operation (to ensure we do not persist a log with an
8977 * inconsistent state for any of these inodes or leading to any
8978 * inconsistencies when replayed). If the transaction was aborted, the
8979 * abortion reason is propagated to userspace when attempting to commit
8980 * the transaction. If the log does not contain any of these inodes, we
8981 * allow the tasks to sync it.
8982 */
8983 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008984 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8985 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8986 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008987 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008988 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008989 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008990
8991 if (root_log_pinned) {
8992 btrfs_end_log_trans(root);
8993 root_log_pinned = false;
8994 }
8995 if (dest_log_pinned) {
8996 btrfs_end_log_trans(dest);
8997 dest_log_pinned = false;
8998 }
8999 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009000 if (!ret && sync_log_root && !commit_transaction) {
9001 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9002 &ctx_root);
9003 if (ret)
9004 commit_transaction = true;
9005 }
9006 if (!ret && sync_log_dest && !commit_transaction) {
9007 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9008 &ctx_dest);
9009 if (ret)
9010 commit_transaction = true;
9011 }
9012 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009013 /*
9014 * We may have set commit_transaction when logging the new name
9015 * in the destination root, in which case we left the source
9016 * root context in the list of log contextes. So make sure we
9017 * remove it to avoid invalid memory accesses, since the context
9018 * was allocated in our stack frame.
9019 */
9020 if (sync_log_root) {
9021 mutex_lock(&root->log_mutex);
9022 list_del_init(&ctx_root.list);
9023 mutex_unlock(&root->log_mutex);
9024 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009025 ret = btrfs_commit_transaction(trans);
9026 } else {
9027 int ret2;
9028
9029 ret2 = btrfs_end_transaction(trans);
9030 ret = ret ? ret : ret2;
9031 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009032out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009033 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9034 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009035 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009036
Filipe Mananae6c61712019-11-08 16:11:56 +00009037 ASSERT(list_empty(&ctx_root.list));
9038 ASSERT(list_empty(&ctx_dest.list));
9039
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009040 return ret;
9041}
9042
9043static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9044 struct btrfs_root *root,
9045 struct inode *dir,
9046 struct dentry *dentry)
9047{
9048 int ret;
9049 struct inode *inode;
9050 u64 objectid;
9051 u64 index;
9052
9053 ret = btrfs_find_free_ino(root, &objectid);
9054 if (ret)
9055 return ret;
9056
9057 inode = btrfs_new_inode(trans, root, dir,
9058 dentry->d_name.name,
9059 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009060 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009061 objectid,
9062 S_IFCHR | WHITEOUT_MODE,
9063 &index);
9064
9065 if (IS_ERR(inode)) {
9066 ret = PTR_ERR(inode);
9067 return ret;
9068 }
9069
9070 inode->i_op = &btrfs_special_inode_operations;
9071 init_special_inode(inode, inode->i_mode,
9072 WHITEOUT_DEV);
9073
9074 ret = btrfs_init_inode_security(trans, inode, dir,
9075 &dentry->d_name);
9076 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009077 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009079 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9080 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009081 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009082 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009083
9084 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009085out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009086 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009087 if (ret)
9088 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009089 iput(inode);
9090
Filipe Mananac9901612016-05-05 01:41:57 +01009091 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009092}
9093
Chris Mason39279cc2007-06-12 06:35:45 -04009094static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009095 struct inode *new_dir, struct dentry *new_dentry,
9096 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009097{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009098 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009099 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009100 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009101 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9102 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009103 struct inode *new_inode = d_inode(new_dentry);
9104 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009105 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009106 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009107 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009108 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009109 struct btrfs_log_ctx ctx;
9110 bool sync_log = false;
9111 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009112
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009113 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009114 return -EPERM;
9115
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009116 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009117 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009118 return -EXDEV;
9119
Li Zefan33345d012011-04-20 10:31:50 +08009120 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009121 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009122 return -ENOTEMPTY;
9123
Chris Mason39279cc2007-06-12 06:35:45 -04009124 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009125 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009126 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009127
9128
9129 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009130 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009131 new_dentry->d_name.name,
9132 new_dentry->d_name.len);
9133
9134 if (ret) {
9135 if (ret == -EEXIST) {
9136 /* we shouldn't get
9137 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309138 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009139 return ret;
9140 }
9141 } else {
9142 /* maybe -EOVERFLOW */
9143 return ret;
9144 }
9145 }
9146 ret = 0;
9147
Chris Mason5a3f23d2009-03-31 13:27:11 -04009148 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009149 * we're using rename to replace one file with another. Start IO on it
9150 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009151 */
Chris Mason8d875f92014-08-12 10:47:42 -07009152 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009153 filemap_flush(old_inode->i_mapping);
9154
Yan, Zheng76dda932009-09-21 16:00:26 -04009155 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009156 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009157 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009158 /*
9159 * We want to reserve the absolute worst case amount of items. So if
9160 * both inodes are subvols and we need to unlink them then that would
9161 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009162 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009163 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9164 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009165 * If our rename has the whiteout flag, we need more 5 units for the
9166 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9167 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009168 */
Filipe Manana5062af32016-05-05 10:26:26 +01009169 trans_num_items = 11;
9170 if (flags & RENAME_WHITEOUT)
9171 trans_num_items += 5;
9172 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009173 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009174 ret = PTR_ERR(trans);
9175 goto out_notrans;
9176 }
Chris Mason5f39d392007-10-15 16:14:19 -04009177
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009178 if (dest != root)
9179 btrfs_record_root_in_trans(trans, dest);
9180
Nikolay Borisov877574e2017-02-20 13:50:33 +02009181 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009182 if (ret)
9183 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009184
Miao Xie67de1172013-12-26 13:07:06 +08009185 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009186 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009187 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009188 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009189 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009190 btrfs_pin_log_trans(root);
9191 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009192 ret = btrfs_insert_inode_ref(trans, dest,
9193 new_dentry->d_name.name,
9194 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009195 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009196 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009197 if (ret)
9198 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009199 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009200
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009201 inode_inc_iversion(old_dir);
9202 inode_inc_iversion(new_dir);
9203 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009204 old_dir->i_ctime = old_dir->i_mtime =
9205 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009206 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009207
Chris Mason12fcfd22009-03-24 10:24:20 -04009208 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009209 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9210 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009211
Li Zefan33345d012011-04-20 10:31:50 +08009212 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009213 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009214 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009215 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9216 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009217 old_dentry->d_name.name,
9218 old_dentry->d_name.len);
9219 if (!ret)
9220 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009221 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009222 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009223 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009224 goto out_fail;
9225 }
Chris Mason39279cc2007-06-12 06:35:45 -04009226
9227 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009228 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009229 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009230 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009231 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009232 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009233 BUG_ON(new_inode->i_nlink == 0);
9234 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009235 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9236 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009237 new_dentry->d_name.name,
9238 new_dentry->d_name.len);
9239 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009240 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009241 ret = btrfs_orphan_add(trans,
9242 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009243 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009244 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009245 goto out_fail;
9246 }
Chris Mason39279cc2007-06-12 06:35:45 -04009247 }
Josef Bacikaec74772008-07-24 12:12:38 -04009248
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009249 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009250 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009251 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009252 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009253 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009254 goto out_fail;
9255 }
Chris Mason39279cc2007-06-12 06:35:45 -04009256
Miao Xie67de1172013-12-26 13:07:06 +08009257 if (old_inode->i_nlink == 1)
9258 BTRFS_I(old_inode)->dir_index = index;
9259
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009260 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009261 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009262
Filipe Mananad4682ba2018-06-11 19:24:28 +01009263 btrfs_init_log_ctx(&ctx, old_inode);
9264 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9265 BTRFS_I(old_dir), parent,
9266 false, &ctx);
9267 if (ret == BTRFS_NEED_LOG_SYNC)
9268 sync_log = true;
9269 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9270 commit_transaction = true;
9271 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009272 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009273 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009274 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009275
9276 if (flags & RENAME_WHITEOUT) {
9277 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9278 old_dentry);
9279
9280 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009281 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009282 goto out_fail;
9283 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009284 }
Chris Mason39279cc2007-06-12 06:35:45 -04009285out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009286 /*
9287 * If we have pinned the log and an error happened, we unpin tasks
9288 * trying to sync the log and force them to fallback to a transaction
9289 * commit if the log currently contains any of the inodes involved in
9290 * this rename operation (to ensure we do not persist a log with an
9291 * inconsistent state for any of these inodes or leading to any
9292 * inconsistencies when replayed). If the transaction was aborted, the
9293 * abortion reason is propagated to userspace when attempting to commit
9294 * the transaction. If the log does not contain any of these inodes, we
9295 * allow the tasks to sync it.
9296 */
9297 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009298 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9299 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9300 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009301 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009302 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009303 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009304
9305 btrfs_end_log_trans(root);
9306 log_pinned = false;
9307 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009308 if (!ret && sync_log) {
9309 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9310 if (ret)
9311 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009312 } else if (sync_log) {
9313 mutex_lock(&root->log_mutex);
9314 list_del(&ctx.list);
9315 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009316 }
9317 if (commit_transaction) {
9318 ret = btrfs_commit_transaction(trans);
9319 } else {
9320 int ret2;
9321
9322 ret2 = btrfs_end_transaction(trans);
9323 ret = ret ? ret : ret2;
9324 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009325out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009326 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009327 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009328
Chris Mason39279cc2007-06-12 06:35:45 -04009329 return ret;
9330}
9331
Miklos Szeredi80ace852014-07-23 15:15:32 +02009332static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9333 struct inode *new_dir, struct dentry *new_dentry,
9334 unsigned int flags)
9335{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009336 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009337 return -EINVAL;
9338
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009339 if (flags & RENAME_EXCHANGE)
9340 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9341 new_dentry);
9342
9343 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009344}
9345
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009346struct btrfs_delalloc_work {
9347 struct inode *inode;
9348 struct completion completion;
9349 struct list_head list;
9350 struct btrfs_work work;
9351};
9352
Miao Xie8ccf6f192012-10-25 09:28:04 +00009353static void btrfs_run_delalloc_work(struct btrfs_work *work)
9354{
9355 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009356 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009357
9358 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9359 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009360 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009361 filemap_flush(inode->i_mapping);
9362 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9363 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009364 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009365
Nikolay Borisov076da912018-04-23 10:54:16 +03009366 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009367 complete(&delalloc_work->completion);
9368}
9369
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009370static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009371{
9372 struct btrfs_delalloc_work *work;
9373
David Sterba100d5702015-12-08 14:39:32 +01009374 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009375 if (!work)
9376 return NULL;
9377
9378 init_completion(&work->completion);
9379 INIT_LIST_HEAD(&work->list);
9380 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009381 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009382
9383 return work;
9384}
9385
Chris Masond352ac62008-09-29 15:18:18 -04009386/*
9387 * some fairly slow code that needs optimization. This walks the list
9388 * of all the inodes with pending delalloc and forces them to disk.
9389 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009390static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009391{
Chris Masonea8c2812008-08-04 23:17:27 -04009392 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009393 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009394 struct btrfs_delalloc_work *work, *next;
9395 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009396 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009397 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009398
Miao Xie8ccf6f192012-10-25 09:28:04 +00009399 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009400 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009401
Miao Xie573bfb72014-03-06 13:55:03 +08009402 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009403 spin_lock(&root->delalloc_lock);
9404 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009405 while (!list_empty(&splice)) {
9406 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009407 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009408
Miao Xieeb73c1b2013-05-15 07:48:22 +00009409 list_move_tail(&binode->delalloc_inodes,
9410 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009411 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009412 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009413 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009414 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009415 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009416 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009417
Ethan Lien3cd24c62018-11-01 14:49:03 +08009418 if (snapshot)
9419 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9420 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009421 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009422 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009423 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009424 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009425 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009426 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009427 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009428 btrfs_queue_work(root->fs_info->flush_workers,
9429 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009430 ret++;
9431 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009432 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009433 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009434 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009435 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009436 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009437
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009438out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009439 list_for_each_entry_safe(work, next, &works, list) {
9440 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009441 wait_for_completion(&work->completion);
9442 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009443 }
9444
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009445 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009446 spin_lock(&root->delalloc_lock);
9447 list_splice_tail(&splice, &root->delalloc_inodes);
9448 spin_unlock(&root->delalloc_lock);
9449 }
Miao Xie573bfb72014-03-06 13:55:03 +08009450 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451 return ret;
9452}
9453
Ethan Lien3cd24c62018-11-01 14:49:03 +08009454int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009456 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009457 int ret;
9458
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009459 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009460 return -EROFS;
9461
Ethan Lien3cd24c62018-11-01 14:49:03 +08009462 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009463 if (ret > 0)
9464 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009465 return ret;
9466}
9467
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009468int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009469{
9470 struct btrfs_root *root;
9471 struct list_head splice;
9472 int ret;
9473
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009474 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009475 return -EROFS;
9476
9477 INIT_LIST_HEAD(&splice);
9478
Miao Xie573bfb72014-03-06 13:55:03 +08009479 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009480 spin_lock(&fs_info->delalloc_root_lock);
9481 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009482 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009483 root = list_first_entry(&splice, struct btrfs_root,
9484 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009485 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009486 BUG_ON(!root);
9487 list_move_tail(&root->delalloc_root,
9488 &fs_info->delalloc_roots);
9489 spin_unlock(&fs_info->delalloc_root_lock);
9490
Ethan Lien3cd24c62018-11-01 14:49:03 +08009491 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009492 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009493 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009494 goto out;
9495
Miao Xie6c255e62014-03-06 13:55:01 +08009496 if (nr != -1) {
9497 nr -= ret;
9498 WARN_ON(nr < 0);
9499 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009500 spin_lock(&fs_info->delalloc_root_lock);
9501 }
9502 spin_unlock(&fs_info->delalloc_root_lock);
9503
Miao Xie6c255e62014-03-06 13:55:01 +08009504 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009505out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009506 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009507 spin_lock(&fs_info->delalloc_root_lock);
9508 list_splice_tail(&splice, &fs_info->delalloc_roots);
9509 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009510 }
Miao Xie573bfb72014-03-06 13:55:03 +08009511 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009512 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009513}
9514
Chris Mason39279cc2007-06-12 06:35:45 -04009515static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9516 const char *symname)
9517{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009518 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009519 struct btrfs_trans_handle *trans;
9520 struct btrfs_root *root = BTRFS_I(dir)->root;
9521 struct btrfs_path *path;
9522 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009523 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009524 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009525 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309526 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009527 int name_len;
9528 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009529 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009530 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009531 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009532
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009533 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009534 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009535 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009536
Josef Bacik9ed74f22009-09-11 16:12:44 -04009537 /*
9538 * 2 items for inode item and ref
9539 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009540 * 1 item for updating parent inode item
9541 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009542 * 1 item for xattr if selinux is on
9543 */
Filipe Manana9269d122015-12-31 18:16:29 +00009544 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009545 if (IS_ERR(trans))
9546 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009547
Li Zefan581bb052011-04-20 10:06:11 +08009548 err = btrfs_find_free_ino(root, &objectid);
9549 if (err)
9550 goto out_unlock;
9551
Josef Bacikaec74772008-07-24 12:12:38 -04009552 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009553 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9554 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009555 if (IS_ERR(inode)) {
9556 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009557 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009558 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009559 }
Chris Mason39279cc2007-06-12 06:35:45 -04009560
Casey Schauflerad19db72011-12-15 10:09:07 -05009561 /*
9562 * If the active LSM wants to access the inode during
9563 * d_instantiate it needs these. Smack checks to see
9564 * if the filesystem supports xattrs by looking at the
9565 * ops vector.
9566 */
9567 inode->i_fop = &btrfs_file_operations;
9568 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009569 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009570 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9571
9572 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9573 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009574 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009575
Chris Mason39279cc2007-06-12 06:35:45 -04009576 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009577 if (!path) {
9578 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009579 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009580 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009581 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009582 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009583 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009584 datasize = btrfs_file_extent_calc_inline_size(name_len);
9585 err = btrfs_insert_empty_item(trans, root, path, &key,
9586 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009587 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009588 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009589 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009590 }
Chris Mason5f39d392007-10-15 16:14:19 -04009591 leaf = path->nodes[0];
9592 ei = btrfs_item_ptr(leaf, path->slots[0],
9593 struct btrfs_file_extent_item);
9594 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9595 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009596 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009597 btrfs_set_file_extent_encryption(leaf, ei, 0);
9598 btrfs_set_file_extent_compression(leaf, ei, 0);
9599 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9600 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9601
Chris Mason39279cc2007-06-12 06:35:45 -04009602 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009603 write_extent_buffer(leaf, symname, ptr, name_len);
9604 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009605 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009606
Chris Mason39279cc2007-06-12 06:35:45 -04009607 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009608 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009609 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009610 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009611 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009612 /*
9613 * Last step, add directory indexes for our symlink inode. This is the
9614 * last step to avoid extra cleanup of these indexes if an error happens
9615 * elsewhere above.
9616 */
9617 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009618 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9619 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009620 if (err)
9621 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009622
Al Viro1e2e5472018-05-04 08:23:01 -04009623 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009624
9625out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009626 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009627 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009628 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009629 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009630 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009631 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009632 return err;
9633}
Chris Mason16432982008-04-10 10:23:21 -04009634
Qu Wenruo203f44c52020-06-10 09:04:40 +08009635static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9636 struct inode *inode, struct btrfs_key *ins,
9637 u64 file_offset)
9638{
9639 struct btrfs_file_extent_item stack_fi;
9640 u64 start = ins->objectid;
9641 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009642 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009643
9644 memset(&stack_fi, 0, sizeof(stack_fi));
9645
9646 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9647 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9648 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9649 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9650 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9651 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9652 /* Encryption and other encoding is reserved and all 0 */
9653
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009654 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009655 if (ret < 0)
9656 return ret;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009657 return insert_reserved_file_extent(trans, BTRFS_I(inode), file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009658 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009659}
Josef Bacik0af3d002010-06-21 14:48:16 -04009660static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9661 u64 start, u64 num_bytes, u64 min_size,
9662 loff_t actual_len, u64 *alloc_hint,
9663 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009664{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009665 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009666 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9667 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009668 struct btrfs_root *root = BTRFS_I(inode)->root;
9669 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009670 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009671 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009672 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009673 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009674 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009675 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009676 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009677 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009678
Josef Bacik0af3d002010-06-21 14:48:16 -04009679 if (trans)
9680 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009681 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009682 if (own_trans) {
9683 trans = btrfs_start_transaction(root, 3);
9684 if (IS_ERR(trans)) {
9685 ret = PTR_ERR(trans);
9686 break;
9687 }
Yan Zhengd899e052008-10-30 14:25:28 -04009688 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009689
Byongho Leeee221842015-12-15 01:42:10 +09009690 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009691 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009692 /*
9693 * If we are severely fragmented we could end up with really
9694 * small allocations, so if the allocator is returning small
9695 * chunks lets make its job easier by only searching for those
9696 * sized chunks.
9697 */
9698 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009699 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9700 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009701 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009702 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009703 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009704 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009705 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009706
9707 /*
9708 * We've reserved this space, and thus converted it from
9709 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9710 * from here on out we will only need to clear our reservation
9711 * for the remaining unreserved area, so advance our
9712 * clear_offset by our extent size.
9713 */
9714 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009715 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009716
Josef Bacik0b670dc2015-09-23 17:11:16 -04009717 last_alloc = ins.offset;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009718 ret = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009719 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009720 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009721 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009722 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009723 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009724 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009725 break;
9726 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009727
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009728 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009729 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009730
Josef Bacik5dc562c2012-08-17 13:14:17 -04009731 em = alloc_extent_map();
9732 if (!em) {
9733 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9734 &BTRFS_I(inode)->runtime_flags);
9735 goto next;
9736 }
9737
9738 em->start = cur_offset;
9739 em->orig_start = cur_offset;
9740 em->len = ins.offset;
9741 em->block_start = ins.objectid;
9742 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009743 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009744 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009745 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9746 em->generation = trans->transid;
9747
9748 while (1) {
9749 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009750 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009751 write_unlock(&em_tree->lock);
9752 if (ret != -EEXIST)
9753 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009754 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009755 cur_offset + ins.offset - 1,
9756 0);
9757 }
9758 free_extent_map(em);
9759next:
Yan Zhengd899e052008-10-30 14:25:28 -04009760 num_bytes -= ins.offset;
9761 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009762 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009763
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009764 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009765 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009766 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009767 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009768 (actual_len > inode->i_size) &&
9769 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009770 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009771 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009772 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009773 i_size = cur_offset;
9774 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009775 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009776 }
9777
Yan Zhengd899e052008-10-30 14:25:28 -04009778 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009779
9780 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009781 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009782 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009783 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009784 break;
9785 }
Yan Zhengd899e052008-10-30 14:25:28 -04009786
Josef Bacik0af3d002010-06-21 14:48:16 -04009787 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009788 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009789 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009790 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009791 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009792 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009793 return ret;
9794}
9795
Josef Bacik0af3d002010-06-21 14:48:16 -04009796int btrfs_prealloc_file_range(struct inode *inode, int mode,
9797 u64 start, u64 num_bytes, u64 min_size,
9798 loff_t actual_len, u64 *alloc_hint)
9799{
9800 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9801 min_size, actual_len, alloc_hint,
9802 NULL);
9803}
9804
9805int btrfs_prealloc_file_range_trans(struct inode *inode,
9806 struct btrfs_trans_handle *trans, int mode,
9807 u64 start, u64 num_bytes, u64 min_size,
9808 loff_t actual_len, u64 *alloc_hint)
9809{
9810 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9811 min_size, actual_len, alloc_hint, trans);
9812}
9813
Chris Masone6dcd2d2008-07-17 12:53:50 -04009814static int btrfs_set_page_dirty(struct page *page)
9815{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009816 return __set_page_dirty_nobuffers(page);
9817}
9818
Al Viro10556cb22011-06-20 19:28:19 -04009819static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009820{
Li Zefanb83cc962010-12-20 16:04:08 +08009821 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009822 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009823
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009824 if (mask & MAY_WRITE &&
9825 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9826 if (btrfs_root_readonly(root))
9827 return -EROFS;
9828 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9829 return -EACCES;
9830 }
Al Viro2830ba72011-06-20 19:16:29 -04009831 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009832}
Chris Mason39279cc2007-06-12 06:35:45 -04009833
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009834static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9835{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009836 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009837 struct btrfs_trans_handle *trans;
9838 struct btrfs_root *root = BTRFS_I(dir)->root;
9839 struct inode *inode = NULL;
9840 u64 objectid;
9841 u64 index;
9842 int ret = 0;
9843
9844 /*
9845 * 5 units required for adding orphan entry
9846 */
9847 trans = btrfs_start_transaction(root, 5);
9848 if (IS_ERR(trans))
9849 return PTR_ERR(trans);
9850
9851 ret = btrfs_find_free_ino(root, &objectid);
9852 if (ret)
9853 goto out;
9854
9855 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009856 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009857 if (IS_ERR(inode)) {
9858 ret = PTR_ERR(inode);
9859 inode = NULL;
9860 goto out;
9861 }
9862
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009863 inode->i_fop = &btrfs_file_operations;
9864 inode->i_op = &btrfs_file_inode_operations;
9865
9866 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009867 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9868
Chris Masonb0d5d102014-09-08 13:08:51 -07009869 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9870 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009871 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009872
9873 ret = btrfs_update_inode(trans, root, inode);
9874 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009875 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009876 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009877 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009878 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009879
Filipe Manana5762b5c2014-08-01 00:10:32 +01009880 /*
9881 * We set number of links to 0 in btrfs_new_inode(), and here we set
9882 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9883 * through:
9884 *
9885 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9886 */
9887 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009888 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009889 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009890 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009891out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009892 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009893 if (ret && inode)
9894 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009895 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009896 return ret;
9897}
9898
David Sterba5cdc84b2018-07-18 20:32:52 +02009899void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009900{
David Sterba5cdc84b2018-07-18 20:32:52 +02009901 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009902 unsigned long index = start >> PAGE_SHIFT;
9903 unsigned long end_index = end >> PAGE_SHIFT;
9904 struct page *page;
9905
9906 while (index <= end_index) {
9907 page = find_get_page(inode->i_mapping, index);
9908 ASSERT(page); /* Pages should be in the extent_io_tree */
9909 set_page_writeback(page);
9910 put_page(page);
9911 index++;
9912 }
9913}
9914
Omar Sandovaled46ff32016-11-03 10:28:14 -07009915#ifdef CONFIG_SWAP
9916/*
9917 * Add an entry indicating a block group or device which is pinned by a
9918 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9919 * negative errno on failure.
9920 */
9921static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9922 bool is_block_group)
9923{
9924 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9925 struct btrfs_swapfile_pin *sp, *entry;
9926 struct rb_node **p;
9927 struct rb_node *parent = NULL;
9928
9929 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9930 if (!sp)
9931 return -ENOMEM;
9932 sp->ptr = ptr;
9933 sp->inode = inode;
9934 sp->is_block_group = is_block_group;
9935
9936 spin_lock(&fs_info->swapfile_pins_lock);
9937 p = &fs_info->swapfile_pins.rb_node;
9938 while (*p) {
9939 parent = *p;
9940 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9941 if (sp->ptr < entry->ptr ||
9942 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9943 p = &(*p)->rb_left;
9944 } else if (sp->ptr > entry->ptr ||
9945 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9946 p = &(*p)->rb_right;
9947 } else {
9948 spin_unlock(&fs_info->swapfile_pins_lock);
9949 kfree(sp);
9950 return 1;
9951 }
9952 }
9953 rb_link_node(&sp->node, parent, p);
9954 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9955 spin_unlock(&fs_info->swapfile_pins_lock);
9956 return 0;
9957}
9958
9959/* Free all of the entries pinned by this swapfile. */
9960static void btrfs_free_swapfile_pins(struct inode *inode)
9961{
9962 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9963 struct btrfs_swapfile_pin *sp;
9964 struct rb_node *node, *next;
9965
9966 spin_lock(&fs_info->swapfile_pins_lock);
9967 node = rb_first(&fs_info->swapfile_pins);
9968 while (node) {
9969 next = rb_next(node);
9970 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9971 if (sp->inode == inode) {
9972 rb_erase(&sp->node, &fs_info->swapfile_pins);
9973 if (sp->is_block_group)
9974 btrfs_put_block_group(sp->ptr);
9975 kfree(sp);
9976 }
9977 node = next;
9978 }
9979 spin_unlock(&fs_info->swapfile_pins_lock);
9980}
9981
9982struct btrfs_swap_info {
9983 u64 start;
9984 u64 block_start;
9985 u64 block_len;
9986 u64 lowest_ppage;
9987 u64 highest_ppage;
9988 unsigned long nr_pages;
9989 int nr_extents;
9990};
9991
9992static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9993 struct btrfs_swap_info *bsi)
9994{
9995 unsigned long nr_pages;
9996 u64 first_ppage, first_ppage_reported, next_ppage;
9997 int ret;
9998
9999 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10000 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10001 PAGE_SIZE) >> PAGE_SHIFT;
10002
10003 if (first_ppage >= next_ppage)
10004 return 0;
10005 nr_pages = next_ppage - first_ppage;
10006
10007 first_ppage_reported = first_ppage;
10008 if (bsi->start == 0)
10009 first_ppage_reported++;
10010 if (bsi->lowest_ppage > first_ppage_reported)
10011 bsi->lowest_ppage = first_ppage_reported;
10012 if (bsi->highest_ppage < (next_ppage - 1))
10013 bsi->highest_ppage = next_ppage - 1;
10014
10015 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10016 if (ret < 0)
10017 return ret;
10018 bsi->nr_extents += ret;
10019 bsi->nr_pages += nr_pages;
10020 return 0;
10021}
10022
10023static void btrfs_swap_deactivate(struct file *file)
10024{
10025 struct inode *inode = file_inode(file);
10026
10027 btrfs_free_swapfile_pins(inode);
10028 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10029}
10030
10031static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10032 sector_t *span)
10033{
10034 struct inode *inode = file_inode(file);
10035 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10036 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10037 struct extent_state *cached_state = NULL;
10038 struct extent_map *em = NULL;
10039 struct btrfs_device *device = NULL;
10040 struct btrfs_swap_info bsi = {
10041 .lowest_ppage = (sector_t)-1ULL,
10042 };
10043 int ret = 0;
10044 u64 isize;
10045 u64 start;
10046
10047 /*
10048 * If the swap file was just created, make sure delalloc is done. If the
10049 * file changes again after this, the user is doing something stupid and
10050 * we don't really care.
10051 */
10052 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10053 if (ret)
10054 return ret;
10055
10056 /*
10057 * The inode is locked, so these flags won't change after we check them.
10058 */
10059 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10060 btrfs_warn(fs_info, "swapfile must not be compressed");
10061 return -EINVAL;
10062 }
10063 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10064 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10065 return -EINVAL;
10066 }
10067 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10068 btrfs_warn(fs_info, "swapfile must not be checksummed");
10069 return -EINVAL;
10070 }
10071
10072 /*
10073 * Balance or device remove/replace/resize can move stuff around from
10074 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10075 * concurrently while we are mapping the swap extents, and
10076 * fs_info->swapfile_pins prevents them from running while the swap file
10077 * is active and moving the extents. Note that this also prevents a
10078 * concurrent device add which isn't actually necessary, but it's not
10079 * really worth the trouble to allow it.
10080 */
10081 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10082 btrfs_warn(fs_info,
10083 "cannot activate swapfile while exclusive operation is running");
10084 return -EBUSY;
10085 }
10086 /*
10087 * Snapshots can create extents which require COW even if NODATACOW is
10088 * set. We use this counter to prevent snapshots. We must increment it
10089 * before walking the extents because we don't want a concurrent
10090 * snapshot to run after we've already checked the extents.
10091 */
10092 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10093
10094 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10095
10096 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10097 start = 0;
10098 while (start < isize) {
10099 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010100 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010101 u64 len = isize - start;
10102
Omar Sandoval39b07b52019-12-02 17:34:23 -080010103 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010104 if (IS_ERR(em)) {
10105 ret = PTR_ERR(em);
10106 goto out;
10107 }
10108
10109 if (em->block_start == EXTENT_MAP_HOLE) {
10110 btrfs_warn(fs_info, "swapfile must not have holes");
10111 ret = -EINVAL;
10112 goto out;
10113 }
10114 if (em->block_start == EXTENT_MAP_INLINE) {
10115 /*
10116 * It's unlikely we'll ever actually find ourselves
10117 * here, as a file small enough to fit inline won't be
10118 * big enough to store more than the swap header, but in
10119 * case something changes in the future, let's catch it
10120 * here rather than later.
10121 */
10122 btrfs_warn(fs_info, "swapfile must not be inline");
10123 ret = -EINVAL;
10124 goto out;
10125 }
10126 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10127 btrfs_warn(fs_info, "swapfile must not be compressed");
10128 ret = -EINVAL;
10129 goto out;
10130 }
10131
10132 logical_block_start = em->block_start + (start - em->start);
10133 len = min(len, em->len - (start - em->start));
10134 free_extent_map(em);
10135 em = NULL;
10136
Boris Burkova84d5d42020-08-18 11:00:05 -070010137 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010138 if (ret < 0) {
10139 goto out;
10140 } else if (ret) {
10141 ret = 0;
10142 } else {
10143 btrfs_warn(fs_info,
10144 "swapfile must not be copy-on-write");
10145 ret = -EINVAL;
10146 goto out;
10147 }
10148
10149 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10150 if (IS_ERR(em)) {
10151 ret = PTR_ERR(em);
10152 goto out;
10153 }
10154
10155 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10156 btrfs_warn(fs_info,
10157 "swapfile must have single data profile");
10158 ret = -EINVAL;
10159 goto out;
10160 }
10161
10162 if (device == NULL) {
10163 device = em->map_lookup->stripes[0].dev;
10164 ret = btrfs_add_swapfile_pin(inode, device, false);
10165 if (ret == 1)
10166 ret = 0;
10167 else if (ret)
10168 goto out;
10169 } else if (device != em->map_lookup->stripes[0].dev) {
10170 btrfs_warn(fs_info, "swapfile must be on one device");
10171 ret = -EINVAL;
10172 goto out;
10173 }
10174
10175 physical_block_start = (em->map_lookup->stripes[0].physical +
10176 (logical_block_start - em->start));
10177 len = min(len, em->len - (logical_block_start - em->start));
10178 free_extent_map(em);
10179 em = NULL;
10180
10181 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10182 if (!bg) {
10183 btrfs_warn(fs_info,
10184 "could not find block group containing swapfile");
10185 ret = -EINVAL;
10186 goto out;
10187 }
10188
10189 ret = btrfs_add_swapfile_pin(inode, bg, true);
10190 if (ret) {
10191 btrfs_put_block_group(bg);
10192 if (ret == 1)
10193 ret = 0;
10194 else
10195 goto out;
10196 }
10197
10198 if (bsi.block_len &&
10199 bsi.block_start + bsi.block_len == physical_block_start) {
10200 bsi.block_len += len;
10201 } else {
10202 if (bsi.block_len) {
10203 ret = btrfs_add_swap_extent(sis, &bsi);
10204 if (ret)
10205 goto out;
10206 }
10207 bsi.start = start;
10208 bsi.block_start = physical_block_start;
10209 bsi.block_len = len;
10210 }
10211
10212 start += len;
10213 }
10214
10215 if (bsi.block_len)
10216 ret = btrfs_add_swap_extent(sis, &bsi);
10217
10218out:
10219 if (!IS_ERR_OR_NULL(em))
10220 free_extent_map(em);
10221
10222 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10223
10224 if (ret)
10225 btrfs_swap_deactivate(file);
10226
10227 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10228
10229 if (ret)
10230 return ret;
10231
10232 if (device)
10233 sis->bdev = device->bdev;
10234 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10235 sis->max = bsi.nr_pages;
10236 sis->pages = bsi.nr_pages - 1;
10237 sis->highest_bit = bsi.nr_pages - 1;
10238 return bsi.nr_extents;
10239}
10240#else
10241static void btrfs_swap_deactivate(struct file *file)
10242{
10243}
10244
10245static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10246 sector_t *span)
10247{
10248 return -EOPNOTSUPP;
10249}
10250#endif
10251
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010252static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010253 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010254 .lookup = btrfs_lookup,
10255 .create = btrfs_create,
10256 .unlink = btrfs_unlink,
10257 .link = btrfs_link,
10258 .mkdir = btrfs_mkdir,
10259 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010260 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010261 .symlink = btrfs_symlink,
10262 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010263 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010264 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010265 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010266 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010267 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010268 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010269 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010270};
Yan, Zheng76dda932009-09-21 16:00:26 -040010271
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010272static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010273 .llseek = generic_file_llseek,
10274 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010275 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010276 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010277 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010278#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010279 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010280#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010281 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010282 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010283};
10284
David Sterba20e55062015-11-19 11:42:28 +010010285static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010286 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010287 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010288 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10289};
10290
Chris Mason35054392009-01-21 13:11:13 -050010291/*
10292 * btrfs doesn't support the bmap operation because swapfiles
10293 * use bmap to make a mapping of extents in the file. They assume
10294 * these extents won't change over the life of the file and they
10295 * use the bmap result to do IO directly to the drive.
10296 *
10297 * the btrfs bmap call would return logical addresses that aren't
10298 * suitable for IO and they also will change frequently as COW
10299 * operations happen. So, swapfile + btrfs == corruption.
10300 *
10301 * For now we're avoiding this by dropping bmap.
10302 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010303static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010304 .readpage = btrfs_readpage,
10305 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010306 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010307 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010308 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010309 .invalidatepage = btrfs_invalidatepage,
10310 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010311#ifdef CONFIG_MIGRATION
10312 .migratepage = btrfs_migratepage,
10313#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010314 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010315 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010316 .swap_activate = btrfs_swap_activate,
10317 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010318};
10319
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010320static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010321 .getattr = btrfs_getattr,
10322 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010323 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010324 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010325 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010326 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010327 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010328 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010329};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010330static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010331 .getattr = btrfs_getattr,
10332 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010333 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010334 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010335 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010336 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010337 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010338};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010339static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010340 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010341 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010342 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010343 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010344 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010345 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010346};
Yan, Zheng76dda932009-09-21 16:00:26 -040010347
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010348const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010349 .d_delete = btrfs_dentry_delete,
10350};