blob: cdd9872b66f8a800e09e3917089dfa715dac894c [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);
Chris Mason771ed682008-11-06 22:02:51 -050083static noinline int cow_file_range(struct inode *inode,
84 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);
Liu Bo6f9994d2017-01-31 07:50:22 -080087static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
88 u64 orig_start, u64 block_start,
89 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
Qu Wenruo524272602017-03-08 10:25:52 +080093static void __endio_write_update_ordered(struct inode *inode,
94 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 */
107static inline void btrfs_cleanup_ordered_extents(struct 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) {
119 page = find_get_page(inode->i_mapping, index);
120 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
137 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 Borisovd02c0e22018-03-02 09:43:15 +0200277static noinline int cow_file_range_inline(struct 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 Borisovd02c0e22018-03-02 09:43:15 +0200282 struct btrfs_root *root = BTRFS_I(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;
Chris Masonc8b97812008-10-29 14:49:59 -0400285 u64 isize = i_size_read(inode);
286 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 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400317 trans->block_rsv = &BTRFS_I(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
326 ret = __btrfs_drop_extents(trans, root, inode, path,
327 start, aligned_end, NULL,
328 1, 1, extent_item_size, &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,
337 root, 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
Josef Bacikbdc20e62013-02-28 13:23:38 -0500348 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200349 btrfs_drop_extent_cache(BTRFS_I(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 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800357 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 */
415static inline bool inode_can_compress(struct inode *inode)
416{
417 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
418 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
419 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 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300427static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800430
Qu Wenruo42c16da2019-07-01 05:12:46 +0000431 if (!inode_can_compress(inode)) {
432 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
433 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
434 btrfs_ino(BTRFS_I(inode)));
435 return 0;
436 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800437 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400438 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800439 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200440 /* defrag ioctl */
441 if (BTRFS_I(inode)->defrag_compress)
442 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800443 /* bad compression ratios */
444 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
445 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800447 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200448 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300449 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800450 return 0;
451}
452
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200453static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800454 u64 start, u64 end, u64 num_bytes, u64 small_write)
455{
456 /* If this is a small write inside eof, kick off a defrag */
457 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200458 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800459 btrfs_add_inode_defrag(NULL, inode);
460}
461
Chris Masonc8b97812008-10-29 14:49:59 -0400462/*
Chris Mason771ed682008-11-06 22:02:51 -0500463 * we create compressed extents in two phases. The first
464 * phase compresses a range of pages that have already been
465 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400466 *
Chris Mason771ed682008-11-06 22:02:51 -0500467 * This is done inside an ordered work queue, and the compression
468 * is spread across many cpus. The actual IO submission is step
469 * two, and the ordered work queue takes care of making sure that
470 * happens in the same order things were put onto the queue by
471 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400472 *
Chris Mason771ed682008-11-06 22:02:51 -0500473 * If this code finds it can't get good compression, it puts an
474 * entry onto the work queue to write the uncompressed bytes. This
475 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300476 * are written in the same order that the flusher thread sent them
477 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400478 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300479static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400480{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200481 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200484 u64 start = async_chunk->start;
485 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400486 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400487 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400488 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400489 struct page **pages = NULL;
490 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400491 unsigned long total_compressed = 0;
492 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400493 int i;
494 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400495 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300496 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400497 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400498
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200499 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
500 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400501
Josef Bacikd98da492019-10-11 09:03:54 -0400502 /*
503 * We need to save i_size before now because it could change in between
504 * us evaluating the size and assigning it. This is because we lock and
505 * unlock the page in truncate and fallocate, and then modify the i_size
506 * later on.
507 *
508 * The barriers are to emulate READ_ONCE, remove that once i_size_read
509 * does that for us.
510 */
511 barrier();
512 i_size = i_size_read(inode);
513 barrier();
514 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400515again:
516 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100518 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
519 nr_pages = min_t(unsigned long, nr_pages,
520 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400521
Chris Masonf03d9301f2009-02-04 09:31:06 -0500522 /*
523 * we don't want to send crud past the end of i_size through
524 * compression, that's just a waste of CPU time. So, if the
525 * end of the file is before the start of our current
526 * requested range of bytes, we bail out to the uncompressed
527 * cleanup code that can deal with all of this.
528 *
529 * It isn't really the fastest way to fix things, but this is a
530 * very uncommon corner.
531 */
532 if (actual_end <= start)
533 goto cleanup_and_bail_uncompressed;
534
Chris Masonc8b97812008-10-29 14:49:59 -0400535 total_compressed = actual_end - start;
536
Shilong Wang4bcbb332014-10-07 18:44:35 -0400537 /*
538 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400539 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400540 */
541 if (total_compressed <= blocksize &&
542 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
543 goto cleanup_and_bail_uncompressed;
544
David Sterba069eac72017-02-14 19:36:54 +0100545 total_compressed = min_t(unsigned long, total_compressed,
546 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400547 total_in = 0;
548 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400549
Chris Mason771ed682008-11-06 22:02:51 -0500550 /*
551 * we do compression for mount -o compress and when the
552 * inode has not been flagged as nocompress. This flag can
553 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400554 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300555 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400556 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100557 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800558 if (!pages) {
559 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100560 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800561 goto cont;
562 }
Chris Mason179e29e2007-11-01 11:28:41 -0400563
David Sterbaeec63c62017-07-17 19:41:31 +0200564 if (BTRFS_I(inode)->defrag_compress)
565 compress_type = BTRFS_I(inode)->defrag_compress;
566 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200567 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800568
Chris Mason4adaa612013-03-26 13:07:00 -0400569 /*
570 * we need to call clear_page_dirty_for_io on each
571 * page in the range. Otherwise applications with the file
572 * mmap'd can wander in and change the page contents while
573 * we are compressing them.
574 *
575 * If the compression fails for any reason, we set the pages
576 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300577 *
578 * Note that the remaining part is redirtied, the start pointer
579 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400580 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300581 if (!redirty) {
582 extent_range_clear_dirty_for_io(inode, start, end);
583 redirty = 1;
584 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200585
586 /* Compression level is applied here and only here */
587 ret = btrfs_compress_pages(
588 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800589 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100590 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100591 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800592 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100593 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400594
595 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100596 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100597 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400598 char *kaddr;
599
600 /* zero the tail end of the last page, we might be
601 * sending it down to disk
602 */
603 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800604 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400605 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300606 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800607 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400608 }
609 will_compress = 1;
610 }
611 }
Li Zefan560f7d72011-09-08 10:22:01 +0800612cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400613 if (start == 0) {
614 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300615 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400616 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500617 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400618 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200619 ret = cow_file_range_inline(inode, start, end, 0,
620 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400621 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500622 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200623 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000624 total_compressed,
625 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400626 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100627 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400628 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400629 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
630 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100631 unsigned long page_error_op;
632
Filipe Mananae6eb4312014-10-10 10:45:12 +0100633 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400634
Chris Mason771ed682008-11-06 22:02:51 -0500635 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 * inline extent creation worked or returned error,
637 * we don't need to create any more async work items.
638 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400639 *
640 * We use DO_ACCOUNTING here because we need the
641 * delalloc_release_metadata to be done _after_ we drop
642 * our outstanding extent for clearing delalloc for this
643 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500644 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300645 extent_clear_unlock_delalloc(inode, start, end, NULL,
646 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800647 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400648 PAGE_CLEAR_DIRTY |
649 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100650 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400651 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300652
653 for (i = 0; i < nr_pages; i++) {
654 WARN_ON(pages[i]->mapping);
655 put_page(pages[i]);
656 }
657 kfree(pages);
658
659 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400660 }
661 }
662
663 if (will_compress) {
664 /*
665 * we aren't doing an inline extent round the compressed size
666 * up to a block size boundary so the allocator does sane
667 * things
668 */
Qu Wenruofda28322013-02-26 08:10:22 +0000669 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400670
671 /*
672 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300673 * win, compare the page count read with the blocks on disk,
674 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400675 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300676 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300677 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300678 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700679
680 /*
681 * The async work queues will take care of doing actual
682 * allocation on disk for these compressed pages, and
683 * will submit them to the elevator.
684 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200685 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100686 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700687 compress_type);
688
Timofey Titovets11708622017-10-03 18:06:01 +0300689 if (start + total_in < end) {
690 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700691 pages = NULL;
692 cond_resched();
693 goto again;
694 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300695 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400696 }
697 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700698 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400699 /*
700 * the compression code ran but failed to make things smaller,
701 * free any pages it allocated and our page pointer array
702 */
David Sterba4d3a8002017-02-14 19:04:07 +0100703 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400704 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300705 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400706 }
707 kfree(pages);
708 pages = NULL;
709 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100710 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400711
712 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200714 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500715 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500716 }
Chris Masonc8b97812008-10-29 14:49:59 -0400717 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500718cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700719 /*
720 * No compression, but we still need to write the pages in the file
721 * we've been given so far. redirty the locked page if it corresponds
722 * to our extent and set things up for the async work queue to run
723 * cow_file_range to do the normal delalloc dance.
724 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700725 if (async_chunk->locked_page &&
726 (page_offset(async_chunk->locked_page) >= start &&
727 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200728 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700729 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700730 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700731
732 if (redirty)
733 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200734 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700735 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300736 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500737
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300738 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500739}
Chris Mason771ed682008-11-06 22:02:51 -0500740
Filipe Manana40ae8372014-10-06 22:14:24 +0100741static void free_async_extent_pages(struct async_extent *async_extent)
742{
743 int i;
744
745 if (!async_extent->pages)
746 return;
747
748 for (i = 0; i < async_extent->nr_pages; i++) {
749 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300750 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100751 }
752 kfree(async_extent->pages);
753 async_extent->nr_pages = 0;
754 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500755}
756
757/*
758 * phase two of compressed writeback. This is the ordered portion
759 * of the code, which only gets called in the order the work was
760 * queued. We walk all the async extents created by compress_file_range
761 * and send them down to the disk.
762 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200763static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500764{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200765 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400766 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500767 struct async_extent *async_extent;
768 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500769 struct btrfs_key ins;
770 struct extent_map *em;
771 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200772 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500774
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200776 while (!list_empty(&async_chunk->extents)) {
777 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500778 struct async_extent, list);
779 list_del(&async_extent->list);
780
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500781retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200782 lock_extent(io_tree, async_extent->start,
783 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500784 /* did the compression code fall back to uncompressed IO? */
785 if (!async_extent->pages) {
786 int page_started = 0;
787 unsigned long nr_written = 0;
788
Chris Mason771ed682008-11-06 22:02:51 -0500789 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200790 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300794 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500795
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100796 /* JDM XXX */
797
Chris Mason771ed682008-11-06 22:02:51 -0500798 /*
799 * if page_started, cow_file_range inserted an
800 * inline extent and took care of all the unlocking
801 * and IO for us. Otherwise, we need to submit
802 * all those pages down to the drive.
803 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500804 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200805 extent_write_locked_range(inode,
806 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500807 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500808 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500809 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700810 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200811 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500812 kfree(async_extent);
813 cond_resched();
814 continue;
815 }
816
Wang Xiaoguang18513092016-07-25 15:51:40 +0800817 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500818 async_extent->compressed_size,
819 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800820 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500821 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100822 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500823
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400824 if (ret == -ENOSPC) {
825 unlock_extent(io_tree, async_extent->start,
826 async_extent->start +
827 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800828
829 /*
830 * we need to redirty the pages if we decide to
831 * fallback to uncompressed IO, otherwise we
832 * will not submit these pages down to lower
833 * layers.
834 */
835 extent_range_redirty_for_io(inode,
836 async_extent->start,
837 async_extent->start +
838 async_extent->ram_size - 1);
839
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100840 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400841 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500842 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500843 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000844 /*
845 * here we're doing allocation and writeback of the
846 * compressed pages
847 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800848 em = create_io_em(inode, async_extent->start,
849 async_extent->ram_size, /* len */
850 async_extent->start, /* orig_start */
851 ins.objectid, /* block_start */
852 ins.offset, /* block_len */
853 ins.offset, /* orig_block_len */
854 async_extent->ram_size, /* ram_bytes */
855 async_extent->compress_type,
856 BTRFS_ORDERED_COMPRESSED);
857 if (IS_ERR(em))
858 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500859 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800860 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861
Li Zefan261507a02010-12-17 14:21:50 +0800862 ret = btrfs_add_ordered_extent_compress(inode,
863 async_extent->start,
864 ins.objectid,
865 async_extent->ram_size,
866 ins.offset,
867 BTRFS_ORDERED_COMPRESSED,
868 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100869 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200870 btrfs_drop_extent_cache(BTRFS_I(inode),
871 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100872 async_extent->start +
873 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500874 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100875 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400876 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500877
Chris Mason771ed682008-11-06 22:02:51 -0500878 /*
879 * clear dirty, set writeback and unlock the pages.
880 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400881 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400882 async_extent->start +
883 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400884 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
885 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400886 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200887 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500888 async_extent->start,
889 async_extent->ram_size,
890 ins.objectid,
891 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600892 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700893 async_chunk->write_flags,
894 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100895 struct page *p = async_extent->pages[0];
896 const u64 start = async_extent->start;
897 const u64 end = start + async_extent->ram_size - 1;
898
899 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200900 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200901
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100902 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300903 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800904 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100905 PAGE_END_WRITEBACK |
906 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100907 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100908 }
Chris Mason771ed682008-11-06 22:02:51 -0500909 alloc_hint = ins.objectid + ins.offset;
910 kfree(async_extent);
911 cond_resched();
912 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100913 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400915 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400916 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400918 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500919 async_extent->start +
920 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400921 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100922 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400923 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
924 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100925 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
926 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100927 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500929 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500930}
931
Josef Bacik4b46fce2010-05-23 11:00:55 -0400932static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
933 u64 num_bytes)
934{
935 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
936 struct extent_map *em;
937 u64 alloc_hint = 0;
938
939 read_lock(&em_tree->lock);
940 em = search_extent_mapping(em_tree, start, num_bytes);
941 if (em) {
942 /*
943 * if block start isn't an actual block number then find the
944 * first block in this inode and use that as a hint. If that
945 * block is also bogus then just don't worry about it.
946 */
947 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
948 free_extent_map(em);
949 em = search_extent_mapping(em_tree, 0, 0);
950 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
951 alloc_hint = em->block_start;
952 if (em)
953 free_extent_map(em);
954 } else {
955 alloc_hint = em->block_start;
956 free_extent_map(em);
957 }
958 }
959 read_unlock(&em_tree->lock);
960
961 return alloc_hint;
962}
963
Chris Mason771ed682008-11-06 22:02:51 -0500964/*
965 * when extent_io.c finds a delayed allocation range in the file,
966 * the call backs end up in this code. The basic idea is to
967 * allocate extents on disk for the range, and create ordered data structs
968 * in ram to track those extents.
969 *
970 * locked_page is the page that writepage had locked already. We use
971 * it to make sure we don't do extra locks or unlocks.
972 *
973 * *page_started is set to one if we unlock locked_page and do everything
974 * required to start IO on it. It may be clean and already done with
975 * IO when we return.
976 */
Josef Bacik00361582013-08-14 14:02:47 -0400977static noinline int cow_file_range(struct inode *inode,
978 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300979 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300980 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500981{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400982 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400983 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500984 u64 alloc_hint = 0;
985 u64 num_bytes;
986 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000987 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100988 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400989 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500990 struct btrfs_key ins;
991 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000992 unsigned clear_bits;
993 unsigned long page_ops;
994 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500995 int ret = 0;
996
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200997 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400998 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500999 ret = -EINVAL;
1000 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001001 }
Chris Mason771ed682008-11-06 22:02:51 -05001002
Qu Wenruofda28322013-02-26 08:10:22 +00001003 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001004 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001005 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001006
Nikolay Borisov6158e1c2017-02-20 13:50:43 +02001007 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001008
Chris Mason771ed682008-11-06 22:02:51 -05001009 if (start == 0) {
1010 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001011 ret = cow_file_range_inline(inode, start, end, 0,
1012 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001013 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001014 /*
1015 * We use DO_ACCOUNTING here because we need the
1016 * delalloc_release_metadata to be run _after_ we drop
1017 * our outstanding extent for clearing delalloc for this
1018 * range.
1019 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001020 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001021 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001022 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1023 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1025 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001026 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001028 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001029 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001032 }
1033 }
Chris Masonc8b97812008-10-29 14:49:59 -04001034
Josef Bacik4b46fce2010-05-23 11:00:55 -04001035 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001036 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1037 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001038
Filipe Manana432cd2a2020-06-08 13:32:55 +01001039 /*
1040 * Relocation relies on the relocated extents to have exactly the same
1041 * size as the original extents. Normally writeback for relocation data
1042 * extents follows a NOCOW path because relocation preallocates the
1043 * extents. However, due to an operation such as scrub turning a block
1044 * group to RO mode, it may fallback to COW mode, so we must make sure
1045 * an extent allocated during COW has exactly the requested size and can
1046 * not be split into smaller extents, otherwise relocation breaks and
1047 * fails during the stage where it updates the bytenr of file extent
1048 * items.
1049 */
1050 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1051 min_alloc_size = num_bytes;
1052 else
1053 min_alloc_size = fs_info->sectorsize;
1054
Anand Jain3752d222018-02-15 12:29:38 +08001055 while (num_bytes > 0) {
1056 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001057 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001058 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001059 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001060 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001061 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001062 cur_alloc_size = ins.offset;
1063 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001064
Chris Mason771ed682008-11-06 22:02:51 -05001065 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001066 em = create_io_em(inode, start, ins.offset, /* len */
1067 start, /* orig_start */
1068 ins.objectid, /* block_start */
1069 ins.offset, /* block_len */
1070 ins.offset, /* orig_block_len */
1071 ram_size, /* ram_bytes */
1072 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001073 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001074 if (IS_ERR(em)) {
1075 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001076 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001077 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001078 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001079
Chris Masone6dcd2d2008-07-17 12:53:50 -04001080 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001081 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001082 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001083 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001084
Yan Zheng17d217f2008-12-12 10:03:38 -05001085 if (root->root_key.objectid ==
1086 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1087 ret = btrfs_reloc_clone_csums(inode, start,
1088 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001089 /*
1090 * Only drop cache here, and process as normal.
1091 *
1092 * We must not allow extent_clear_unlock_delalloc()
1093 * at out_unlock label to free meta of this ordered
1094 * extent, as its meta should be freed by
1095 * btrfs_finish_ordered_io().
1096 *
1097 * So we must continue until @start is increased to
1098 * skip current ordered extent.
1099 */
Josef Bacik00361582013-08-14 14:02:47 -04001100 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001101 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1102 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001103 }
1104
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001105 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001106
Chris Masonc8b97812008-10-29 14:49:59 -04001107 /* we're not doing compressed IO, don't unlock the first
1108 * page (which the caller expects to stay locked), don't
1109 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001110 *
1111 * Do set the Private2 bit so we know this page was properly
1112 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001113 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001114 page_ops = unlock ? PAGE_UNLOCK : 0;
1115 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001116
Josef Bacikc2790a22013-07-29 11:20:47 -04001117 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001118 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001119 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001120 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001122 if (num_bytes < cur_alloc_size)
1123 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001124 else
Anand Jain3752d222018-02-15 12:29:38 +08001125 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001126 alloc_hint = ins.objectid + ins.offset;
1127 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001128 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001129
1130 /*
1131 * btrfs_reloc_clone_csums() error, since start is increased
1132 * extent_clear_unlock_delalloc() at out_unlock label won't
1133 * free metadata of current ordered extent, we're OK to exit.
1134 */
1135 if (ret)
1136 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001137 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001138out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001139 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001140
Filipe Mananad9f85962014-08-25 10:43:00 +01001141out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001142 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001143out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001145 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001146out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001147 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1148 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001149 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1150 PAGE_END_WRITEBACK;
1151 /*
1152 * If we reserved an extent for our delalloc range (or a subrange) and
1153 * failed to create the respective ordered extent, then it means that
1154 * when we reserved the extent we decremented the extent's size from
1155 * the data space_info's bytes_may_use counter and incremented the
1156 * space_info's bytes_reserved counter by the same amount. We must make
1157 * sure extent_clear_unlock_delalloc() does not try to decrement again
1158 * the data space_info's bytes_may_use counter, therefore we do not pass
1159 * it the flag EXTENT_CLEAR_DATA_RESV.
1160 */
1161 if (extent_reserved) {
1162 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001163 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001164 locked_page,
1165 clear_bits,
1166 page_ops);
1167 start += cur_alloc_size;
1168 if (start >= end)
1169 goto out;
1170 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001171 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001172 clear_bits | EXTENT_CLEAR_DATA_RESV,
1173 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001174 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001175}
Chris Masonc8b97812008-10-29 14:49:59 -04001176
Chris Mason771ed682008-11-06 22:02:51 -05001177/*
1178 * work queue call back to started compression on a file and pages
1179 */
1180static noinline void async_cow_start(struct btrfs_work *work)
1181{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001182 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001183 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001184
Nikolay Borisovb5326272019-03-12 17:20:25 +02001185 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001186
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001187 compressed_extents = compress_file_range(async_chunk);
1188 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001189 btrfs_add_delayed_iput(async_chunk->inode);
1190 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001191 }
Chris Mason771ed682008-11-06 22:02:51 -05001192}
1193
1194/*
1195 * work queue call back to submit previously compressed pages
1196 */
1197static noinline void async_cow_submit(struct btrfs_work *work)
1198{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001199 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1200 work);
1201 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001202 unsigned long nr_pages;
1203
Nikolay Borisovb5326272019-03-12 17:20:25 +02001204 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001205 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001206
David Sterba093258e2018-02-26 16:15:17 +01001207 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001208 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001209 5 * SZ_1M)
1210 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001211
Nikolay Borisov4546d172019-01-03 10:50:03 +02001212 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001213 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001214 * in which case we don't have anything to submit, yet we need to
1215 * always adjust ->async_delalloc_pages as its paired with the init
1216 * happening in cow_file_range_async
1217 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001218 if (async_chunk->inode)
1219 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001220}
Chris Masonc8b97812008-10-29 14:49:59 -04001221
Chris Mason771ed682008-11-06 22:02:51 -05001222static noinline void async_cow_free(struct btrfs_work *work)
1223{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001224 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001225
Nikolay Borisovb5326272019-03-12 17:20:25 +02001226 async_chunk = container_of(work, struct async_chunk, work);
1227 if (async_chunk->inode)
1228 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001229 if (async_chunk->blkcg_css)
1230 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231 /*
1232 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001233 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001234 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001235 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001236 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001237}
1238
Chris Masonec39f762019-07-10 12:28:17 -07001239static int cow_file_range_async(struct inode *inode,
1240 struct writeback_control *wbc,
1241 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001242 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001243 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001246 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001247 struct async_cow *ctx;
1248 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001249 unsigned long nr_pages;
1250 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001251 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1252 int i;
1253 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001254 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001255 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001256
Nikolay Borisov69684c52019-03-12 17:20:28 +02001257 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001258
1259 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1260 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1261 num_chunks = 1;
1262 should_compress = false;
1263 } else {
1264 should_compress = true;
1265 }
1266
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001267 nofs_flag = memalloc_nofs_save();
1268 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1269 memalloc_nofs_restore(nofs_flag);
1270
Nikolay Borisov97db1202019-03-12 17:20:24 +02001271 if (!ctx) {
1272 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1273 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1274 EXTENT_DO_ACCOUNTING;
1275 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1276 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1277 PAGE_SET_ERROR;
1278
Nikolay Borisov74e91942019-07-17 16:18:16 +03001279 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001280 clear_bits, page_ops);
1281 return -ENOMEM;
1282 }
1283
1284 async_chunk = ctx->chunks;
1285 atomic_set(&ctx->num_chunks, num_chunks);
1286
1287 for (i = 0; i < num_chunks; i++) {
1288 if (should_compress)
1289 cur_end = min(end, start + SZ_512K - 1);
1290 else
1291 cur_end = end;
1292
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001293 /*
1294 * igrab is called higher up in the call chain, take only the
1295 * lightweight reference for the callback lifetime
1296 */
1297 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001298 async_chunk[i].pending = &ctx->num_chunks;
1299 async_chunk[i].inode = inode;
1300 async_chunk[i].start = start;
1301 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001302 async_chunk[i].write_flags = write_flags;
1303 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001304
Chris Mason1d53c9e2019-07-10 12:28:16 -07001305 /*
1306 * The locked_page comes all the way from writepage and its
1307 * the original page we were actually given. As we spread
1308 * this large delalloc region across multiple async_chunk
1309 * structs, only the first struct needs a pointer to locked_page
1310 *
1311 * This way we don't need racey decisions about who is supposed
1312 * to unlock it.
1313 */
1314 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001315 /*
1316 * Depending on the compressibility, the pages might or
1317 * might not go through async. We want all of them to
1318 * be accounted against wbc once. Let's do it here
1319 * before the paths diverge. wbc accounting is used
1320 * only for foreign writeback detection and doesn't
1321 * need full accuracy. Just account the whole thing
1322 * against the first page.
1323 */
1324 wbc_account_cgroup_owner(wbc, locked_page,
1325 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001326 async_chunk[i].locked_page = locked_page;
1327 locked_page = NULL;
1328 } else {
1329 async_chunk[i].locked_page = NULL;
1330 }
1331
Chris Masonec39f762019-07-10 12:28:17 -07001332 if (blkcg_css != blkcg_root_css) {
1333 css_get(blkcg_css);
1334 async_chunk[i].blkcg_css = blkcg_css;
1335 } else {
1336 async_chunk[i].blkcg_css = NULL;
1337 }
1338
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001339 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1340 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001341
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001343 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001344
Nikolay Borisov97db1202019-03-12 17:20:24 +02001345 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001346
Chris Mason771ed682008-11-06 22:02:51 -05001347 *nr_written += nr_pages;
1348 start = cur_end + 1;
1349 }
1350 *page_started = 1;
1351 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001352}
1353
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001354static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 u64 bytenr, u64 num_bytes)
1356{
1357 int ret;
1358 struct btrfs_ordered_sum *sums;
1359 LIST_HEAD(list);
1360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001361 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001362 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001363 if (ret == 0 && list_empty(&list))
1364 return 0;
1365
1366 while (!list_empty(&list)) {
1367 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1368 list_del(&sums->list);
1369 kfree(sums);
1370 }
Liu Bo58113752018-01-31 17:09:13 -07001371 if (ret < 0)
1372 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001373 return 1;
1374}
1375
Filipe Manana467dc472020-05-27 11:16:07 +01001376static int fallback_to_cow(struct inode *inode, struct page *locked_page,
1377 const u64 start, const u64 end,
1378 int *page_started, unsigned long *nr_written)
1379{
Filipe Manana2166e5e2020-05-27 11:16:19 +01001380 const bool is_space_ino = btrfs_is_free_space_inode(BTRFS_I(inode));
Filipe Manana6bd335b2020-06-08 13:33:05 +01001381 const bool is_reloc_ino = (BTRFS_I(inode)->root->root_key.objectid ==
1382 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001383 const u64 range_bytes = end + 1 - start;
Filipe Manana467dc472020-05-27 11:16:07 +01001384 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1385 u64 range_start = start;
1386 u64 count;
1387
1388 /*
1389 * If EXTENT_NORESERVE is set it means that when the buffered write was
1390 * made we had not enough available data space and therefore we did not
1391 * reserve data space for it, since we though we could do NOCOW for the
1392 * respective file range (either there is prealloc extent or the inode
1393 * has the NOCOW bit set).
1394 *
1395 * However when we need to fallback to COW mode (because for example the
1396 * block group for the corresponding extent was turned to RO mode by a
1397 * scrub or relocation) we need to do the following:
1398 *
1399 * 1) We increment the bytes_may_use counter of the data space info.
1400 * If COW succeeds, it allocates a new data extent and after doing
1401 * that it decrements the space info's bytes_may_use counter and
1402 * increments its bytes_reserved counter by the same amount (we do
1403 * this at btrfs_add_reserved_bytes()). So we need to increment the
1404 * bytes_may_use counter to compensate (when space is reserved at
1405 * buffered write time, the bytes_may_use counter is incremented);
1406 *
1407 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1408 * that if the COW path fails for any reason, it decrements (through
1409 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1410 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001411 *
1412 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001413 * space cache inode or an inode of the data relocation tree, we must
1414 * also increment bytes_may_use of the data space_info for the same
1415 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001416 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001417 * group that contains that extent to RO mode and therefore force COW
1418 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001419 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001420 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001421 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001422 if (count > 0 || is_space_ino || is_reloc_ino) {
1423 u64 bytes = count;
Filipe Manana467dc472020-05-27 11:16:07 +01001424 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1425 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1426
Filipe Manana6bd335b2020-06-08 13:33:05 +01001427 if (is_space_ino || is_reloc_ino)
1428 bytes = range_bytes;
1429
Filipe Manana467dc472020-05-27 11:16:07 +01001430 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001431 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001432 spin_unlock(&sinfo->lock);
1433
Filipe Manana2166e5e2020-05-27 11:16:19 +01001434 if (count > 0)
1435 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1436 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001437 }
1438
1439 return cow_file_range(inode, locked_page, start, end, page_started,
1440 nr_written, 1);
1441}
1442
Chris Masond352ac62008-09-29 15:18:18 -04001443/*
1444 * when nowcow writeback call back. This checks for snapshots or COW copies
1445 * of the extents that exist in the file, and COWs the file as required.
1446 *
1447 * If no cow copies or snapshots exist, we write directly to the existing
1448 * blocks on disk
1449 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001450static noinline int run_delalloc_nocow(struct inode *inode,
1451 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001452 const u64 start, const u64 end,
1453 int *page_started, int force,
1454 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001456 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001457 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001458 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001459 u64 cow_start = (u64)-1;
1460 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001461 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001462 bool check_prev = true;
1463 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001464 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001465 bool nocow = false;
1466 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001467
1468 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001469 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001470 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001472 EXTENT_DO_ACCOUNTING |
1473 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001474 PAGE_CLEAR_DIRTY |
1475 PAGE_SET_WRITEBACK |
1476 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001477 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001478 }
Li Zefan82d59022011-04-20 10:33:24 +08001479
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001481 struct btrfs_key found_key;
1482 struct btrfs_file_extent_item *fi;
1483 struct extent_buffer *leaf;
1484 u64 extent_end;
1485 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001486 u64 num_bytes = 0;
1487 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001488 u64 ram_bytes;
1489 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001490
1491 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001492
Liu Boe4c3b2d2017-01-30 12:25:28 -08001493 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001494 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001495 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001496 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001497
1498 /*
1499 * If there is no extent for our range when doing the initial
1500 * search, then go back to the previous slot as it will be the
1501 * one containing the search offset
1502 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001503 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1504 leaf = path->nodes[0];
1505 btrfs_item_key_to_cpu(leaf, &found_key,
1506 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001507 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001508 found_key.type == BTRFS_EXTENT_DATA_KEY)
1509 path->slots[0]--;
1510 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001511 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001512next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001513 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001514 leaf = path->nodes[0];
1515 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1516 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001517 if (ret < 0) {
1518 if (cow_start != (u64)-1)
1519 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001520 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001521 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001522 if (ret > 0)
1523 break;
1524 leaf = path->nodes[0];
1525 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001526
Yan Zheng80ff3852008-10-30 14:20:02 -04001527 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001528
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001529 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001530 if (found_key.objectid > ino)
1531 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001532 /*
1533 * Keep searching until we find an EXTENT_ITEM or there are no
1534 * more extents for this inode
1535 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001536 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1537 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1538 path->slots[0]++;
1539 goto next_slot;
1540 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001541
1542 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001543 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 found_key.offset > end)
1545 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001546
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001547 /*
1548 * If the found extent starts after requested offset, then
1549 * adjust extent_end to be right before this extent begins
1550 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001551 if (found_key.offset > cur_offset) {
1552 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001553 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 goto out_check;
1555 }
Chris Masonc31f8832008-01-08 15:46:31 -05001556
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001557 /*
1558 * Found extent which begins before our range and potentially
1559 * intersect it
1560 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001561 fi = btrfs_item_ptr(leaf, path->slots[0],
1562 struct btrfs_file_extent_item);
1563 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001564
Josef Bacikcc95bef2013-04-04 14:31:27 -04001565 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001566 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1567 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001569 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001570 extent_end = found_key.offset +
1571 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001572 disk_num_bytes =
1573 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001574 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001575 * If the extent we got ends before our current offset,
1576 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001577 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001578 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001579 path->slots[0]++;
1580 goto next_slot;
1581 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001582 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001583 if (disk_bytenr == 0)
1584 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001585 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001586 if (btrfs_file_extent_compression(leaf, fi) ||
1587 btrfs_file_extent_encryption(leaf, fi) ||
1588 btrfs_file_extent_other_encoding(leaf, fi))
1589 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001590 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001591 * If extent is created before the last volume's snapshot
1592 * this implies the extent is shared, hence we can't do
1593 * nocow. This is the same check as in
1594 * btrfs_cross_ref_exist but without calling
1595 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001596 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001597 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001598 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001599 btrfs_root_last_snapshot(&root->root_item))
1600 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001601 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1602 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001603 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001604 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001605 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001606 ret = btrfs_cross_ref_exist(root, ino,
1607 found_key.offset -
1608 extent_offset, disk_bytenr);
1609 if (ret) {
1610 /*
1611 * ret could be -EIO if the above fails to read
1612 * metadata.
1613 */
1614 if (ret < 0) {
1615 if (cow_start != (u64)-1)
1616 cur_offset = cow_start;
1617 goto error;
1618 }
1619
Nikolay Borisov3e024842019-08-21 10:42:03 +03001620 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001621 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001622 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001623 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001624 disk_bytenr += cur_offset - found_key.offset;
1625 num_bytes = min(end + 1, extent_end) - cur_offset;
1626 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001627 * If there are pending snapshots for this root, we
1628 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001629 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001630 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001631 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001632 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001633 * force cow if csum exists in the range.
1634 * this ensure that csum for a given extent are
1635 * either valid or do not exist.
1636 */
Liu Bo58113752018-01-31 17:09:13 -07001637 ret = csum_exist_in_range(fs_info, disk_bytenr,
1638 num_bytes);
1639 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001640 /*
1641 * ret could be -EIO if the above fails to read
1642 * metadata.
1643 */
1644 if (ret < 0) {
1645 if (cow_start != (u64)-1)
1646 cur_offset = cow_start;
1647 goto error;
1648 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001649 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001650 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001651 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001652 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001653 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001654 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001655 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001656 extent_end = found_key.offset + ram_bytes;
1657 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001658 /* Skip extents outside of our requested range */
1659 if (extent_end <= start) {
1660 path->slots[0]++;
1661 goto next_slot;
1662 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001663 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001664 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001665 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001666 }
1667out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001668 /*
1669 * If nocow is false then record the beginning of the range
1670 * that needs to be COWed
1671 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001672 if (!nocow) {
1673 if (cow_start == (u64)-1)
1674 cow_start = cur_offset;
1675 cur_offset = extent_end;
1676 if (cur_offset > end)
1677 break;
1678 path->slots[0]++;
1679 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001680 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001681
David Sterbab3b4aa72011-04-21 01:20:15 +02001682 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001683
1684 /*
1685 * COW range from cow_start to found_key.offset - 1. As the key
1686 * will contain the beginning of the first extent that can be
1687 * NOCOW, following one which needs to be COW'ed
1688 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001689 if (cow_start != (u64)-1) {
Filipe Manana467dc472020-05-27 11:16:07 +01001690 ret = fallback_to_cow(inode, locked_page, cow_start,
1691 found_key.offset - 1,
1692 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001693 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001694 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001695 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001696 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001697
Yan Zhengd899e052008-10-30 14:25:28 -04001698 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001699 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001700 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001701
1702 em = create_io_em(inode, cur_offset, num_bytes,
1703 orig_start,
1704 disk_bytenr, /* block_start */
1705 num_bytes, /* block_len */
1706 disk_num_bytes, /* orig_block_len */
1707 ram_bytes, BTRFS_COMPRESS_NONE,
1708 BTRFS_ORDERED_PREALLOC);
1709 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001710 ret = PTR_ERR(em);
1711 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001712 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001713 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001714 ret = btrfs_add_ordered_extent(inode, cur_offset,
1715 disk_bytenr, num_bytes,
1716 num_bytes,
1717 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001718 if (ret) {
1719 btrfs_drop_extent_cache(BTRFS_I(inode),
1720 cur_offset,
1721 cur_offset + num_bytes - 1,
1722 0);
1723 goto error;
1724 }
Yan Zhengd899e052008-10-30 14:25:28 -04001725 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001726 ret = btrfs_add_ordered_extent(inode, cur_offset,
1727 disk_bytenr, num_bytes,
1728 num_bytes,
1729 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001730 if (ret)
1731 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001732 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001733
Filipe Mananaf78c4362016-05-09 13:15:41 +01001734 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001735 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001736 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001737
Yan, Zhengefa56462010-05-16 10:49:59 -04001738 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001739 BTRFS_DATA_RELOC_TREE_OBJECTID)
1740 /*
1741 * Error handled later, as we must prevent
1742 * extent_clear_unlock_delalloc() in error handler
1743 * from freeing metadata of created ordered extent.
1744 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001745 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1746 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001747
Josef Bacikc2790a22013-07-29 11:20:47 -04001748 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001749 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001750 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001751 EXTENT_DELALLOC |
1752 EXTENT_CLEAR_DATA_RESV,
1753 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1754
Yan Zheng80ff3852008-10-30 14:20:02 -04001755 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001756
1757 /*
1758 * btrfs_reloc_clone_csums() error, now we're OK to call error
1759 * handler, as metadata for created ordered extent will only
1760 * be freed by btrfs_finish_ordered_io().
1761 */
1762 if (ret)
1763 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001764 if (cur_offset > end)
1765 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001766 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001767 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001768
Robbie Ko506481b2018-10-30 18:04:04 +08001769 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001770 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001771
Yan Zheng80ff3852008-10-30 14:20:02 -04001772 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001773 cur_offset = end;
Filipe Manana467dc472020-05-27 11:16:07 +01001774 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1775 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001776 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001777 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001778 }
1779
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001780error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001781 if (nocow)
1782 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1783
Josef Bacik17ca04a2012-05-31 15:58:55 -04001784 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001785 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001786 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001787 EXTENT_DELALLOC | EXTENT_DEFRAG |
1788 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1789 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001790 PAGE_SET_WRITEBACK |
1791 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001792 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001793 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001794}
1795
Wang Shilong47059d92014-07-03 18:22:07 +08001796static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1797{
1798
1799 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1800 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1801 return 0;
1802
1803 /*
1804 * @defrag_bytes is a hint value, no spinlock held here,
1805 * if is not zero, it means the file is defragging.
1806 * Force cow if given extent needs to be defragged.
1807 */
1808 if (BTRFS_I(inode)->defrag_bytes &&
1809 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1810 EXTENT_DEFRAG, 0, NULL))
1811 return 1;
1812
1813 return 0;
1814}
1815
Chris Masond352ac62008-09-29 15:18:18 -04001816/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001817 * Function to process delayed allocation (create CoW) for ranges which are
1818 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001819 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001820int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001821 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1822 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001823{
Chris Masonbe20aa92007-12-17 20:14:01 -05001824 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001825 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001826
Wang Shilong47059d92014-07-03 18:22:07 +08001827 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001828 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001829 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001830 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001831 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001832 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001833 } else if (!inode_can_compress(inode) ||
1834 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001835 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001836 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001837 } else {
1838 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1839 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001840 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001841 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001842 }
Qu Wenruo524272602017-03-08 10:25:52 +08001843 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001844 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1845 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001846 return ret;
1847}
1848
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001849void btrfs_split_delalloc_extent(struct inode *inode,
1850 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001851{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001852 u64 size;
1853
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001854 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001855 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001856 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001857
Josef Bacikdcab6a32015-02-11 15:08:59 -05001858 size = orig->end - orig->start + 1;
1859 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001860 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001861 u64 new_size;
1862
1863 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001864 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001865 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001866 */
1867 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001868 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001869 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001870 num_extents += count_max_extents(new_size);
1871 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001872 return;
1873 }
1874
Josef Bacik9e0baf62011-07-15 15:16:44 +00001875 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001876 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001877 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001878}
1879
1880/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001881 * Handle merged delayed allocation extents so we can keep track of new extents
1882 * that are just merged onto old extents, such as when we are doing sequential
1883 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001884 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001885void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1886 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001887{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001888 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001889 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001890
Josef Bacik9ed74f22009-09-11 16:12:44 -04001891 /* not delalloc, ignore it */
1892 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001893 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001894
Josef Bacik8461a3d2015-03-13 15:12:08 -04001895 if (new->start > other->start)
1896 new_size = new->end - other->start + 1;
1897 else
1898 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001899
1900 /* we're not bigger than the max, unreserve the space and go */
1901 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1902 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001903 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001904 spin_unlock(&BTRFS_I(inode)->lock);
1905 return;
1906 }
1907
1908 /*
Josef Bacikba117212015-03-13 15:01:24 -04001909 * We have to add up either side to figure out how many extents were
1910 * accounted for before we merged into one big extent. If the number of
1911 * extents we accounted for is <= the amount we need for the new range
1912 * then we can return, otherwise drop. Think of it like this
1913 *
1914 * [ 4k][MAX_SIZE]
1915 *
1916 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1917 * need 2 outstanding extents, on one side we have 1 and the other side
1918 * we have 1 so they are == and we can return. But in this case
1919 *
1920 * [MAX_SIZE+4k][MAX_SIZE+4k]
1921 *
1922 * Each range on their own accounts for 2 extents, but merged together
1923 * they are only 3 extents worth of accounting, so we need to drop in
1924 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001925 */
Josef Bacikba117212015-03-13 15:01:24 -04001926 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001927 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001928 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001929 num_extents += count_max_extents(old_size);
1930 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001931 return;
1932
Josef Bacik9e0baf62011-07-15 15:16:44 +00001933 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001934 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001935 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001936}
1937
Miao Xieeb73c1b2013-05-15 07:48:22 +00001938static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1939 struct inode *inode)
1940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1942
Miao Xieeb73c1b2013-05-15 07:48:22 +00001943 spin_lock(&root->delalloc_lock);
1944 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1945 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1946 &root->delalloc_inodes);
1947 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1948 &BTRFS_I(inode)->runtime_flags);
1949 root->nr_delalloc_inodes++;
1950 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001951 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001952 BUG_ON(!list_empty(&root->delalloc_root));
1953 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 &fs_info->delalloc_roots);
1955 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001956 }
1957 }
1958 spin_unlock(&root->delalloc_lock);
1959}
1960
Nikolay Borisov2b877332018-04-27 12:21:51 +03001961
1962void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1963 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001964{
David Sterba3ffbd682018-06-29 10:56:42 +02001965 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001966
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001967 if (!list_empty(&inode->delalloc_inodes)) {
1968 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001969 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001970 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001971 root->nr_delalloc_inodes--;
1972 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001973 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001974 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001975 BUG_ON(list_empty(&root->delalloc_root));
1976 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001977 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001978 }
1979 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001980}
1981
1982static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1983 struct btrfs_inode *inode)
1984{
1985 spin_lock(&root->delalloc_lock);
1986 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001987 spin_unlock(&root->delalloc_lock);
1988}
1989
Chris Masond352ac62008-09-29 15:18:18 -04001990/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001991 * Properly track delayed allocation bytes in the inode and to maintain the
1992 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001993 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001994void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1995 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001996{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001997 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1998
Wang Shilong47059d92014-07-03 18:22:07 +08001999 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2000 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002001 /*
2002 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002003 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002004 * bit, which is only set or cleared with irqs on
2005 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002006 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002007 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002008 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002009 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002010 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002011
Josef Bacik8b62f872017-10-19 14:15:55 -04002012 spin_lock(&BTRFS_I(inode)->lock);
2013 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2014 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002015
Josef Bacik6a3891c2015-03-16 17:38:52 -04002016 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002017 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002018 return;
2019
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002020 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2021 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002022 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002023 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002024 if (*bits & EXTENT_DEFRAG)
2025 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002026 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002027 &BTRFS_I(inode)->runtime_flags))
2028 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002029 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002030 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002031
2032 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2033 (*bits & EXTENT_DELALLOC_NEW)) {
2034 spin_lock(&BTRFS_I(inode)->lock);
2035 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2036 state->start;
2037 spin_unlock(&BTRFS_I(inode)->lock);
2038 }
Chris Mason291d6732008-01-29 15:55:23 -05002039}
2040
Chris Masond352ac62008-09-29 15:18:18 -04002041/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002042 * Once a range is no longer delalloc this function ensures that proper
2043 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002044 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002045void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2046 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002047{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002048 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2049 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002050 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002051 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002052
Filipe Manana4a4b9642017-07-27 19:52:55 +01002053 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2054 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002055 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002056 spin_unlock(&inode->lock);
2057 }
Wang Shilong47059d92014-07-03 18:22:07 +08002058
Chris Mason75eff682008-12-15 15:54:40 -05002059 /*
2060 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002061 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002062 * bit, which is only set or cleared with irqs on
2063 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002064 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002065 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002066 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002067
Josef Bacik8b62f872017-10-19 14:15:55 -04002068 spin_lock(&inode->lock);
2069 btrfs_mod_outstanding_extents(inode, -num_extents);
2070 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002071
Josef Bacikb6d08f02013-09-27 14:57:43 -04002072 /*
2073 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002074 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002075 * error.
2076 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002077 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002078 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002079 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002080
Josef Bacik6a3891c2015-03-16 17:38:52 -04002081 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002082 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002083 return;
2084
Filipe Mananaa315e682017-03-06 23:04:20 +00002085 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2086 do_list && !(state->state & EXTENT_NORESERVE) &&
2087 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002088 btrfs_free_reserved_data_space_noquota(
2089 &inode->vfs_inode,
Filipe Manana46d4dac2020-06-09 11:19:33 +01002090 len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002091
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002092 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2093 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002094 spin_lock(&inode->lock);
2095 inode->delalloc_bytes -= len;
2096 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002097 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002098 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002099 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002100 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002101 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002102
2103 if ((state->state & EXTENT_DELALLOC_NEW) &&
2104 (*bits & EXTENT_DELALLOC_NEW)) {
2105 spin_lock(&inode->lock);
2106 ASSERT(inode->new_delalloc_bytes >= len);
2107 inode->new_delalloc_bytes -= len;
2108 spin_unlock(&inode->lock);
2109 }
Chris Mason291d6732008-01-29 15:55:23 -05002110}
2111
Chris Masond352ac62008-09-29 15:18:18 -04002112/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002113 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2114 * in a chunk's stripe. This function ensures that bios do not span a
2115 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002116 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002117 * @page - The page we are about to add to the bio
2118 * @size - size we want to add to the bio
2119 * @bio - bio we want to ensure is smaller than a stripe
2120 * @bio_flags - flags of the bio
2121 *
2122 * return 1 if page cannot be added to the bio
2123 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002124 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002125 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002126int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2127 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 struct inode *inode = page->mapping->host;
2130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002131 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002132 u64 length = 0;
2133 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002134 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002135 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002136
Chris Mason771ed682008-11-06 22:02:51 -05002137 if (bio_flags & EXTENT_BIO_COMPRESSED)
2138 return 0;
2139
Kent Overstreet4f024f32013-10-11 15:44:27 -07002140 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002141 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002142 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2143 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002144 if (ret < 0)
2145 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002146
2147 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002148 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002149 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002150}
2151
Chris Masond352ac62008-09-29 15:18:18 -04002152/*
2153 * in order to insert checksums into the metadata in large chunks,
2154 * we wait until bio submission time. All the pages in the bio are
2155 * checksummed and sums are attached onto the ordered extent record.
2156 *
2157 * At IO completion time the cums attached on the ordered extent record
2158 * are inserted into the btree
2159 */
David Sterbad0ee3932018-03-08 14:35:48 +01002160static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002161 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002162{
Josef Bacikc6100a42017-05-05 11:57:13 -04002163 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002164 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002165
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002166 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002167 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002168 return 0;
2169}
Chris Masone0156402008-04-16 11:15:20 -04002170
Chris Mason4a69a412008-11-06 22:03:00 -05002171/*
Chris Masoncad321a2008-12-17 14:51:42 -05002172 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002173 * on write, or reading the csums from the tree before a read.
2174 *
2175 * Rules about async/sync submit,
2176 * a) read: sync submit
2177 *
2178 * b) write without checksum: sync submit
2179 *
2180 * c) write with checksum:
2181 * c-1) if bio is issued by fsync: sync submit
2182 * (sync_writers != 0)
2183 *
2184 * c-2) if root is reloc root: sync submit
2185 * (only in case of buffered IO)
2186 *
2187 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002188 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002189static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002190 int mirror_num,
2191 unsigned long bio_flags)
2192
Chris Mason44b8bd72008-04-16 11:14:51 -04002193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002194 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002195 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302196 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002197 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002198 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002199 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002200
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002201 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002202
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002203 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302204 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002205
Mike Christie37226b22016-06-05 14:31:52 -05002206 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002207 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002208 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002209 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002210
Chris Masond20f7042008-12-08 16:58:54 -05002211 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002212 ret = btrfs_submit_compressed_read(inode, bio,
2213 mirror_num,
2214 bio_flags);
2215 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002216 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002217 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002218 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002219 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002220 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002221 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002222 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002223 /* csum items have already been cloned */
2224 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2225 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002226 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002227 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002228 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002229 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002230 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002231 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002232 if (ret)
2233 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002234 }
2235
Chris Mason0b86a832008-03-24 15:01:56 -04002236mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002237 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002238
2239out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002240 if (ret) {
2241 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002242 bio_endio(bio);
2243 }
Stefan Behrens61891922012-11-05 18:51:52 +01002244 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002245}
Chris Mason6885f302008-02-20 16:11:05 -05002246
Chris Masond352ac62008-09-29 15:18:18 -04002247/*
2248 * given a list of ordered sums record them in the inode. This happens
2249 * at IO completion time based on sums calculated at bio submission time.
2250 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002251static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002252 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002253{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002254 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002255 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002256
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002257 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002258 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002259 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002260 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002261 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002262 if (ret)
2263 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002264 }
2265 return 0;
2266}
2267
Josef Bacik2ac55d42010-02-03 19:33:23 +00002268int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002269 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002270 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002271{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002272 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002273 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002274 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002275}
2276
Chris Masond352ac62008-09-29 15:18:18 -04002277/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002278struct btrfs_writepage_fixup {
2279 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002280 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002281 struct btrfs_work work;
2282};
2283
Christoph Hellwigb2950862008-12-02 09:54:17 -05002284static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002285{
2286 struct btrfs_writepage_fixup *fixup;
2287 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002288 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002289 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002290 struct page *page;
2291 struct inode *inode;
2292 u64 page_start;
2293 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002294 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002295 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002296
2297 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2298 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002299 inode = fixup->inode;
2300 page_start = page_offset(page);
2301 page_end = page_offset(page) + PAGE_SIZE - 1;
2302
2303 /*
2304 * This is similar to page_mkwrite, we need to reserve the space before
2305 * we take the page lock.
2306 */
2307 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2308 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002309again:
Chris Mason247e7432008-07-17 12:53:51 -04002310 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002311
Chris Mason25f3c502020-01-21 11:51:42 -05002312 /*
2313 * Before we queued this fixup, we took a reference on the page.
2314 * page->mapping may go NULL, but it shouldn't be moved to a different
2315 * address space.
2316 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002317 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2318 /*
2319 * Unfortunately this is a little tricky, either
2320 *
2321 * 1) We got here and our page had already been dealt with and
2322 * we reserved our space, thus ret == 0, so we need to just
2323 * drop our space reservation and bail. This can happen the
2324 * first time we come into the fixup worker, or could happen
2325 * while waiting for the ordered extent.
2326 * 2) Our page was already dealt with, but we happened to get an
2327 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2328 * this case we obviously don't have anything to release, but
2329 * because the page was already dealt with we don't want to
2330 * mark the page with an error, so make sure we're resetting
2331 * ret to 0. This is why we have this check _before_ the ret
2332 * check, because we do not want to have a surprise ENOSPC
2333 * when the page was already properly dealt with.
2334 */
2335 if (!ret) {
2336 btrfs_delalloc_release_extents(BTRFS_I(inode),
2337 PAGE_SIZE);
2338 btrfs_delalloc_release_space(inode, data_reserved,
2339 page_start, PAGE_SIZE,
2340 true);
2341 }
2342 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002343 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002344 }
Chris Mason25f3c502020-01-21 11:51:42 -05002345
2346 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002347 * We can't mess with the page state unless it is locked, so now that
2348 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002349 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002350 if (ret)
2351 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002352
David Sterbaff13db42015-12-03 14:30:40 +01002353 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002354 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002355
2356 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002357 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002358 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002359
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002360 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002361 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002362 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002363 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002364 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002365 unlock_page(page);
2366 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002367 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002368 goto again;
2369 }
Chris Mason247e7432008-07-17 12:53:51 -04002370
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002371 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002372 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002373 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002374 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002375
Chris Mason25f3c502020-01-21 11:51:42 -05002376 /*
2377 * Everything went as planned, we're now the owner of a dirty page with
2378 * delayed allocation bits set and space reserved for our COW
2379 * destination.
2380 *
2381 * The page was dirty when we started, nothing should have cleaned it.
2382 */
2383 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002384 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002385out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002386 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002387 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002388 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2389 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002390 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002391 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002392out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002393 if (ret) {
2394 /*
2395 * We hit ENOSPC or other errors. Update the mapping and page
2396 * to reflect the errors and clean the page.
2397 */
2398 mapping_set_error(page->mapping, ret);
2399 end_extent_writepage(page, ret, page_start, page_end);
2400 clear_page_dirty_for_io(page);
2401 SetPageError(page);
2402 }
2403 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002404 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002405 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002406 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002407 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002408 /*
2409 * As a precaution, do a delayed iput in case it would be the last iput
2410 * that could need flushing space. Recursing back to fixup worker would
2411 * deadlock.
2412 */
2413 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002414}
2415
2416/*
2417 * There are a few paths in the higher layers of the kernel that directly
2418 * set the page dirty bit without asking the filesystem if it is a
2419 * good idea. This causes problems because we want to make sure COW
2420 * properly happens and the data=ordered rules are followed.
2421 *
Chris Masonc8b97812008-10-29 14:49:59 -04002422 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002423 * hasn't been properly setup for IO. We kick off an async process
2424 * to fix it up. The async helper will wait for ordered extents, set
2425 * the delalloc bit and make it safe to write the page.
2426 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002427int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002428{
2429 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002431 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002432
Chris Mason8b62b722009-09-02 16:53:46 -04002433 /* this page is properly in the ordered list */
2434 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002435 return 0;
2436
Chris Mason25f3c502020-01-21 11:51:42 -05002437 /*
2438 * PageChecked is set below when we create a fixup worker for this page,
2439 * don't try to create another one if we're already PageChecked()
2440 *
2441 * The extent_io writepage code will redirty the page if we send back
2442 * EAGAIN.
2443 */
Chris Mason247e7432008-07-17 12:53:51 -04002444 if (PageChecked(page))
2445 return -EAGAIN;
2446
2447 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2448 if (!fixup)
2449 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002450
Josef Bacikf4b13632020-01-21 14:34:52 -05002451 /*
2452 * We are already holding a reference to this inode from
2453 * write_cache_pages. We need to hold it because the space reservation
2454 * takes place outside of the page lock, and we can't trust
2455 * page->mapping outside of the page lock.
2456 */
2457 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002458 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002459 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002460 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002461 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002462 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002463 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002464
2465 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002466}
2467
Yan Zhengd899e052008-10-30 14:25:28 -04002468static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2469 struct inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002470 struct btrfs_file_extent_item *stack_fi,
2471 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002472{
2473 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002474 struct btrfs_path *path;
2475 struct extent_buffer *leaf;
2476 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002477 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2478 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2479 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2480 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002481 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002482 int ret;
2483
2484 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002485 if (!path)
2486 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002487
Chris Masona1ed8352009-09-11 12:27:37 -04002488 /*
2489 * we may be replacing one extent in the tree with another.
2490 * The new extent is pinned in the extent map, and we don't want
2491 * to drop it from the cache until it is completely in the btree.
2492 *
2493 * So, tell btrfs_drop_extents to leave this extent in the cache.
2494 * the caller is expected to unpin it and allow it to be merged
2495 * with the others.
2496 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002497 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2498 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002499 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002500 if (ret)
2501 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002502
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002503 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002504 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002505 ins.offset = file_pos;
2506 ins.type = BTRFS_EXTENT_DATA_KEY;
2507
2508 path->leave_spinning = 1;
2509 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002510 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002511 if (ret)
2512 goto out;
2513 }
Yan Zhengd899e052008-10-30 14:25:28 -04002514 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002515 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2516 write_extent_buffer(leaf, stack_fi,
2517 btrfs_item_ptr_offset(leaf, path->slots[0]),
2518 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002519
Yan Zhengd899e052008-10-30 14:25:28 -04002520 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002521 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002522
2523 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002524
2525 ins.objectid = disk_bytenr;
2526 ins.offset = disk_num_bytes;
2527 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002528
Josef Bacik9ddc9592020-01-17 09:02:22 -05002529 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos,
2530 ram_bytes);
2531 if (ret)
2532 goto out;
2533
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002534 ret = btrfs_alloc_reserved_file_extent(trans, root,
2535 btrfs_ino(BTRFS_I(inode)),
Qu Wenruo9729f102020-06-10 09:04:41 +08002536 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002537out:
Yan Zhengd899e052008-10-30 14:25:28 -04002538 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002539
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002540 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002541}
2542
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002543static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002544 u64 start, u64 len)
2545{
David Sterba32da53862019-10-29 19:20:18 +01002546 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002547
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002548 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002549 ASSERT(cache);
2550
2551 spin_lock(&cache->lock);
2552 cache->delalloc_bytes -= len;
2553 spin_unlock(&cache->lock);
2554
2555 btrfs_put_block_group(cache);
2556}
2557
Qu Wenruo203f44c52020-06-10 09:04:40 +08002558static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2559 struct inode *inode,
2560 struct btrfs_ordered_extent *oe)
2561{
2562 struct btrfs_file_extent_item stack_fi;
2563 u64 logical_len;
2564
2565 memset(&stack_fi, 0, sizeof(stack_fi));
2566 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2567 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2568 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2569 oe->disk_num_bytes);
2570 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2571 logical_len = oe->truncated_len;
2572 else
2573 logical_len = oe->num_bytes;
2574 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2575 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2576 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2577 /* Encryption and other encoding is reserved and all 0 */
2578
2579 return insert_reserved_file_extent(trans, inode, oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002580 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002581}
2582
2583/*
2584 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002585 * an ordered extent if the range of bytes in the file it covers are
2586 * fully written.
2587 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002588static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002589{
Josef Bacik5fd02042012-05-02 14:00:54 -04002590 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002591 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002592 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002593 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002594 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002595 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002596 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002597 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002598 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002599 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002600 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002601 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002602 bool range_locked = false;
2603 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002604 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002605 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002606
Omar Sandovalbffe6332019-12-02 17:34:19 -08002607 start = ordered_extent->file_offset;
2608 end = start + ordered_extent->num_bytes - 1;
2609
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002610 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2611 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2612 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2613 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002614
Nikolay Borisov8d510122019-10-08 20:43:06 +03002615 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002616
Josef Bacik5fd02042012-05-02 14:00:54 -04002617 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2618 ret = -EIO;
2619 goto out;
2620 }
2621
Omar Sandovalbffe6332019-12-02 17:34:19 -08002622 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002623
Josef Bacik77cef2e2013-08-29 13:57:21 -04002624 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2625 truncated = true;
2626 logical_len = ordered_extent->truncated_len;
2627 /* Truncated the entire extent, don't bother adding */
2628 if (!logical_len)
2629 goto out;
2630 }
2631
Yan, Zhengc2167752009-11-12 09:34:21 +00002632 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002633 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002634
Josef Bacikd923afe2020-01-17 09:02:23 -05002635 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002636 if (freespace_inode)
2637 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002638 else
2639 trans = btrfs_join_transaction(root);
2640 if (IS_ERR(trans)) {
2641 ret = PTR_ERR(trans);
2642 trans = NULL;
2643 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002644 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002645 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002646 ret = btrfs_update_inode_fallback(trans, root, inode);
2647 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002648 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002649 goto out;
2650 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002651
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002652 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002653 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002654
Nikolay Borisov8d510122019-10-08 20:43:06 +03002655 if (freespace_inode)
2656 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002657 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002658 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002659 if (IS_ERR(trans)) {
2660 ret = PTR_ERR(trans);
2661 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002662 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002663 }
Chris Masona79b7d42014-05-22 16:18:52 -07002664
Josef Bacik69fe2d72017-10-19 14:15:57 -04002665 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002666
Chris Masonc8b97812008-10-29 14:49:59 -04002667 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002668 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002669 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002670 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002671 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002672 ordered_extent->file_offset,
2673 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002674 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002675 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002676 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002677 ret = insert_ordered_extent_file_extent(trans, inode,
2678 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002679 if (!ret) {
2680 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002681 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002682 ordered_extent->disk_bytenr,
2683 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002684 }
Yan Zhengd899e052008-10-30 14:25:28 -04002685 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002686 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002687 ordered_extent->file_offset,
2688 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002691 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002693
Nikolay Borisovac01f262018-01-08 10:59:43 +02002694 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2695 if (ret) {
2696 btrfs_abort_transaction(trans, ret);
2697 goto out;
2698 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002699
Josef Bacikd923afe2020-01-17 09:02:23 -05002700 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002701 ret = btrfs_update_inode_fallback(trans, root, inode);
2702 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002703 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002704 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002705 }
2706 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002707out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002708 clear_bits = EXTENT_DEFRAG;
2709 if (range_locked)
2710 clear_bits |= EXTENT_LOCKED;
2711 if (clear_new_delalloc_bytes)
2712 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002713 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2714 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002715 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002716
Miao Xiea698d0752012-09-20 01:51:59 -06002717 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002718 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002719
Josef Bacik77cef2e2013-08-29 13:57:21 -04002720 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002721 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002722
2723 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002724 unwritten_start += logical_len;
2725 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002726
2727 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002728 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002729
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002730 /*
2731 * If the ordered extent had an IOERR or something else went
2732 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 * back to the allocator. We only free the extent in the
2734 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002735 *
2736 * If we made it past insert_reserved_file_extent before we
2737 * errored out then we don't need to do this as the accounting
2738 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002739 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002740 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002741 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002742 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002743 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2744 /*
2745 * Discard the range before returning it back to the
2746 * free space pool
2747 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002748 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002749 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002750 ordered_extent->disk_bytenr,
2751 ordered_extent->disk_num_bytes,
2752 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002753 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002754 ordered_extent->disk_bytenr,
2755 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002756 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002757 }
2758
Josef Bacik5fd02042012-05-02 14:00:54 -04002759 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002760 * This needs to be done to make sure anybody waiting knows we are done
2761 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 */
2763 btrfs_remove_ordered_extent(inode, ordered_extent);
2764
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765 /* once for us */
2766 btrfs_put_ordered_extent(ordered_extent);
2767 /* once for the tree */
2768 btrfs_put_ordered_extent(ordered_extent);
2769
Josef Bacik5fd02042012-05-02 14:00:54 -04002770 return ret;
2771}
2772
2773static void finish_ordered_fn(struct btrfs_work *work)
2774{
2775 struct btrfs_ordered_extent *ordered_extent;
2776 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2777 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002778}
2779
Nikolay Borisovc6297322018-11-08 10:18:08 +02002780void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2781 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002782{
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002785 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002786 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002787
liubo1abe9b82011-03-24 11:18:59 +00002788 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2789
Chris Mason8b62b722009-09-02 16:53:46 -04002790 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002791 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2792 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002793 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002794
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002795 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002796 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002797 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002798 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002799
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002800 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002801 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002802}
2803
Omar Sandoval47df7762020-04-16 14:46:17 -07002804static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2805 int icsum, struct page *page, int pgoff, u64 start,
2806 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002807{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2809 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002810 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002811 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2812 u8 *csum_expected;
2813 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002814
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002815 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002816
2817 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002818 shash->tfm = fs_info->csum_shash;
2819
Eric Biggersfd080012020-04-30 23:51:59 -07002820 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002821
2822 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002823 goto zeroit;
2824
2825 kunmap_atomic(kaddr);
2826 return 0;
2827zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002828 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2829 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002830 memset(kaddr + pgoff, 1, len);
2831 flush_dcache_page(page);
2832 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002833 return -EIO;
2834}
2835
Chris Masond352ac62008-09-29 15:18:18 -04002836/*
Chris Masond352ac62008-09-29 15:18:18 -04002837 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002838 * if there's a match, we allow the bio to finish. If not, the code in
2839 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002840 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002841static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2842 u64 phy_offset, struct page *page,
2843 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002844{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002845 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002846 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002847 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002848 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002849
Chris Masond20f7042008-12-08 16:58:54 -05002850 if (PageChecked(page)) {
2851 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002852 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002853 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002854
2855 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002856 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002857
Yan Zheng17d217f2008-12-12 10:03:38 -05002858 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002859 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002860 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002861 return 0;
2862 }
2863
Miao Xiefacc8a222013-07-25 19:22:34 +08002864 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002865 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2866 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002867}
Chris Masonb888db22007-08-27 16:49:44 -04002868
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002869/*
2870 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2871 *
2872 * @inode: The inode we want to perform iput on
2873 *
2874 * This function uses the generic vfs_inode::i_count to track whether we should
2875 * just decrement it (in case it's > 1) or if this is the last iput then link
2876 * the inode to the delayed iput machinery. Delayed iputs are processed at
2877 * transaction commit time/superblock commit/cleaner kthread.
2878 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002879void btrfs_add_delayed_iput(struct inode *inode)
2880{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002882 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002883
2884 if (atomic_add_unless(&inode->i_count, -1, 1))
2885 return;
2886
Josef Bacik034f7842018-12-03 11:06:52 -05002887 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002889 ASSERT(list_empty(&binode->delayed_iput));
2890 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002891 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002892 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2893 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002894}
2895
Josef Bacik63611e72019-06-18 10:59:18 -04002896static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2897 struct btrfs_inode *inode)
2898{
2899 list_del_init(&inode->delayed_iput);
2900 spin_unlock(&fs_info->delayed_iput_lock);
2901 iput(&inode->vfs_inode);
2902 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2903 wake_up(&fs_info->delayed_iputs_wait);
2904 spin_lock(&fs_info->delayed_iput_lock);
2905}
2906
2907static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2908 struct btrfs_inode *inode)
2909{
2910 if (!list_empty(&inode->delayed_iput)) {
2911 spin_lock(&fs_info->delayed_iput_lock);
2912 if (!list_empty(&inode->delayed_iput))
2913 run_delayed_iput_locked(fs_info, inode);
2914 spin_unlock(&fs_info->delayed_iput_lock);
2915 }
2916}
2917
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002918void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002919{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002920
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002921 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002922 while (!list_empty(&fs_info->delayed_iputs)) {
2923 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002924
David Sterba8089fe62015-11-19 14:15:51 +01002925 inode = list_first_entry(&fs_info->delayed_iputs,
2926 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002927 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002928 }
David Sterba8089fe62015-11-19 14:15:51 +01002929 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002930}
2931
Josef Bacik034f7842018-12-03 11:06:52 -05002932/**
2933 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2934 * @fs_info - the fs_info for this fs
2935 * @return - EINTR if we were killed, 0 if nothing's pending
2936 *
2937 * This will wait on any delayed iputs that are currently running with KILLABLE
2938 * set. Once they are all done running we will return, unless we are killed in
2939 * which case we return EINTR. This helps in user operations like fallocate etc
2940 * that might get blocked on the iputs.
2941 */
2942int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2943{
2944 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2945 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2946 if (ret)
2947 return -EINTR;
2948 return 0;
2949}
2950
Yan, Zhengd68fc572010-05-16 10:49:58 -04002951/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002952 * This creates an orphan entry for the given inode in case something goes wrong
2953 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002954 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002955int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002956 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002957{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002959
Omar Sandoval27919062018-05-11 13:13:37 -07002960 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2961 if (ret && ret != -EEXIST) {
2962 btrfs_abort_transaction(trans, ret);
2963 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002964 }
2965
Yan, Zhengd68fc572010-05-16 10:49:58 -04002966 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002967}
2968
2969/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002970 * We have done the delete so we can go ahead and remove the orphan item for
2971 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002972 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002973static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002974 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002975{
Omar Sandoval27919062018-05-11 13:13:37 -07002976 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002977}
2978
2979/*
2980 * this cleans up any orphans that may be left on the list from the last use
2981 * of this root.
2982 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002983int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002984{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002985 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002986 struct btrfs_path *path;
2987 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002988 struct btrfs_key key, found_key;
2989 struct btrfs_trans_handle *trans;
2990 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002991 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002992 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002993
Yan, Zhengd68fc572010-05-16 10:49:58 -04002994 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002995 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002996
2997 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002998 if (!path) {
2999 ret = -ENOMEM;
3000 goto out;
3001 }
David Sterbae4058b52015-11-27 16:31:35 +01003002 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003003
3004 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003005 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003006 key.offset = (u64)-1;
3007
Josef Bacik7b128762008-07-24 12:17:14 -04003008 while (1) {
3009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003010 if (ret < 0)
3011 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003012
3013 /*
3014 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003015 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003016 * find the key and see if we have stuff that matches
3017 */
3018 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003019 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003020 if (path->slots[0] == 0)
3021 break;
3022 path->slots[0]--;
3023 }
3024
3025 /* pull out the item */
3026 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003027 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3028
3029 /* make sure the item matches what we want */
3030 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3031 break;
David Sterba962a2982014-06-04 18:41:45 +02003032 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003033 break;
3034
3035 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003036 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003037
3038 /*
3039 * this is where we are basically btrfs_lookup, without the
3040 * crossing root thing. we store the inode number in the
3041 * offset of the orphan item.
3042 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003043
3044 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003045 btrfs_err(fs_info,
3046 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003047 ret = -EINVAL;
3048 goto out;
3049 }
3050
3051 last_objectid = found_key.offset;
3052
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003053 found_key.objectid = found_key.offset;
3054 found_key.type = BTRFS_INODE_ITEM_KEY;
3055 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003056 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303057 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003058 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003059 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003060
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003061 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003062 struct btrfs_root *dead_root;
3063 struct btrfs_fs_info *fs_info = root->fs_info;
3064 int is_dead_root = 0;
3065
3066 /*
3067 * this is an orphan in the tree root. Currently these
3068 * could come from 2 sources:
3069 * a) a snapshot deletion in progress
3070 * b) a free space cache inode
3071 * We need to distinguish those two, as the snapshot
3072 * orphan must not get deleted.
3073 * find_dead_roots already ran before us, so if this
3074 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003075 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003076 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003077
3078 spin_lock(&fs_info->fs_roots_radix_lock);
3079 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3080 (unsigned long)found_key.objectid);
3081 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3082 is_dead_root = 1;
3083 spin_unlock(&fs_info->fs_roots_radix_lock);
3084
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003085 if (is_dead_root) {
3086 /* prevent this orphan from being found again */
3087 key.offset = found_key.objectid - 1;
3088 continue;
3089 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003090
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003091 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003092
Josef Bacika8c9e572011-09-21 16:55:59 -04003093 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003094 * If we have an inode with links, there are a couple of
3095 * possibilities. Old kernels (before v3.12) used to create an
3096 * orphan item for truncate indicating that there were possibly
3097 * extent items past i_size that needed to be deleted. In v3.12,
3098 * truncate was changed to update i_size in sync with the extent
3099 * items, but the (useless) orphan item was still created. Since
3100 * v4.18, we don't create the orphan item for truncate at all.
3101 *
3102 * So, this item could mean that we need to do a truncate, but
3103 * only if this filesystem was last used on a pre-v3.12 kernel
3104 * and was not cleanly unmounted. The odds of that are quite
3105 * slim, and it's a pain to do the truncate now, so just delete
3106 * the orphan item.
3107 *
3108 * It's also possible that this orphan item was supposed to be
3109 * deleted but wasn't. The inode number may have been reused,
3110 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003111 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003112 if (ret == -ENOENT || inode->i_nlink) {
3113 if (!ret)
3114 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003115 trans = btrfs_start_transaction(root, 1);
3116 if (IS_ERR(trans)) {
3117 ret = PTR_ERR(trans);
3118 goto out;
3119 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003120 btrfs_debug(fs_info, "auto deleting %Lu",
3121 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003122 ret = btrfs_del_orphan_item(trans, root,
3123 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003124 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003125 if (ret)
3126 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003127 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003128 }
Josef Bacik7b128762008-07-24 12:17:14 -04003129
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003130 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003131
3132 /* this will do delete_inode and everything for us */
3133 iput(inode);
3134 }
Miao Xie3254c872011-11-10 20:45:05 -05003135 /* release the path since we're done with it */
3136 btrfs_release_path(path);
3137
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3139
Omar Sandovala575cee2018-05-11 13:13:38 -07003140 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003141 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003142 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003143 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003144 }
Josef Bacik7b128762008-07-24 12:17:14 -04003145
3146 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003147 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003148
3149out:
3150 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003151 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003152 btrfs_free_path(path);
3153 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003154}
3155
Chris Masond352ac62008-09-29 15:18:18 -04003156/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003157 * very simple check to peek ahead in the leaf looking for xattrs. If we
3158 * don't find any xattrs, we know there can't be any acls.
3159 *
3160 * slot is the slot the inode is in, objectid is the objectid of the inode
3161 */
3162static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003163 int slot, u64 objectid,
3164 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003165{
3166 u32 nritems = btrfs_header_nritems(leaf);
3167 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003168 static u64 xattr_access = 0;
3169 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003170 int scanned = 0;
3171
Josef Bacikf23b5a52013-06-19 10:16:26 -04003172 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003173 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3174 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3175 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3176 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003177 }
3178
Chris Mason46a53cc2009-04-27 11:47:50 -04003179 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003180 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003181 while (slot < nritems) {
3182 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3183
3184 /* we found a different objectid, there must not be acls */
3185 if (found_key.objectid != objectid)
3186 return 0;
3187
3188 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003189 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003190 if (*first_xattr_slot == -1)
3191 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003192 if (found_key.offset == xattr_access ||
3193 found_key.offset == xattr_default)
3194 return 1;
3195 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003196
3197 /*
3198 * we found a key greater than an xattr key, there can't
3199 * be any acls later on
3200 */
3201 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3202 return 0;
3203
3204 slot++;
3205 scanned++;
3206
3207 /*
3208 * it goes inode, inode backrefs, xattrs, extents,
3209 * so if there are a ton of hard links to an inode there can
3210 * be a lot of backrefs. Don't waste time searching too hard,
3211 * this is just an optimization
3212 */
3213 if (scanned >= 8)
3214 break;
3215 }
3216 /* we hit the end of the leaf before we found an xattr or
3217 * something larger than an xattr. We have to assume the inode
3218 * has acls
3219 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003220 if (*first_xattr_slot == -1)
3221 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003222 return 1;
3223}
3224
3225/*
Chris Masond352ac62008-09-29 15:18:18 -04003226 * read an inode from the btree into the in-memory inode
3227 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003228static int btrfs_read_locked_inode(struct inode *inode,
3229 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003230{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003232 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003233 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003234 struct btrfs_inode_item *inode_item;
3235 struct btrfs_root *root = BTRFS_I(inode)->root;
3236 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003237 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003238 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003239 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003240 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003241 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003242 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003243
3244 ret = btrfs_fill_inode(inode, &rdev);
3245 if (!ret)
3246 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003247
Filipe Manana4222ea72018-10-24 10:13:03 +01003248 if (!path) {
3249 path = btrfs_alloc_path();
3250 if (!path)
3251 return -ENOMEM;
3252 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003253
Chris Mason39279cc2007-06-12 06:35:45 -04003254 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003255
Chris Mason39279cc2007-06-12 06:35:45 -04003256 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003257 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003258 if (path != in_path)
3259 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003260 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003261 }
Chris Mason39279cc2007-06-12 06:35:45 -04003262
Chris Mason5f39d392007-10-15 16:14:19 -04003263 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003264
3265 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003266 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003267
Chris Mason5f39d392007-10-15 16:14:19 -04003268 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3269 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003270 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003271 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003272 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3273 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003274 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003275 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3276 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003277
David Sterbaa937b972014-12-12 17:39:12 +01003278 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3279 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003280
David Sterbaa937b972014-12-12 17:39:12 +01003281 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3282 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003283
David Sterbaa937b972014-12-12 17:39:12 +01003284 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3285 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003286
chandan r9cc97d62012-07-04 12:48:07 +05303287 BTRFS_I(inode)->i_otime.tv_sec =
3288 btrfs_timespec_sec(leaf, &inode_item->otime);
3289 BTRFS_I(inode)->i_otime.tv_nsec =
3290 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003291
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003292 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003293 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003294 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3295
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003296 inode_set_iversion_queried(inode,
3297 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003298 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003299 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003300 rdev = btrfs_inode_rdev(leaf, inode_item);
3301
Josef Bacikaec74772008-07-24 12:12:38 -04003302 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003303 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003304
3305cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003306 /*
3307 * If we were modified in the current generation and evicted from memory
3308 * and then re-read we need to do a full sync since we don't have any
3309 * idea about which extents were modified before we were evicted from
3310 * cache.
3311 *
3312 * This is required for both inode re-read from disk and delayed inode
3313 * in delayed_nodes_tree.
3314 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003315 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003316 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3317 &BTRFS_I(inode)->runtime_flags);
3318
Filipe Mananabde6c242015-07-24 00:00:19 +01003319 /*
3320 * We don't persist the id of the transaction where an unlink operation
3321 * against the inode was last made. So here we assume the inode might
3322 * have been evicted, and therefore the exact value of last_unlink_trans
3323 * lost, and set it to last_trans to avoid metadata inconsistencies
3324 * between the inode and its parent if the inode is fsync'ed and the log
3325 * replayed. For example, in the scenario:
3326 *
3327 * touch mydir/foo
3328 * ln mydir/foo mydir/bar
3329 * sync
3330 * unlink mydir/bar
3331 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3332 * xfs_io -c fsync mydir/foo
3333 * <power failure>
3334 * mount fs, triggers fsync log replay
3335 *
3336 * We must make sure that when we fsync our inode foo we also log its
3337 * parent inode, otherwise after log replay the parent still has the
3338 * dentry with the "bar" name but our inode foo has a link count of 1
3339 * and doesn't have an inode ref with the name "bar" anymore.
3340 *
3341 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003342 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003343 * transaction commits on fsync if our inode is a directory, or if our
3344 * inode is not a directory, logging its parent unnecessarily.
3345 */
3346 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3347
Miao Xie67de1172013-12-26 13:07:06 +08003348 path->slots[0]++;
3349 if (inode->i_nlink != 1 ||
3350 path->slots[0] >= btrfs_header_nritems(leaf))
3351 goto cache_acl;
3352
3353 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003354 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003355 goto cache_acl;
3356
3357 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3358 if (location.type == BTRFS_INODE_REF_KEY) {
3359 struct btrfs_inode_ref *ref;
3360
3361 ref = (struct btrfs_inode_ref *)ptr;
3362 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3363 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3364 struct btrfs_inode_extref *extref;
3365
3366 extref = (struct btrfs_inode_extref *)ptr;
3367 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3368 extref);
3369 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003370cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003371 /*
3372 * try to precache a NULL acl entry for files that don't have
3373 * any xattrs or acls
3374 */
Li Zefan33345d012011-04-20 10:31:50 +08003375 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003376 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003377 if (first_xattr_slot != -1) {
3378 path->slots[0] = first_xattr_slot;
3379 ret = btrfs_load_inode_props(inode, path);
3380 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003382 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003383 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003384 root->root_key.objectid, ret);
3385 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003386 if (path != in_path)
3387 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003388
Al Viro72c04902009-06-24 16:58:48 -04003389 if (!maybe_acls)
3390 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003391
Chris Mason39279cc2007-06-12 06:35:45 -04003392 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003393 case S_IFREG:
3394 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003395 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 inode->i_fop = &btrfs_file_operations;
3397 inode->i_op = &btrfs_file_inode_operations;
3398 break;
3399 case S_IFDIR:
3400 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003401 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 break;
3403 case S_IFLNK:
3404 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003405 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003406 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003408 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003409 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003410 init_special_inode(inode, inode->i_mode, rdev);
3411 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003412 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003413
David Sterba7b6a2212018-03-26 18:40:21 +02003414 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003415 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003416}
3417
Chris Masond352ac62008-09-29 15:18:18 -04003418/*
3419 * given a leaf and an inode, copy the inode fields into the leaf
3420 */
Chris Masone02119d2008-09-05 16:13:11 -04003421static void fill_inode_item(struct btrfs_trans_handle *trans,
3422 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003423 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003424 struct inode *inode)
3425{
Liu Bo51fab692012-12-27 09:01:21 +00003426 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbac82f8232019-08-09 17:48:21 +02003428 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003429
David Sterbacc4c13d2020-04-29 02:15:56 +02003430 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3431 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3432 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3433 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3434 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003435
David Sterbacc4c13d2020-04-29 02:15:56 +02003436 btrfs_set_token_timespec_sec(&token, &item->atime,
3437 inode->i_atime.tv_sec);
3438 btrfs_set_token_timespec_nsec(&token, &item->atime,
3439 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003440
David Sterbacc4c13d2020-04-29 02:15:56 +02003441 btrfs_set_token_timespec_sec(&token, &item->mtime,
3442 inode->i_mtime.tv_sec);
3443 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3444 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003445
David Sterbacc4c13d2020-04-29 02:15:56 +02003446 btrfs_set_token_timespec_sec(&token, &item->ctime,
3447 inode->i_ctime.tv_sec);
3448 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3449 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003450
David Sterbacc4c13d2020-04-29 02:15:56 +02003451 btrfs_set_token_timespec_sec(&token, &item->otime,
3452 BTRFS_I(inode)->i_otime.tv_sec);
3453 btrfs_set_token_timespec_nsec(&token, &item->otime,
3454 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303455
David Sterbacc4c13d2020-04-29 02:15:56 +02003456 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3457 btrfs_set_token_inode_generation(&token, item,
3458 BTRFS_I(inode)->generation);
3459 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3460 btrfs_set_token_inode_transid(&token, item, trans->transid);
3461 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3462 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3463 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003464}
3465
Chris Masond352ac62008-09-29 15:18:18 -04003466/*
3467 * copy everything in the in-memory inode into the btree.
3468 */
Chris Mason21151332011-11-10 20:39:08 -05003469static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003470 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003471{
3472 struct btrfs_inode_item *inode_item;
3473 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003474 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003475 int ret;
3476
3477 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003478 if (!path)
3479 return -ENOMEM;
3480
Chris Masonb9473432009-03-13 11:00:37 -04003481 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003482 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3483 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003484 if (ret) {
3485 if (ret > 0)
3486 ret = -ENOENT;
3487 goto failed;
3488 }
3489
Chris Mason5f39d392007-10-15 16:14:19 -04003490 leaf = path->nodes[0];
3491 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003492 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003493
Chris Masone02119d2008-09-05 16:13:11 -04003494 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003496 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 ret = 0;
3498failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003499 btrfs_free_path(path);
3500 return ret;
3501}
3502
Chris Masond352ac62008-09-29 15:18:18 -04003503/*
Chris Mason21151332011-11-10 20:39:08 -05003504 * copy everything in the in-memory inode into the btree.
3505 */
3506noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3507 struct btrfs_root *root, struct inode *inode)
3508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003509 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003510 int ret;
3511
3512 /*
3513 * If the inode is a free space inode, we can deadlock during commit
3514 * if we put it into the delayed code.
3515 *
3516 * The data relocation inode should also be directly updated
3517 * without delay
3518 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003519 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003520 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003522 btrfs_update_root_times(trans, root);
3523
Chris Mason21151332011-11-10 20:39:08 -05003524 ret = btrfs_delayed_update_inode(trans, root, inode);
3525 if (!ret)
3526 btrfs_set_inode_last_trans(trans, inode);
3527 return ret;
3528 }
3529
3530 return btrfs_update_inode_item(trans, root, inode);
3531}
3532
Josef Bacikbe6aef62012-10-22 15:43:12 -04003533noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3534 struct btrfs_root *root,
3535 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003536{
3537 int ret;
3538
3539 ret = btrfs_update_inode(trans, root, inode);
3540 if (ret == -ENOSPC)
3541 return btrfs_update_inode_item(trans, root, inode);
3542 return ret;
3543}
3544
3545/*
Chris Masond352ac62008-09-29 15:18:18 -04003546 * unlink helper that gets used here in inode.c and in the tree logging
3547 * recovery code. It remove a link in a directory with a given name, and
3548 * also drops the back refs in the inode to the directory
3549 */
Al Viro92986792011-03-04 17:14:37 +00003550static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003552 struct btrfs_inode *dir,
3553 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003554 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003557 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003558 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003559 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003560 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003561 u64 ino = btrfs_ino(inode);
3562 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003563
3564 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003565 if (!path) {
3566 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003567 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 }
3569
Chris Masonb9473432009-03-13 11:00:37 -04003570 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003571 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003572 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003573 if (IS_ERR_OR_NULL(di)) {
3574 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003575 goto err;
3576 }
Chris Mason39279cc2007-06-12 06:35:45 -04003577 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003578 if (ret)
3579 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003580 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003581
Miao Xie67de1172013-12-26 13:07:06 +08003582 /*
3583 * If we don't have dir index, we have to get it by looking up
3584 * the inode ref, since we get the inode ref, remove it directly,
3585 * it is unnecessary to do delayed deletion.
3586 *
3587 * But if we have dir index, needn't search inode ref to get it.
3588 * Since the inode ref is close to the inode item, it is better
3589 * that we delay to delete it, and just do this deletion when
3590 * we update the inode item.
3591 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003592 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003593 ret = btrfs_delayed_delete_inode_ref(inode);
3594 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003595 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003596 goto skip_backref;
3597 }
3598 }
3599
Li Zefan33345d012011-04-20 10:31:50 +08003600 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3601 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003603 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003604 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003605 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003606 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003607 goto err;
3608 }
Miao Xie67de1172013-12-26 13:07:06 +08003609skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003610 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003611 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003612 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003613 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 }
Chris Mason39279cc2007-06-12 06:35:45 -04003615
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003616 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3617 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003619 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 goto err;
3621 }
Chris Masone02119d2008-09-05 16:13:11 -04003622
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003623 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3624 index);
Chris Mason6418c962010-10-30 07:34:24 -04003625 if (ret == -ENOENT)
3626 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003627 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003628 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003629
3630 /*
3631 * If we have a pending delayed iput we could end up with the final iput
3632 * being run in btrfs-cleaner context. If we have enough of these built
3633 * up we can end up burning a lot of time in btrfs-cleaner without any
3634 * way to throttle the unlinks. Since we're currently holding a ref on
3635 * the inode we can run the delayed iput here without any issues as the
3636 * final iput won't be done until after we drop the ref we're currently
3637 * holding.
3638 */
3639 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003640err:
3641 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003642 if (ret)
3643 goto out;
3644
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003645 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003646 inode_inc_iversion(&inode->vfs_inode);
3647 inode_inc_iversion(&dir->vfs_inode);
3648 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3649 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3650 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003651out:
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return ret;
3653}
3654
Al Viro92986792011-03-04 17:14:37 +00003655int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003657 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003658 const char *name, int name_len)
3659{
3660 int ret;
3661 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3662 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003663 drop_nlink(&inode->vfs_inode);
3664 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003665 }
3666 return ret;
3667}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668
3669/*
3670 * helper to start transaction for unlink and rmdir.
3671 *
Josef Bacikd52be812013-05-29 14:54:47 -04003672 * unlink and rmdir are special in btrfs, they do not always free space, so
3673 * if we cannot make our reservations the normal way try and see if there is
3674 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3675 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 */
Josef Bacikd52be812013-05-29 14:54:47 -04003677static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680
Josef Bacike70bea52011-10-11 14:18:24 -04003681 /*
3682 * 1 for the possible orphan item
3683 * 1 for the dir item
3684 * 1 for the dir index
3685 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003686 * 1 for the inode
3687 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003688 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689}
3690
Chris Mason39279cc2007-06-12 06:35:45 -04003691static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3692{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003694 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003695 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003696 int ret;
3697
Josef Bacikd52be812013-05-29 14:54:47 -04003698 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003700 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003701
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003702 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3703 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003704
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003705 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3706 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3707 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003708 if (ret)
3709 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003710
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003712 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003713 if (ret)
3714 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715 }
Josef Bacik7b128762008-07-24 12:17:14 -04003716
Tsutomu Itohb5324022011-07-19 07:27:20 +00003717out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003718 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003719 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003720 return ret;
3721}
3722
Misono Tomohirof60a2362018-04-18 11:34:52 +09003723static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003724 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003725{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003726 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003727 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728 struct btrfs_path *path;
3729 struct extent_buffer *leaf;
3730 struct btrfs_dir_item *di;
3731 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003732 const char *name = dentry->d_name.name;
3733 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003734 u64 index;
3735 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003736 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003737 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003738
Josef Bacik045d3962019-12-18 17:20:27 -05003739 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3740 objectid = inode->root->root_key.objectid;
3741 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3742 objectid = inode->location.objectid;
3743 } else {
3744 WARN_ON(1);
3745 return -EINVAL;
3746 }
3747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003748 path = btrfs_alloc_path();
3749 if (!path)
3750 return -ENOMEM;
3751
Li Zefan33345d012011-04-20 10:31:50 +08003752 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003753 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003755 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 goto out;
3757 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003758
3759 leaf = path->nodes[0];
3760 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3761 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3762 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003763 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003764 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003765 goto out;
3766 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003767 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003768
Josef Bacikd49d3282019-12-18 17:20:28 -05003769 /*
3770 * This is a placeholder inode for a subvolume we didn't have a
3771 * reference to at the time of the snapshot creation. In the meantime
3772 * we could have renamed the real subvol link into our snapshot, so
3773 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3774 * Instead simply lookup the dir_index_item for this entry so we can
3775 * remove it. Otherwise we know we have a ref to the root and we can
3776 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3777 */
3778 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003779 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003780 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003781 if (IS_ERR_OR_NULL(di)) {
3782 if (!di)
3783 ret = -ENOENT;
3784 else
3785 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003786 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003787 goto out;
3788 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003789
3790 leaf = path->nodes[0];
3791 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003792 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003793 btrfs_release_path(path);
3794 } else {
3795 ret = btrfs_del_root_ref(trans, objectid,
3796 root->root_key.objectid, dir_ino,
3797 &index, name, name_len);
3798 if (ret) {
3799 btrfs_abort_transaction(trans, ret);
3800 goto out;
3801 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003803
Lu Fengqi9add2942018-08-01 11:32:26 +08003804 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003806 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 goto out;
3808 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003809
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003810 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003811 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003812 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003813 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003815 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003817 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819}
3820
Misono Tomohiroec42f162018-04-18 11:34:13 +09003821/*
3822 * Helper to check if the subvolume references other subvolumes or if it's
3823 * default.
3824 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003825static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003826{
3827 struct btrfs_fs_info *fs_info = root->fs_info;
3828 struct btrfs_path *path;
3829 struct btrfs_dir_item *di;
3830 struct btrfs_key key;
3831 u64 dir_id;
3832 int ret;
3833
3834 path = btrfs_alloc_path();
3835 if (!path)
3836 return -ENOMEM;
3837
3838 /* Make sure this root isn't set as the default subvol */
3839 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3840 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3841 dir_id, "default", 7, 0);
3842 if (di && !IS_ERR(di)) {
3843 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3844 if (key.objectid == root->root_key.objectid) {
3845 ret = -EPERM;
3846 btrfs_err(fs_info,
3847 "deleting default subvolume %llu is not allowed",
3848 key.objectid);
3849 goto out;
3850 }
3851 btrfs_release_path(path);
3852 }
3853
3854 key.objectid = root->root_key.objectid;
3855 key.type = BTRFS_ROOT_REF_KEY;
3856 key.offset = (u64)-1;
3857
3858 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3859 if (ret < 0)
3860 goto out;
3861 BUG_ON(ret == 0);
3862
3863 ret = 0;
3864 if (path->slots[0] > 0) {
3865 path->slots[0]--;
3866 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3867 if (key.objectid == root->root_key.objectid &&
3868 key.type == BTRFS_ROOT_REF_KEY)
3869 ret = -ENOTEMPTY;
3870 }
3871out:
3872 btrfs_free_path(path);
3873 return ret;
3874}
3875
Nikolay Borisov20a68002018-04-27 14:36:24 +03003876/* Delete all dentries for inodes belonging to the root */
3877static void btrfs_prune_dentries(struct btrfs_root *root)
3878{
3879 struct btrfs_fs_info *fs_info = root->fs_info;
3880 struct rb_node *node;
3881 struct rb_node *prev;
3882 struct btrfs_inode *entry;
3883 struct inode *inode;
3884 u64 objectid = 0;
3885
3886 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3887 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3888
3889 spin_lock(&root->inode_lock);
3890again:
3891 node = root->inode_tree.rb_node;
3892 prev = NULL;
3893 while (node) {
3894 prev = node;
3895 entry = rb_entry(node, struct btrfs_inode, rb_node);
3896
David Sterba37508512018-06-29 10:56:40 +02003897 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003898 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003899 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003900 node = node->rb_right;
3901 else
3902 break;
3903 }
3904 if (!node) {
3905 while (prev) {
3906 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003907 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003908 node = prev;
3909 break;
3910 }
3911 prev = rb_next(prev);
3912 }
3913 }
3914 while (node) {
3915 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003916 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003917 inode = igrab(&entry->vfs_inode);
3918 if (inode) {
3919 spin_unlock(&root->inode_lock);
3920 if (atomic_read(&inode->i_count) > 1)
3921 d_prune_aliases(inode);
3922 /*
3923 * btrfs_drop_inode will have it removed from the inode
3924 * cache when its usage count hits zero.
3925 */
3926 iput(inode);
3927 cond_resched();
3928 spin_lock(&root->inode_lock);
3929 goto again;
3930 }
3931
3932 if (cond_resched_lock(&root->inode_lock))
3933 goto again;
3934
3935 node = rb_next(node);
3936 }
3937 spin_unlock(&root->inode_lock);
3938}
3939
Misono Tomohirof60a2362018-04-18 11:34:52 +09003940int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3941{
3942 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3943 struct btrfs_root *root = BTRFS_I(dir)->root;
3944 struct inode *inode = d_inode(dentry);
3945 struct btrfs_root *dest = BTRFS_I(inode)->root;
3946 struct btrfs_trans_handle *trans;
3947 struct btrfs_block_rsv block_rsv;
3948 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003949 int ret;
3950 int err;
3951
3952 /*
3953 * Don't allow to delete a subvolume with send in progress. This is
3954 * inside the inode lock so the error handling that has to drop the bit
3955 * again is not run concurrently.
3956 */
3957 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003958 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003959 spin_unlock(&dest->root_item_lock);
3960 btrfs_warn(fs_info,
3961 "attempt to delete subvolume %llu during send",
3962 dest->root_key.objectid);
3963 return -EPERM;
3964 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003965 root_flags = btrfs_root_flags(&dest->root_item);
3966 btrfs_set_root_flags(&dest->root_item,
3967 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3968 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003969
3970 down_write(&fs_info->subvol_sem);
3971
3972 err = may_destroy_subvol(dest);
3973 if (err)
3974 goto out_up_write;
3975
3976 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3977 /*
3978 * One for dir inode,
3979 * two for dir entries,
3980 * two for root ref/backref.
3981 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003982 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003983 if (err)
3984 goto out_up_write;
3985
3986 trans = btrfs_start_transaction(root, 0);
3987 if (IS_ERR(trans)) {
3988 err = PTR_ERR(trans);
3989 goto out_release;
3990 }
3991 trans->block_rsv = &block_rsv;
3992 trans->bytes_reserved = block_rsv.size;
3993
3994 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3995
Josef Bacik045d3962019-12-18 17:20:27 -05003996 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003997 if (ret) {
3998 err = ret;
3999 btrfs_abort_transaction(trans, ret);
4000 goto out_end_trans;
4001 }
4002
4003 btrfs_record_root_in_trans(trans, dest);
4004
4005 memset(&dest->root_item.drop_progress, 0,
4006 sizeof(dest->root_item.drop_progress));
4007 dest->root_item.drop_level = 0;
4008 btrfs_set_root_refs(&dest->root_item, 0);
4009
4010 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4011 ret = btrfs_insert_orphan_item(trans,
4012 fs_info->tree_root,
4013 dest->root_key.objectid);
4014 if (ret) {
4015 btrfs_abort_transaction(trans, ret);
4016 err = ret;
4017 goto out_end_trans;
4018 }
4019 }
4020
Lu Fengqid1957792018-05-29 15:01:54 +08004021 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004022 BTRFS_UUID_KEY_SUBVOL,
4023 dest->root_key.objectid);
4024 if (ret && ret != -ENOENT) {
4025 btrfs_abort_transaction(trans, ret);
4026 err = ret;
4027 goto out_end_trans;
4028 }
4029 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004030 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004031 dest->root_item.received_uuid,
4032 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4033 dest->root_key.objectid);
4034 if (ret && ret != -ENOENT) {
4035 btrfs_abort_transaction(trans, ret);
4036 err = ret;
4037 goto out_end_trans;
4038 }
4039 }
4040
4041out_end_trans:
4042 trans->block_rsv = NULL;
4043 trans->bytes_reserved = 0;
4044 ret = btrfs_end_transaction(trans);
4045 if (ret && !err)
4046 err = ret;
4047 inode->i_flags |= S_DEAD;
4048out_release:
4049 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4050out_up_write:
4051 up_write(&fs_info->subvol_sem);
4052 if (err) {
4053 spin_lock(&dest->root_item_lock);
4054 root_flags = btrfs_root_flags(&dest->root_item);
4055 btrfs_set_root_flags(&dest->root_item,
4056 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4057 spin_unlock(&dest->root_item_lock);
4058 } else {
4059 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004060 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004061 ASSERT(dest->send_in_progress == 0);
4062
4063 /* the last ref */
4064 if (dest->ino_cache_inode) {
4065 iput(dest->ino_cache_inode);
4066 dest->ino_cache_inode = NULL;
4067 }
4068 }
4069
4070 return err;
4071}
4072
Chris Mason39279cc2007-06-12 06:35:45 -04004073static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4074{
David Howells2b0143b2015-03-17 22:25:59 +00004075 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004076 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004077 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004078 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004079 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004080
David Sterbab3ae2442012-09-13 16:04:34 -06004081 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004082 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004083 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004084 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004085
Josef Bacikd52be812013-05-29 14:54:47 -04004086 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004087 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004088 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004089
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004090 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004091 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 goto out;
4093 }
4094
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004095 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004096 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004098
Filipe Manana44f714d2016-06-06 16:11:13 +01004099 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4100
Chris Mason39279cc2007-06-12 06:35:45 -04004101 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004102 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4103 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4104 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004105 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004106 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004107 /*
4108 * Propagate the last_unlink_trans value of the deleted dir to
4109 * its parent directory. This is to prevent an unrecoverable
4110 * log tree in the case we do something like this:
4111 * 1) create dir foo
4112 * 2) create snapshot under dir foo
4113 * 3) delete the snapshot
4114 * 4) rmdir foo
4115 * 5) mkdir foo
4116 * 6) fsync foo or some file inside foo
4117 */
4118 if (last_unlink_trans >= trans->transid)
4119 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4120 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004122 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004123 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004124
Chris Mason39279cc2007-06-12 06:35:45 -04004125 return err;
4126}
4127
Josef Bacikddfae632017-10-19 14:16:02 -04004128/*
4129 * Return this if we need to call truncate_block for the last bit of the
4130 * truncate.
4131 */
4132#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004133
Chris Mason323ac952008-10-01 19:05:46 -04004134/*
Chris Mason39279cc2007-06-12 06:35:45 -04004135 * this can truncate away extent items, csum items and directory items.
4136 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004137 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004138 *
4139 * csum items that cross the new i_size are truncated to the new size
4140 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004141 *
4142 * min_type is the minimum key type to truncate down to. If set to 0, this
4143 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004144 */
Yan, Zheng80825102009-11-12 09:35:36 +00004145int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4146 struct btrfs_root *root,
4147 struct inode *inode,
4148 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004149{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004150 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004151 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004152 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004154 struct btrfs_key key;
4155 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004156 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004157 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004158 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004159 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004160 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004161 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004162 int found_extent;
4163 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004164 int pending_del_nr = 0;
4165 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004166 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004167 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004168 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004169 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004170 bool be_nice = false;
4171 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004172 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4173 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004174
4175 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004176
Chris Mason28ed1342014-12-17 09:41:04 -08004177 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004178 * For non-free space inodes and non-shareable roots, we want to back
4179 * off from time to time. This means all inodes in subvolume roots,
4180 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004181 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004182 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004183 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004184 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004185
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004186 path = btrfs_alloc_path();
4187 if (!path)
4188 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004189 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004190
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004191 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004192 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4193 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004194
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004195 /*
4196 * We want to drop from the next block forward in case this
4197 * new size is not block aligned since we will be keeping the
4198 * last block of the extent just the way it is.
4199 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004200 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004201 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004202 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004203 }
Yan, Zheng80825102009-11-12 09:35:36 +00004204
Miao Xie16cdcec2011-04-22 18:12:22 +08004205 /*
4206 * This function is also used to drop the items in the log tree before
4207 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004208 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004209 * items.
4210 */
4211 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004212 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004213
Li Zefan33345d012011-04-20 10:31:50 +08004214 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004216 key.type = (u8)-1;
4217
Chris Mason85e21ba2008-01-29 15:11:36 -05004218search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004219 /*
4220 * with a 16K leaf size and 128MB extents, you can actually queue
4221 * up a huge file in a single leaf. Most of the time that
4222 * bytes_deleted is > 0, it will be huge by the time we get here
4223 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004224 if (be_nice && bytes_deleted > SZ_32M &&
4225 btrfs_should_end_transaction(trans)) {
4226 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004227 goto out;
4228 }
Chris Masond3977122009-01-05 21:25:51 -05004229
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004230 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4231 if (ret < 0)
4232 goto out;
4233
Chris Mason85e21ba2008-01-29 15:11:36 -05004234 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004235 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004236 /* there are no items in the tree for us to truncate, we're
4237 * done
4238 */
Yan, Zheng80825102009-11-12 09:35:36 +00004239 if (path->slots[0] == 0)
4240 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004241 path->slots[0]--;
4242 }
4243
Chris Masond3977122009-01-05 21:25:51 -05004244 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004245 u64 clear_start = 0, clear_len = 0;
4246
Chris Mason39279cc2007-06-12 06:35:45 -04004247 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004248 leaf = path->nodes[0];
4249 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004250 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004251
Li Zefan33345d012011-04-20 10:31:50 +08004252 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004253 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004254
Chris Mason85e21ba2008-01-29 15:11:36 -05004255 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004256 break;
4257
Chris Mason5f39d392007-10-15 16:14:19 -04004258 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004259 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004260 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004261 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004262 extent_type = btrfs_file_extent_type(leaf, fi);
4263 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004264 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004265 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004266
4267 trace_btrfs_truncate_show_fi_regular(
4268 BTRFS_I(inode), leaf, fi,
4269 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004270 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004271 item_end += btrfs_file_extent_ram_bytes(leaf,
4272 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004273
4274 trace_btrfs_truncate_show_fi_inline(
4275 BTRFS_I(inode), leaf, fi, path->slots[0],
4276 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004277 }
Yan008630c2007-11-07 13:31:09 -05004278 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004279 }
Yan, Zheng80825102009-11-12 09:35:36 +00004280 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004281 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004282 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004283 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004284 break;
4285 if (found_key.offset >= new_size)
4286 del_item = 1;
4287 else
4288 del_item = 0;
4289 }
Chris Mason39279cc2007-06-12 06:35:45 -04004290 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004291 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004292 if (found_type != BTRFS_EXTENT_DATA_KEY)
4293 goto delete;
4294
4295 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004296 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004297
4298 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004299 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004300 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004301 u64 orig_num_bytes =
4302 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004303 extent_num_bytes = ALIGN(new_size -
4304 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004305 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004306 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004307 btrfs_set_file_extent_num_bytes(leaf, fi,
4308 extent_num_bytes);
4309 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004310 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004311 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004312 &root->state) &&
4313 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004314 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004315 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004316 } else {
Chris Masondb945352007-10-15 16:15:53 -04004317 extent_num_bytes =
4318 btrfs_file_extent_disk_num_bytes(leaf,
4319 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004320 extent_offset = found_key.offset -
4321 btrfs_file_extent_offset(leaf, fi);
4322
Chris Mason39279cc2007-06-12 06:35:45 -04004323 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004324 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004325 if (extent_start != 0) {
4326 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004327 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004328 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004329 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004330 }
4331 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004332 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004333 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004334 /*
4335 * we can't truncate inline items that have had
4336 * special encodings
4337 */
4338 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004339 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004340 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4341 btrfs_file_extent_compression(leaf, fi) == 0) {
4342 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004343
Josef Bacikddfae632017-10-19 14:16:02 -04004344 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4345 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004346 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004347 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004348 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004349 * We have to bail so the last_size is set to
4350 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004351 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004352 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004353 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004354 } else {
4355 /*
4356 * Inline extents are special, we just treat
4357 * them as a full sector worth in the file
4358 * extent tree just for simplicity sake.
4359 */
4360 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004361 }
Josef Bacikddfae632017-10-19 14:16:02 -04004362
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004363 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004364 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 }
Chris Mason179e29e2007-11-01 11:28:41 -04004366delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004367 /*
4368 * We use btrfs_truncate_inode_items() to clean up log trees for
4369 * multiple fsyncs, and in this case we don't want to clear the
4370 * file extent range because it's just the log.
4371 */
4372 if (root == BTRFS_I(inode)->root) {
4373 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4374 clear_start, clear_len);
4375 if (ret) {
4376 btrfs_abort_transaction(trans, ret);
4377 break;
4378 }
4379 }
4380
Josef Bacikddfae632017-10-19 14:16:02 -04004381 if (del_item)
4382 last_size = found_key.offset;
4383 else
4384 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004385 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004386 if (!pending_del_nr) {
4387 /* no pending yet, add ourselves */
4388 pending_del_slot = path->slots[0];
4389 pending_del_nr = 1;
4390 } else if (pending_del_nr &&
4391 path->slots[0] + 1 == pending_del_slot) {
4392 /* hop on the pending chunk */
4393 pending_del_nr++;
4394 pending_del_slot = path->slots[0];
4395 } else {
Chris Masond3977122009-01-05 21:25:51 -05004396 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004397 }
Chris Mason39279cc2007-06-12 06:35:45 -04004398 } else {
4399 break;
4400 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004401 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004402
Miao Xie27cdeb72014-04-02 19:51:05 +08004403 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004404 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004405 struct btrfs_ref ref = { 0 };
4406
Chris Mason28ed1342014-12-17 09:41:04 -08004407 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004408
4409 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4410 extent_start, extent_num_bytes, 0);
4411 ref.real_root = root->root_key.objectid;
4412 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4413 ino, extent_offset);
4414 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004415 if (ret) {
4416 btrfs_abort_transaction(trans, ret);
4417 break;
4418 }
Chris Mason28f75a02015-02-04 06:59:29 -08004419 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004420 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004421 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004422 }
Chris Mason39279cc2007-06-12 06:35:45 -04004423 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004424
Yan, Zheng80825102009-11-12 09:35:36 +00004425 if (found_type == BTRFS_INODE_ITEM_KEY)
4426 break;
4427
4428 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004429 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004430 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004431 if (pending_del_nr) {
4432 ret = btrfs_del_items(trans, root, path,
4433 pending_del_slot,
4434 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004435 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004436 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004437 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004438 }
Yan, Zheng80825102009-11-12 09:35:36 +00004439 pending_del_nr = 0;
4440 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004441 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004442
Chris Mason28f75a02015-02-04 06:59:29 -08004443 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004444 * We can generate a lot of delayed refs, so we need to
4445 * throttle every once and a while and make sure we're
4446 * adding enough space to keep up with the work we are
4447 * generating. Since we hold a transaction here we
4448 * can't flush, and we don't want to FLUSH_LIMIT because
4449 * we could have generated too many delayed refs to
4450 * actually allocate, so just bail if we're short and
4451 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004452 */
Josef Bacik28bad212018-12-03 10:20:38 -05004453 if (should_throttle) {
4454 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4455 BTRFS_RESERVE_NO_FLUSH);
4456 if (ret) {
4457 ret = -EAGAIN;
4458 break;
4459 }
Chris Mason28f75a02015-02-04 06:59:29 -08004460 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004461 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004462 } else {
4463 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004464 }
Chris Mason39279cc2007-06-12 06:35:45 -04004465 }
Yan, Zheng80825102009-11-12 09:35:36 +00004466out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004467 if (ret >= 0 && pending_del_nr) {
4468 int err;
4469
4470 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004471 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004472 if (err) {
4473 btrfs_abort_transaction(trans, err);
4474 ret = err;
4475 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004476 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004477 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4478 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004479 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004480 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004481 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004482 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4483 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004484 }
Chris Mason28ed1342014-12-17 09:41:04 -08004485
Chris Mason39279cc2007-06-12 06:35:45 -04004486 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004487 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004488}
4489
Chris Masona52d9a82007-08-27 16:49:44 -04004490/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304491 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004492 * @inode - inode that we're zeroing
4493 * @from - the offset to start zeroing
4494 * @len - the length to zero, 0 to zero the entire range respective to the
4495 * offset
4496 * @front - zero up to the offset instead of from the offset on
4497 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304498 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004499 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004500 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304501int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004502 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004503{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004504 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004505 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4507 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004508 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004509 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004510 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004511 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004512 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304513 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004514 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004515 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004516 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304517 u64 block_start;
4518 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004519
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004520 if (IS_ALIGNED(offset, blocksize) &&
4521 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004522 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304523
Josef Bacik8b62f872017-10-19 14:15:55 -04004524 block_start = round_down(from, blocksize);
4525 block_end = block_start + blocksize - 1;
4526
Qu Wenruo364ecf32017-02-27 15:10:38 +08004527 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004528 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004529 if (ret)
4530 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004531
Chris Mason211c17f2008-05-15 09:13:45 -04004532again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004533 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004534 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004535 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004536 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004537 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004538 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004539 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004540 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004541
Chris Masona52d9a82007-08-27 16:49:44 -04004542 if (!PageUptodate(page)) {
4543 ret = btrfs_readpage(NULL, page);
4544 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004545 if (page->mapping != mapping) {
4546 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004547 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004548 goto again;
4549 }
Chris Masona52d9a82007-08-27 16:49:44 -04004550 if (!PageUptodate(page)) {
4551 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004552 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004553 }
4554 }
Chris Mason211c17f2008-05-15 09:13:45 -04004555 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004556
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304557 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004558 set_page_extent_mapped(page);
4559
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304560 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004561 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304562 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004563 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004564 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004565 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004566 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004567 btrfs_put_ordered_extent(ordered);
4568 goto again;
4569 }
4570
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304571 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004572 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4573 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004574
Filipe Mananae3b8a482017-11-04 00:16:59 +00004575 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004576 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004577 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304578 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004579 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004580 goto out_unlock;
4581 }
4582
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304583 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004584 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304585 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004586 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004587 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304588 memset(kaddr + (block_start - page_offset(page)),
4589 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004590 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304591 memset(kaddr + (block_start - page_offset(page)) + offset,
4592 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004593 flush_dcache_page(page);
4594 kunmap(page);
4595 }
Chris Mason247e7432008-07-17 12:53:51 -04004596 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004597 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004598 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004599
Chris Mason89642222008-07-24 09:41:53 -04004600out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004601 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004602 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004603 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004604 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004605 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004606 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004607out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004608 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004609 return ret;
4610}
4611
Josef Bacik16e75492013-10-22 12:18:51 -04004612static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4613 u64 offset, u64 len)
4614{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004615 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004616 struct btrfs_trans_handle *trans;
4617 int ret;
4618
4619 /*
4620 * Still need to make sure the inode looks like it's been updated so
4621 * that any holes get logged if we fsync.
4622 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004623 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4624 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004625 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4626 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4627 return 0;
4628 }
4629
4630 /*
4631 * 1 - for the one we're dropping
4632 * 1 - for the one we're adding
4633 * 1 - for updating the inode.
4634 */
4635 trans = btrfs_start_transaction(root, 3);
4636 if (IS_ERR(trans))
4637 return PTR_ERR(trans);
4638
4639 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4640 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004641 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004642 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004643 return ret;
4644 }
4645
David Sterbaf85b7372017-01-20 14:54:07 +01004646 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4647 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004648 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004649 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004650 else
4651 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004652 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004653 return ret;
4654}
4655
Josef Bacik695a0d02011-03-04 15:46:53 -05004656/*
4657 * This function puts in dummy file extents for the area we're creating a hole
4658 * for. So if we are truncating this file to a larger size we need to insert
4659 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4660 * the range between oldsize and size
4661 */
Josef Bacika41ad392011-01-31 15:30:16 -05004662int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004663{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004664 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004665 struct btrfs_root *root = BTRFS_I(inode)->root;
4666 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004667 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004668 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004669 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004670 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4671 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004672 u64 last_byte;
4673 u64 cur_offset;
4674 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004675 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004676
Josef Bacika71754f2013-06-17 17:14:39 -04004677 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304678 * If our size started in the middle of a block we need to zero out the
4679 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004680 * expose stale data.
4681 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304682 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004683 if (err)
4684 return err;
4685
Yan Zheng9036c102008-10-30 14:19:41 -04004686 if (size <= hole_start)
4687 return 0;
4688
David Sterbab272ae22020-02-05 19:09:33 +01004689 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004690 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004691 cur_offset = hole_start;
4692 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004693 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004694 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004695 if (IS_ERR(em)) {
4696 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004697 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004698 break;
4699 }
Yan Zheng9036c102008-10-30 14:19:41 -04004700 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004701 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004702 hole_size = last_byte - cur_offset;
4703
Yan, Zheng80825102009-11-12 09:35:36 +00004704 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004705 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004706
Josef Bacik16e75492013-10-22 12:18:51 -04004707 err = maybe_insert_hole(root, inode, cur_offset,
4708 hole_size);
4709 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004710 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004711
4712 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4713 cur_offset, hole_size);
4714 if (err)
4715 break;
4716
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004717 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004718 cur_offset + hole_size - 1, 0);
4719 hole_em = alloc_extent_map();
4720 if (!hole_em) {
4721 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4722 &BTRFS_I(inode)->runtime_flags);
4723 goto next;
4724 }
4725 hole_em->start = cur_offset;
4726 hole_em->len = hole_size;
4727 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004728
Josef Bacik5dc562c2012-08-17 13:14:17 -04004729 hole_em->block_start = EXTENT_MAP_HOLE;
4730 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004731 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004732 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004733 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004734 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004735
4736 while (1) {
4737 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004738 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004739 write_unlock(&em_tree->lock);
4740 if (err != -EEXIST)
4741 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004742 btrfs_drop_extent_cache(BTRFS_I(inode),
4743 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004744 cur_offset +
4745 hole_size - 1, 0);
4746 }
4747 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004748 } else {
4749 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4750 cur_offset, hole_size);
4751 if (err)
4752 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004753 }
Josef Bacik16e75492013-10-22 12:18:51 -04004754next:
Yan Zheng9036c102008-10-30 14:19:41 -04004755 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004756 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004757 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004758 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004759 break;
4760 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004761 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004762 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004763 return err;
4764}
4765
Eric Sandeen3972f262013-01-12 02:57:22 +00004766static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004767{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004768 struct btrfs_root *root = BTRFS_I(inode)->root;
4769 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004770 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004771 loff_t newsize = attr->ia_size;
4772 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004773 int ret;
4774
Eric Sandeen3972f262013-01-12 02:57:22 +00004775 /*
4776 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4777 * special case where we need to update the times despite not having
4778 * these flags set. For all other operations the VFS set these flags
4779 * explicitly if it wants a timestamp update.
4780 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004781 if (newsize != oldsize) {
4782 inode_inc_iversion(inode);
4783 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4784 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004785 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004786 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004787
Josef Bacika41ad392011-01-31 15:30:16 -05004788 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004789 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004790 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004791 * This is to ensure the snapshot captures a fully consistent
4792 * state of this file - if the snapshot captures this expanding
4793 * truncation, it must capture all writes that happened before
4794 * this truncation.
4795 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004796 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004797 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004798 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004799 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004800 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004801 }
Yan, Zheng80825102009-11-12 09:35:36 +00004802
Miao Xief4a2f4c2011-12-14 20:12:01 -05004803 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004804 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004805 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004806 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004807 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004808
4809 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004810 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304811 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004812 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004813 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004814 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004815 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004816
Josef Bacika41ad392011-01-31 15:30:16 -05004817 /*
4818 * We're truncating a file that used to have good data down to
4819 * zero. Make sure it gets into the ordered flush list so that
4820 * any new writes get down to disk quickly.
4821 */
4822 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004823 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4824 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004825
Josef Bacika41ad392011-01-31 15:30:16 -05004826 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004827
David Sterba8e0fa5d2020-06-09 19:21:48 +02004828 /* Disable nonlocked read DIO to avoid the endless truncate */
4829 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004830 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004831 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004832
Filipe Manana213e8c52018-02-06 20:40:31 +00004833 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004834 if (ret && inode->i_nlink) {
4835 int err;
4836
4837 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004838 * Truncate failed, so fix up the in-memory size. We
4839 * adjusted disk_i_size down as we removed extents, so
4840 * wait for disk_i_size to be stable and then update the
4841 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004842 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004843 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004844 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004845 return err;
4846 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004847 }
Yan, Zheng80825102009-11-12 09:35:36 +00004848 }
4849
Josef Bacika41ad392011-01-31 15:30:16 -05004850 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004851}
4852
Chris Mason39279cc2007-06-12 06:35:45 -04004853static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4854{
David Howells2b0143b2015-03-17 22:25:59 +00004855 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004856 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004857 int err;
4858
Li Zefanb83cc962010-12-20 16:04:08 +08004859 if (btrfs_root_readonly(root))
4860 return -EROFS;
4861
Jan Kara31051c82016-05-26 16:55:18 +02004862 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004863 if (err)
4864 return err;
4865
Chris Mason5a3f23d2009-03-31 13:27:11 -04004866 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004867 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004868 if (err)
4869 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004870 }
Yan Zheng9036c102008-10-30 14:19:41 -04004871
Christoph Hellwig10257742010-06-04 11:30:02 +02004872 if (attr->ia_valid) {
4873 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004874 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004875 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004876
Josef Bacik22c44fe2011-11-30 10:45:38 -05004877 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004878 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004879 }
4880
Chris Mason39279cc2007-06-12 06:35:45 -04004881 return err;
4882}
Chris Mason61295eb2008-01-14 16:24:38 -05004883
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004884/*
4885 * While truncating the inode pages during eviction, we get the VFS calling
4886 * btrfs_invalidatepage() against each page of the inode. This is slow because
4887 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4888 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4889 * extent_state structures over and over, wasting lots of time.
4890 *
4891 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4892 * those expensive operations on a per page basis and do only the ordered io
4893 * finishing, while we release here the extent_map and extent_state structures,
4894 * without the excessive merging and splitting.
4895 */
4896static void evict_inode_truncate_pages(struct inode *inode)
4897{
4898 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4899 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4900 struct rb_node *node;
4901
4902 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004903 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004904
4905 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004906 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004907 struct extent_map *em;
4908
Liu Bo07e1ce02018-08-23 03:51:52 +08004909 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004910 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004911 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4912 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004913 remove_extent_mapping(map_tree, em);
4914 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004915 if (need_resched()) {
4916 write_unlock(&map_tree->lock);
4917 cond_resched();
4918 write_lock(&map_tree->lock);
4919 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004920 }
4921 write_unlock(&map_tree->lock);
4922
Filipe Manana6ca07092015-05-26 00:55:42 +01004923 /*
4924 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004925 * We can have ongoing bios started by readahead that have
4926 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004927 * still in progress (unlocked the pages in the bio but did not yet
4928 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004929 * ranges can still be locked and eviction started because before
4930 * submitting those bios, which are executed by a separate task (work
4931 * queue kthread), inode references (inode->i_count) were not taken
4932 * (which would be dropped in the end io callback of each bio).
4933 * Therefore here we effectively end up waiting for those bios and
4934 * anyone else holding locked ranges without having bumped the inode's
4935 * reference count - if we don't do it, when they access the inode's
4936 * io_tree to unlock a range it may be too late, leading to an
4937 * use-after-free issue.
4938 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 spin_lock(&io_tree->lock);
4940 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4941 struct extent_state *state;
4942 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004943 u64 start;
4944 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004945 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004946
4947 node = rb_first(&io_tree->state);
4948 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004949 start = state->start;
4950 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004951 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004952 spin_unlock(&io_tree->lock);
4953
David Sterbaff13db42015-12-03 14:30:40 +01004954 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004955
4956 /*
4957 * If still has DELALLOC flag, the extent didn't reach disk,
4958 * and its reserved space won't be freed by delayed_ref.
4959 * So we need to free its reserved space here.
4960 * (Refer to comment in btrfs_invalidatepage, case 2)
4961 *
4962 * Note, end is the bytenr of last byte, so we need + 1 here.
4963 */
Filipe Manana421f0922018-10-12 13:02:48 +01004964 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004965 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004966
Filipe Manana6ca07092015-05-26 00:55:42 +01004967 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004968 EXTENT_LOCKED | EXTENT_DELALLOC |
4969 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4970 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004971
Filipe Manana7064dd52014-08-08 02:47:05 +01004972 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004973 spin_lock(&io_tree->lock);
4974 }
4975 spin_unlock(&io_tree->lock);
4976}
4977
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004978static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004979 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004980{
4981 struct btrfs_fs_info *fs_info = root->fs_info;
4982 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004983 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004984 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004985 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004986
Josef Bacikd3984c92019-08-01 18:19:37 -04004987 /*
4988 * Eviction should be taking place at some place safe because of our
4989 * delayed iputs. However the normal flushing code will run delayed
4990 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4991 *
4992 * We reserve the delayed_refs_extra here again because we can't use
4993 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4994 * above. We reserve our extra bit here because we generate a ton of
4995 * delayed refs activity by truncating.
4996 *
4997 * If we cannot make our reservation we'll attempt to steal from the
4998 * global reserve, because we really want to be able to free up space.
4999 */
5000 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5001 BTRFS_RESERVE_FLUSH_EVICT);
5002 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005003 /*
5004 * Try to steal from the global reserve if there is space for
5005 * it.
5006 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005007 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5008 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5009 btrfs_warn(fs_info,
5010 "could not allocate space for delete; will truncate on mount");
5011 return ERR_PTR(-ENOSPC);
5012 }
5013 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005014 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005015
5016 trans = btrfs_join_transaction(root);
5017 if (IS_ERR(trans))
5018 return trans;
5019
5020 if (delayed_refs_extra) {
5021 trans->block_rsv = &fs_info->trans_block_rsv;
5022 trans->bytes_reserved = delayed_refs_extra;
5023 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5024 delayed_refs_extra, 1);
5025 }
5026 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005027}
5028
Al Virobd555972010-06-07 11:35:40 -04005029void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005030{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005031 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005032 struct btrfs_trans_handle *trans;
5033 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005034 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005035 int ret;
5036
liubo1abe9b82011-03-24 11:18:59 +00005037 trace_btrfs_inode_evict(inode);
5038
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005039 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005040 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005041 return;
5042 }
5043
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005044 evict_inode_truncate_pages(inode);
5045
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005046 if (inode->i_nlink &&
5047 ((btrfs_root_refs(&root->root_item) != 0 &&
5048 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005049 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005050 goto no_delete;
5051
Omar Sandoval27919062018-05-11 13:13:37 -07005052 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005053 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005054
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005055 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005056
Omar Sandoval7b40b692018-05-11 13:13:33 -07005057 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005058 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005059
Yan, Zheng76dda932009-09-21 16:00:26 -04005060 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005061 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5062 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005063 goto no_delete;
5064 }
5065
Nikolay Borisovaa790212017-01-10 20:35:40 +02005066 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005067 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005068 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005069
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005070 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005071 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005072 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005073 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005074 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005075
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005076 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005077
Yan, Zheng80825102009-11-12 09:35:36 +00005078 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005079 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005080 if (IS_ERR(trans))
5081 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005082
5083 trans->block_rsv = rsv;
5084
Yan, Zhengd68fc572010-05-16 10:49:58 -04005085 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005086 trans->block_rsv = &fs_info->trans_block_rsv;
5087 btrfs_end_transaction(trans);
5088 btrfs_btree_balance_dirty(fs_info);
5089 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5090 goto free_rsv;
5091 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005092 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005093 }
5094
Josef Bacik4ef31a42013-08-13 14:10:08 -04005095 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005096 * Errors here aren't a big deal, it just means we leave orphan items in
5097 * the tree. They will be cleaned up on the next mount. If the inode
5098 * number gets reused, cleanup deletes the orphan item without doing
5099 * anything, and unlink reuses the existing orphan item.
5100 *
5101 * If it turns out that we are dropping too many of these, we might want
5102 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005103 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005104 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005105 if (!IS_ERR(trans)) {
5106 trans->block_rsv = rsv;
5107 btrfs_orphan_del(trans, BTRFS_I(inode));
5108 trans->block_rsv = &fs_info->trans_block_rsv;
5109 btrfs_end_transaction(trans);
5110 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005111
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005112 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005113 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005114 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005115
Omar Sandoval27919062018-05-11 13:13:37 -07005116free_rsv:
5117 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005118no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005119 /*
5120 * If we didn't successfully delete, the orphan item will still be in
5121 * the tree and we'll retry on the next mount. Again, we might also want
5122 * to retry these periodically in the future.
5123 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005124 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005125 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005126}
5127
5128/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005129 * Return the key found in the dir entry in the location pointer, fill @type
5130 * with BTRFS_FT_*, and return 0.
5131 *
Su Yue005d6712018-03-05 17:13:37 +08005132 * If no dir entries were found, returns -ENOENT.
5133 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005134 */
5135static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005136 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005137{
5138 const char *name = dentry->d_name.name;
5139 int namelen = dentry->d_name.len;
5140 struct btrfs_dir_item *di;
5141 struct btrfs_path *path;
5142 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005143 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005144
5145 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005146 if (!path)
5147 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005148
David Sterbaf85b7372017-01-20 14:54:07 +01005149 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5150 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005151 if (IS_ERR_OR_NULL(di)) {
5152 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005153 goto out;
5154 }
Chris Masond3977122009-01-05 21:25:51 -05005155
Chris Mason5f39d392007-10-15 16:14:19 -04005156 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005157 if (location->type != BTRFS_INODE_ITEM_KEY &&
5158 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005159 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005160 btrfs_warn(root->fs_info,
5161"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5162 __func__, name, btrfs_ino(BTRFS_I(dir)),
5163 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005164 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005165 if (!ret)
5166 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005167out:
Chris Mason39279cc2007-06-12 06:35:45 -04005168 btrfs_free_path(path);
5169 return ret;
5170}
5171
5172/*
5173 * when we hit a tree root in a directory, the btrfs part of the inode
5174 * needs to be changed to reflect the root directory of the tree root. This
5175 * is kind of like crossing a mount point.
5176 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005177static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005178 struct inode *dir,
5179 struct dentry *dentry,
5180 struct btrfs_key *location,
5181 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005182{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005183 struct btrfs_path *path;
5184 struct btrfs_root *new_root;
5185 struct btrfs_root_ref *ref;
5186 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005187 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005188 int ret;
5189 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005190
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005191 path = btrfs_alloc_path();
5192 if (!path) {
5193 err = -ENOMEM;
5194 goto out;
5195 }
Chris Mason39279cc2007-06-12 06:35:45 -04005196
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005197 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005198 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5199 key.type = BTRFS_ROOT_REF_KEY;
5200 key.offset = location->objectid;
5201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005202 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005203 if (ret) {
5204 if (ret < 0)
5205 err = ret;
5206 goto out;
5207 }
Chris Mason39279cc2007-06-12 06:35:45 -04005208
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209 leaf = path->nodes[0];
5210 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005211 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005212 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5213 goto out;
5214
5215 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5216 (unsigned long)(ref + 1),
5217 dentry->d_name.len);
5218 if (ret)
5219 goto out;
5220
David Sterbab3b4aa72011-04-21 01:20:15 +02005221 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005222
David Sterba56e93572020-05-15 19:35:55 +02005223 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 if (IS_ERR(new_root)) {
5225 err = PTR_ERR(new_root);
5226 goto out;
5227 }
5228
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005229 *sub_root = new_root;
5230 location->objectid = btrfs_root_dirid(&new_root->root_item);
5231 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005232 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005233 err = 0;
5234out:
5235 btrfs_free_path(path);
5236 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005237}
5238
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005239static void inode_tree_add(struct inode *inode)
5240{
5241 struct btrfs_root *root = BTRFS_I(inode)->root;
5242 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005243 struct rb_node **p;
5244 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005245 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005246 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005247
Al Viro1d3382cb2010-10-23 15:19:20 -04005248 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005249 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005250 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005251 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005252 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005253 while (*p) {
5254 parent = *p;
5255 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5256
David Sterba37508512018-06-29 10:56:40 +02005257 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005258 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005259 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005260 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005261 else {
5262 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005263 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005264 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005265 RB_CLEAR_NODE(parent);
5266 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005267 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005268 }
5269 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005270 rb_link_node(new, parent, p);
5271 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005272 spin_unlock(&root->inode_lock);
5273}
5274
5275static void inode_tree_del(struct inode *inode)
5276{
5277 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005278 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005279
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005280 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005283 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005284 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005285 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005286 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005287
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005288 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005289 spin_lock(&root->inode_lock);
5290 empty = RB_EMPTY_ROOT(&root->inode_tree);
5291 spin_unlock(&root->inode_lock);
5292 if (empty)
5293 btrfs_add_dead_root(root);
5294 }
5295}
5296
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005297
Chris Masone02119d2008-09-05 16:13:11 -04005298static int btrfs_init_locked_inode(struct inode *inode, void *p)
5299{
5300 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005301
5302 inode->i_ino = args->ino;
5303 BTRFS_I(inode)->location.objectid = args->ino;
5304 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5305 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005306 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5307 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005308 return 0;
5309}
5310
5311static int btrfs_find_actor(struct inode *inode, void *opaque)
5312{
5313 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005314
5315 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005316 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005317}
5318
David Sterba0202e832020-05-15 19:35:59 +02005319static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005320 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005321{
5322 struct inode *inode;
5323 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005324 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005325
David Sterba0202e832020-05-15 19:35:59 +02005326 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005327 args.root = root;
5328
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005329 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005330 btrfs_init_locked_inode,
5331 (void *)&args);
5332 return inode;
5333}
5334
David Sterba4c66e0d2019-10-03 19:09:35 +02005335/*
David Sterba0202e832020-05-15 19:35:59 +02005336 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005337 * Path can be preallocated to prevent recursing back to iget through
5338 * allocator. NULL is also valid but may require an additional allocation
5339 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305340 */
David Sterba0202e832020-05-15 19:35:59 +02005341struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005342 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305343{
5344 struct inode *inode;
5345
David Sterba0202e832020-05-15 19:35:59 +02005346 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305347 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005348 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305349
5350 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005351 int ret;
5352
Filipe Manana4222ea72018-10-24 10:13:03 +01005353 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005354 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005355 inode_tree_add(inode);
5356 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005357 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005358 iget_failed(inode);
5359 /*
5360 * ret > 0 can come from btrfs_search_slot called by
5361 * btrfs_read_locked_inode, this means the inode item
5362 * was not found.
5363 */
5364 if (ret > 0)
5365 ret = -ENOENT;
5366 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005367 }
5368 }
5369
Balaji Rao1a54ef82008-07-21 02:01:04 +05305370 return inode;
5371}
5372
David Sterba0202e832020-05-15 19:35:59 +02005373struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005374{
David Sterba0202e832020-05-15 19:35:59 +02005375 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005376}
5377
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005378static struct inode *new_simple_dir(struct super_block *s,
5379 struct btrfs_key *key,
5380 struct btrfs_root *root)
5381{
5382 struct inode *inode = new_inode(s);
5383
5384 if (!inode)
5385 return ERR_PTR(-ENOMEM);
5386
Josef Bacik5c8fd992020-02-14 16:11:43 -05005387 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005388 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005389 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005390
5391 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005392 /*
5393 * We only need lookup, the rest is read-only and there's no inode
5394 * associated with the dentry
5395 */
5396 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005397 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 inode->i_fop = &simple_dir_operations;
5399 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005400 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305401 inode->i_atime = inode->i_mtime;
5402 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005403 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005404
5405 return inode;
5406}
5407
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005408static inline u8 btrfs_inode_type(struct inode *inode)
5409{
5410 /*
5411 * Compile-time asserts that generic FT_* types still match
5412 * BTRFS_FT_* types
5413 */
5414 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5415 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5416 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5417 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5418 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5419 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5420 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5421 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5422
5423 return fs_umode_to_ftype(inode->i_mode);
5424}
5425
Chris Mason3de45862008-11-17 21:02:50 -05005426struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005428 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005429 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005431 struct btrfs_root *sub_root = root;
5432 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005433 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005434 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005435
5436 if (dentry->d_name.len > BTRFS_NAME_LEN)
5437 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005438
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005439 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005440 if (ret < 0)
5441 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005442
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005443 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005444 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005445 if (IS_ERR(inode))
5446 return inode;
5447
5448 /* Do extra check against inode mode with di_type */
5449 if (btrfs_inode_type(inode) != di_type) {
5450 btrfs_crit(fs_info,
5451"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5452 inode->i_mode, btrfs_inode_type(inode),
5453 di_type);
5454 iput(inode);
5455 return ERR_PTR(-EUCLEAN);
5456 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005457 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005458 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005459
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005460 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005461 &location, &sub_root);
5462 if (ret < 0) {
5463 if (ret != -ENOENT)
5464 inode = ERR_PTR(ret);
5465 else
5466 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5467 } else {
David Sterba0202e832020-05-15 19:35:59 +02005468 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005469 }
Josef Bacik87270022020-01-24 09:32:31 -05005470 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005471 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005472
Julia Lawall34d19ba2011-01-24 19:55:19 +00005473 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005474 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005475 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005476 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005478 if (ret) {
5479 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005480 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005481 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005482 }
5483
Chris Mason3de45862008-11-17 21:02:50 -05005484 return inode;
5485}
5486
Nick Pigginfe15ce42011-01-07 17:49:23 +11005487static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005488{
5489 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005490 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005491
Li Zefan848cce02012-02-21 17:04:28 +08005492 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005493 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005494
Li Zefan848cce02012-02-21 17:04:28 +08005495 if (inode) {
5496 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005497 if (btrfs_root_refs(&root->root_item) == 0)
5498 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005499
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005500 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005501 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005502 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005503 return 0;
5504}
5505
Chris Mason3de45862008-11-17 21:02:50 -05005506static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005507 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005508{
Al Viro3837d202018-10-10 16:38:27 -04005509 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005510
Al Viro3837d202018-10-10 16:38:27 -04005511 if (inode == ERR_PTR(-ENOENT))
5512 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005513 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005514}
5515
Josef Bacik23b5ec72017-07-24 15:14:25 -04005516/*
5517 * All this infrastructure exists because dir_emit can fault, and we are holding
5518 * the tree lock when doing readdir. For now just allocate a buffer and copy
5519 * our information into that, and then dir_emit from the buffer. This is
5520 * similar to what NFS does, only we don't keep the buffer around in pagecache
5521 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5522 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5523 * tree lock.
5524 */
5525static int btrfs_opendir(struct inode *inode, struct file *file)
5526{
5527 struct btrfs_file_private *private;
5528
5529 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5530 if (!private)
5531 return -ENOMEM;
5532 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5533 if (!private->filldir_buf) {
5534 kfree(private);
5535 return -ENOMEM;
5536 }
5537 file->private_data = private;
5538 return 0;
5539}
5540
5541struct dir_entry {
5542 u64 ino;
5543 u64 offset;
5544 unsigned type;
5545 int name_len;
5546};
5547
5548static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5549{
5550 while (entries--) {
5551 struct dir_entry *entry = addr;
5552 char *name = (char *)(entry + 1);
5553
David Sterba92d32172018-04-16 21:10:14 +02005554 ctx->pos = get_unaligned(&entry->offset);
5555 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5556 get_unaligned(&entry->ino),
5557 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005558 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005559 addr += sizeof(struct dir_entry) +
5560 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005561 ctx->pos++;
5562 }
5563 return 0;
5564}
5565
Al Viro9cdda8d2013-05-22 16:48:09 -04005566static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005567{
Al Viro9cdda8d2013-05-22 16:48:09 -04005568 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005569 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005570 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005571 struct btrfs_dir_item *di;
5572 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005573 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005574 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005575 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005576 struct list_head ins_list;
5577 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005578 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005579 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005580 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005581 char *name_ptr;
5582 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005583 int entries = 0;
5584 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005585 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005586 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005587
Al Viro9cdda8d2013-05-22 16:48:09 -04005588 if (!dir_emit_dots(file, ctx))
5589 return 0;
5590
David Woodhouse49593bf2008-08-17 17:08:36 +01005591 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005592 if (!path)
5593 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005594
Josef Bacik23b5ec72017-07-24 15:14:25 -04005595 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005596 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005597
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005598 INIT_LIST_HEAD(&ins_list);
5599 INIT_LIST_HEAD(&del_list);
5600 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005601
Josef Bacik23b5ec72017-07-24 15:14:25 -04005602again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005603 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005604 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005605 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005606
Chris Mason39279cc2007-06-12 06:35:45 -04005607 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5608 if (ret < 0)
5609 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005610
5611 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005612 struct dir_entry *entry;
5613
Chris Mason5f39d392007-10-15 16:14:19 -04005614 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005615 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005616 if (slot >= btrfs_header_nritems(leaf)) {
5617 ret = btrfs_next_leaf(root, path);
5618 if (ret < 0)
5619 goto err;
5620 else if (ret > 0)
5621 break;
5622 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005623 }
Chris Mason3de45862008-11-17 21:02:50 -05005624
Chris Mason5f39d392007-10-15 16:14:19 -04005625 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5626
5627 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005628 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005629 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005630 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005631 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005632 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005633 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005634 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005635 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005636 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005637 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5638 PAGE_SIZE) {
5639 btrfs_release_path(path);
5640 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5641 if (ret)
5642 goto nopos;
5643 addr = private->filldir_buf;
5644 entries = 0;
5645 total_len = 0;
5646 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005648
5649 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005650 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005651 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005652 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5653 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005654 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005655 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005656 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005657 put_unaligned(location.objectid, &entry->ino);
5658 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005659 entries++;
5660 addr += sizeof(struct dir_entry) + name_len;
5661 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005662next:
5663 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005664 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005665 btrfs_release_path(path);
5666
5667 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5668 if (ret)
5669 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005670
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005671 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005672 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005673 goto nopos;
5674
Zach Browndb62efb2013-07-11 16:19:42 -07005675 /*
5676 * Stop new entries from being returned after we return the last
5677 * entry.
5678 *
5679 * New directory entries are assigned a strictly increasing
5680 * offset. This means that new entries created during readdir
5681 * are *guaranteed* to be seen in the future by that readdir.
5682 * This has broken buggy programs which operate on names as
5683 * they're returned by readdir. Until we re-use freed offsets
5684 * we have this hack to stop new entries from being returned
5685 * under the assumption that they'll never reach this huge
5686 * offset.
5687 *
5688 * This is being careful not to overflow 32bit loff_t unless the
5689 * last entry requires it because doing so has broken 32bit apps
5690 * in the past.
5691 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005692 if (ctx->pos >= INT_MAX)
5693 ctx->pos = LLONG_MAX;
5694 else
5695 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005696nopos:
5697 ret = 0;
5698err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005699 if (put)
5700 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005701 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005702 return ret;
5703}
5704
Chris Mason39279cc2007-06-12 06:35:45 -04005705/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005706 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005707 * inode changes. But, it is most likely to find the inode in cache.
5708 * FIXME, needs more benchmarking...there are no reasons other than performance
5709 * to keep or drop this code.
5710 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005711static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005712{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005714 struct btrfs_root *root = BTRFS_I(inode)->root;
5715 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005716 int ret;
5717
Josef Bacik72ac3c02012-05-23 14:13:11 -04005718 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005719 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005720
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005721 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005722 if (IS_ERR(trans))
5723 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005724
5725 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005726 if (ret && ret == -ENOSPC) {
5727 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005728 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005729 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005730 if (IS_ERR(trans))
5731 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005732
Chris Mason94b60442010-05-26 11:02:00 -04005733 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005734 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005735 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005736 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005737 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005738
5739 return ret;
5740}
5741
5742/*
5743 * This is a copy of file_update_time. We need this so we can return error on
5744 * ENOSPC for updating the inode in the case of file write and mmap writes.
5745 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005746static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005747 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005748{
Alexander Block2bc5565282012-06-15 09:49:33 +02005749 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005750 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005751
5752 if (btrfs_root_readonly(root))
5753 return -EROFS;
5754
Josef Bacike41f9412012-03-26 09:46:47 -04005755 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005756 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005757 if (flags & S_CTIME)
5758 inode->i_ctime = *now;
5759 if (flags & S_MTIME)
5760 inode->i_mtime = *now;
5761 if (flags & S_ATIME)
5762 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005763 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005764}
5765
Chris Masond352ac62008-09-29 15:18:18 -04005766/*
5767 * find the highest existing sequence number in a directory
5768 * and then set the in-memory index_cnt variable to reflect
5769 * free sequence numbers
5770 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005771static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005772{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005773 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005774 struct btrfs_key key, found_key;
5775 struct btrfs_path *path;
5776 struct extent_buffer *leaf;
5777 int ret;
5778
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005779 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005780 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005781 key.offset = (u64)-1;
5782
5783 path = btrfs_alloc_path();
5784 if (!path)
5785 return -ENOMEM;
5786
5787 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5788 if (ret < 0)
5789 goto out;
5790 /* FIXME: we should be able to handle this */
5791 if (ret == 0)
5792 goto out;
5793 ret = 0;
5794
5795 /*
5796 * MAGIC NUMBER EXPLANATION:
5797 * since we search a directory based on f_pos we have to start at 2
5798 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5799 * else has to start at 2
5800 */
5801 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005802 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005803 goto out;
5804 }
5805
5806 path->slots[0]--;
5807
5808 leaf = path->nodes[0];
5809 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5810
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005811 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005812 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005813 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005814 goto out;
5815 }
5816
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005817 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005818out:
5819 btrfs_free_path(path);
5820 return ret;
5821}
5822
Chris Masond352ac62008-09-29 15:18:18 -04005823/*
5824 * helper to find a free sequence number in a given directory. This current
5825 * code is very simple, later versions will do smarter things in the btree
5826 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005827int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005828{
5829 int ret = 0;
5830
Nikolay Borisov877574e2017-02-20 13:50:33 +02005831 if (dir->index_cnt == (u64)-1) {
5832 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005833 if (ret) {
5834 ret = btrfs_set_inode_index_count(dir);
5835 if (ret)
5836 return ret;
5837 }
Josef Bacikaec74772008-07-24 12:12:38 -04005838 }
5839
Nikolay Borisov877574e2017-02-20 13:50:33 +02005840 *index = dir->index_cnt;
5841 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005842
5843 return ret;
5844}
5845
Chris Masonb0d5d102014-09-08 13:08:51 -07005846static int btrfs_insert_inode_locked(struct inode *inode)
5847{
5848 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005849
5850 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005851 args.root = BTRFS_I(inode)->root;
5852
5853 return insert_inode_locked4(inode,
5854 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5855 btrfs_find_actor, &args);
5856}
5857
Anand Jain19aee8d2017-07-18 17:37:05 +08005858/*
5859 * Inherit flags from the parent inode.
5860 *
5861 * Currently only the compression flags and the cow flags are inherited.
5862 */
5863static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5864{
5865 unsigned int flags;
5866
5867 if (!dir)
5868 return;
5869
5870 flags = BTRFS_I(dir)->flags;
5871
5872 if (flags & BTRFS_INODE_NOCOMPRESS) {
5873 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5874 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5875 } else if (flags & BTRFS_INODE_COMPRESS) {
5876 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5877 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5878 }
5879
5880 if (flags & BTRFS_INODE_NODATACOW) {
5881 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5882 if (S_ISREG(inode->i_mode))
5883 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5884 }
5885
David Sterba7b6a2212018-03-26 18:40:21 +02005886 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005887}
5888
Chris Mason39279cc2007-06-12 06:35:45 -04005889static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5890 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005891 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005892 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005893 u64 ref_objectid, u64 objectid,
5894 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005895{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005896 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005897 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005898 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005899 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005900 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005901 struct btrfs_inode_ref *ref;
5902 struct btrfs_key key[2];
5903 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005904 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005905 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005906 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005907 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005908
Chris Mason5f39d392007-10-15 16:14:19 -04005909 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005910 if (!path)
5911 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005912
Josef Bacik11a19a92019-09-09 10:12:04 -04005913 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005914 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005915 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005916 if (!inode) {
5917 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005918 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005919 }
Chris Mason39279cc2007-06-12 06:35:45 -04005920
Li Zefan581bb052011-04-20 10:06:11 +08005921 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005922 * O_TMPFILE, set link count to 0, so that after this point,
5923 * we fill in an inode item with the correct link count.
5924 */
5925 if (!name)
5926 set_nlink(inode, 0);
5927
5928 /*
Li Zefan581bb052011-04-20 10:06:11 +08005929 * we have to initialize this early, so we can reclaim the inode
5930 * number if we fail afterwards in this function.
5931 */
5932 inode->i_ino = objectid;
5933
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005934 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005935 trace_btrfs_inode_request(dir);
5936
Nikolay Borisov877574e2017-02-20 13:50:33 +02005937 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005938 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005939 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005940 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005941 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005942 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005943 } else if (dir) {
5944 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005945 }
5946 /*
5947 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005948 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005949 * number
5950 */
5951 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005952 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005953 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005954 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005955 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005956
Josef Bacik5dc562c2012-08-17 13:14:17 -04005957 /*
5958 * We could have gotten an inode number from somebody who was fsynced
5959 * and then removed in this same transaction, so let's just set full
5960 * sync since it will be a full sync anyway and this will blow away the
5961 * old info in the log.
5962 */
5963 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5964
Chris Mason9c583092008-01-29 15:15:18 -05005965 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005966 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005967 key[0].offset = 0;
5968
Chris Mason9c583092008-01-29 15:15:18 -05005969 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005970
5971 if (name) {
5972 /*
5973 * Start new inodes with an inode_ref. This is slightly more
5974 * efficient for small numbers of hard links since they will
5975 * be packed into one item. Extended refs will kick in if we
5976 * add more hard links than can fit in the ref item.
5977 */
5978 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005979 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005980 key[1].offset = ref_objectid;
5981
5982 sizes[1] = name_len + sizeof(*ref);
5983 }
Chris Mason9c583092008-01-29 15:15:18 -05005984
Chris Masonb0d5d102014-09-08 13:08:51 -07005985 location = &BTRFS_I(inode)->location;
5986 location->objectid = objectid;
5987 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005988 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005989
5990 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005991 if (ret < 0) {
5992 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005993 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005994 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005995
Chris Masonb9473432009-03-13 11:00:37 -04005996 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005997 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005998 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005999 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006000
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006001 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006002 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306003
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006004 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306005 inode->i_atime = inode->i_mtime;
6006 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006007 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306008
Chris Mason5f39d392007-10-15 16:14:19 -04006009 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6010 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006011 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006012 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006013 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006014
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006015 if (name) {
6016 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6017 struct btrfs_inode_ref);
6018 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6019 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6020 ptr = (unsigned long)(ref + 1);
6021 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6022 }
Chris Mason9c583092008-01-29 15:15:18 -05006023
Chris Mason5f39d392007-10-15 16:14:19 -04006024 btrfs_mark_buffer_dirty(path->nodes[0]);
6025 btrfs_free_path(path);
6026
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006027 btrfs_inherit_iflags(inode, dir);
6028
Al Viro569254b2011-07-24 17:08:40 -04006029 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006030 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006031 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006032 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006033 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6034 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006035 }
6036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006037 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006038
6039 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006040 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006041
Alexander Block8ea05e32012-07-25 17:35:53 +02006042 btrfs_update_root_times(trans, root);
6043
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006044 ret = btrfs_inode_inherit_props(trans, inode, dir);
6045 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006046 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006047 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006048 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006049
Chris Mason39279cc2007-06-12 06:35:45 -04006050 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006051
6052fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006053 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006054fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006055 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006056 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006057 btrfs_free_path(path);
6058 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006059}
6060
Chris Masond352ac62008-09-29 15:18:18 -04006061/*
6062 * utility function to add 'inode' into 'parent_inode' with
6063 * a give name and a given sequence number.
6064 * if 'add_backref' is true, also insert a backref from the
6065 * inode to the parent directory.
6066 */
Chris Masone02119d2008-09-05 16:13:11 -04006067int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006068 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006069 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006070{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006071 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006072 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006073 struct btrfs_root *root = parent_inode->root;
6074 u64 ino = btrfs_ino(inode);
6075 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006076
Li Zefan33345d012011-04-20 10:31:50 +08006077 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006078 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006079 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006080 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006081 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006082 key.offset = 0;
6083 }
Chris Mason39279cc2007-06-12 06:35:45 -04006084
Li Zefan33345d012011-04-20 10:31:50 +08006085 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006086 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006087 root->root_key.objectid, parent_ino,
6088 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006089 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006090 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6091 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006092 }
6093
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006094 /* Nothing to clean up yet */
6095 if (ret)
6096 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006097
Lu Fengqi684572d2018-08-04 21:10:57 +08006098 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006099 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006100 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006101 goto fail_dir_item;
6102 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006103 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006104 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006105 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006106
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006107 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006108 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006109 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006110 /*
6111 * If we are replaying a log tree, we do not want to update the mtime
6112 * and ctime of the parent directory with the current time, since the
6113 * log replay procedure is responsible for setting them to their correct
6114 * values (the ones it had when the fsync was done).
6115 */
6116 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6117 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6118
6119 parent_inode->vfs_inode.i_mtime = now;
6120 parent_inode->vfs_inode.i_ctime = now;
6121 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006122 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006123 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006124 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006125 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006126
6127fail_dir_item:
6128 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6129 u64 local_index;
6130 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006131 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006132 root->root_key.objectid, parent_ino,
6133 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006134 if (err)
6135 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006136 } else if (add_backref) {
6137 u64 local_index;
6138 int err;
6139
6140 err = btrfs_del_inode_ref(trans, root, name, name_len,
6141 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006142 if (err)
6143 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006144 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006145
6146 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006147 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006148}
6149
6150static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006151 struct btrfs_inode *dir, struct dentry *dentry,
6152 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006153{
Josef Bacika1b075d2010-11-19 20:36:11 +00006154 int err = btrfs_add_link(trans, dir, inode,
6155 dentry->d_name.name, dentry->d_name.len,
6156 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006157 if (err > 0)
6158 err = -EEXIST;
6159 return err;
6160}
6161
Josef Bacik618e21d2007-07-11 10:18:17 -04006162static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006163 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006164{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006165 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006166 struct btrfs_trans_handle *trans;
6167 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006168 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006169 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006170 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006171 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006172
Josef Bacik9ed74f22009-09-11 16:12:44 -04006173 /*
6174 * 2 for inode item and ref
6175 * 2 for dir items
6176 * 1 for xattr if selinux is on
6177 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006178 trans = btrfs_start_transaction(root, 5);
6179 if (IS_ERR(trans))
6180 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006181
Li Zefan581bb052011-04-20 10:06:11 +08006182 err = btrfs_find_free_ino(root, &objectid);
6183 if (err)
6184 goto out_unlock;
6185
Josef Bacikaec74772008-07-24 12:12:38 -04006186 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006187 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6188 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006189 if (IS_ERR(inode)) {
6190 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006191 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006192 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006193 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006194
Casey Schauflerad19db72011-12-15 10:09:07 -05006195 /*
6196 * If the active LSM wants to access the inode during
6197 * d_instantiate it needs these. Smack checks to see
6198 * if the filesystem supports xattrs by looking at the
6199 * ops vector.
6200 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006201 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006202 init_special_inode(inode, inode->i_mode, rdev);
6203
6204 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006205 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006206 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006207
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006208 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6209 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006210 if (err)
6211 goto out_unlock;
6212
6213 btrfs_update_inode(trans, root, inode);
6214 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006215
Josef Bacik618e21d2007-07-11 10:18:17 -04006216out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006217 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006218 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006219 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006220 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006221 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006222 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006223 return err;
6224}
6225
Chris Mason39279cc2007-06-12 06:35:45 -04006226static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006227 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006228{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006229 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006230 struct btrfs_trans_handle *trans;
6231 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006232 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006233 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006234 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006235 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006236
Josef Bacik9ed74f22009-09-11 16:12:44 -04006237 /*
6238 * 2 for inode item and ref
6239 * 2 for dir items
6240 * 1 for xattr if selinux is on
6241 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006242 trans = btrfs_start_transaction(root, 5);
6243 if (IS_ERR(trans))
6244 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006245
Li Zefan581bb052011-04-20 10:06:11 +08006246 err = btrfs_find_free_ino(root, &objectid);
6247 if (err)
6248 goto out_unlock;
6249
Josef Bacikaec74772008-07-24 12:12:38 -04006250 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006251 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6252 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006253 if (IS_ERR(inode)) {
6254 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006255 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006256 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006257 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006258 /*
6259 * If the active LSM wants to access the inode during
6260 * d_instantiate it needs these. Smack checks to see
6261 * if the filesystem supports xattrs by looking at the
6262 * ops vector.
6263 */
6264 inode->i_fop = &btrfs_file_operations;
6265 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006266 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006267
6268 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6269 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006270 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006271
6272 err = btrfs_update_inode(trans, root, inode);
6273 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006274 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006275
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006276 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6277 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006278 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006279 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006280
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006281 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006282 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006283
Chris Mason39279cc2007-06-12 06:35:45 -04006284out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006285 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006286 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006287 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006288 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006289 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006290 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006291 return err;
6292}
6293
6294static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6295 struct dentry *dentry)
6296{
Filipe Manana271dba452016-01-05 16:24:05 +00006297 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006298 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006299 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006300 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006301 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006302 int err;
6303 int drop_inode = 0;
6304
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006305 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006306 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006307 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006308
Mark Fashehf1863732012-08-08 11:32:27 -07006309 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006310 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006311
Nikolay Borisov877574e2017-02-20 13:50:33 +02006312 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006313 if (err)
6314 goto fail;
6315
Yan, Zhenga22285a2010-05-16 10:48:46 -04006316 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006317 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006318 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006319 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006320 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006321 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006322 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006323 if (IS_ERR(trans)) {
6324 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006325 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006326 goto fail;
6327 }
Chris Mason5f39d392007-10-15 16:14:19 -04006328
Miao Xie67de1172013-12-26 13:07:06 +08006329 /* There are several dir indexes for this inode, clear the cache. */
6330 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006331 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006332 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006333 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006334 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006335 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006336
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006337 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6338 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006339
Yan, Zhenga5719522009-09-24 09:17:31 -04006340 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006341 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006342 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006343 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006344 int ret;
6345
Yan, Zhenga5719522009-09-24 09:17:31 -04006346 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006347 if (err)
6348 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006349 if (inode->i_nlink == 1) {
6350 /*
6351 * If new hard link count is 1, it's a file created
6352 * with open(2) O_TMPFILE flag.
6353 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006354 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006355 if (err)
6356 goto fail;
6357 }
Al Viro08c422c2011-12-23 07:58:13 -05006358 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006359 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6360 true, NULL);
6361 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6362 err = btrfs_commit_transaction(trans);
6363 trans = NULL;
6364 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006365 }
Chris Mason39279cc2007-06-12 06:35:45 -04006366
Chris Mason1832a6d2007-12-21 16:27:21 -05006367fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006368 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006369 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006370 if (drop_inode) {
6371 inode_dec_link_count(inode);
6372 iput(inode);
6373 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006374 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006375 return err;
6376}
6377
Al Viro18bb1db2011-07-26 01:41:39 -04006378static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006379{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006380 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006381 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006382 struct btrfs_trans_handle *trans;
6383 struct btrfs_root *root = BTRFS_I(dir)->root;
6384 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006385 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006386 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006387
Josef Bacik9ed74f22009-09-11 16:12:44 -04006388 /*
6389 * 2 items for inode and ref
6390 * 2 items for dir items
6391 * 1 for xattr if selinux is on
6392 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006393 trans = btrfs_start_transaction(root, 5);
6394 if (IS_ERR(trans))
6395 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006396
Li Zefan581bb052011-04-20 10:06:11 +08006397 err = btrfs_find_free_ino(root, &objectid);
6398 if (err)
6399 goto out_fail;
6400
Josef Bacikaec74772008-07-24 12:12:38 -04006401 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006402 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6403 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006404 if (IS_ERR(inode)) {
6405 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006406 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006407 goto out_fail;
6408 }
Chris Mason5f39d392007-10-15 16:14:19 -04006409
Chris Masonb0d5d102014-09-08 13:08:51 -07006410 /* these must be set before we unlock the inode */
6411 inode->i_op = &btrfs_dir_inode_operations;
6412 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006413
Eric Paris2a7dba32011-02-01 11:05:39 -05006414 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006415 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006416 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006417
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006418 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006419 err = btrfs_update_inode(trans, root, inode);
6420 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006421 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006422
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006423 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6424 dentry->d_name.name,
6425 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006426 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006427 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006428
Al Viro1e2e5472018-05-04 08:23:01 -04006429 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006430
6431out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006432 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006433 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006434 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006435 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006436 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006437 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006438 return err;
6439}
6440
Chris Masonc8b97812008-10-29 14:49:59 -04006441static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006442 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006443 size_t pg_offset, u64 extent_offset,
6444 struct btrfs_file_extent_item *item)
6445{
6446 int ret;
6447 struct extent_buffer *leaf = path->nodes[0];
6448 char *tmp;
6449 size_t max_size;
6450 unsigned long inline_size;
6451 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006452 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006453
6454 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006455 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006456 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6457 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006458 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006459 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006460 if (!tmp)
6461 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006462 ptr = btrfs_file_extent_inline_start(item);
6463
6464 read_extent_buffer(leaf, tmp, ptr, inline_size);
6465
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006466 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006467 ret = btrfs_decompress(compress_type, tmp, page,
6468 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006469
6470 /*
6471 * decompression code contains a memset to fill in any space between the end
6472 * of the uncompressed data and the end of max_size in case the decompressed
6473 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6474 * the end of an inline extent and the beginning of the next block, so we
6475 * cover that region here.
6476 */
6477
6478 if (max_size + pg_offset < PAGE_SIZE) {
6479 char *map = kmap(page);
6480 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6481 kunmap(page);
6482 }
Chris Masonc8b97812008-10-29 14:49:59 -04006483 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006484 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006485}
6486
Omar Sandoval39b07b52019-12-02 17:34:23 -08006487/**
6488 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6489 * @inode: file to search in
6490 * @page: page to read extent data into if the extent is inline
6491 * @pg_offset: offset into @page to copy to
6492 * @start: file offset
6493 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006494 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006495 * This returns the first &struct extent_map which overlaps with the given
6496 * range, reading it from the B-tree and caching it if necessary. Note that
6497 * there may be more extents which overlap the given range after the returned
6498 * extent_map.
6499 *
6500 * If @page is not NULL and the extent is inline, this also reads the extent
6501 * data directly into the page and marks the extent up to date in the io_tree.
6502 *
6503 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006504 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006505struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006506 struct page *page, size_t pg_offset,
6507 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006508{
David Sterba3ffbd682018-06-29 10:56:42 +02006509 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006510 int ret;
6511 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006512 u64 extent_start = 0;
6513 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006514 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006515 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006516 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006517 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006518 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006519 struct extent_buffer *leaf;
6520 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006521 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006522 struct extent_map_tree *em_tree = &inode->extent_tree;
6523 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006524
Chris Mason890871b2009-09-02 16:24:52 -04006525 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006526 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006527 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006528
Chris Masona52d9a82007-08-27 16:49:44 -04006529 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006530 if (em->start > start || em->start + em->len <= start)
6531 free_extent_map(em);
6532 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006533 free_extent_map(em);
6534 else
6535 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006536 }
David Sterba172ddd62011-04-21 00:48:27 +02006537 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006538 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006539 err = -ENOMEM;
6540 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006541 }
Chris Masond1310b22008-01-24 16:13:08 -05006542 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006543 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006544 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006545 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006546
Liu Bobee6ec82018-08-17 05:05:28 +08006547 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006548 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006549 err = -ENOMEM;
6550 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006551 }
6552
Liu Bobee6ec82018-08-17 05:05:28 +08006553 /* Chances are we'll be called again, so go ahead and do readahead */
6554 path->reada = READA_FORWARD;
6555
Liu Boe49aabd2018-08-25 13:47:09 +08006556 /*
6557 * Unless we're going to uncompress the inline extent, no sleep would
6558 * happen.
6559 */
6560 path->leave_spinning = 1;
6561
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006562 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006563 if (ret < 0) {
6564 err = ret;
6565 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006566 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006567 if (path->slots[0] == 0)
6568 goto not_found;
6569 path->slots[0]--;
6570 }
6571
Chris Mason5f39d392007-10-15 16:14:19 -04006572 leaf = path->nodes[0];
6573 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006574 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006575 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006576 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006577 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006578 /*
6579 * If we backup past the first extent we want to move forward
6580 * and see if there is an extent in front of us, otherwise we'll
6581 * say there is a hole for our whole search range which can
6582 * cause problems.
6583 */
6584 extent_end = start;
6585 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006586 }
6587
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006588 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006589 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006590 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006591 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6592 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006593 /* Only regular file could have regular/prealloc extent */
6594 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6595 ret = -EUCLEAN;
6596 btrfs_crit(fs_info,
6597 "regular/prealloc extent found for non-regular inode %llu",
6598 btrfs_ino(inode));
6599 goto out;
6600 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006601 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6602 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006603 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006604 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6605 path->slots[0],
6606 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006607 }
Josef Bacik25a50342013-10-14 12:08:38 -04006608next:
Yan Zheng9036c102008-10-30 14:19:41 -04006609 if (start >= extent_end) {
6610 path->slots[0]++;
6611 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6612 ret = btrfs_next_leaf(root, path);
6613 if (ret < 0) {
6614 err = ret;
6615 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006616 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006617 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006618 }
Yan Zheng9036c102008-10-30 14:19:41 -04006619 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006620 }
Yan Zheng9036c102008-10-30 14:19:41 -04006621 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6622 if (found_key.objectid != objectid ||
6623 found_key.type != BTRFS_EXTENT_DATA_KEY)
6624 goto not_found;
6625 if (start + len <= found_key.offset)
6626 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006627 if (start > found_key.offset)
6628 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006629
6630 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006631 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006632 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006633 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006634 em->block_start = EXTENT_MAP_HOLE;
6635 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006636 }
6637
Omar Sandoval39b07b52019-12-02 17:34:23 -08006638 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006639
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006640 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6641 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006642 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006643 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006644 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006645 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006646 size_t size;
6647 size_t extent_offset;
6648 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006649
Omar Sandoval39b07b52019-12-02 17:34:23 -08006650 if (!page)
Yan689f9342007-10-29 11:41:07 -04006651 goto out;
Yan689f9342007-10-29 11:41:07 -04006652
Qu Wenruoe41ca582018-06-06 15:41:49 +08006653 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006654 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006655 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6656 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006657 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006658 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006659 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006660 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006661 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006662
6663 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006664 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006665 if (btrfs_file_extent_compression(leaf, item) !=
6666 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006667 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006668 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006669 if (ret) {
6670 err = ret;
6671 goto out;
6672 }
Chris Masonc8b97812008-10-29 14:49:59 -04006673 } else {
6674 map = kmap(page);
6675 read_extent_buffer(leaf, map + pg_offset, ptr,
6676 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006677 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006678 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006679 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006680 copy_size);
6681 }
Chris Masonc8b97812008-10-29 14:49:59 -04006682 kunmap(page);
6683 }
Chris Mason179e29e2007-11-01 11:28:41 -04006684 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006685 }
Chris Masond1310b22008-01-24 16:13:08 -05006686 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006687 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006688 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006689 }
6690not_found:
6691 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006692 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006693 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006694 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006695insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006696 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006697 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006698 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006699 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6700 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006701 err = -EIO;
6702 goto out;
6703 }
Chris Masond1310b22008-01-24 16:13:08 -05006704
6705 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006706 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006707 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006708 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006709out:
Liu Boc6414282018-08-23 07:36:17 +08006710 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006711
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006712 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006713
Chris Masona52d9a82007-08-27 16:49:44 -04006714 if (err) {
6715 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006716 return ERR_PTR(err);
6717 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006718 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006719 return em;
6720}
6721
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006722struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006723 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006724{
6725 struct extent_map *em;
6726 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006727 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006728 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006729 u64 delalloc_len;
6730 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006731 int err = 0;
6732
Omar Sandoval39b07b52019-12-02 17:34:23 -08006733 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006734 if (IS_ERR(em))
6735 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006736 /*
6737 * If our em maps to:
6738 * - a hole or
6739 * - a pre-alloc extent,
6740 * there might actually be delalloc bytes behind it.
6741 */
6742 if (em->block_start != EXTENT_MAP_HOLE &&
6743 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6744 return em;
6745 else
6746 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006747
6748 /* check to see if we've wrapped (len == -1 or similar) */
6749 end = start + len;
6750 if (end < start)
6751 end = (u64)-1;
6752 else
6753 end -= 1;
6754
6755 em = NULL;
6756
6757 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006758 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006759 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006760 delalloc_end = delalloc_start + delalloc_len;
6761 if (delalloc_end < delalloc_start)
6762 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006763
6764 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006765 * We didn't find anything useful, return the original results from
6766 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006767 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006768 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006769 em = hole_em;
6770 hole_em = NULL;
6771 goto out;
6772 }
6773
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006774 /*
6775 * Adjust the delalloc_start to make sure it doesn't go backwards from
6776 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006777 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006778 delalloc_start = max(start, delalloc_start);
6779 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006780
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006781 if (delalloc_len > 0) {
6782 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006783 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006784 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006785
David Sterba172ddd62011-04-21 00:48:27 +02006786 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006787 if (!em) {
6788 err = -ENOMEM;
6789 goto out;
6790 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006791
6792 ASSERT(hole_em);
6793 /*
6794 * When btrfs_get_extent can't find anything it returns one
6795 * huge hole
6796 *
6797 * Make sure what it found really fits our range, and adjust to
6798 * make sure it is based on the start from the caller
6799 */
6800 if (hole_end <= start || hole_em->start > end) {
6801 free_extent_map(hole_em);
6802 hole_em = NULL;
6803 } else {
6804 hole_start = max(hole_em->start, start);
6805 hole_len = hole_end - hole_start;
6806 }
6807
6808 if (hole_em && delalloc_start > hole_start) {
6809 /*
6810 * Our hole starts before our delalloc, so we have to
6811 * return just the parts of the hole that go until the
6812 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006813 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006814 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006815 em->start = hole_start;
6816 em->orig_start = hole_start;
6817 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006818 * Don't adjust block start at all, it is fixed at
6819 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006820 */
6821 em->block_start = hole_em->block_start;
6822 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006823 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6824 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006825 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006826 /*
6827 * Hole is out of passed range or it starts after
6828 * delalloc range
6829 */
6830 em->start = delalloc_start;
6831 em->len = delalloc_len;
6832 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006833 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006834 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006835 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006836 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006837 return hole_em;
6838 }
6839out:
6840
6841 free_extent_map(hole_em);
6842 if (err) {
6843 free_extent_map(em);
6844 return ERR_PTR(err);
6845 }
6846 return em;
6847}
6848
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006849static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6850 const u64 start,
6851 const u64 len,
6852 const u64 orig_start,
6853 const u64 block_start,
6854 const u64 block_len,
6855 const u64 orig_block_len,
6856 const u64 ram_bytes,
6857 const int type)
6858{
6859 struct extent_map *em = NULL;
6860 int ret;
6861
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006862 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006863 em = create_io_em(inode, start, len, orig_start,
6864 block_start, block_len, orig_block_len,
6865 ram_bytes,
6866 BTRFS_COMPRESS_NONE, /* compress_type */
6867 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006868 if (IS_ERR(em))
6869 goto out;
6870 }
6871 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6872 len, block_len, type);
6873 if (ret) {
6874 if (em) {
6875 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006876 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006877 start + len - 1, 0);
6878 }
6879 em = ERR_PTR(ret);
6880 }
6881 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006882
6883 return em;
6884}
6885
Josef Bacik4b46fce2010-05-23 11:00:55 -04006886static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6887 u64 start, u64 len)
6888{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006890 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006891 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006892 struct btrfs_key ins;
6893 u64 alloc_hint;
6894 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006895
Josef Bacik4b46fce2010-05-23 11:00:55 -04006896 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006897 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006898 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006899 if (ret)
6900 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006901
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006902 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6903 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006904 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006905 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006906 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006907 btrfs_free_reserved_extent(fs_info, ins.objectid,
6908 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006909
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910 return em;
6911}
6912
Chris Mason46bfbb52010-05-26 11:04:10 -04006913/*
6914 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6915 * block must be cow'd
6916 */
Josef Bacik00361582013-08-14 14:02:47 -04006917noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006918 u64 *orig_start, u64 *orig_block_len,
6919 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006920{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006922 struct btrfs_path *path;
6923 int ret;
6924 struct extent_buffer *leaf;
6925 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006926 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006927 struct btrfs_file_extent_item *fi;
6928 struct btrfs_key key;
6929 u64 disk_bytenr;
6930 u64 backref_offset;
6931 u64 extent_end;
6932 u64 num_bytes;
6933 int slot;
6934 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006935 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006936
Chris Mason46bfbb52010-05-26 11:04:10 -04006937 path = btrfs_alloc_path();
6938 if (!path)
6939 return -ENOMEM;
6940
David Sterbaf85b7372017-01-20 14:54:07 +01006941 ret = btrfs_lookup_file_extent(NULL, root, path,
6942 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006943 if (ret < 0)
6944 goto out;
6945
6946 slot = path->slots[0];
6947 if (ret == 1) {
6948 if (slot == 0) {
6949 /* can't find the item, must cow */
6950 ret = 0;
6951 goto out;
6952 }
6953 slot--;
6954 }
6955 ret = 0;
6956 leaf = path->nodes[0];
6957 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006958 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006959 key.type != BTRFS_EXTENT_DATA_KEY) {
6960 /* not our file or wrong item type, must cow */
6961 goto out;
6962 }
6963
6964 if (key.offset > offset) {
6965 /* Wrong offset, must cow */
6966 goto out;
6967 }
6968
6969 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6970 found_type = btrfs_file_extent_type(leaf, fi);
6971 if (found_type != BTRFS_FILE_EXTENT_REG &&
6972 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6973 /* not a regular extent, must cow */
6974 goto out;
6975 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006976
6977 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6978 goto out;
6979
Miao Xiee77751a2013-12-27 21:11:50 +08006980 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6981 if (extent_end <= offset)
6982 goto out;
6983
Chris Mason46bfbb52010-05-26 11:04:10 -04006984 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006985 if (disk_bytenr == 0)
6986 goto out;
6987
6988 if (btrfs_file_extent_compression(leaf, fi) ||
6989 btrfs_file_extent_encryption(leaf, fi) ||
6990 btrfs_file_extent_other_encoding(leaf, fi))
6991 goto out;
6992
Ethan Lien78d42952018-05-17 14:58:29 +08006993 /*
6994 * Do the same check as in btrfs_cross_ref_exist but without the
6995 * unnecessary search.
6996 */
6997 if (btrfs_file_extent_generation(leaf, fi) <=
6998 btrfs_root_last_snapshot(&root->root_item))
6999 goto out;
7000
Chris Mason46bfbb52010-05-26 11:04:10 -04007001 backref_offset = btrfs_file_extent_offset(leaf, fi);
7002
Josef Bacik7ee9e442013-06-21 16:37:03 -04007003 if (orig_start) {
7004 *orig_start = key.offset - backref_offset;
7005 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7006 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7007 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007008
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007009 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007010 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007011
7012 num_bytes = min(offset + *len, extent_end) - offset;
7013 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7014 u64 range_end;
7015
Jeff Mahoneyda170662016-06-15 09:22:56 -04007016 range_end = round_up(offset + num_bytes,
7017 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007018 ret = test_range_bit(io_tree, offset, range_end,
7019 EXTENT_DELALLOC, 0, NULL);
7020 if (ret) {
7021 ret = -EAGAIN;
7022 goto out;
7023 }
7024 }
7025
Josef Bacik1bda19e2013-10-18 12:10:36 -04007026 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007027
7028 /*
7029 * look for other files referencing this extent, if we
7030 * find any we must cow
7031 */
Josef Bacik00361582013-08-14 14:02:47 -04007032
Liu Boe4c3b2d2017-01-30 12:25:28 -08007033 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007034 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007035 if (ret) {
7036 ret = 0;
7037 goto out;
7038 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007039
7040 /*
7041 * adjust disk_bytenr and num_bytes to cover just the bytes
7042 * in this extent we are about to write. If there
7043 * are any csums in that range we have to cow in order
7044 * to keep the csums correct
7045 */
7046 disk_bytenr += backref_offset;
7047 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007048 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7049 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007050 /*
7051 * all of the above have passed, it is safe to overwrite this extent
7052 * without cow
7053 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007054 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007055 ret = 1;
7056out:
7057 btrfs_free_path(path);
7058 return ret;
7059}
7060
Josef Bacikeb838e72012-07-31 16:28:48 -04007061static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007062 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007063{
7064 struct btrfs_ordered_extent *ordered;
7065 int ret = 0;
7066
7067 while (1) {
7068 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007069 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007070 /*
7071 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007072 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007073 * extents in this range.
7074 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007075 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007076 lockend - lockstart + 1);
7077
7078 /*
7079 * We need to make sure there are no buffered pages in this
7080 * range either, we could have raced between the invalidate in
7081 * generic_file_direct_write and locking the extent. The
7082 * invalidate needs to happen so that reads after a write do not
7083 * get stale data.
7084 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007085 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007086 (!writing || !filemap_range_has_page(inode->i_mapping,
7087 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007088 break;
7089
7090 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007091 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007092
7093 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007094 /*
7095 * If we are doing a DIO read and the ordered extent we
7096 * found is for a buffered write, we can not wait for it
7097 * to complete and retry, because if we do so we can
7098 * deadlock with concurrent buffered writes on page
7099 * locks. This happens only if our DIO read covers more
7100 * than one extent map, if at this point has already
7101 * created an ordered extent for a previous extent map
7102 * and locked its range in the inode's io tree, and a
7103 * concurrent write against that previous extent map's
7104 * range and this range started (we unlock the ranges
7105 * in the io tree only when the bios complete and
7106 * buffered writes always lock pages before attempting
7107 * to lock range in the io tree).
7108 */
7109 if (writing ||
7110 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7111 btrfs_start_ordered_extent(inode, ordered, 1);
7112 else
7113 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007114 btrfs_put_ordered_extent(ordered);
7115 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007116 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007117 * We could trigger writeback for this range (and wait
7118 * for it to complete) and then invalidate the pages for
7119 * this range (through invalidate_inode_pages2_range()),
7120 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007121 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007122 * triggered a readahead) on a page lock due to an
7123 * ordered dio extent we created before but did not have
7124 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007125 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007126 * ordered extent to complete while holding a lock on
7127 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007128 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007129 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007130 }
7131
Filipe Mananaade77022016-02-18 14:28:55 +00007132 if (ret)
7133 break;
7134
Josef Bacikeb838e72012-07-31 16:28:48 -04007135 cond_resched();
7136 }
7137
7138 return ret;
7139}
7140
Liu Bo6f9994d2017-01-31 07:50:22 -08007141/* The callers of this must take lock_extent() */
7142static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7143 u64 orig_start, u64 block_start,
7144 u64 block_len, u64 orig_block_len,
7145 u64 ram_bytes, int compress_type,
7146 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007147{
7148 struct extent_map_tree *em_tree;
7149 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007150 int ret;
7151
Liu Bo6f9994d2017-01-31 07:50:22 -08007152 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7153 type == BTRFS_ORDERED_COMPRESSED ||
7154 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007155 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007156
Josef Bacik69ffb542012-09-11 15:40:07 -04007157 em_tree = &BTRFS_I(inode)->extent_tree;
7158 em = alloc_extent_map();
7159 if (!em)
7160 return ERR_PTR(-ENOMEM);
7161
7162 em->start = start;
7163 em->orig_start = orig_start;
7164 em->len = len;
7165 em->block_len = block_len;
7166 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007167 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007168 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007169 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007170 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007171 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007172 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007173 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007174 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7175 em->compress_type = compress_type;
7176 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007177
7178 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007179 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007180 em->start + em->len - 1, 0);
7181 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007182 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007183 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007184 /*
7185 * The caller has taken lock_extent(), who could race with us
7186 * to add em?
7187 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007188 } while (ret == -EEXIST);
7189
7190 if (ret) {
7191 free_extent_map(em);
7192 return ERR_PTR(ret);
7193 }
7194
Liu Bo6f9994d2017-01-31 07:50:22 -08007195 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007196 return em;
7197}
7198
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007199
David Sterba55e20bd2020-06-09 19:56:06 +02007200static int btrfs_get_blocks_direct_read(struct extent_map *em,
7201 struct buffer_head *bh_result,
7202 struct inode *inode,
7203 u64 start, u64 len)
7204{
7205 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7206
7207 if (em->block_start == EXTENT_MAP_HOLE ||
7208 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7209 return -ENOENT;
7210
7211 len = min(len, em->len - (start - em->start));
7212
7213 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7214 inode->i_blkbits;
7215 bh_result->b_size = len;
7216 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7217 set_buffer_mapped(bh_result);
7218
7219 return 0;
7220}
7221
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007222static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007223 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007224 struct inode *inode,
7225 struct btrfs_dio_data *dio_data,
7226 u64 start, u64 len)
7227{
7228 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7229 struct extent_map *em = *map;
7230 int ret = 0;
7231
7232 /*
7233 * We don't allocate a new extent in the following cases
7234 *
7235 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7236 * existing extent.
7237 * 2) The extent is marked as PREALLOC. We're good to go here and can
7238 * just use the extent.
7239 *
7240 */
7241 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7242 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7243 em->block_start != EXTENT_MAP_HOLE)) {
7244 int type;
7245 u64 block_start, orig_start, orig_block_len, ram_bytes;
7246
7247 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7248 type = BTRFS_ORDERED_PREALLOC;
7249 else
7250 type = BTRFS_ORDERED_NOCOW;
7251 len = min(len, em->len - (start - em->start));
7252 block_start = em->block_start + (start - em->start);
7253
7254 if (can_nocow_extent(inode, start, &len, &orig_start,
7255 &orig_block_len, &ram_bytes) == 1 &&
7256 btrfs_inc_nocow_writers(fs_info, block_start)) {
7257 struct extent_map *em2;
7258
7259 em2 = btrfs_create_dio_extent(inode, start, len,
7260 orig_start, block_start,
7261 len, orig_block_len,
7262 ram_bytes, type);
7263 btrfs_dec_nocow_writers(fs_info, block_start);
7264 if (type == BTRFS_ORDERED_PREALLOC) {
7265 free_extent_map(em);
7266 *map = em = em2;
7267 }
7268
7269 if (em2 && IS_ERR(em2)) {
7270 ret = PTR_ERR(em2);
7271 goto out;
7272 }
7273 /*
7274 * For inode marked NODATACOW or extent marked PREALLOC,
7275 * use the existing or preallocated extent, so does not
7276 * need to adjust btrfs_space_info's bytes_may_use.
7277 */
Filipe Manana46d4dac2020-06-09 11:19:33 +01007278 btrfs_free_reserved_data_space_noquota(inode, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007279 goto skip_cow;
7280 }
7281 }
7282
7283 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007284 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007285 free_extent_map(em);
7286 *map = em = btrfs_new_extent_direct(inode, start, len);
7287 if (IS_ERR(em)) {
7288 ret = PTR_ERR(em);
7289 goto out;
7290 }
7291
7292 len = min(len, em->len - (start - em->start));
7293
7294skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007295 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7296 inode->i_blkbits;
7297 bh_result->b_size = len;
7298 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7299 set_buffer_mapped(bh_result);
7300
7301 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7302 set_buffer_new(bh_result);
7303
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007304 /*
7305 * Need to update the i_size under the extent lock so buffered
7306 * readers will get the updated i_size when we unlock.
7307 */
David Sterba55e20bd2020-06-09 19:56:06 +02007308 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007309 i_size_write(inode, start + len);
7310
David Sterba55e20bd2020-06-09 19:56:06 +02007311 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007312 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007313 dio_data->unsubmitted_oe_range_end = start + len;
7314 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007315out:
7316 return ret;
7317}
7318
David Sterba55e20bd2020-06-09 19:56:06 +02007319static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7320 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007323 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007324 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307325 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007326 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007327 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007328 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007329 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007330
David Sterba55e20bd2020-06-09 19:56:06 +02007331 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007332 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007333
Josef Bacikc3298612012-08-03 16:49:19 -04007334 lockstart = start;
7335 lockend = start + len - 1;
7336
David Sterba55e20bd2020-06-09 19:56:06 +02007337 if (current->journal_info) {
7338 /*
7339 * Need to pull our outstanding extents and set journal_info to NULL so
7340 * that anything that needs to check if there's a transaction doesn't get
7341 * confused.
7342 */
7343 dio_data = current->journal_info;
7344 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007345 }
7346
Josef Bacikeb838e72012-07-31 16:28:48 -04007347 /*
7348 * If this errors out it's because we couldn't invalidate pagecache for
7349 * this range and we need to fallback to buffered.
7350 */
David Sterba55e20bd2020-06-09 19:56:06 +02007351 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7352 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007353 ret = -ENOTBLK;
7354 goto err;
7355 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007356
Omar Sandoval39b07b52019-12-02 17:34:23 -08007357 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007358 if (IS_ERR(em)) {
7359 ret = PTR_ERR(em);
7360 goto unlock_err;
7361 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007362
7363 /*
7364 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7365 * io. INLINE is special, and we could probably kludge it in here, but
7366 * it's still buffered so for safety lets just fall back to the generic
7367 * buffered path.
7368 *
7369 * For COMPRESSED we _have_ to read the entire extent in so we can
7370 * decompress it, so there will be buffering required no matter what we
7371 * do, so go ahead and fallback to buffered.
7372 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007373 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007374 * to buffered IO. Don't blame me, this is the price we pay for using
7375 * the generic code.
7376 */
7377 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7378 em->block_start == EXTENT_MAP_INLINE) {
7379 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007380 ret = -ENOTBLK;
7381 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007382 }
7383
David Sterba55e20bd2020-06-09 19:56:06 +02007384 if (create) {
7385 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7386 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007387 if (ret < 0)
7388 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007389
7390 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7391 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007392 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007393 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7394 start, len);
7395 /* Can be negative only if we read from a hole */
7396 if (ret < 0) {
7397 ret = 0;
7398 free_extent_map(em);
7399 goto unlock_err;
7400 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007401 /*
7402 * We need to unlock only the end area that we aren't using.
7403 * The rest is going to be unlocked by the endio routine.
7404 */
David Sterba55e20bd2020-06-09 19:56:06 +02007405 lockstart = start + bh_result->b_size;
7406 if (lockstart < lockend) {
7407 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7408 lockstart, lockend, &cached_state);
7409 } else {
7410 free_extent_state(cached_state);
7411 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007412 }
7413
Josef Bacik4b46fce2010-05-23 11:00:55 -04007414 free_extent_map(em);
7415
7416 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007417
7418unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007419 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7420 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007421err:
David Sterba55e20bd2020-06-09 19:56:06 +02007422 if (dio_data)
7423 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007424 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007425}
7426
Omar Sandoval769b4f22020-04-16 14:46:22 -07007427static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7428{
7429 /*
7430 * This implies a barrier so that stores to dio_bio->bi_status before
7431 * this and loads of dio_bio->bi_status after this are fully ordered.
7432 */
7433 if (!refcount_dec_and_test(&dip->refs))
7434 return;
7435
7436 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7437 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7438 dip->bytes,
7439 !dip->dio_bio->bi_status);
7440 } else {
7441 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7442 dip->logical_offset,
7443 dip->logical_offset + dip->bytes - 1);
7444 }
7445
David Sterba55e20bd2020-06-09 19:56:06 +02007446 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007447 kfree(dip);
7448}
7449
Omar Sandoval77d5d682020-04-16 14:46:25 -07007450static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7451 int mirror_num,
7452 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007453{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007454 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007455 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007456 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007457
Mike Christie37226b22016-06-05 14:31:52 -05007458 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007459
Omar Sandoval5c047a62020-04-16 14:46:24 -07007460 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007461 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007462 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007463
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007464 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007465 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007466 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007467 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007468 return ret;
7469}
7470
Omar Sandoval769b4f22020-04-16 14:46:22 -07007471static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7472 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007473 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007474{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007475 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7476 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007477 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007478 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7479 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007480 struct bio_vec bvec;
7481 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007482 u64 start = io_bio->logical;
7483 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007484 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007485
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007486 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7487 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307488
Liu Bo17347ce2017-05-15 15:33:27 -07007489 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7490 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007491 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007492 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007493 if (uptodate &&
7494 (!csum || !check_data_csum(inode, io_bio, icsum,
7495 bvec.bv_page, pgoff,
7496 start, sectorsize))) {
7497 clean_io_failure(fs_info, failure_tree, io_tree,
7498 start, bvec.bv_page,
7499 btrfs_ino(BTRFS_I(inode)),
7500 pgoff);
7501 } else {
7502 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007503
Omar Sandoval77d5d682020-04-16 14:46:25 -07007504 status = btrfs_submit_read_repair(inode,
7505 &io_bio->bio,
7506 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007507 bvec.bv_page, pgoff,
7508 start,
7509 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007510 io_bio->mirror_num,
7511 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007512 if (status)
7513 err = status;
7514 }
7515 start += sectorsize;
7516 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307517 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307518 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007519 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007520 return err;
7521}
7522
Qu Wenruo524272602017-03-08 10:25:52 +08007523static void __endio_write_update_ordered(struct inode *inode,
7524 const u64 offset, const u64 bytes,
7525 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007526{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007527 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007528 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007529 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007530 u64 ordered_offset = offset;
7531 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007532 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007533
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007534 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007535 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007536 else
Qu Wenruo524272602017-03-08 10:25:52 +08007537 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007538
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007539 while (ordered_offset < offset + bytes) {
7540 last_offset = ordered_offset;
7541 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7542 &ordered_offset,
7543 ordered_bytes,
7544 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007545 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7546 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007547 btrfs_queue_work(wq, &ordered->work);
7548 }
7549 /*
7550 * If btrfs_dec_test_ordered_pending does not find any ordered
7551 * extent in the range, we can exit.
7552 */
7553 if (ordered_offset == last_offset)
7554 return;
7555 /*
7556 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007557 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007558 */
7559 if (ordered_offset < offset + bytes) {
7560 ordered_bytes = offset + bytes - ordered_offset;
7561 ordered = NULL;
7562 }
Chris Mason163cf092010-11-28 19:56:33 -05007563 }
Filipe Manana14543772015-11-24 16:23:54 +00007564}
7565
David Sterbad0ee3932018-03-08 14:35:48 +01007566static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007567 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007568{
Josef Bacikc6100a42017-05-05 11:57:13 -04007569 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007570 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007571 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007572 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007573 return 0;
7574}
7575
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007576static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007577{
7578 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007579 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007580
Miao Xie8b110e32014-09-12 18:44:03 +08007581 if (err)
7582 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007583 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007584 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7585 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007586 (unsigned long long)bio->bi_iter.bi_sector,
7587 bio->bi_iter.bi_size, err);
7588
Omar Sandoval769b4f22020-04-16 14:46:22 -07007589 if (bio_op(bio) == REQ_OP_READ) {
7590 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007591 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007592 }
7593
Omar Sandoval769b4f22020-04-16 14:46:22 -07007594 if (err)
7595 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007596
Miao Xiee65e1532010-11-22 03:04:43 +00007597 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007598 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007599}
7600
David Sterbad0ee3932018-03-08 14:35:48 +01007601static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7602 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007605 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007606 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007607 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007608
Liu Bo4c274bc2017-11-01 17:19:27 -06007609 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007610 if (async_submit)
7611 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7612
Josef Bacik5fd02042012-05-02 14:00:54 -04007613 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007614 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007615 if (ret)
7616 goto err;
7617 }
Miao Xiee65e1532010-11-22 03:04:43 +00007618
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007619 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007620 goto map;
7621
7622 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007623 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7624 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007625 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007626 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007627 } else if (write) {
7628 /*
7629 * If we aren't doing async submit, calculate the csum of the
7630 * bio now.
7631 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007632 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007633 if (ret)
7634 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007635 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007636 u64 csum_offset;
7637
7638 csum_offset = file_offset - dip->logical_offset;
7639 csum_offset >>= inode->i_sb->s_blocksize_bits;
7640 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7641 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007642 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007643map:
Chris Mason08635ba2019-07-10 12:28:14 -07007644 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007645err:
Miao Xiee65e1532010-11-22 03:04:43 +00007646 return ret;
7647}
7648
Omar Sandovalc36cac22020-04-16 14:46:13 -07007649/*
7650 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7651 * or ordered extents whether or not we submit any bios.
7652 */
7653static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7654 struct inode *inode,
7655 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007656{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007657 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007658 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7659 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007660 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007661
Omar Sandoval85879572020-04-16 14:46:21 -07007662 dip_size = sizeof(*dip);
7663 if (!write && csum) {
7664 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7665 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7666 size_t nblocks;
7667
7668 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7669 dip_size += csum_size * nblocks;
7670 }
7671
7672 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007673 if (!dip)
7674 return NULL;
7675
Omar Sandovalc36cac22020-04-16 14:46:13 -07007676 dip->inode = inode;
7677 dip->logical_offset = file_offset;
7678 dip->bytes = dio_bio->bi_iter.bi_size;
7679 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007680 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007681 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007682
7683 if (write) {
7684 struct btrfs_dio_data *dio_data = current->journal_info;
7685
7686 /*
7687 * Setting range start and end to the same value means that
7688 * no cleanup will happen in btrfs_direct_IO
7689 */
7690 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7691 dip->bytes;
7692 dio_data->unsubmitted_oe_range_start =
7693 dio_data->unsubmitted_oe_range_end;
7694 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007695 return dip;
7696}
7697
David Sterba55e20bd2020-06-09 19:56:06 +02007698static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7699 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007700{
7701 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007702 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007703 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007704 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7705 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007706 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007707 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007708 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007709 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007710 u64 submit_len;
7711 int clone_offset = 0;
7712 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307713 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007714 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007715 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007716
Omar Sandovalc36cac22020-04-16 14:46:13 -07007717 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7718 if (!dip) {
7719 if (!write) {
7720 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7721 file_offset + dio_bio->bi_iter.bi_size - 1);
7722 }
7723 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007724 dio_end_io(dio_bio);
7725 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007726 }
7727
Omar Sandoval85879572020-04-16 14:46:21 -07007728 if (!write && csum) {
7729 /*
7730 * Load the csums up front to reduce csum tree searches and
7731 * contention when submitting bios.
7732 */
7733 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7734 dip->csums);
7735 if (status != BLK_STS_OK)
7736 goto out_err;
7737 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007738
Omar Sandoval769b4f22020-04-16 14:46:22 -07007739 start_sector = dio_bio->bi_iter.bi_sector;
7740 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007741
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007742 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007743 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7744 start_sector << 9, submit_len,
7745 &geom);
7746 if (ret) {
7747 status = errno_to_blk_status(ret);
7748 goto out_err;
7749 }
7750 ASSERT(geom.len <= INT_MAX);
7751
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007752 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007753
Liu Bo725130b2017-05-16 09:51:39 -07007754 /*
7755 * This will never fail as it's passing GPF_NOFS and
7756 * the allocation is backed by btrfs_bioset.
7757 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007758 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007759 bio->bi_private = dip;
7760 bio->bi_end_io = btrfs_end_dio_bio;
7761 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007762
Liu Bo725130b2017-05-16 09:51:39 -07007763 ASSERT(submit_len >= clone_len);
7764 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007765
Liu Bo725130b2017-05-16 09:51:39 -07007766 /*
7767 * Increase the count before we submit the bio so we know
7768 * the end IO handler won't happen before we increase the
7769 * count. Otherwise, the dip might get freed before we're
7770 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007771 *
7772 * We transfer the initial reference to the last bio, so we
7773 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007774 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007775 if (submit_len > 0) {
7776 refcount_inc(&dip->refs);
7777 /*
7778 * If we are submitting more than one bio, submit them
7779 * all asynchronously. The exception is RAID 5 or 6, as
7780 * asynchronous checksums make it difficult to collect
7781 * full stripe writes.
7782 */
7783 if (!raid56)
7784 async_submit = 1;
7785 }
Miao Xiee65e1532010-11-22 03:04:43 +00007786
David Sterbad0ee3932018-03-08 14:35:48 +01007787 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007788 async_submit);
7789 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007790 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007791 if (submit_len > 0)
7792 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007793 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007794 }
Liu Bo725130b2017-05-16 09:51:39 -07007795
7796 clone_offset += clone_len;
7797 start_sector += clone_len >> 9;
7798 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007799 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007800 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007801
Miao Xiee65e1532010-11-22 03:04:43 +00007802out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007803 dip->dio_bio->bi_status = status;
7804 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007805}
7806
David Sterbaf4c48b42020-06-09 19:19:27 +02007807static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7808 const struct iov_iter *iter, loff_t offset)
7809{
7810 int seg;
7811 int i;
7812 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7813 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007814
David Sterbaf4c48b42020-06-09 19:19:27 +02007815 if (offset & blocksize_mask)
7816 goto out;
7817
7818 if (iov_iter_alignment(iter) & blocksize_mask)
7819 goto out;
7820
7821 /* If this is a write we don't need to check anymore */
7822 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7823 return 0;
7824 /*
7825 * Check to make sure we don't have duplicate iov_base's in this
7826 * iovec, if so return EINVAL, otherwise we'll get csum errors
7827 * when reading back.
7828 */
7829 for (seg = 0; seg < iter->nr_segs; seg++) {
7830 for (i = seg + 1; i < iter->nr_segs; i++) {
7831 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7832 goto out;
7833 }
7834 }
7835 retval = 0;
7836out:
7837 return retval;
7838}
7839
David Sterba55e20bd2020-06-09 19:56:06 +02007840static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007841{
7842 struct file *file = iocb->ki_filp;
7843 struct inode *inode = file->f_mapping->host;
7844 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007845 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007846 struct extent_changeset *data_reserved = NULL;
7847 loff_t offset = iocb->ki_pos;
7848 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007849 int flags = 0;
7850 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007851 bool relock = false;
7852 ssize_t ret;
7853
7854 if (check_direct_IO(fs_info, iter, offset))
7855 return 0;
7856
David Sterba55e20bd2020-06-09 19:56:06 +02007857 inode_dio_begin(inode);
7858
7859 /*
7860 * The generic stuff only does filemap_write_and_wait_range, which
7861 * isn't enough if we've written compressed pages to this area, so
7862 * we need to flush the dirty pages again to make absolutely sure
7863 * that any outstanding dirty pages are on disk.
7864 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007865 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007866 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7867 &BTRFS_I(inode)->runtime_flags))
7868 filemap_fdatawrite_range(inode->i_mapping, offset,
7869 offset + count - 1);
7870
David Sterbaf4c48b42020-06-09 19:19:27 +02007871 if (iov_iter_rw(iter) == WRITE) {
7872 /*
7873 * If the write DIO is beyond the EOF, we need update
7874 * the isize, but it is protected by i_mutex. So we can
7875 * not unlock the i_mutex at this case.
7876 */
7877 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007878 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007879 inode_unlock(inode);
7880 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007881 }
David Sterba55e20bd2020-06-09 19:56:06 +02007882 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7883 offset, count);
7884 if (ret)
7885 goto out;
7886
7887 /*
7888 * We need to know how many extents we reserved so that we can
7889 * do the accounting properly if we go over the number we
7890 * originally calculated. Abuse current->journal_info for this.
7891 */
7892 dio_data.reserve = round_up(count,
7893 fs_info->sectorsize);
7894 dio_data.unsubmitted_oe_range_start = (u64)offset;
7895 dio_data.unsubmitted_oe_range_end = (u64)offset;
7896 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007897 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007898 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7899 &BTRFS_I(inode)->runtime_flags)) {
7900 inode_dio_end(inode);
7901 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7902 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007903 }
7904
David Sterba55e20bd2020-06-09 19:56:06 +02007905 ret = __blockdev_direct_IO(iocb, inode,
7906 fs_info->fs_devices->latest_bdev,
7907 iter, btrfs_get_blocks_direct, NULL,
7908 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007909 if (iov_iter_rw(iter) == WRITE) {
7910 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007911 current->journal_info = NULL;
7912 if (ret < 0 && ret != -EIOCBQUEUED) {
7913 if (dio_data.reserve)
7914 btrfs_delalloc_release_space(inode, data_reserved,
7915 offset, dio_data.reserve, true);
7916 /*
7917 * On error we might have left some ordered extents
7918 * without submitting corresponding bios for them, so
7919 * cleanup them up to avoid other tasks getting them
7920 * and waiting for them to complete forever.
7921 */
7922 if (dio_data.unsubmitted_oe_range_start <
7923 dio_data.unsubmitted_oe_range_end)
7924 __endio_write_update_ordered(inode,
7925 dio_data.unsubmitted_oe_range_start,
7926 dio_data.unsubmitted_oe_range_end -
7927 dio_data.unsubmitted_oe_range_start,
7928 false);
7929 } else if (ret >= 0 && (size_t)ret < count)
7930 btrfs_delalloc_release_space(inode, data_reserved,
7931 offset, count - (size_t)ret, true);
7932 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007933 }
7934out:
David Sterba55e20bd2020-06-09 19:56:06 +02007935 if (wakeup)
7936 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007937 if (relock)
7938 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007939
David Sterbaf4c48b42020-06-09 19:19:27 +02007940 extent_changeset_free(data_reserved);
7941 return ret;
7942}
Chris Mason16432982008-04-10 10:23:21 -04007943
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007944static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7945 __u64 start, __u64 len)
7946{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007947 int ret;
7948
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007949 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007950 if (ret)
7951 return ret;
7952
David Sterba2135fb92017-06-23 04:09:57 +02007953 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007954}
7955
Chris Mason9ebefb182007-06-15 13:50:00 -04007956int btrfs_readpage(struct file *file, struct page *page)
7957{
David Sterba71ad38b2020-02-05 19:09:35 +01007958 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007959}
Chris Mason1832a6d2007-12-21 16:27:21 -05007960
Chris Mason39279cc2007-06-12 06:35:45 -04007961static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7962{
Josef Bacikbe7bd732015-10-22 15:05:09 -04007963 struct inode *inode = page->mapping->host;
7964 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04007965
7966 if (current->flags & PF_MEMALLOC) {
7967 redirty_page_for_writepage(wbc, page);
7968 unlock_page(page);
7969 return 0;
7970 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04007971
7972 /*
7973 * If we are under memory pressure we will call this directly from the
7974 * VM, we need to make sure we have the inode referenced for the ordered
7975 * extent. If not just return like we didn't do anything.
7976 */
7977 if (!igrab(inode)) {
7978 redirty_page_for_writepage(wbc, page);
7979 return AOP_WRITEPAGE_ACTIVATE;
7980 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02007981 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04007982 btrfs_add_delayed_iput(inode);
7983 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04007984}
Chris Mason39279cc2007-06-12 06:35:45 -04007985
Eric Sandeen48a3b632013-04-25 20:41:01 +00007986static int btrfs_writepages(struct address_space *mapping,
7987 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007988{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03007989 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04007990}
7991
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007992static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05007993{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007994 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05007995}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03007996
Chris Masone6dcd2d2008-07-17 12:53:50 -04007997static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007998{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03007999 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008000 if (ret == 1)
8001 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008002 return ret;
8003}
Chris Mason39279cc2007-06-12 06:35:45 -04008004
Chris Masone6dcd2d2008-07-17 12:53:50 -04008005static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8006{
Chris Mason98509cf2008-09-11 15:51:43 -04008007 if (PageWriteback(page) || PageDirty(page))
8008 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008009 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008010}
8011
Roman Gushchinf8e66082020-03-04 16:57:35 -08008012#ifdef CONFIG_MIGRATION
8013static int btrfs_migratepage(struct address_space *mapping,
8014 struct page *newpage, struct page *page,
8015 enum migrate_mode mode)
8016{
8017 int ret;
8018
8019 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8020 if (ret != MIGRATEPAGE_SUCCESS)
8021 return ret;
8022
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008023 if (page_has_private(page))
8024 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008025
8026 if (PagePrivate2(page)) {
8027 ClearPagePrivate2(page);
8028 SetPagePrivate2(newpage);
8029 }
8030
8031 if (mode != MIGRATE_SYNC_NO_COPY)
8032 migrate_page_copy(newpage, page);
8033 else
8034 migrate_page_states(newpage, page);
8035 return MIGRATEPAGE_SUCCESS;
8036}
8037#endif
8038
Lukas Czernerd47992f2013-05-21 23:17:23 -04008039static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8040 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008041{
Josef Bacik5fd02042012-05-02 14:00:54 -04008042 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008043 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008044 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008045 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008046 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008047 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308048 u64 start;
8049 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008050 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008051
Chris Mason8b62b722009-09-02 16:53:46 -04008052 /*
8053 * we have the page locked, so new writeback can't start,
8054 * and the dirty bit won't be cleared while we are here.
8055 *
8056 * Wait for IO on this page so that we can safely clear
8057 * the PagePrivate2 bit and do ordered accounting
8058 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008059 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008060
Josef Bacik5fd02042012-05-02 14:00:54 -04008061 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008062 if (offset) {
8063 btrfs_releasepage(page, GFP_NOFS);
8064 return;
8065 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008066
8067 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008068 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308069again:
8070 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008071 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308072 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008073 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008074 end = min(page_end,
8075 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008076 /*
8077 * IO on this page will never be started, so we need
8078 * to account for any ordered extents now
8079 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008080 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308081 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008082 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008083 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008084 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008085 /*
8086 * whoever cleared the private bit is responsible
8087 * for the finish_ordered_io
8088 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008089 if (TestClearPagePrivate2(page)) {
8090 struct btrfs_ordered_inode_tree *tree;
8091 u64 new_len;
8092
8093 tree = &BTRFS_I(inode)->ordered_tree;
8094
8095 spin_lock_irq(&tree->lock);
8096 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308097 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008098 if (new_len < ordered->truncated_len)
8099 ordered->truncated_len = new_len;
8100 spin_unlock_irq(&tree->lock);
8101
8102 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308103 start,
8104 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008105 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008106 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008107 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008108 if (!inode_evicting) {
8109 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308110 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008111 &cached_state);
8112 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308113
8114 start = end + 1;
8115 if (start < page_end)
8116 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008117 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008118
Qu Wenruob9d0b382015-09-29 10:35:16 +08008119 /*
8120 * Qgroup reserved space handler
8121 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008122 * 1) Already written to disk or ordered extent already submitted
8123 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8124 * Qgroup will be handled by its qgroup_record then.
8125 * btrfs_qgroup_free_data() call will do nothing here.
8126 *
8127 * 2) Not written to disk yet
8128 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8129 * bit of its io_tree, and free the qgroup reserved data space.
8130 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008131 */
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008132 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008133 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008134 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008135 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8136 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008137 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008138
8139 __btrfs_releasepage(page, GFP_NOFS);
8140 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008141
Chris Mason4a096752008-07-21 10:29:44 -04008142 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008143 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008144}
8145
Chris Mason9ebefb182007-06-15 13:50:00 -04008146/*
8147 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8148 * called from a page fault handler when a page is first dirtied. Hence we must
8149 * be careful to check for EOF conditions here. We set the page up correctly
8150 * for a written page which means we get ENOSPC checking when writing into
8151 * holes and correct delalloc and unwritten extent mapping on filesystems that
8152 * support these features.
8153 *
8154 * We are not allowed to take the i_mutex here so we have to play games to
8155 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008156 * truncate_setsize() writes the inode size before removing pages, once we have
8157 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008158 * beyond EOF, then the page is guaranteed safe against truncation until we
8159 * unlock the page.
8160 */
Souptick Joardera528a242018-06-06 19:54:44 +05308161vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008162{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008163 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008164 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008165 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008166 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8167 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008168 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008169 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008170 char *kaddr;
8171 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008172 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308173 vm_fault_t ret;
8174 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008175 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308176 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008177 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008178 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308179 u64 end;
8180
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008181 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008182
Jan Karab2b5ef52012-06-12 16:20:45 +02008183 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008184 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008185 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308186 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008187
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308188 /*
8189 * Reserving delalloc space after obtaining the page lock can lead to
8190 * deadlock. For example, if a dirty page is locked by this function
8191 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8192 * dirty page write out, then the btrfs_writepage() function could
8193 * end up waiting indefinitely to get a lock on the page currently
8194 * being processed by btrfs_page_mkwrite() function.
8195 */
Souptick Joardera528a242018-06-06 19:54:44 +05308196 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308197 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308198 if (!ret2) {
8199 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008200 reserved = 1;
8201 }
Souptick Joardera528a242018-06-06 19:54:44 +05308202 if (ret2) {
8203 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008204 if (reserved)
8205 goto out;
8206 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008207 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008208
Nick Piggin56a76f82009-03-31 15:23:23 -07008209 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008210again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008211 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008212 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008213
Chris Mason9ebefb182007-06-15 13:50:00 -04008214 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008215 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008216 /* page got truncated out from underneath us */
8217 goto out_unlock;
8218 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008219 wait_on_page_writeback(page);
8220
David Sterbaff13db42015-12-03 14:30:40 +01008221 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008222 set_page_extent_mapped(page);
8223
Chris Masoneb84ae02008-07-17 13:53:27 -04008224 /*
8225 * we can't set the delalloc bits if there are pending ordered
8226 * extents. Drop our locks and wait for them to finish
8227 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008228 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8229 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008230 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008231 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008232 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008233 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008234 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008235 btrfs_put_ordered_extent(ordered);
8236 goto again;
8237 }
8238
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008239 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008240 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008241 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008242 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308243 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008244 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008245 page_start, PAGE_SIZE - reserved_space,
8246 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308247 }
8248 }
8249
Josef Bacikfbf19082009-10-01 17:10:23 -04008250 /*
Liu Bo54160342016-12-13 12:15:19 -08008251 * page_mkwrite gets called when the page is firstly dirtied after it's
8252 * faulted in, but write(2) could also dirty a page and set delalloc
8253 * bits, thus in this case for space account reason, we still need to
8254 * clear any delalloc bits within this page range since we have to
8255 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008256 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308257 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008258 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8259 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008260
Souptick Joardera528a242018-06-06 19:54:44 +05308261 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008262 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308263 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008264 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008265 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008266 ret = VM_FAULT_SIGBUS;
8267 goto out_unlock;
8268 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008269
8270 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008271 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008272 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008273 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008274 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008275
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008276 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008278 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008279 flush_dcache_page(page);
8280 kunmap(page);
8281 }
Chris Mason247e7432008-07-17 12:53:51 -04008282 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008283 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008284 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008285
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008286 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008287 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008288 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008289
David Sterbae43bbe52017-12-12 21:43:52 +01008290 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008291
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008292 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8293 sb_end_pagefault(inode->i_sb);
8294 extent_changeset_free(data_reserved);
8295 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008296
8297out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008298 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008299out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008300 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008301 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008302 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008303out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008304 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008305 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008306 return ret;
8307}
8308
Filipe Manana213e8c52018-02-06 20:40:31 +00008309static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008311 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008312 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008313 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008314 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008315 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008316 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008317 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008318
Filipe Manana213e8c52018-02-06 20:40:31 +00008319 if (!skip_writeback) {
8320 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8321 (u64)-1);
8322 if (ret)
8323 return ret;
8324 }
Chris Mason39279cc2007-06-12 06:35:45 -04008325
Josef Bacikfcb80c22011-05-03 10:40:22 -04008326 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008327 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8328 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008329 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008330 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008331 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008332 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008333 * be free'd up by the truncate operation, but also have some slack
8334 * space reserved in case it uses space during the truncate (thank you
8335 * very much snapshotting).
8336 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008337 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008338 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008339 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008340 * doesn't end up using space reserved for updating the inode. We also
8341 * need to be able to stop the transaction and start a new one, which
8342 * means we need to be able to update the inode several times, and we
8343 * have no idea of knowing how many times that will be, so we can't just
8344 * reserve 1 item for the entirety of the operation, so that has to be
8345 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008346 *
8347 * So that leaves us with
8348 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008349 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008350 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008351 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008352 * updating the inode.
8353 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008354 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008355 if (!rsv)
8356 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008357 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008358 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008359
Josef Bacik907cbce2011-08-08 13:46:15 -04008360 /*
Josef Bacik07127182011-08-19 10:29:59 -04008361 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008362 * 1 for updating the inode.
8363 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008364 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008365 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008366 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008367 goto out;
8368 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008369
Josef Bacik907cbce2011-08-08 13:46:15 -04008370 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008371 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008372 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008373 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008374
Chris Mason5a3f23d2009-03-31 13:27:11 -04008375 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008376 * So if we truncate and then write and fsync we normally would just
8377 * write the extents that changed, which is a problem if we need to
8378 * first truncate that entire inode. So set this flag so we write out
8379 * all of the extents in the inode to the sync log so we're completely
8380 * safe.
8381 */
8382 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008383 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008384
Yan, Zheng80825102009-11-12 09:35:36 +00008385 while (1) {
8386 ret = btrfs_truncate_inode_items(trans, root, inode,
8387 inode->i_size,
8388 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008389 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008390 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008391 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008392
Yan, Zheng80825102009-11-12 09:35:36 +00008393 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008394 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008395 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008396
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008397 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008398 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008399
8400 trans = btrfs_start_transaction(root, 2);
8401 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008402 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008403 trans = NULL;
8404 break;
8405 }
8406
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008407 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008408 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008409 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008410 BUG_ON(ret); /* shouldn't happen */
8411 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008412 }
8413
Josef Bacikddfae632017-10-19 14:16:02 -04008414 /*
8415 * We can't call btrfs_truncate_block inside a trans handle as we could
8416 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8417 * we've truncated everything except the last little bit, and can do
8418 * btrfs_truncate_block and then update the disk_i_size.
8419 */
8420 if (ret == NEED_TRUNCATE_BLOCK) {
8421 btrfs_end_transaction(trans);
8422 btrfs_btree_balance_dirty(fs_info);
8423
8424 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8425 if (ret)
8426 goto out;
8427 trans = btrfs_start_transaction(root, 1);
8428 if (IS_ERR(trans)) {
8429 ret = PTR_ERR(trans);
8430 goto out;
8431 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008432 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008433 }
8434
Chris Mason917c16b2011-11-08 14:49:59 -05008435 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008436 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008437
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008438 trans->block_rsv = &fs_info->trans_block_rsv;
8439 ret2 = btrfs_update_inode(trans, root, inode);
8440 if (ret2 && !ret)
8441 ret = ret2;
8442
8443 ret2 = btrfs_end_transaction(trans);
8444 if (ret2 && !ret)
8445 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008446 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008447 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008448out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008449 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008450
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008451 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008452}
8453
Sven Wegener3b963622008-06-09 21:57:42 -04008454/*
Chris Masond352ac62008-09-29 15:18:18 -04008455 * create a new subvolume directory/inode (helper for the ioctl).
8456 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008457int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008458 struct btrfs_root *new_root,
8459 struct btrfs_root *parent_root,
8460 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008461{
Chris Mason39279cc2007-06-12 06:35:45 -04008462 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008463 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008464 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008465
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008466 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8467 new_dirid, new_dirid,
8468 S_IFDIR | (~current_umask() & S_IRWXUGO),
8469 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008470 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008471 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008472 inode->i_op = &btrfs_dir_inode_operations;
8473 inode->i_fop = &btrfs_dir_file_operations;
8474
Miklos Szeredibfe86842011-10-28 14:13:29 +02008475 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008476 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008477 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008478
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008479 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8480 if (err)
8481 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008482 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008483 new_root->root_key.objectid, err);
8484
Yan, Zheng76dda932009-09-21 16:00:26 -04008485 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008486
Yan, Zheng76dda932009-09-21 16:00:26 -04008487 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008488 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008489}
8490
Chris Mason39279cc2007-06-12 06:35:45 -04008491struct inode *btrfs_alloc_inode(struct super_block *sb)
8492{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008493 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008494 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008495 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008496
David Sterba712e36c2017-10-31 17:08:27 +01008497 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008498 if (!ei)
8499 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008500
8501 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008502 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008503 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008504 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008505 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008506 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008507 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008508 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008509 ei->disk_i_size = 0;
8510 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008511 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008512 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008513 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008514 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008515 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008516
Josef Bacik9e0baf62011-07-15 15:16:44 +00008517 spin_lock_init(&ei->lock);
8518 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008519 if (sb->s_magic != BTRFS_TEST_MAGIC)
8520 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8521 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008522 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008523 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008524 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008525
Miao Xie16cdcec2011-04-22 18:12:22 +08008526 ei->delayed_node = NULL;
8527
chandan r9cc97d62012-07-04 12:48:07 +05308528 ei->i_otime.tv_sec = 0;
8529 ei->i_otime.tv_nsec = 0;
8530
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008531 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008532 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008533 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8534 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8535 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008536 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8537 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008538 ei->io_tree.track_uptodate = true;
8539 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008540 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008541 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008542 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008543 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008544 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008545 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008546 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008547
8548 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008549}
8550
Josef Bacikaaedb552013-10-11 14:44:09 -04008551#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8552void btrfs_test_destroy_inode(struct inode *inode)
8553{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008554 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008555 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8556}
8557#endif
8558
Al Viro26602ca2019-04-10 15:14:41 -04008559void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008560{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008561 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8562}
8563
Chris Mason39279cc2007-06-12 06:35:45 -04008564void btrfs_destroy_inode(struct inode *inode)
8565{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008566 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008567 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008568 struct btrfs_root *root = BTRFS_I(inode)->root;
8569
Al Virob3d9b7a2012-06-09 13:51:19 -04008570 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008571 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008572 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8573 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008574 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008575 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008576 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008577 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008578 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008579
Chris Mason5a3f23d2009-03-31 13:27:11 -04008580 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008581 * This can happen where we create an inode, but somebody else also
8582 * created the same inode and we need to destroy the one we already
8583 * created.
8584 */
8585 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008586 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008587
Chris Masond3977122009-01-05 21:25:51 -05008588 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008589 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8590 if (!ordered)
8591 break;
8592 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008593 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008594 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008595 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008596 btrfs_remove_ordered_extent(inode, ordered);
8597 btrfs_put_ordered_extent(ordered);
8598 btrfs_put_ordered_extent(ordered);
8599 }
8600 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008601 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008602 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008603 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008604 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008605 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008606}
8607
Al Viro45321ac2010-06-07 13:43:19 -04008608int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008609{
8610 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008611
Naohiro Aota6379ef92013-06-06 09:56:34 +00008612 if (root == NULL)
8613 return 1;
8614
Liu Bofa6ac872013-02-20 14:10:23 +00008615 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008616 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008617 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008618 else
Al Viro45321ac2010-06-07 13:43:19 -04008619 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008620}
8621
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008622static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008623{
8624 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8625
8626 inode_init_once(&ei->vfs_inode);
8627}
8628
David Sterbae67c7182018-02-19 17:24:18 +01008629void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008630{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008631 /*
8632 * Make sure all delayed rcu free inodes are flushed before we
8633 * destroy cache.
8634 */
8635 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008636 kmem_cache_destroy(btrfs_inode_cachep);
8637 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008638 kmem_cache_destroy(btrfs_path_cachep);
8639 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008640 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008641}
8642
Liu Bof5c29bd2017-11-02 17:21:50 -06008643int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008644{
David Sterba837e1972012-09-07 03:00:48 -06008645 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008646 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008647 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8648 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008649 if (!btrfs_inode_cachep)
8650 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008651
David Sterba837e1972012-09-07 03:00:48 -06008652 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008653 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008654 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008655 if (!btrfs_trans_handle_cachep)
8656 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008657
David Sterba837e1972012-09-07 03:00:48 -06008658 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008659 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008660 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008661 if (!btrfs_path_cachep)
8662 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008663
David Sterba837e1972012-09-07 03:00:48 -06008664 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008665 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008666 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008667 if (!btrfs_free_space_cachep)
8668 goto fail;
8669
Christophe Leroy3acd4852019-08-21 15:05:55 +00008670 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8671 PAGE_SIZE, PAGE_SIZE,
8672 SLAB_RED_ZONE, NULL);
8673 if (!btrfs_free_space_bitmap_cachep)
8674 goto fail;
8675
Chris Mason39279cc2007-06-12 06:35:45 -04008676 return 0;
8677fail:
8678 btrfs_destroy_cachep();
8679 return -ENOMEM;
8680}
8681
David Howellsa528d352017-01-31 16:46:22 +00008682static int btrfs_getattr(const struct path *path, struct kstat *stat,
8683 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008684{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008685 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008686 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008687 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008688 u32 bi_flags = BTRFS_I(inode)->flags;
8689
8690 stat->result_mask |= STATX_BTIME;
8691 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8692 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8693 if (bi_flags & BTRFS_INODE_APPEND)
8694 stat->attributes |= STATX_ATTR_APPEND;
8695 if (bi_flags & BTRFS_INODE_COMPRESS)
8696 stat->attributes |= STATX_ATTR_COMPRESSED;
8697 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8698 stat->attributes |= STATX_ATTR_IMMUTABLE;
8699 if (bi_flags & BTRFS_INODE_NODUMP)
8700 stat->attributes |= STATX_ATTR_NODUMP;
8701
8702 stat->attributes_mask |= (STATX_ATTR_APPEND |
8703 STATX_ATTR_COMPRESSED |
8704 STATX_ATTR_IMMUTABLE |
8705 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008706
Chris Mason39279cc2007-06-12 06:35:45 -04008707 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008708 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008709
8710 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008711 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008712 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008713 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008714 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008715 return 0;
8716}
8717
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008718static int btrfs_rename_exchange(struct inode *old_dir,
8719 struct dentry *old_dentry,
8720 struct inode *new_dir,
8721 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008722{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008723 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008724 struct btrfs_trans_handle *trans;
8725 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008726 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008727 struct inode *new_inode = new_dentry->d_inode;
8728 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008729 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008730 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008731 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8732 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008733 u64 old_idx = 0;
8734 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008735 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008736 bool root_log_pinned = false;
8737 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008738 struct btrfs_log_ctx ctx_root;
8739 struct btrfs_log_ctx ctx_dest;
8740 bool sync_log_root = false;
8741 bool sync_log_dest = false;
8742 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008743
8744 /* we only allow rename subvolume link between subvolumes */
8745 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8746 return -EXDEV;
8747
Filipe Mananad4682ba2018-06-11 19:24:28 +01008748 btrfs_init_log_ctx(&ctx_root, old_inode);
8749 btrfs_init_log_ctx(&ctx_dest, new_inode);
8750
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008751 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008752 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8753 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008754 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008755
8756 /*
8757 * We want to reserve the absolute worst case amount of items. So if
8758 * both inodes are subvols and we need to unlink them then that would
8759 * require 4 item modifications, but if they are both normal inodes it
8760 * would require 5 item modifications, so we'll assume their normal
8761 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8762 * should cover the worst case number of items we'll modify.
8763 */
8764 trans = btrfs_start_transaction(root, 12);
8765 if (IS_ERR(trans)) {
8766 ret = PTR_ERR(trans);
8767 goto out_notrans;
8768 }
8769
Josef Bacik3e174092019-11-15 15:43:06 -05008770 if (dest != root)
8771 btrfs_record_root_in_trans(trans, dest);
8772
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008773 /*
8774 * We need to find a free sequence number both in the source and
8775 * in the destination directory for the exchange.
8776 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008777 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008778 if (ret)
8779 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008780 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008781 if (ret)
8782 goto out_fail;
8783
8784 BTRFS_I(old_inode)->dir_index = 0ULL;
8785 BTRFS_I(new_inode)->dir_index = 0ULL;
8786
8787 /* Reference for the source. */
8788 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8789 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008790 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008791 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008792 btrfs_pin_log_trans(root);
8793 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008794 ret = btrfs_insert_inode_ref(trans, dest,
8795 new_dentry->d_name.name,
8796 new_dentry->d_name.len,
8797 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008798 btrfs_ino(BTRFS_I(new_dir)),
8799 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008800 if (ret)
8801 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008802 }
8803
8804 /* And now for the dest. */
8805 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8806 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008807 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008808 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008809 btrfs_pin_log_trans(dest);
8810 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008811 ret = btrfs_insert_inode_ref(trans, root,
8812 old_dentry->d_name.name,
8813 old_dentry->d_name.len,
8814 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008815 btrfs_ino(BTRFS_I(old_dir)),
8816 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008817 if (ret)
8818 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008819 }
8820
8821 /* Update inode version and ctime/mtime. */
8822 inode_inc_iversion(old_dir);
8823 inode_inc_iversion(new_dir);
8824 inode_inc_iversion(old_inode);
8825 inode_inc_iversion(new_inode);
8826 old_dir->i_ctime = old_dir->i_mtime = ctime;
8827 new_dir->i_ctime = new_dir->i_mtime = ctime;
8828 old_inode->i_ctime = ctime;
8829 new_inode->i_ctime = ctime;
8830
8831 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008832 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8833 BTRFS_I(old_inode), 1);
8834 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8835 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008836 }
8837
8838 /* src is a subvolume */
8839 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008840 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008841 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008842 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8843 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008844 old_dentry->d_name.name,
8845 old_dentry->d_name.len);
8846 if (!ret)
8847 ret = btrfs_update_inode(trans, root, old_inode);
8848 }
8849 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008850 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008851 goto out_fail;
8852 }
8853
8854 /* dest is a subvolume */
8855 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008856 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008857 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008858 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8859 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008860 new_dentry->d_name.name,
8861 new_dentry->d_name.len);
8862 if (!ret)
8863 ret = btrfs_update_inode(trans, dest, new_inode);
8864 }
8865 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008866 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008867 goto out_fail;
8868 }
8869
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008870 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008871 new_dentry->d_name.name,
8872 new_dentry->d_name.len, 0, old_idx);
8873 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008874 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008875 goto out_fail;
8876 }
8877
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008878 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008879 old_dentry->d_name.name,
8880 old_dentry->d_name.len, 0, new_idx);
8881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008882 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008883 goto out_fail;
8884 }
8885
8886 if (old_inode->i_nlink == 1)
8887 BTRFS_I(old_inode)->dir_index = old_idx;
8888 if (new_inode->i_nlink == 1)
8889 BTRFS_I(new_inode)->dir_index = new_idx;
8890
Filipe Manana86e8aa02016-05-05 02:02:27 +01008891 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008892 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008893 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8894 BTRFS_I(old_dir), parent,
8895 false, &ctx_root);
8896 if (ret == BTRFS_NEED_LOG_SYNC)
8897 sync_log_root = true;
8898 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8899 commit_transaction = true;
8900 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008901 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008902 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008903 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008904 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008905 if (!commit_transaction) {
8906 parent = old_dentry->d_parent;
8907 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8908 BTRFS_I(new_dir), parent,
8909 false, &ctx_dest);
8910 if (ret == BTRFS_NEED_LOG_SYNC)
8911 sync_log_dest = true;
8912 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8913 commit_transaction = true;
8914 ret = 0;
8915 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008916 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008917 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008918 }
8919out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008920 /*
8921 * If we have pinned a log and an error happened, we unpin tasks
8922 * trying to sync the log and force them to fallback to a transaction
8923 * commit if the log currently contains any of the inodes involved in
8924 * this rename operation (to ensure we do not persist a log with an
8925 * inconsistent state for any of these inodes or leading to any
8926 * inconsistencies when replayed). If the transaction was aborted, the
8927 * abortion reason is propagated to userspace when attempting to commit
8928 * the transaction. If the log does not contain any of these inodes, we
8929 * allow the tasks to sync it.
8930 */
8931 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008932 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8933 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8934 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008935 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008936 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008937 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008938
8939 if (root_log_pinned) {
8940 btrfs_end_log_trans(root);
8941 root_log_pinned = false;
8942 }
8943 if (dest_log_pinned) {
8944 btrfs_end_log_trans(dest);
8945 dest_log_pinned = false;
8946 }
8947 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008948 if (!ret && sync_log_root && !commit_transaction) {
8949 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8950 &ctx_root);
8951 if (ret)
8952 commit_transaction = true;
8953 }
8954 if (!ret && sync_log_dest && !commit_transaction) {
8955 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8956 &ctx_dest);
8957 if (ret)
8958 commit_transaction = true;
8959 }
8960 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008961 /*
8962 * We may have set commit_transaction when logging the new name
8963 * in the destination root, in which case we left the source
8964 * root context in the list of log contextes. So make sure we
8965 * remove it to avoid invalid memory accesses, since the context
8966 * was allocated in our stack frame.
8967 */
8968 if (sync_log_root) {
8969 mutex_lock(&root->log_mutex);
8970 list_del_init(&ctx_root.list);
8971 mutex_unlock(&root->log_mutex);
8972 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008973 ret = btrfs_commit_transaction(trans);
8974 } else {
8975 int ret2;
8976
8977 ret2 = btrfs_end_transaction(trans);
8978 ret = ret ? ret : ret2;
8979 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008980out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05008981 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8982 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008983 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008984
Filipe Mananae6c61712019-11-08 16:11:56 +00008985 ASSERT(list_empty(&ctx_root.list));
8986 ASSERT(list_empty(&ctx_dest.list));
8987
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008988 return ret;
8989}
8990
8991static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
8992 struct btrfs_root *root,
8993 struct inode *dir,
8994 struct dentry *dentry)
8995{
8996 int ret;
8997 struct inode *inode;
8998 u64 objectid;
8999 u64 index;
9000
9001 ret = btrfs_find_free_ino(root, &objectid);
9002 if (ret)
9003 return ret;
9004
9005 inode = btrfs_new_inode(trans, root, dir,
9006 dentry->d_name.name,
9007 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009008 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009009 objectid,
9010 S_IFCHR | WHITEOUT_MODE,
9011 &index);
9012
9013 if (IS_ERR(inode)) {
9014 ret = PTR_ERR(inode);
9015 return ret;
9016 }
9017
9018 inode->i_op = &btrfs_special_inode_operations;
9019 init_special_inode(inode, inode->i_mode,
9020 WHITEOUT_DEV);
9021
9022 ret = btrfs_init_inode_security(trans, inode, dir,
9023 &dentry->d_name);
9024 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009025 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009026
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009027 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9028 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009029 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009030 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009031
9032 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009033out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009034 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009035 if (ret)
9036 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009037 iput(inode);
9038
Filipe Mananac9901612016-05-05 01:41:57 +01009039 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009040}
9041
Chris Mason39279cc2007-06-12 06:35:45 -04009042static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009043 struct inode *new_dir, struct dentry *new_dentry,
9044 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009045{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009046 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009047 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009048 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009049 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9050 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009051 struct inode *new_inode = d_inode(new_dentry);
9052 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009053 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009054 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009055 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009056 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009057 struct btrfs_log_ctx ctx;
9058 bool sync_log = false;
9059 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009060
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009061 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009062 return -EPERM;
9063
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009064 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009065 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009066 return -EXDEV;
9067
Li Zefan33345d012011-04-20 10:31:50 +08009068 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009069 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009070 return -ENOTEMPTY;
9071
Chris Mason39279cc2007-06-12 06:35:45 -04009072 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009073 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009074 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009075
9076
9077 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009078 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009079 new_dentry->d_name.name,
9080 new_dentry->d_name.len);
9081
9082 if (ret) {
9083 if (ret == -EEXIST) {
9084 /* we shouldn't get
9085 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309086 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009087 return ret;
9088 }
9089 } else {
9090 /* maybe -EOVERFLOW */
9091 return ret;
9092 }
9093 }
9094 ret = 0;
9095
Chris Mason5a3f23d2009-03-31 13:27:11 -04009096 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009097 * we're using rename to replace one file with another. Start IO on it
9098 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009099 */
Chris Mason8d875f92014-08-12 10:47:42 -07009100 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009101 filemap_flush(old_inode->i_mapping);
9102
Yan, Zheng76dda932009-09-21 16:00:26 -04009103 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009104 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009105 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009106 /*
9107 * We want to reserve the absolute worst case amount of items. So if
9108 * both inodes are subvols and we need to unlink them then that would
9109 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009110 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009111 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9112 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009113 * If our rename has the whiteout flag, we need more 5 units for the
9114 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9115 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009116 */
Filipe Manana5062af32016-05-05 10:26:26 +01009117 trans_num_items = 11;
9118 if (flags & RENAME_WHITEOUT)
9119 trans_num_items += 5;
9120 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009121 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009122 ret = PTR_ERR(trans);
9123 goto out_notrans;
9124 }
Chris Mason5f39d392007-10-15 16:14:19 -04009125
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009126 if (dest != root)
9127 btrfs_record_root_in_trans(trans, dest);
9128
Nikolay Borisov877574e2017-02-20 13:50:33 +02009129 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009130 if (ret)
9131 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009132
Miao Xie67de1172013-12-26 13:07:06 +08009133 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009134 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009135 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009136 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009137 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009138 btrfs_pin_log_trans(root);
9139 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009140 ret = btrfs_insert_inode_ref(trans, dest,
9141 new_dentry->d_name.name,
9142 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009143 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009144 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009145 if (ret)
9146 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009147 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009148
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009149 inode_inc_iversion(old_dir);
9150 inode_inc_iversion(new_dir);
9151 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009152 old_dir->i_ctime = old_dir->i_mtime =
9153 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009154 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009155
Chris Mason12fcfd22009-03-24 10:24:20 -04009156 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009157 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9158 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009159
Li Zefan33345d012011-04-20 10:31:50 +08009160 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009161 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009162 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009163 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9164 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009165 old_dentry->d_name.name,
9166 old_dentry->d_name.len);
9167 if (!ret)
9168 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009169 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009170 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009171 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009172 goto out_fail;
9173 }
Chris Mason39279cc2007-06-12 06:35:45 -04009174
9175 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009176 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009177 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009178 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009179 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009180 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009181 BUG_ON(new_inode->i_nlink == 0);
9182 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009183 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9184 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009185 new_dentry->d_name.name,
9186 new_dentry->d_name.len);
9187 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009188 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009189 ret = btrfs_orphan_add(trans,
9190 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009191 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009192 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009193 goto out_fail;
9194 }
Chris Mason39279cc2007-06-12 06:35:45 -04009195 }
Josef Bacikaec74772008-07-24 12:12:38 -04009196
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009197 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009198 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009199 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009200 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009201 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009202 goto out_fail;
9203 }
Chris Mason39279cc2007-06-12 06:35:45 -04009204
Miao Xie67de1172013-12-26 13:07:06 +08009205 if (old_inode->i_nlink == 1)
9206 BTRFS_I(old_inode)->dir_index = index;
9207
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009208 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009209 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009210
Filipe Mananad4682ba2018-06-11 19:24:28 +01009211 btrfs_init_log_ctx(&ctx, old_inode);
9212 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9213 BTRFS_I(old_dir), parent,
9214 false, &ctx);
9215 if (ret == BTRFS_NEED_LOG_SYNC)
9216 sync_log = true;
9217 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9218 commit_transaction = true;
9219 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009220 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009221 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009222 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009223
9224 if (flags & RENAME_WHITEOUT) {
9225 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9226 old_dentry);
9227
9228 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009229 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009230 goto out_fail;
9231 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009232 }
Chris Mason39279cc2007-06-12 06:35:45 -04009233out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009234 /*
9235 * If we have pinned the log and an error happened, we unpin tasks
9236 * trying to sync the log and force them to fallback to a transaction
9237 * commit if the log currently contains any of the inodes involved in
9238 * this rename operation (to ensure we do not persist a log with an
9239 * inconsistent state for any of these inodes or leading to any
9240 * inconsistencies when replayed). If the transaction was aborted, the
9241 * abortion reason is propagated to userspace when attempting to commit
9242 * the transaction. If the log does not contain any of these inodes, we
9243 * allow the tasks to sync it.
9244 */
9245 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009246 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9247 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9248 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009249 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009250 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009251 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009252
9253 btrfs_end_log_trans(root);
9254 log_pinned = false;
9255 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009256 if (!ret && sync_log) {
9257 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9258 if (ret)
9259 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009260 } else if (sync_log) {
9261 mutex_lock(&root->log_mutex);
9262 list_del(&ctx.list);
9263 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009264 }
9265 if (commit_transaction) {
9266 ret = btrfs_commit_transaction(trans);
9267 } else {
9268 int ret2;
9269
9270 ret2 = btrfs_end_transaction(trans);
9271 ret = ret ? ret : ret2;
9272 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009273out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009274 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009275 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009276
Chris Mason39279cc2007-06-12 06:35:45 -04009277 return ret;
9278}
9279
Miklos Szeredi80ace852014-07-23 15:15:32 +02009280static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9281 struct inode *new_dir, struct dentry *new_dentry,
9282 unsigned int flags)
9283{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009284 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009285 return -EINVAL;
9286
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009287 if (flags & RENAME_EXCHANGE)
9288 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9289 new_dentry);
9290
9291 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009292}
9293
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009294struct btrfs_delalloc_work {
9295 struct inode *inode;
9296 struct completion completion;
9297 struct list_head list;
9298 struct btrfs_work work;
9299};
9300
Miao Xie8ccf6f192012-10-25 09:28:04 +00009301static void btrfs_run_delalloc_work(struct btrfs_work *work)
9302{
9303 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009304 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009305
9306 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9307 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009308 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009309 filemap_flush(inode->i_mapping);
9310 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9311 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009312 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009313
Nikolay Borisov076da912018-04-23 10:54:16 +03009314 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009315 complete(&delalloc_work->completion);
9316}
9317
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009318static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009319{
9320 struct btrfs_delalloc_work *work;
9321
David Sterba100d5702015-12-08 14:39:32 +01009322 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009323 if (!work)
9324 return NULL;
9325
9326 init_completion(&work->completion);
9327 INIT_LIST_HEAD(&work->list);
9328 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009329 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009330
9331 return work;
9332}
9333
Chris Masond352ac62008-09-29 15:18:18 -04009334/*
9335 * some fairly slow code that needs optimization. This walks the list
9336 * of all the inodes with pending delalloc and forces them to disk.
9337 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009338static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009339{
Chris Masonea8c2812008-08-04 23:17:27 -04009340 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009341 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009342 struct btrfs_delalloc_work *work, *next;
9343 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009344 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009345 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009346
Miao Xie8ccf6f192012-10-25 09:28:04 +00009347 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009348 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009349
Miao Xie573bfb72014-03-06 13:55:03 +08009350 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009351 spin_lock(&root->delalloc_lock);
9352 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009353 while (!list_empty(&splice)) {
9354 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009355 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009356
Miao Xieeb73c1b2013-05-15 07:48:22 +00009357 list_move_tail(&binode->delalloc_inodes,
9358 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009359 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009360 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009361 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009362 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009363 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009364 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009365
Ethan Lien3cd24c62018-11-01 14:49:03 +08009366 if (snapshot)
9367 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9368 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009369 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009370 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009371 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009372 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009373 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009374 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009375 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009376 btrfs_queue_work(root->fs_info->flush_workers,
9377 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009378 ret++;
9379 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009380 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009381 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009382 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009383 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009384 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009385
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009386out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009387 list_for_each_entry_safe(work, next, &works, list) {
9388 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009389 wait_for_completion(&work->completion);
9390 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009391 }
9392
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009393 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009394 spin_lock(&root->delalloc_lock);
9395 list_splice_tail(&splice, &root->delalloc_inodes);
9396 spin_unlock(&root->delalloc_lock);
9397 }
Miao Xie573bfb72014-03-06 13:55:03 +08009398 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009399 return ret;
9400}
9401
Ethan Lien3cd24c62018-11-01 14:49:03 +08009402int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009403{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009404 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009405 int ret;
9406
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009407 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009408 return -EROFS;
9409
Ethan Lien3cd24c62018-11-01 14:49:03 +08009410 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009411 if (ret > 0)
9412 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009413 return ret;
9414}
9415
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009416int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009417{
9418 struct btrfs_root *root;
9419 struct list_head splice;
9420 int ret;
9421
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009422 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009423 return -EROFS;
9424
9425 INIT_LIST_HEAD(&splice);
9426
Miao Xie573bfb72014-03-06 13:55:03 +08009427 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009428 spin_lock(&fs_info->delalloc_root_lock);
9429 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009430 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009431 root = list_first_entry(&splice, struct btrfs_root,
9432 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009433 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009434 BUG_ON(!root);
9435 list_move_tail(&root->delalloc_root,
9436 &fs_info->delalloc_roots);
9437 spin_unlock(&fs_info->delalloc_root_lock);
9438
Ethan Lien3cd24c62018-11-01 14:49:03 +08009439 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009440 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009441 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009442 goto out;
9443
Miao Xie6c255e62014-03-06 13:55:01 +08009444 if (nr != -1) {
9445 nr -= ret;
9446 WARN_ON(nr < 0);
9447 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009448 spin_lock(&fs_info->delalloc_root_lock);
9449 }
9450 spin_unlock(&fs_info->delalloc_root_lock);
9451
Miao Xie6c255e62014-03-06 13:55:01 +08009452 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009453out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009454 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009455 spin_lock(&fs_info->delalloc_root_lock);
9456 list_splice_tail(&splice, &fs_info->delalloc_roots);
9457 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009458 }
Miao Xie573bfb72014-03-06 13:55:03 +08009459 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009460 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009461}
9462
Chris Mason39279cc2007-06-12 06:35:45 -04009463static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9464 const char *symname)
9465{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009466 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009467 struct btrfs_trans_handle *trans;
9468 struct btrfs_root *root = BTRFS_I(dir)->root;
9469 struct btrfs_path *path;
9470 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009471 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009472 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009473 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309474 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009475 int name_len;
9476 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009477 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009478 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009479 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009480
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009481 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009482 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009483 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009484
Josef Bacik9ed74f22009-09-11 16:12:44 -04009485 /*
9486 * 2 items for inode item and ref
9487 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009488 * 1 item for updating parent inode item
9489 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009490 * 1 item for xattr if selinux is on
9491 */
Filipe Manana9269d122015-12-31 18:16:29 +00009492 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009493 if (IS_ERR(trans))
9494 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009495
Li Zefan581bb052011-04-20 10:06:11 +08009496 err = btrfs_find_free_ino(root, &objectid);
9497 if (err)
9498 goto out_unlock;
9499
Josef Bacikaec74772008-07-24 12:12:38 -04009500 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009501 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9502 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009503 if (IS_ERR(inode)) {
9504 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009505 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009506 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009507 }
Chris Mason39279cc2007-06-12 06:35:45 -04009508
Casey Schauflerad19db72011-12-15 10:09:07 -05009509 /*
9510 * If the active LSM wants to access the inode during
9511 * d_instantiate it needs these. Smack checks to see
9512 * if the filesystem supports xattrs by looking at the
9513 * ops vector.
9514 */
9515 inode->i_fop = &btrfs_file_operations;
9516 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009517 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009518 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9519
9520 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9521 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009522 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009523
Chris Mason39279cc2007-06-12 06:35:45 -04009524 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009525 if (!path) {
9526 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009527 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009528 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009529 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009530 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009531 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009532 datasize = btrfs_file_extent_calc_inline_size(name_len);
9533 err = btrfs_insert_empty_item(trans, root, path, &key,
9534 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009535 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009536 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009537 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009538 }
Chris Mason5f39d392007-10-15 16:14:19 -04009539 leaf = path->nodes[0];
9540 ei = btrfs_item_ptr(leaf, path->slots[0],
9541 struct btrfs_file_extent_item);
9542 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9543 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009544 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009545 btrfs_set_file_extent_encryption(leaf, ei, 0);
9546 btrfs_set_file_extent_compression(leaf, ei, 0);
9547 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9548 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9549
Chris Mason39279cc2007-06-12 06:35:45 -04009550 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009551 write_extent_buffer(leaf, symname, ptr, name_len);
9552 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009553 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009554
Chris Mason39279cc2007-06-12 06:35:45 -04009555 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009556 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009557 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009558 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009559 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009560 /*
9561 * Last step, add directory indexes for our symlink inode. This is the
9562 * last step to avoid extra cleanup of these indexes if an error happens
9563 * elsewhere above.
9564 */
9565 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009566 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9567 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009568 if (err)
9569 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009570
Al Viro1e2e5472018-05-04 08:23:01 -04009571 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009572
9573out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009574 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009575 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009576 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009577 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009578 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009579 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009580 return err;
9581}
Chris Mason16432982008-04-10 10:23:21 -04009582
Qu Wenruo203f44c52020-06-10 09:04:40 +08009583static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9584 struct inode *inode, struct btrfs_key *ins,
9585 u64 file_offset)
9586{
9587 struct btrfs_file_extent_item stack_fi;
9588 u64 start = ins->objectid;
9589 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009590 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009591
9592 memset(&stack_fi, 0, sizeof(stack_fi));
9593
9594 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9595 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9596 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9597 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9598 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9599 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9600 /* Encryption and other encoding is reserved and all 0 */
9601
Qu Wenruo9729f102020-06-10 09:04:41 +08009602 ret = btrfs_qgroup_release_data(inode, file_offset, len);
9603 if (ret < 0)
9604 return ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009605 return insert_reserved_file_extent(trans, inode, file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009606 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009607}
Josef Bacik0af3d002010-06-21 14:48:16 -04009608static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9609 u64 start, u64 num_bytes, u64 min_size,
9610 loff_t actual_len, u64 *alloc_hint,
9611 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009612{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009613 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009614 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9615 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009616 struct btrfs_root *root = BTRFS_I(inode)->root;
9617 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009618 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009619 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009620 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009621 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009622 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009623 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009624 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009625 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009626
Josef Bacik0af3d002010-06-21 14:48:16 -04009627 if (trans)
9628 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009629 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009630 if (own_trans) {
9631 trans = btrfs_start_transaction(root, 3);
9632 if (IS_ERR(trans)) {
9633 ret = PTR_ERR(trans);
9634 break;
9635 }
Yan Zhengd899e052008-10-30 14:25:28 -04009636 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009637
Byongho Leeee221842015-12-15 01:42:10 +09009638 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009639 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009640 /*
9641 * If we are severely fragmented we could end up with really
9642 * small allocations, so if the allocator is returning small
9643 * chunks lets make its job easier by only searching for those
9644 * sized chunks.
9645 */
9646 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009647 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9648 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009649 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009650 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009651 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009652 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009653 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009654
9655 /*
9656 * We've reserved this space, and thus converted it from
9657 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9658 * from here on out we will only need to clear our reservation
9659 * for the remaining unreserved area, so advance our
9660 * clear_offset by our extent size.
9661 */
9662 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009663 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009664
Josef Bacik0b670dc2015-09-23 17:11:16 -04009665 last_alloc = ins.offset;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009666 ret = insert_prealloc_file_extent(trans, inode, &ins,
9667 cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009668 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009669 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009670 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009671 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009672 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009673 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009674 break;
9675 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009676
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009677 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009678 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009679
Josef Bacik5dc562c2012-08-17 13:14:17 -04009680 em = alloc_extent_map();
9681 if (!em) {
9682 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9683 &BTRFS_I(inode)->runtime_flags);
9684 goto next;
9685 }
9686
9687 em->start = cur_offset;
9688 em->orig_start = cur_offset;
9689 em->len = ins.offset;
9690 em->block_start = ins.objectid;
9691 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009692 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009693 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009694 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9695 em->generation = trans->transid;
9696
9697 while (1) {
9698 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009699 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009700 write_unlock(&em_tree->lock);
9701 if (ret != -EEXIST)
9702 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009703 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009704 cur_offset + ins.offset - 1,
9705 0);
9706 }
9707 free_extent_map(em);
9708next:
Yan Zhengd899e052008-10-30 14:25:28 -04009709 num_bytes -= ins.offset;
9710 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009711 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009712
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009713 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009714 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009715 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009716 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009717 (actual_len > inode->i_size) &&
9718 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009719 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009720 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009721 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009722 i_size = cur_offset;
9723 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009724 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009725 }
9726
Yan Zhengd899e052008-10-30 14:25:28 -04009727 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009728
9729 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009730 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009731 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009732 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009733 break;
9734 }
Yan Zhengd899e052008-10-30 14:25:28 -04009735
Josef Bacik0af3d002010-06-21 14:48:16 -04009736 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009737 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009738 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009739 if (clear_offset < end)
9740 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9741 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009742 return ret;
9743}
9744
Josef Bacik0af3d002010-06-21 14:48:16 -04009745int btrfs_prealloc_file_range(struct inode *inode, int mode,
9746 u64 start, u64 num_bytes, u64 min_size,
9747 loff_t actual_len, u64 *alloc_hint)
9748{
9749 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9750 min_size, actual_len, alloc_hint,
9751 NULL);
9752}
9753
9754int btrfs_prealloc_file_range_trans(struct inode *inode,
9755 struct btrfs_trans_handle *trans, int mode,
9756 u64 start, u64 num_bytes, u64 min_size,
9757 loff_t actual_len, u64 *alloc_hint)
9758{
9759 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9760 min_size, actual_len, alloc_hint, trans);
9761}
9762
Chris Masone6dcd2d2008-07-17 12:53:50 -04009763static int btrfs_set_page_dirty(struct page *page)
9764{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009765 return __set_page_dirty_nobuffers(page);
9766}
9767
Al Viro10556cb22011-06-20 19:28:19 -04009768static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009769{
Li Zefanb83cc962010-12-20 16:04:08 +08009770 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009771 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009772
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009773 if (mask & MAY_WRITE &&
9774 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9775 if (btrfs_root_readonly(root))
9776 return -EROFS;
9777 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9778 return -EACCES;
9779 }
Al Viro2830ba72011-06-20 19:16:29 -04009780 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009781}
Chris Mason39279cc2007-06-12 06:35:45 -04009782
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009783static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9784{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009785 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009786 struct btrfs_trans_handle *trans;
9787 struct btrfs_root *root = BTRFS_I(dir)->root;
9788 struct inode *inode = NULL;
9789 u64 objectid;
9790 u64 index;
9791 int ret = 0;
9792
9793 /*
9794 * 5 units required for adding orphan entry
9795 */
9796 trans = btrfs_start_transaction(root, 5);
9797 if (IS_ERR(trans))
9798 return PTR_ERR(trans);
9799
9800 ret = btrfs_find_free_ino(root, &objectid);
9801 if (ret)
9802 goto out;
9803
9804 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009805 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009806 if (IS_ERR(inode)) {
9807 ret = PTR_ERR(inode);
9808 inode = NULL;
9809 goto out;
9810 }
9811
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009812 inode->i_fop = &btrfs_file_operations;
9813 inode->i_op = &btrfs_file_inode_operations;
9814
9815 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009816 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9817
Chris Masonb0d5d102014-09-08 13:08:51 -07009818 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9819 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009820 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009821
9822 ret = btrfs_update_inode(trans, root, inode);
9823 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009824 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009825 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009826 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009827 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009828
Filipe Manana5762b5c2014-08-01 00:10:32 +01009829 /*
9830 * We set number of links to 0 in btrfs_new_inode(), and here we set
9831 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9832 * through:
9833 *
9834 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9835 */
9836 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009837 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009838 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009839 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009840out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009841 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009842 if (ret && inode)
9843 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009844 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009845 return ret;
9846}
9847
David Sterba5cdc84b2018-07-18 20:32:52 +02009848void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009849{
David Sterba5cdc84b2018-07-18 20:32:52 +02009850 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009851 unsigned long index = start >> PAGE_SHIFT;
9852 unsigned long end_index = end >> PAGE_SHIFT;
9853 struct page *page;
9854
9855 while (index <= end_index) {
9856 page = find_get_page(inode->i_mapping, index);
9857 ASSERT(page); /* Pages should be in the extent_io_tree */
9858 set_page_writeback(page);
9859 put_page(page);
9860 index++;
9861 }
9862}
9863
Omar Sandovaled46ff32016-11-03 10:28:14 -07009864#ifdef CONFIG_SWAP
9865/*
9866 * Add an entry indicating a block group or device which is pinned by a
9867 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9868 * negative errno on failure.
9869 */
9870static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9871 bool is_block_group)
9872{
9873 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9874 struct btrfs_swapfile_pin *sp, *entry;
9875 struct rb_node **p;
9876 struct rb_node *parent = NULL;
9877
9878 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9879 if (!sp)
9880 return -ENOMEM;
9881 sp->ptr = ptr;
9882 sp->inode = inode;
9883 sp->is_block_group = is_block_group;
9884
9885 spin_lock(&fs_info->swapfile_pins_lock);
9886 p = &fs_info->swapfile_pins.rb_node;
9887 while (*p) {
9888 parent = *p;
9889 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9890 if (sp->ptr < entry->ptr ||
9891 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9892 p = &(*p)->rb_left;
9893 } else if (sp->ptr > entry->ptr ||
9894 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9895 p = &(*p)->rb_right;
9896 } else {
9897 spin_unlock(&fs_info->swapfile_pins_lock);
9898 kfree(sp);
9899 return 1;
9900 }
9901 }
9902 rb_link_node(&sp->node, parent, p);
9903 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9904 spin_unlock(&fs_info->swapfile_pins_lock);
9905 return 0;
9906}
9907
9908/* Free all of the entries pinned by this swapfile. */
9909static void btrfs_free_swapfile_pins(struct inode *inode)
9910{
9911 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9912 struct btrfs_swapfile_pin *sp;
9913 struct rb_node *node, *next;
9914
9915 spin_lock(&fs_info->swapfile_pins_lock);
9916 node = rb_first(&fs_info->swapfile_pins);
9917 while (node) {
9918 next = rb_next(node);
9919 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9920 if (sp->inode == inode) {
9921 rb_erase(&sp->node, &fs_info->swapfile_pins);
9922 if (sp->is_block_group)
9923 btrfs_put_block_group(sp->ptr);
9924 kfree(sp);
9925 }
9926 node = next;
9927 }
9928 spin_unlock(&fs_info->swapfile_pins_lock);
9929}
9930
9931struct btrfs_swap_info {
9932 u64 start;
9933 u64 block_start;
9934 u64 block_len;
9935 u64 lowest_ppage;
9936 u64 highest_ppage;
9937 unsigned long nr_pages;
9938 int nr_extents;
9939};
9940
9941static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9942 struct btrfs_swap_info *bsi)
9943{
9944 unsigned long nr_pages;
9945 u64 first_ppage, first_ppage_reported, next_ppage;
9946 int ret;
9947
9948 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9949 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9950 PAGE_SIZE) >> PAGE_SHIFT;
9951
9952 if (first_ppage >= next_ppage)
9953 return 0;
9954 nr_pages = next_ppage - first_ppage;
9955
9956 first_ppage_reported = first_ppage;
9957 if (bsi->start == 0)
9958 first_ppage_reported++;
9959 if (bsi->lowest_ppage > first_ppage_reported)
9960 bsi->lowest_ppage = first_ppage_reported;
9961 if (bsi->highest_ppage < (next_ppage - 1))
9962 bsi->highest_ppage = next_ppage - 1;
9963
9964 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9965 if (ret < 0)
9966 return ret;
9967 bsi->nr_extents += ret;
9968 bsi->nr_pages += nr_pages;
9969 return 0;
9970}
9971
9972static void btrfs_swap_deactivate(struct file *file)
9973{
9974 struct inode *inode = file_inode(file);
9975
9976 btrfs_free_swapfile_pins(inode);
9977 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9978}
9979
9980static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9981 sector_t *span)
9982{
9983 struct inode *inode = file_inode(file);
9984 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9985 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9986 struct extent_state *cached_state = NULL;
9987 struct extent_map *em = NULL;
9988 struct btrfs_device *device = NULL;
9989 struct btrfs_swap_info bsi = {
9990 .lowest_ppage = (sector_t)-1ULL,
9991 };
9992 int ret = 0;
9993 u64 isize;
9994 u64 start;
9995
9996 /*
9997 * If the swap file was just created, make sure delalloc is done. If the
9998 * file changes again after this, the user is doing something stupid and
9999 * we don't really care.
10000 */
10001 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10002 if (ret)
10003 return ret;
10004
10005 /*
10006 * The inode is locked, so these flags won't change after we check them.
10007 */
10008 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10009 btrfs_warn(fs_info, "swapfile must not be compressed");
10010 return -EINVAL;
10011 }
10012 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10013 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10014 return -EINVAL;
10015 }
10016 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10017 btrfs_warn(fs_info, "swapfile must not be checksummed");
10018 return -EINVAL;
10019 }
10020
10021 /*
10022 * Balance or device remove/replace/resize can move stuff around from
10023 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10024 * concurrently while we are mapping the swap extents, and
10025 * fs_info->swapfile_pins prevents them from running while the swap file
10026 * is active and moving the extents. Note that this also prevents a
10027 * concurrent device add which isn't actually necessary, but it's not
10028 * really worth the trouble to allow it.
10029 */
10030 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10031 btrfs_warn(fs_info,
10032 "cannot activate swapfile while exclusive operation is running");
10033 return -EBUSY;
10034 }
10035 /*
10036 * Snapshots can create extents which require COW even if NODATACOW is
10037 * set. We use this counter to prevent snapshots. We must increment it
10038 * before walking the extents because we don't want a concurrent
10039 * snapshot to run after we've already checked the extents.
10040 */
10041 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10042
10043 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10044
10045 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10046 start = 0;
10047 while (start < isize) {
10048 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010049 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010050 u64 len = isize - start;
10051
Omar Sandoval39b07b52019-12-02 17:34:23 -080010052 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010053 if (IS_ERR(em)) {
10054 ret = PTR_ERR(em);
10055 goto out;
10056 }
10057
10058 if (em->block_start == EXTENT_MAP_HOLE) {
10059 btrfs_warn(fs_info, "swapfile must not have holes");
10060 ret = -EINVAL;
10061 goto out;
10062 }
10063 if (em->block_start == EXTENT_MAP_INLINE) {
10064 /*
10065 * It's unlikely we'll ever actually find ourselves
10066 * here, as a file small enough to fit inline won't be
10067 * big enough to store more than the swap header, but in
10068 * case something changes in the future, let's catch it
10069 * here rather than later.
10070 */
10071 btrfs_warn(fs_info, "swapfile must not be inline");
10072 ret = -EINVAL;
10073 goto out;
10074 }
10075 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10076 btrfs_warn(fs_info, "swapfile must not be compressed");
10077 ret = -EINVAL;
10078 goto out;
10079 }
10080
10081 logical_block_start = em->block_start + (start - em->start);
10082 len = min(len, em->len - (start - em->start));
10083 free_extent_map(em);
10084 em = NULL;
10085
10086 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10087 if (ret < 0) {
10088 goto out;
10089 } else if (ret) {
10090 ret = 0;
10091 } else {
10092 btrfs_warn(fs_info,
10093 "swapfile must not be copy-on-write");
10094 ret = -EINVAL;
10095 goto out;
10096 }
10097
10098 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10099 if (IS_ERR(em)) {
10100 ret = PTR_ERR(em);
10101 goto out;
10102 }
10103
10104 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10105 btrfs_warn(fs_info,
10106 "swapfile must have single data profile");
10107 ret = -EINVAL;
10108 goto out;
10109 }
10110
10111 if (device == NULL) {
10112 device = em->map_lookup->stripes[0].dev;
10113 ret = btrfs_add_swapfile_pin(inode, device, false);
10114 if (ret == 1)
10115 ret = 0;
10116 else if (ret)
10117 goto out;
10118 } else if (device != em->map_lookup->stripes[0].dev) {
10119 btrfs_warn(fs_info, "swapfile must be on one device");
10120 ret = -EINVAL;
10121 goto out;
10122 }
10123
10124 physical_block_start = (em->map_lookup->stripes[0].physical +
10125 (logical_block_start - em->start));
10126 len = min(len, em->len - (logical_block_start - em->start));
10127 free_extent_map(em);
10128 em = NULL;
10129
10130 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10131 if (!bg) {
10132 btrfs_warn(fs_info,
10133 "could not find block group containing swapfile");
10134 ret = -EINVAL;
10135 goto out;
10136 }
10137
10138 ret = btrfs_add_swapfile_pin(inode, bg, true);
10139 if (ret) {
10140 btrfs_put_block_group(bg);
10141 if (ret == 1)
10142 ret = 0;
10143 else
10144 goto out;
10145 }
10146
10147 if (bsi.block_len &&
10148 bsi.block_start + bsi.block_len == physical_block_start) {
10149 bsi.block_len += len;
10150 } else {
10151 if (bsi.block_len) {
10152 ret = btrfs_add_swap_extent(sis, &bsi);
10153 if (ret)
10154 goto out;
10155 }
10156 bsi.start = start;
10157 bsi.block_start = physical_block_start;
10158 bsi.block_len = len;
10159 }
10160
10161 start += len;
10162 }
10163
10164 if (bsi.block_len)
10165 ret = btrfs_add_swap_extent(sis, &bsi);
10166
10167out:
10168 if (!IS_ERR_OR_NULL(em))
10169 free_extent_map(em);
10170
10171 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10172
10173 if (ret)
10174 btrfs_swap_deactivate(file);
10175
10176 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10177
10178 if (ret)
10179 return ret;
10180
10181 if (device)
10182 sis->bdev = device->bdev;
10183 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10184 sis->max = bsi.nr_pages;
10185 sis->pages = bsi.nr_pages - 1;
10186 sis->highest_bit = bsi.nr_pages - 1;
10187 return bsi.nr_extents;
10188}
10189#else
10190static void btrfs_swap_deactivate(struct file *file)
10191{
10192}
10193
10194static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10195 sector_t *span)
10196{
10197 return -EOPNOTSUPP;
10198}
10199#endif
10200
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010201static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010202 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010203 .lookup = btrfs_lookup,
10204 .create = btrfs_create,
10205 .unlink = btrfs_unlink,
10206 .link = btrfs_link,
10207 .mkdir = btrfs_mkdir,
10208 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010209 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010210 .symlink = btrfs_symlink,
10211 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010212 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010213 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010214 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010215 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010216 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010217 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010218 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010219};
Yan, Zheng76dda932009-09-21 16:00:26 -040010220
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010221static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010222 .llseek = generic_file_llseek,
10223 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010224 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010225 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010226 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010227#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010228 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010229#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010230 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010231 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010232};
10233
David Sterba20e55062015-11-19 11:42:28 +010010234static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010235 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010236 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010237 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10238};
10239
Chris Mason35054392009-01-21 13:11:13 -050010240/*
10241 * btrfs doesn't support the bmap operation because swapfiles
10242 * use bmap to make a mapping of extents in the file. They assume
10243 * these extents won't change over the life of the file and they
10244 * use the bmap result to do IO directly to the drive.
10245 *
10246 * the btrfs bmap call would return logical addresses that aren't
10247 * suitable for IO and they also will change frequently as COW
10248 * operations happen. So, swapfile + btrfs == corruption.
10249 *
10250 * For now we're avoiding this by dropping bmap.
10251 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010252static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010253 .readpage = btrfs_readpage,
10254 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010255 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010256 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010257 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010258 .invalidatepage = btrfs_invalidatepage,
10259 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010260#ifdef CONFIG_MIGRATION
10261 .migratepage = btrfs_migratepage,
10262#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010263 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010264 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010265 .swap_activate = btrfs_swap_activate,
10266 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010267};
10268
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010269static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010270 .getattr = btrfs_getattr,
10271 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010272 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010273 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010274 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010275 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010276 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010277 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010278};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010279static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010280 .getattr = btrfs_getattr,
10281 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010282 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010283 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010284 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010285 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010286 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010287};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010288static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010289 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010290 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010291 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010292 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010293 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010294 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010295};
Yan, Zheng76dda932009-09-21 16:00:26 -040010296
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010297const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010298 .d_delete = btrfs_dentry_delete,
10299};