blob: 516192eccf52ff742d6bcd99667767b712233cb2 [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;
Qu Wenruo9729f102020-06-10 09:04:41 +08002564 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002565
2566 memset(&stack_fi, 0, sizeof(stack_fi));
2567 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2568 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2569 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2570 oe->disk_num_bytes);
2571 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2572 logical_len = oe->truncated_len;
2573 else
2574 logical_len = oe->num_bytes;
2575 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2576 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2577 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2578 /* Encryption and other encoding is reserved and all 0 */
2579
Qu Wenruo9729f102020-06-10 09:04:41 +08002580 ret = btrfs_qgroup_release_data(inode, oe->file_offset, logical_len);
2581 if (ret < 0)
2582 return ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002583 return insert_reserved_file_extent(trans, inode, oe->file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08002584 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002585}
2586
2587/*
2588 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002589 * an ordered extent if the range of bytes in the file it covers are
2590 * fully written.
2591 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002592static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002593{
Josef Bacik5fd02042012-05-02 14:00:54 -04002594 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002595 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002596 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002597 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002598 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002599 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002600 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002601 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002602 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002603 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002604 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002605 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002606 bool range_locked = false;
2607 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002608 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002609 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002610
Omar Sandovalbffe6332019-12-02 17:34:19 -08002611 start = ordered_extent->file_offset;
2612 end = start + ordered_extent->num_bytes - 1;
2613
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002614 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2615 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2616 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2617 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002618
Nikolay Borisov8d510122019-10-08 20:43:06 +03002619 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002620
Josef Bacik5fd02042012-05-02 14:00:54 -04002621 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2622 ret = -EIO;
2623 goto out;
2624 }
2625
Omar Sandovalbffe6332019-12-02 17:34:19 -08002626 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002627
Josef Bacik77cef2e2013-08-29 13:57:21 -04002628 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2629 truncated = true;
2630 logical_len = ordered_extent->truncated_len;
2631 /* Truncated the entire extent, don't bother adding */
2632 if (!logical_len)
2633 goto out;
2634 }
2635
Yan, Zhengc2167752009-11-12 09:34:21 +00002636 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002637 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002638
2639 /*
2640 * For mwrite(mmap + memset to write) case, we still reserve
2641 * space for NOCOW range.
2642 * As NOCOW won't cause a new delayed ref, just free the space
2643 */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002644 btrfs_qgroup_free_data(inode, NULL, start,
2645 ordered_extent->num_bytes);
Josef Bacikd923afe2020-01-17 09:02:23 -05002646 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002647 if (freespace_inode)
2648 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002649 else
2650 trans = btrfs_join_transaction(root);
2651 if (IS_ERR(trans)) {
2652 ret = PTR_ERR(trans);
2653 trans = NULL;
2654 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002655 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002656 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002657 ret = btrfs_update_inode_fallback(trans, root, inode);
2658 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002659 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002660 goto out;
2661 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002662
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002663 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002664 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002665
Nikolay Borisov8d510122019-10-08 20:43:06 +03002666 if (freespace_inode)
2667 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002668 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002669 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002670 if (IS_ERR(trans)) {
2671 ret = PTR_ERR(trans);
2672 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002673 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002674 }
Chris Masona79b7d42014-05-22 16:18:52 -07002675
Josef Bacik69fe2d72017-10-19 14:15:57 -04002676 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002677
Chris Masonc8b97812008-10-29 14:49:59 -04002678 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002679 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002680 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002681 BUG_ON(compress_type);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002682 btrfs_qgroup_free_data(inode, NULL, start,
2683 ordered_extent->num_bytes);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002684 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002685 ordered_extent->file_offset,
2686 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002687 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002688 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002689 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002690 ret = insert_ordered_extent_file_extent(trans, inode,
2691 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002692 if (!ret) {
2693 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002694 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002695 ordered_extent->disk_bytenr,
2696 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002697 }
Yan Zhengd899e052008-10-30 14:25:28 -04002698 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002699 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002700 ordered_extent->file_offset,
2701 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002702 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002703 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002704 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002705 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002706
Nikolay Borisovac01f262018-01-08 10:59:43 +02002707 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2708 if (ret) {
2709 btrfs_abort_transaction(trans, ret);
2710 goto out;
2711 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002712
Josef Bacikd923afe2020-01-17 09:02:23 -05002713 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002714 ret = btrfs_update_inode_fallback(trans, root, inode);
2715 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002716 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002717 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002718 }
2719 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002720out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002721 clear_bits = EXTENT_DEFRAG;
2722 if (range_locked)
2723 clear_bits |= EXTENT_LOCKED;
2724 if (clear_new_delalloc_bytes)
2725 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002726 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2727 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002728 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002729
Miao Xiea698d0752012-09-20 01:51:59 -06002730 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002731 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002732
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002734 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002735
2736 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002737 unwritten_start += logical_len;
2738 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002739
2740 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002741 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002742
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002743 /*
2744 * If the ordered extent had an IOERR or something else went
2745 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002746 * back to the allocator. We only free the extent in the
2747 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002748 *
2749 * If we made it past insert_reserved_file_extent before we
2750 * errored out then we don't need to do this as the accounting
2751 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002752 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002753 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002754 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002755 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002756 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2757 /*
2758 * Discard the range before returning it back to the
2759 * free space pool
2760 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002761 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002762 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002763 ordered_extent->disk_bytenr,
2764 ordered_extent->disk_num_bytes,
2765 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002766 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002767 ordered_extent->disk_bytenr,
2768 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002769 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002770 }
2771
Josef Bacik5fd02042012-05-02 14:00:54 -04002772 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002773 * This needs to be done to make sure anybody waiting knows we are done
2774 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 */
2776 btrfs_remove_ordered_extent(inode, ordered_extent);
2777
Chris Masone6dcd2d2008-07-17 12:53:50 -04002778 /* once for us */
2779 btrfs_put_ordered_extent(ordered_extent);
2780 /* once for the tree */
2781 btrfs_put_ordered_extent(ordered_extent);
2782
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 return ret;
2784}
2785
2786static void finish_ordered_fn(struct btrfs_work *work)
2787{
2788 struct btrfs_ordered_extent *ordered_extent;
2789 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2790 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791}
2792
Nikolay Borisovc6297322018-11-08 10:18:08 +02002793void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2794 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002795{
Josef Bacik5fd02042012-05-02 14:00:54 -04002796 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002797 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002798 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002799 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002800
liubo1abe9b82011-03-24 11:18:59 +00002801 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2802
Chris Mason8b62b722009-09-02 16:53:46 -04002803 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002804 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2805 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002806 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002807
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002808 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002810 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002812
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002813 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002814 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002815}
2816
Omar Sandoval47df7762020-04-16 14:46:17 -07002817static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2818 int icsum, struct page *page, int pgoff, u64 start,
2819 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002820{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002821 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2822 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002823 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002824 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2825 u8 *csum_expected;
2826 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002827
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002828 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002829
2830 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002831 shash->tfm = fs_info->csum_shash;
2832
Eric Biggersfd080012020-04-30 23:51:59 -07002833 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002834
2835 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002836 goto zeroit;
2837
2838 kunmap_atomic(kaddr);
2839 return 0;
2840zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002841 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2842 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002843 memset(kaddr + pgoff, 1, len);
2844 flush_dcache_page(page);
2845 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002846 return -EIO;
2847}
2848
Chris Masond352ac62008-09-29 15:18:18 -04002849/*
Chris Masond352ac62008-09-29 15:18:18 -04002850 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002851 * if there's a match, we allow the bio to finish. If not, the code in
2852 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002853 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002854static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2855 u64 phy_offset, struct page *page,
2856 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002857{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002858 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002859 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002860 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002861 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002862
Chris Masond20f7042008-12-08 16:58:54 -05002863 if (PageChecked(page)) {
2864 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002865 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002866 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002867
2868 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002869 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002870
Yan Zheng17d217f2008-12-12 10:03:38 -05002871 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002872 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002873 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002874 return 0;
2875 }
2876
Miao Xiefacc8a222013-07-25 19:22:34 +08002877 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002878 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2879 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002880}
Chris Masonb888db22007-08-27 16:49:44 -04002881
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002882/*
2883 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2884 *
2885 * @inode: The inode we want to perform iput on
2886 *
2887 * This function uses the generic vfs_inode::i_count to track whether we should
2888 * just decrement it (in case it's > 1) or if this is the last iput then link
2889 * the inode to the delayed iput machinery. Delayed iputs are processed at
2890 * transaction commit time/superblock commit/cleaner kthread.
2891 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002892void btrfs_add_delayed_iput(struct inode *inode)
2893{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002894 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002895 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002896
2897 if (atomic_add_unless(&inode->i_count, -1, 1))
2898 return;
2899
Josef Bacik034f7842018-12-03 11:06:52 -05002900 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002901 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002902 ASSERT(list_empty(&binode->delayed_iput));
2903 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002904 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002905 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2906 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002907}
2908
Josef Bacik63611e72019-06-18 10:59:18 -04002909static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2910 struct btrfs_inode *inode)
2911{
2912 list_del_init(&inode->delayed_iput);
2913 spin_unlock(&fs_info->delayed_iput_lock);
2914 iput(&inode->vfs_inode);
2915 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2916 wake_up(&fs_info->delayed_iputs_wait);
2917 spin_lock(&fs_info->delayed_iput_lock);
2918}
2919
2920static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2921 struct btrfs_inode *inode)
2922{
2923 if (!list_empty(&inode->delayed_iput)) {
2924 spin_lock(&fs_info->delayed_iput_lock);
2925 if (!list_empty(&inode->delayed_iput))
2926 run_delayed_iput_locked(fs_info, inode);
2927 spin_unlock(&fs_info->delayed_iput_lock);
2928 }
2929}
2930
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002931void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002932{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002933
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002934 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002935 while (!list_empty(&fs_info->delayed_iputs)) {
2936 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002937
David Sterba8089fe62015-11-19 14:15:51 +01002938 inode = list_first_entry(&fs_info->delayed_iputs,
2939 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002940 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002941 }
David Sterba8089fe62015-11-19 14:15:51 +01002942 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002943}
2944
Josef Bacik034f7842018-12-03 11:06:52 -05002945/**
2946 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2947 * @fs_info - the fs_info for this fs
2948 * @return - EINTR if we were killed, 0 if nothing's pending
2949 *
2950 * This will wait on any delayed iputs that are currently running with KILLABLE
2951 * set. Once they are all done running we will return, unless we are killed in
2952 * which case we return EINTR. This helps in user operations like fallocate etc
2953 * that might get blocked on the iputs.
2954 */
2955int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2956{
2957 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2958 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2959 if (ret)
2960 return -EINTR;
2961 return 0;
2962}
2963
Yan, Zhengd68fc572010-05-16 10:49:58 -04002964/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002965 * This creates an orphan entry for the given inode in case something goes wrong
2966 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002967 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002968int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002969 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002970{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002971 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002972
Omar Sandoval27919062018-05-11 13:13:37 -07002973 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2974 if (ret && ret != -EEXIST) {
2975 btrfs_abort_transaction(trans, ret);
2976 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002977 }
2978
Yan, Zhengd68fc572010-05-16 10:49:58 -04002979 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002980}
2981
2982/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002983 * We have done the delete so we can go ahead and remove the orphan item for
2984 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002985 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002986static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002987 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002988{
Omar Sandoval27919062018-05-11 13:13:37 -07002989 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002990}
2991
2992/*
2993 * this cleans up any orphans that may be left on the list from the last use
2994 * of this root.
2995 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002996int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002997{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002998 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002999 struct btrfs_path *path;
3000 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003001 struct btrfs_key key, found_key;
3002 struct btrfs_trans_handle *trans;
3003 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003004 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003005 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003006
Yan, Zhengd68fc572010-05-16 10:49:58 -04003007 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003008 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003009
3010 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003011 if (!path) {
3012 ret = -ENOMEM;
3013 goto out;
3014 }
David Sterbae4058b52015-11-27 16:31:35 +01003015 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003016
3017 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003018 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003019 key.offset = (u64)-1;
3020
Josef Bacik7b128762008-07-24 12:17:14 -04003021 while (1) {
3022 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003023 if (ret < 0)
3024 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003025
3026 /*
3027 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003028 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003029 * find the key and see if we have stuff that matches
3030 */
3031 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003032 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003033 if (path->slots[0] == 0)
3034 break;
3035 path->slots[0]--;
3036 }
3037
3038 /* pull out the item */
3039 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003040 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3041
3042 /* make sure the item matches what we want */
3043 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3044 break;
David Sterba962a2982014-06-04 18:41:45 +02003045 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003046 break;
3047
3048 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003049 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003050
3051 /*
3052 * this is where we are basically btrfs_lookup, without the
3053 * crossing root thing. we store the inode number in the
3054 * offset of the orphan item.
3055 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003056
3057 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003058 btrfs_err(fs_info,
3059 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003060 ret = -EINVAL;
3061 goto out;
3062 }
3063
3064 last_objectid = found_key.offset;
3065
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003066 found_key.objectid = found_key.offset;
3067 found_key.type = BTRFS_INODE_ITEM_KEY;
3068 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003069 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303070 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003071 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003072 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003074 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003075 struct btrfs_root *dead_root;
3076 struct btrfs_fs_info *fs_info = root->fs_info;
3077 int is_dead_root = 0;
3078
3079 /*
3080 * this is an orphan in the tree root. Currently these
3081 * could come from 2 sources:
3082 * a) a snapshot deletion in progress
3083 * b) a free space cache inode
3084 * We need to distinguish those two, as the snapshot
3085 * orphan must not get deleted.
3086 * find_dead_roots already ran before us, so if this
3087 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003088 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003089 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003090
3091 spin_lock(&fs_info->fs_roots_radix_lock);
3092 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3093 (unsigned long)found_key.objectid);
3094 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3095 is_dead_root = 1;
3096 spin_unlock(&fs_info->fs_roots_radix_lock);
3097
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003098 if (is_dead_root) {
3099 /* prevent this orphan from being found again */
3100 key.offset = found_key.objectid - 1;
3101 continue;
3102 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003103
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003104 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003105
Josef Bacika8c9e572011-09-21 16:55:59 -04003106 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003107 * If we have an inode with links, there are a couple of
3108 * possibilities. Old kernels (before v3.12) used to create an
3109 * orphan item for truncate indicating that there were possibly
3110 * extent items past i_size that needed to be deleted. In v3.12,
3111 * truncate was changed to update i_size in sync with the extent
3112 * items, but the (useless) orphan item was still created. Since
3113 * v4.18, we don't create the orphan item for truncate at all.
3114 *
3115 * So, this item could mean that we need to do a truncate, but
3116 * only if this filesystem was last used on a pre-v3.12 kernel
3117 * and was not cleanly unmounted. The odds of that are quite
3118 * slim, and it's a pain to do the truncate now, so just delete
3119 * the orphan item.
3120 *
3121 * It's also possible that this orphan item was supposed to be
3122 * deleted but wasn't. The inode number may have been reused,
3123 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003124 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003125 if (ret == -ENOENT || inode->i_nlink) {
3126 if (!ret)
3127 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003128 trans = btrfs_start_transaction(root, 1);
3129 if (IS_ERR(trans)) {
3130 ret = PTR_ERR(trans);
3131 goto out;
3132 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 btrfs_debug(fs_info, "auto deleting %Lu",
3134 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003135 ret = btrfs_del_orphan_item(trans, root,
3136 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003137 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003138 if (ret)
3139 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003140 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003141 }
Josef Bacik7b128762008-07-24 12:17:14 -04003142
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003143 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003144
3145 /* this will do delete_inode and everything for us */
3146 iput(inode);
3147 }
Miao Xie3254c872011-11-10 20:45:05 -05003148 /* release the path since we're done with it */
3149 btrfs_release_path(path);
3150
Yan, Zhengd68fc572010-05-16 10:49:58 -04003151 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3152
Omar Sandovala575cee2018-05-11 13:13:38 -07003153 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003154 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003155 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003156 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003157 }
Josef Bacik7b128762008-07-24 12:17:14 -04003158
3159 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003161
3162out:
3163 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003164 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003165 btrfs_free_path(path);
3166 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003167}
3168
Chris Masond352ac62008-09-29 15:18:18 -04003169/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003170 * very simple check to peek ahead in the leaf looking for xattrs. If we
3171 * don't find any xattrs, we know there can't be any acls.
3172 *
3173 * slot is the slot the inode is in, objectid is the objectid of the inode
3174 */
3175static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003176 int slot, u64 objectid,
3177 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003178{
3179 u32 nritems = btrfs_header_nritems(leaf);
3180 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003181 static u64 xattr_access = 0;
3182 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003183 int scanned = 0;
3184
Josef Bacikf23b5a52013-06-19 10:16:26 -04003185 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003186 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3187 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3188 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3189 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003190 }
3191
Chris Mason46a53cc2009-04-27 11:47:50 -04003192 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003193 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003194 while (slot < nritems) {
3195 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3196
3197 /* we found a different objectid, there must not be acls */
3198 if (found_key.objectid != objectid)
3199 return 0;
3200
3201 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003202 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003203 if (*first_xattr_slot == -1)
3204 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003205 if (found_key.offset == xattr_access ||
3206 found_key.offset == xattr_default)
3207 return 1;
3208 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003209
3210 /*
3211 * we found a key greater than an xattr key, there can't
3212 * be any acls later on
3213 */
3214 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3215 return 0;
3216
3217 slot++;
3218 scanned++;
3219
3220 /*
3221 * it goes inode, inode backrefs, xattrs, extents,
3222 * so if there are a ton of hard links to an inode there can
3223 * be a lot of backrefs. Don't waste time searching too hard,
3224 * this is just an optimization
3225 */
3226 if (scanned >= 8)
3227 break;
3228 }
3229 /* we hit the end of the leaf before we found an xattr or
3230 * something larger than an xattr. We have to assume the inode
3231 * has acls
3232 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003233 if (*first_xattr_slot == -1)
3234 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003235 return 1;
3236}
3237
3238/*
Chris Masond352ac62008-09-29 15:18:18 -04003239 * read an inode from the btree into the in-memory inode
3240 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003241static int btrfs_read_locked_inode(struct inode *inode,
3242 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003243{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003244 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003245 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003246 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003247 struct btrfs_inode_item *inode_item;
3248 struct btrfs_root *root = BTRFS_I(inode)->root;
3249 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003250 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003251 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003252 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003253 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003254 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003255 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003256
3257 ret = btrfs_fill_inode(inode, &rdev);
3258 if (!ret)
3259 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003260
Filipe Manana4222ea72018-10-24 10:13:03 +01003261 if (!path) {
3262 path = btrfs_alloc_path();
3263 if (!path)
3264 return -ENOMEM;
3265 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003266
Chris Mason39279cc2007-06-12 06:35:45 -04003267 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003268
Chris Mason39279cc2007-06-12 06:35:45 -04003269 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003270 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003271 if (path != in_path)
3272 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003273 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003274 }
Chris Mason39279cc2007-06-12 06:35:45 -04003275
Chris Mason5f39d392007-10-15 16:14:19 -04003276 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003277
3278 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003279 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003280
Chris Mason5f39d392007-10-15 16:14:19 -04003281 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3282 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003283 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003284 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003285 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3286 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003287 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003288 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3289 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003290
David Sterbaa937b972014-12-12 17:39:12 +01003291 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3292 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003293
David Sterbaa937b972014-12-12 17:39:12 +01003294 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3295 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003296
David Sterbaa937b972014-12-12 17:39:12 +01003297 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3298 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003299
chandan r9cc97d62012-07-04 12:48:07 +05303300 BTRFS_I(inode)->i_otime.tv_sec =
3301 btrfs_timespec_sec(leaf, &inode_item->otime);
3302 BTRFS_I(inode)->i_otime.tv_nsec =
3303 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003304
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003305 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003306 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003307 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3308
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003309 inode_set_iversion_queried(inode,
3310 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003311 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003312 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003313 rdev = btrfs_inode_rdev(leaf, inode_item);
3314
Josef Bacikaec74772008-07-24 12:12:38 -04003315 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003316 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003317
3318cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003319 /*
3320 * If we were modified in the current generation and evicted from memory
3321 * and then re-read we need to do a full sync since we don't have any
3322 * idea about which extents were modified before we were evicted from
3323 * cache.
3324 *
3325 * This is required for both inode re-read from disk and delayed inode
3326 * in delayed_nodes_tree.
3327 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003328 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003329 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3330 &BTRFS_I(inode)->runtime_flags);
3331
Filipe Mananabde6c242015-07-24 00:00:19 +01003332 /*
3333 * We don't persist the id of the transaction where an unlink operation
3334 * against the inode was last made. So here we assume the inode might
3335 * have been evicted, and therefore the exact value of last_unlink_trans
3336 * lost, and set it to last_trans to avoid metadata inconsistencies
3337 * between the inode and its parent if the inode is fsync'ed and the log
3338 * replayed. For example, in the scenario:
3339 *
3340 * touch mydir/foo
3341 * ln mydir/foo mydir/bar
3342 * sync
3343 * unlink mydir/bar
3344 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3345 * xfs_io -c fsync mydir/foo
3346 * <power failure>
3347 * mount fs, triggers fsync log replay
3348 *
3349 * We must make sure that when we fsync our inode foo we also log its
3350 * parent inode, otherwise after log replay the parent still has the
3351 * dentry with the "bar" name but our inode foo has a link count of 1
3352 * and doesn't have an inode ref with the name "bar" anymore.
3353 *
3354 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003355 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003356 * transaction commits on fsync if our inode is a directory, or if our
3357 * inode is not a directory, logging its parent unnecessarily.
3358 */
3359 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3360
Miao Xie67de1172013-12-26 13:07:06 +08003361 path->slots[0]++;
3362 if (inode->i_nlink != 1 ||
3363 path->slots[0] >= btrfs_header_nritems(leaf))
3364 goto cache_acl;
3365
3366 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003367 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003368 goto cache_acl;
3369
3370 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3371 if (location.type == BTRFS_INODE_REF_KEY) {
3372 struct btrfs_inode_ref *ref;
3373
3374 ref = (struct btrfs_inode_ref *)ptr;
3375 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3376 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3377 struct btrfs_inode_extref *extref;
3378
3379 extref = (struct btrfs_inode_extref *)ptr;
3380 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3381 extref);
3382 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003383cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003384 /*
3385 * try to precache a NULL acl entry for files that don't have
3386 * any xattrs or acls
3387 */
Li Zefan33345d012011-04-20 10:31:50 +08003388 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003389 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003390 if (first_xattr_slot != -1) {
3391 path->slots[0] = first_xattr_slot;
3392 ret = btrfs_load_inode_props(inode, path);
3393 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003395 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003396 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003397 root->root_key.objectid, ret);
3398 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003399 if (path != in_path)
3400 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003401
Al Viro72c04902009-06-24 16:58:48 -04003402 if (!maybe_acls)
3403 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003404
Chris Mason39279cc2007-06-12 06:35:45 -04003405 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003406 case S_IFREG:
3407 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003408 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003409 inode->i_fop = &btrfs_file_operations;
3410 inode->i_op = &btrfs_file_inode_operations;
3411 break;
3412 case S_IFDIR:
3413 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003414 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003415 break;
3416 case S_IFLNK:
3417 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003418 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003419 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003420 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003421 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003422 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003423 init_special_inode(inode, inode->i_mode, rdev);
3424 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003425 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003426
David Sterba7b6a2212018-03-26 18:40:21 +02003427 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003428 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003429}
3430
Chris Masond352ac62008-09-29 15:18:18 -04003431/*
3432 * given a leaf and an inode, copy the inode fields into the leaf
3433 */
Chris Masone02119d2008-09-05 16:13:11 -04003434static void fill_inode_item(struct btrfs_trans_handle *trans,
3435 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003436 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003437 struct inode *inode)
3438{
Liu Bo51fab692012-12-27 09:01:21 +00003439 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003440
David Sterbac82f8232019-08-09 17:48:21 +02003441 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003442
David Sterbacc4c13d2020-04-29 02:15:56 +02003443 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3444 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3445 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3446 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3447 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003448
David Sterbacc4c13d2020-04-29 02:15:56 +02003449 btrfs_set_token_timespec_sec(&token, &item->atime,
3450 inode->i_atime.tv_sec);
3451 btrfs_set_token_timespec_nsec(&token, &item->atime,
3452 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003453
David Sterbacc4c13d2020-04-29 02:15:56 +02003454 btrfs_set_token_timespec_sec(&token, &item->mtime,
3455 inode->i_mtime.tv_sec);
3456 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3457 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003458
David Sterbacc4c13d2020-04-29 02:15:56 +02003459 btrfs_set_token_timespec_sec(&token, &item->ctime,
3460 inode->i_ctime.tv_sec);
3461 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3462 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003463
David Sterbacc4c13d2020-04-29 02:15:56 +02003464 btrfs_set_token_timespec_sec(&token, &item->otime,
3465 BTRFS_I(inode)->i_otime.tv_sec);
3466 btrfs_set_token_timespec_nsec(&token, &item->otime,
3467 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303468
David Sterbacc4c13d2020-04-29 02:15:56 +02003469 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3470 btrfs_set_token_inode_generation(&token, item,
3471 BTRFS_I(inode)->generation);
3472 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3473 btrfs_set_token_inode_transid(&token, item, trans->transid);
3474 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3475 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3476 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003477}
3478
Chris Masond352ac62008-09-29 15:18:18 -04003479/*
3480 * copy everything in the in-memory inode into the btree.
3481 */
Chris Mason21151332011-11-10 20:39:08 -05003482static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003483 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003484{
3485 struct btrfs_inode_item *inode_item;
3486 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003487 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003488 int ret;
3489
3490 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003491 if (!path)
3492 return -ENOMEM;
3493
Chris Masonb9473432009-03-13 11:00:37 -04003494 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003495 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3496 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 if (ret) {
3498 if (ret > 0)
3499 ret = -ENOENT;
3500 goto failed;
3501 }
3502
Chris Mason5f39d392007-10-15 16:14:19 -04003503 leaf = path->nodes[0];
3504 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003505 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003506
Chris Masone02119d2008-09-05 16:13:11 -04003507 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003508 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003509 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003510 ret = 0;
3511failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003512 btrfs_free_path(path);
3513 return ret;
3514}
3515
Chris Masond352ac62008-09-29 15:18:18 -04003516/*
Chris Mason21151332011-11-10 20:39:08 -05003517 * copy everything in the in-memory inode into the btree.
3518 */
3519noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3520 struct btrfs_root *root, struct inode *inode)
3521{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003522 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003523 int ret;
3524
3525 /*
3526 * If the inode is a free space inode, we can deadlock during commit
3527 * if we put it into the delayed code.
3528 *
3529 * The data relocation inode should also be directly updated
3530 * without delay
3531 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003532 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003533 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003535 btrfs_update_root_times(trans, root);
3536
Chris Mason21151332011-11-10 20:39:08 -05003537 ret = btrfs_delayed_update_inode(trans, root, inode);
3538 if (!ret)
3539 btrfs_set_inode_last_trans(trans, inode);
3540 return ret;
3541 }
3542
3543 return btrfs_update_inode_item(trans, root, inode);
3544}
3545
Josef Bacikbe6aef62012-10-22 15:43:12 -04003546noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3547 struct btrfs_root *root,
3548 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003549{
3550 int ret;
3551
3552 ret = btrfs_update_inode(trans, root, inode);
3553 if (ret == -ENOSPC)
3554 return btrfs_update_inode_item(trans, root, inode);
3555 return ret;
3556}
3557
3558/*
Chris Masond352ac62008-09-29 15:18:18 -04003559 * unlink helper that gets used here in inode.c and in the tree logging
3560 * recovery code. It remove a link in a directory with a given name, and
3561 * also drops the back refs in the inode to the directory
3562 */
Al Viro92986792011-03-04 17:14:37 +00003563static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3564 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003565 struct btrfs_inode *dir,
3566 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003567 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003568{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003569 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003570 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003571 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003572 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003573 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003574 u64 ino = btrfs_ino(inode);
3575 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003576
3577 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003578 if (!path) {
3579 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003580 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003581 }
3582
Chris Masonb9473432009-03-13 11:00:37 -04003583 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003584 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003585 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003586 if (IS_ERR_OR_NULL(di)) {
3587 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003588 goto err;
3589 }
Chris Mason39279cc2007-06-12 06:35:45 -04003590 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003591 if (ret)
3592 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003593 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003594
Miao Xie67de1172013-12-26 13:07:06 +08003595 /*
3596 * If we don't have dir index, we have to get it by looking up
3597 * the inode ref, since we get the inode ref, remove it directly,
3598 * it is unnecessary to do delayed deletion.
3599 *
3600 * But if we have dir index, needn't search inode ref to get it.
3601 * Since the inode ref is close to the inode item, it is better
3602 * that we delay to delete it, and just do this deletion when
3603 * we update the inode item.
3604 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003605 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003606 ret = btrfs_delayed_delete_inode_ref(inode);
3607 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003608 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003609 goto skip_backref;
3610 }
3611 }
3612
Li Zefan33345d012011-04-20 10:31:50 +08003613 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3614 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003615 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003616 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003617 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003618 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003619 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003620 goto err;
3621 }
Miao Xie67de1172013-12-26 13:07:06 +08003622skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003623 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003624 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003625 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003626 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003627 }
Chris Mason39279cc2007-06-12 06:35:45 -04003628
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003629 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3630 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003631 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003632 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003633 goto err;
3634 }
Chris Masone02119d2008-09-05 16:13:11 -04003635
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003636 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3637 index);
Chris Mason6418c962010-10-30 07:34:24 -04003638 if (ret == -ENOENT)
3639 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003640 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003641 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003642
3643 /*
3644 * If we have a pending delayed iput we could end up with the final iput
3645 * being run in btrfs-cleaner context. If we have enough of these built
3646 * up we can end up burning a lot of time in btrfs-cleaner without any
3647 * way to throttle the unlinks. Since we're currently holding a ref on
3648 * the inode we can run the delayed iput here without any issues as the
3649 * final iput won't be done until after we drop the ref we're currently
3650 * holding.
3651 */
3652 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003653err:
3654 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003655 if (ret)
3656 goto out;
3657
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003658 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003659 inode_inc_iversion(&inode->vfs_inode);
3660 inode_inc_iversion(&dir->vfs_inode);
3661 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3662 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3663 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003664out:
Chris Mason39279cc2007-06-12 06:35:45 -04003665 return ret;
3666}
3667
Al Viro92986792011-03-04 17:14:37 +00003668int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3669 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003670 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003671 const char *name, int name_len)
3672{
3673 int ret;
3674 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3675 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003676 drop_nlink(&inode->vfs_inode);
3677 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003678 }
3679 return ret;
3680}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681
3682/*
3683 * helper to start transaction for unlink and rmdir.
3684 *
Josef Bacikd52be812013-05-29 14:54:47 -04003685 * unlink and rmdir are special in btrfs, they do not always free space, so
3686 * if we cannot make our reservations the normal way try and see if there is
3687 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3688 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689 */
Josef Bacikd52be812013-05-29 14:54:47 -04003690static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003692 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693
Josef Bacike70bea52011-10-11 14:18:24 -04003694 /*
3695 * 1 for the possible orphan item
3696 * 1 for the dir item
3697 * 1 for the dir index
3698 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003699 * 1 for the inode
3700 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003701 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702}
3703
Chris Mason39279cc2007-06-12 06:35:45 -04003704static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3705{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003706 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003707 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003708 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003709 int ret;
3710
Josef Bacikd52be812013-05-29 14:54:47 -04003711 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003713 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003714
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003715 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3716 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003717
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003718 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3719 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3720 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003721 if (ret)
3722 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003723
Yan, Zhenga22285a2010-05-16 10:48:46 -04003724 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003725 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003726 if (ret)
3727 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728 }
Josef Bacik7b128762008-07-24 12:17:14 -04003729
Tsutomu Itohb5324022011-07-19 07:27:20 +00003730out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003731 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003732 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003733 return ret;
3734}
3735
Misono Tomohirof60a2362018-04-18 11:34:52 +09003736static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003737 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003738{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003739 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003740 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003741 struct btrfs_path *path;
3742 struct extent_buffer *leaf;
3743 struct btrfs_dir_item *di;
3744 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003745 const char *name = dentry->d_name.name;
3746 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003747 u64 index;
3748 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003749 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003750 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003751
Josef Bacik045d3962019-12-18 17:20:27 -05003752 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3753 objectid = inode->root->root_key.objectid;
3754 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3755 objectid = inode->location.objectid;
3756 } else {
3757 WARN_ON(1);
3758 return -EINVAL;
3759 }
3760
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003761 path = btrfs_alloc_path();
3762 if (!path)
3763 return -ENOMEM;
3764
Li Zefan33345d012011-04-20 10:31:50 +08003765 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003766 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003767 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003768 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003769 goto out;
3770 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003771
3772 leaf = path->nodes[0];
3773 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3774 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3775 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003776 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003777 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003778 goto out;
3779 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003780 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003781
Josef Bacikd49d3282019-12-18 17:20:28 -05003782 /*
3783 * This is a placeholder inode for a subvolume we didn't have a
3784 * reference to at the time of the snapshot creation. In the meantime
3785 * we could have renamed the real subvol link into our snapshot, so
3786 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3787 * Instead simply lookup the dir_index_item for this entry so we can
3788 * remove it. Otherwise we know we have a ref to the root and we can
3789 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3790 */
3791 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003792 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003793 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003794 if (IS_ERR_OR_NULL(di)) {
3795 if (!di)
3796 ret = -ENOENT;
3797 else
3798 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003799 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003800 goto out;
3801 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802
3803 leaf = path->nodes[0];
3804 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003806 btrfs_release_path(path);
3807 } else {
3808 ret = btrfs_del_root_ref(trans, objectid,
3809 root->root_key.objectid, dir_ino,
3810 &index, name, name_len);
3811 if (ret) {
3812 btrfs_abort_transaction(trans, ret);
3813 goto out;
3814 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003815 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003816
Lu Fengqi9add2942018-08-01 11:32:26 +08003817 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003819 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003820 goto out;
3821 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003822
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003823 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003824 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003825 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003826 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003827 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003828 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003829out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003830 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003831 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832}
3833
Misono Tomohiroec42f162018-04-18 11:34:13 +09003834/*
3835 * Helper to check if the subvolume references other subvolumes or if it's
3836 * default.
3837 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003838static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003839{
3840 struct btrfs_fs_info *fs_info = root->fs_info;
3841 struct btrfs_path *path;
3842 struct btrfs_dir_item *di;
3843 struct btrfs_key key;
3844 u64 dir_id;
3845 int ret;
3846
3847 path = btrfs_alloc_path();
3848 if (!path)
3849 return -ENOMEM;
3850
3851 /* Make sure this root isn't set as the default subvol */
3852 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3853 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3854 dir_id, "default", 7, 0);
3855 if (di && !IS_ERR(di)) {
3856 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3857 if (key.objectid == root->root_key.objectid) {
3858 ret = -EPERM;
3859 btrfs_err(fs_info,
3860 "deleting default subvolume %llu is not allowed",
3861 key.objectid);
3862 goto out;
3863 }
3864 btrfs_release_path(path);
3865 }
3866
3867 key.objectid = root->root_key.objectid;
3868 key.type = BTRFS_ROOT_REF_KEY;
3869 key.offset = (u64)-1;
3870
3871 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3872 if (ret < 0)
3873 goto out;
3874 BUG_ON(ret == 0);
3875
3876 ret = 0;
3877 if (path->slots[0] > 0) {
3878 path->slots[0]--;
3879 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3880 if (key.objectid == root->root_key.objectid &&
3881 key.type == BTRFS_ROOT_REF_KEY)
3882 ret = -ENOTEMPTY;
3883 }
3884out:
3885 btrfs_free_path(path);
3886 return ret;
3887}
3888
Nikolay Borisov20a68002018-04-27 14:36:24 +03003889/* Delete all dentries for inodes belonging to the root */
3890static void btrfs_prune_dentries(struct btrfs_root *root)
3891{
3892 struct btrfs_fs_info *fs_info = root->fs_info;
3893 struct rb_node *node;
3894 struct rb_node *prev;
3895 struct btrfs_inode *entry;
3896 struct inode *inode;
3897 u64 objectid = 0;
3898
3899 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3900 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3901
3902 spin_lock(&root->inode_lock);
3903again:
3904 node = root->inode_tree.rb_node;
3905 prev = NULL;
3906 while (node) {
3907 prev = node;
3908 entry = rb_entry(node, struct btrfs_inode, rb_node);
3909
David Sterba37508512018-06-29 10:56:40 +02003910 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003911 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003912 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003913 node = node->rb_right;
3914 else
3915 break;
3916 }
3917 if (!node) {
3918 while (prev) {
3919 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003920 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003921 node = prev;
3922 break;
3923 }
3924 prev = rb_next(prev);
3925 }
3926 }
3927 while (node) {
3928 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003929 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003930 inode = igrab(&entry->vfs_inode);
3931 if (inode) {
3932 spin_unlock(&root->inode_lock);
3933 if (atomic_read(&inode->i_count) > 1)
3934 d_prune_aliases(inode);
3935 /*
3936 * btrfs_drop_inode will have it removed from the inode
3937 * cache when its usage count hits zero.
3938 */
3939 iput(inode);
3940 cond_resched();
3941 spin_lock(&root->inode_lock);
3942 goto again;
3943 }
3944
3945 if (cond_resched_lock(&root->inode_lock))
3946 goto again;
3947
3948 node = rb_next(node);
3949 }
3950 spin_unlock(&root->inode_lock);
3951}
3952
Misono Tomohirof60a2362018-04-18 11:34:52 +09003953int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3954{
3955 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3956 struct btrfs_root *root = BTRFS_I(dir)->root;
3957 struct inode *inode = d_inode(dentry);
3958 struct btrfs_root *dest = BTRFS_I(inode)->root;
3959 struct btrfs_trans_handle *trans;
3960 struct btrfs_block_rsv block_rsv;
3961 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003962 int ret;
3963 int err;
3964
3965 /*
3966 * Don't allow to delete a subvolume with send in progress. This is
3967 * inside the inode lock so the error handling that has to drop the bit
3968 * again is not run concurrently.
3969 */
3970 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003971 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003972 spin_unlock(&dest->root_item_lock);
3973 btrfs_warn(fs_info,
3974 "attempt to delete subvolume %llu during send",
3975 dest->root_key.objectid);
3976 return -EPERM;
3977 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003978 root_flags = btrfs_root_flags(&dest->root_item);
3979 btrfs_set_root_flags(&dest->root_item,
3980 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3981 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003982
3983 down_write(&fs_info->subvol_sem);
3984
3985 err = may_destroy_subvol(dest);
3986 if (err)
3987 goto out_up_write;
3988
3989 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3990 /*
3991 * One for dir inode,
3992 * two for dir entries,
3993 * two for root ref/backref.
3994 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003995 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003996 if (err)
3997 goto out_up_write;
3998
3999 trans = btrfs_start_transaction(root, 0);
4000 if (IS_ERR(trans)) {
4001 err = PTR_ERR(trans);
4002 goto out_release;
4003 }
4004 trans->block_rsv = &block_rsv;
4005 trans->bytes_reserved = block_rsv.size;
4006
4007 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4008
Josef Bacik045d3962019-12-18 17:20:27 -05004009 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004010 if (ret) {
4011 err = ret;
4012 btrfs_abort_transaction(trans, ret);
4013 goto out_end_trans;
4014 }
4015
4016 btrfs_record_root_in_trans(trans, dest);
4017
4018 memset(&dest->root_item.drop_progress, 0,
4019 sizeof(dest->root_item.drop_progress));
4020 dest->root_item.drop_level = 0;
4021 btrfs_set_root_refs(&dest->root_item, 0);
4022
4023 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4024 ret = btrfs_insert_orphan_item(trans,
4025 fs_info->tree_root,
4026 dest->root_key.objectid);
4027 if (ret) {
4028 btrfs_abort_transaction(trans, ret);
4029 err = ret;
4030 goto out_end_trans;
4031 }
4032 }
4033
Lu Fengqid1957792018-05-29 15:01:54 +08004034 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004035 BTRFS_UUID_KEY_SUBVOL,
4036 dest->root_key.objectid);
4037 if (ret && ret != -ENOENT) {
4038 btrfs_abort_transaction(trans, ret);
4039 err = ret;
4040 goto out_end_trans;
4041 }
4042 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004043 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004044 dest->root_item.received_uuid,
4045 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4046 dest->root_key.objectid);
4047 if (ret && ret != -ENOENT) {
4048 btrfs_abort_transaction(trans, ret);
4049 err = ret;
4050 goto out_end_trans;
4051 }
4052 }
4053
4054out_end_trans:
4055 trans->block_rsv = NULL;
4056 trans->bytes_reserved = 0;
4057 ret = btrfs_end_transaction(trans);
4058 if (ret && !err)
4059 err = ret;
4060 inode->i_flags |= S_DEAD;
4061out_release:
4062 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4063out_up_write:
4064 up_write(&fs_info->subvol_sem);
4065 if (err) {
4066 spin_lock(&dest->root_item_lock);
4067 root_flags = btrfs_root_flags(&dest->root_item);
4068 btrfs_set_root_flags(&dest->root_item,
4069 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4070 spin_unlock(&dest->root_item_lock);
4071 } else {
4072 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004073 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004074 ASSERT(dest->send_in_progress == 0);
4075
4076 /* the last ref */
4077 if (dest->ino_cache_inode) {
4078 iput(dest->ino_cache_inode);
4079 dest->ino_cache_inode = NULL;
4080 }
4081 }
4082
4083 return err;
4084}
4085
Chris Mason39279cc2007-06-12 06:35:45 -04004086static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4087{
David Howells2b0143b2015-03-17 22:25:59 +00004088 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004089 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004090 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004092 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004093
David Sterbab3ae2442012-09-13 16:04:34 -06004094 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004095 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004096 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004097 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004098
Josef Bacikd52be812013-05-29 14:54:47 -04004099 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004100 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004101 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004102
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004103 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004104 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105 goto out;
4106 }
4107
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004108 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004109 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004110 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004111
Filipe Manana44f714d2016-06-06 16:11:13 +01004112 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4113
Chris Mason39279cc2007-06-12 06:35:45 -04004114 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004115 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4116 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4117 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004118 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004119 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004120 /*
4121 * Propagate the last_unlink_trans value of the deleted dir to
4122 * its parent directory. This is to prevent an unrecoverable
4123 * log tree in the case we do something like this:
4124 * 1) create dir foo
4125 * 2) create snapshot under dir foo
4126 * 3) delete the snapshot
4127 * 4) rmdir foo
4128 * 5) mkdir foo
4129 * 6) fsync foo or some file inside foo
4130 */
4131 if (last_unlink_trans >= trans->transid)
4132 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4133 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004135 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004136 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004137
Chris Mason39279cc2007-06-12 06:35:45 -04004138 return err;
4139}
4140
Josef Bacikddfae632017-10-19 14:16:02 -04004141/*
4142 * Return this if we need to call truncate_block for the last bit of the
4143 * truncate.
4144 */
4145#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004146
Chris Mason323ac952008-10-01 19:05:46 -04004147/*
Chris Mason39279cc2007-06-12 06:35:45 -04004148 * this can truncate away extent items, csum items and directory items.
4149 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004150 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004151 *
4152 * csum items that cross the new i_size are truncated to the new size
4153 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004154 *
4155 * min_type is the minimum key type to truncate down to. If set to 0, this
4156 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004157 */
Yan, Zheng80825102009-11-12 09:35:36 +00004158int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4159 struct btrfs_root *root,
4160 struct inode *inode,
4161 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004162{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004163 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004164 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004165 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004167 struct btrfs_key key;
4168 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004169 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004170 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004171 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004172 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004173 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004174 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004175 int found_extent;
4176 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004177 int pending_del_nr = 0;
4178 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004179 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004180 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004181 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004182 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004183 bool be_nice = false;
4184 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004185 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4186 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004187
4188 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004189
Chris Mason28ed1342014-12-17 09:41:04 -08004190 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004191 * For non-free space inodes and non-shareable roots, we want to back
4192 * off from time to time. This means all inodes in subvolume roots,
4193 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004194 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004195 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004196 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004197 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004198
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004199 path = btrfs_alloc_path();
4200 if (!path)
4201 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004202 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004203
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004204 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004205 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4206 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004207
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004208 /*
4209 * We want to drop from the next block forward in case this
4210 * new size is not block aligned since we will be keeping the
4211 * last block of the extent just the way it is.
4212 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004213 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004214 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004215 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004216 }
Yan, Zheng80825102009-11-12 09:35:36 +00004217
Miao Xie16cdcec2011-04-22 18:12:22 +08004218 /*
4219 * This function is also used to drop the items in the log tree before
4220 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004221 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004222 * items.
4223 */
4224 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004225 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004226
Li Zefan33345d012011-04-20 10:31:50 +08004227 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004228 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004229 key.type = (u8)-1;
4230
Chris Mason85e21ba2008-01-29 15:11:36 -05004231search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004232 /*
4233 * with a 16K leaf size and 128MB extents, you can actually queue
4234 * up a huge file in a single leaf. Most of the time that
4235 * bytes_deleted is > 0, it will be huge by the time we get here
4236 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004237 if (be_nice && bytes_deleted > SZ_32M &&
4238 btrfs_should_end_transaction(trans)) {
4239 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004240 goto out;
4241 }
Chris Masond3977122009-01-05 21:25:51 -05004242
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004243 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4244 if (ret < 0)
4245 goto out;
4246
Chris Mason85e21ba2008-01-29 15:11:36 -05004247 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004248 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004249 /* there are no items in the tree for us to truncate, we're
4250 * done
4251 */
Yan, Zheng80825102009-11-12 09:35:36 +00004252 if (path->slots[0] == 0)
4253 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004254 path->slots[0]--;
4255 }
4256
Chris Masond3977122009-01-05 21:25:51 -05004257 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004258 u64 clear_start = 0, clear_len = 0;
4259
Chris Mason39279cc2007-06-12 06:35:45 -04004260 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004261 leaf = path->nodes[0];
4262 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004263 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004264
Li Zefan33345d012011-04-20 10:31:50 +08004265 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004266 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004267
Chris Mason85e21ba2008-01-29 15:11:36 -05004268 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004269 break;
4270
Chris Mason5f39d392007-10-15 16:14:19 -04004271 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004272 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004273 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004274 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004275 extent_type = btrfs_file_extent_type(leaf, fi);
4276 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004277 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004278 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004279
4280 trace_btrfs_truncate_show_fi_regular(
4281 BTRFS_I(inode), leaf, fi,
4282 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004283 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004284 item_end += btrfs_file_extent_ram_bytes(leaf,
4285 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004286
4287 trace_btrfs_truncate_show_fi_inline(
4288 BTRFS_I(inode), leaf, fi, path->slots[0],
4289 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004290 }
Yan008630c2007-11-07 13:31:09 -05004291 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 }
Yan, Zheng80825102009-11-12 09:35:36 +00004293 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004294 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004295 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004296 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004297 break;
4298 if (found_key.offset >= new_size)
4299 del_item = 1;
4300 else
4301 del_item = 0;
4302 }
Chris Mason39279cc2007-06-12 06:35:45 -04004303 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004304 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004305 if (found_type != BTRFS_EXTENT_DATA_KEY)
4306 goto delete;
4307
4308 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004309 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004310
4311 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004312 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004313 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004314 u64 orig_num_bytes =
4315 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004316 extent_num_bytes = ALIGN(new_size -
4317 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004318 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004319 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004320 btrfs_set_file_extent_num_bytes(leaf, fi,
4321 extent_num_bytes);
4322 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004323 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004324 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004325 &root->state) &&
4326 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004327 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004328 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004329 } else {
Chris Masondb945352007-10-15 16:15:53 -04004330 extent_num_bytes =
4331 btrfs_file_extent_disk_num_bytes(leaf,
4332 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004333 extent_offset = found_key.offset -
4334 btrfs_file_extent_offset(leaf, fi);
4335
Chris Mason39279cc2007-06-12 06:35:45 -04004336 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004337 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004338 if (extent_start != 0) {
4339 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004340 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004341 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004342 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004343 }
4344 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004345 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004346 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004347 /*
4348 * we can't truncate inline items that have had
4349 * special encodings
4350 */
4351 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004352 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004353 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4354 btrfs_file_extent_compression(leaf, fi) == 0) {
4355 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004356
Josef Bacikddfae632017-10-19 14:16:02 -04004357 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4358 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004359 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004360 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004361 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004362 * We have to bail so the last_size is set to
4363 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004364 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004365 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004366 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004367 } else {
4368 /*
4369 * Inline extents are special, we just treat
4370 * them as a full sector worth in the file
4371 * extent tree just for simplicity sake.
4372 */
4373 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004374 }
Josef Bacikddfae632017-10-19 14:16:02 -04004375
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004376 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004377 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004378 }
Chris Mason179e29e2007-11-01 11:28:41 -04004379delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004380 /*
4381 * We use btrfs_truncate_inode_items() to clean up log trees for
4382 * multiple fsyncs, and in this case we don't want to clear the
4383 * file extent range because it's just the log.
4384 */
4385 if (root == BTRFS_I(inode)->root) {
4386 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4387 clear_start, clear_len);
4388 if (ret) {
4389 btrfs_abort_transaction(trans, ret);
4390 break;
4391 }
4392 }
4393
Josef Bacikddfae632017-10-19 14:16:02 -04004394 if (del_item)
4395 last_size = found_key.offset;
4396 else
4397 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004399 if (!pending_del_nr) {
4400 /* no pending yet, add ourselves */
4401 pending_del_slot = path->slots[0];
4402 pending_del_nr = 1;
4403 } else if (pending_del_nr &&
4404 path->slots[0] + 1 == pending_del_slot) {
4405 /* hop on the pending chunk */
4406 pending_del_nr++;
4407 pending_del_slot = path->slots[0];
4408 } else {
Chris Masond3977122009-01-05 21:25:51 -05004409 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004410 }
Chris Mason39279cc2007-06-12 06:35:45 -04004411 } else {
4412 break;
4413 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004414 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004415
Miao Xie27cdeb72014-04-02 19:51:05 +08004416 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004417 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004418 struct btrfs_ref ref = { 0 };
4419
Chris Mason28ed1342014-12-17 09:41:04 -08004420 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004421
4422 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4423 extent_start, extent_num_bytes, 0);
4424 ref.real_root = root->root_key.objectid;
4425 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4426 ino, extent_offset);
4427 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004428 if (ret) {
4429 btrfs_abort_transaction(trans, ret);
4430 break;
4431 }
Chris Mason28f75a02015-02-04 06:59:29 -08004432 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004433 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004434 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004435 }
Chris Mason39279cc2007-06-12 06:35:45 -04004436 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004437
Yan, Zheng80825102009-11-12 09:35:36 +00004438 if (found_type == BTRFS_INODE_ITEM_KEY)
4439 break;
4440
4441 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004442 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004443 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004444 if (pending_del_nr) {
4445 ret = btrfs_del_items(trans, root, path,
4446 pending_del_slot,
4447 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004448 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004449 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004450 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004451 }
Yan, Zheng80825102009-11-12 09:35:36 +00004452 pending_del_nr = 0;
4453 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004454 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004455
Chris Mason28f75a02015-02-04 06:59:29 -08004456 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004457 * We can generate a lot of delayed refs, so we need to
4458 * throttle every once and a while and make sure we're
4459 * adding enough space to keep up with the work we are
4460 * generating. Since we hold a transaction here we
4461 * can't flush, and we don't want to FLUSH_LIMIT because
4462 * we could have generated too many delayed refs to
4463 * actually allocate, so just bail if we're short and
4464 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004465 */
Josef Bacik28bad212018-12-03 10:20:38 -05004466 if (should_throttle) {
4467 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4468 BTRFS_RESERVE_NO_FLUSH);
4469 if (ret) {
4470 ret = -EAGAIN;
4471 break;
4472 }
Chris Mason28f75a02015-02-04 06:59:29 -08004473 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004474 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004475 } else {
4476 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004477 }
Chris Mason39279cc2007-06-12 06:35:45 -04004478 }
Yan, Zheng80825102009-11-12 09:35:36 +00004479out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004480 if (ret >= 0 && pending_del_nr) {
4481 int err;
4482
4483 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004484 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004485 if (err) {
4486 btrfs_abort_transaction(trans, err);
4487 ret = err;
4488 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004489 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004490 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4491 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004492 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004493 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004494 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004495 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4496 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004497 }
Chris Mason28ed1342014-12-17 09:41:04 -08004498
Chris Mason39279cc2007-06-12 06:35:45 -04004499 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004500 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004501}
4502
Chris Masona52d9a82007-08-27 16:49:44 -04004503/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304504 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004505 * @inode - inode that we're zeroing
4506 * @from - the offset to start zeroing
4507 * @len - the length to zero, 0 to zero the entire range respective to the
4508 * offset
4509 * @front - zero up to the offset instead of from the offset on
4510 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304511 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004512 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004513 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304514int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004515 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004516{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004517 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004518 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004519 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4520 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004521 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004522 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004523 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004524 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004525 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304526 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004527 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004528 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004529 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304530 u64 block_start;
4531 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004532
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004533 if (IS_ALIGNED(offset, blocksize) &&
4534 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004535 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304536
Josef Bacik8b62f872017-10-19 14:15:55 -04004537 block_start = round_down(from, blocksize);
4538 block_end = block_start + blocksize - 1;
4539
Qu Wenruo364ecf32017-02-27 15:10:38 +08004540 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004541 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004542 if (ret)
4543 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004544
Chris Mason211c17f2008-05-15 09:13:45 -04004545again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004546 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004547 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004548 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004549 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004550 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004551 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004552 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004553 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004554
Chris Masona52d9a82007-08-27 16:49:44 -04004555 if (!PageUptodate(page)) {
4556 ret = btrfs_readpage(NULL, page);
4557 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004558 if (page->mapping != mapping) {
4559 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004560 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004561 goto again;
4562 }
Chris Masona52d9a82007-08-27 16:49:44 -04004563 if (!PageUptodate(page)) {
4564 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004565 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004566 }
4567 }
Chris Mason211c17f2008-05-15 09:13:45 -04004568 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004569
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304570 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004571 set_page_extent_mapped(page);
4572
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304573 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004574 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304575 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004576 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004577 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004578 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004579 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580 btrfs_put_ordered_extent(ordered);
4581 goto again;
4582 }
4583
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304584 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004585 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4586 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004587
Filipe Mananae3b8a482017-11-04 00:16:59 +00004588 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004589 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004590 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304591 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004592 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004593 goto out_unlock;
4594 }
4595
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304596 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004597 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304598 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004599 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004600 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304601 memset(kaddr + (block_start - page_offset(page)),
4602 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004603 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304604 memset(kaddr + (block_start - page_offset(page)) + offset,
4605 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004606 flush_dcache_page(page);
4607 kunmap(page);
4608 }
Chris Mason247e7432008-07-17 12:53:51 -04004609 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004610 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004611 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004612
Chris Mason89642222008-07-24 09:41:53 -04004613out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004614 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004615 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004616 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004617 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004618 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004619 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004620out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004621 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 return ret;
4623}
4624
Josef Bacik16e75492013-10-22 12:18:51 -04004625static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4626 u64 offset, u64 len)
4627{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004628 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004629 struct btrfs_trans_handle *trans;
4630 int ret;
4631
4632 /*
4633 * Still need to make sure the inode looks like it's been updated so
4634 * that any holes get logged if we fsync.
4635 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004636 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4637 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004638 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4639 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4640 return 0;
4641 }
4642
4643 /*
4644 * 1 - for the one we're dropping
4645 * 1 - for the one we're adding
4646 * 1 - for updating the inode.
4647 */
4648 trans = btrfs_start_transaction(root, 3);
4649 if (IS_ERR(trans))
4650 return PTR_ERR(trans);
4651
4652 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4653 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004654 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004655 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004656 return ret;
4657 }
4658
David Sterbaf85b7372017-01-20 14:54:07 +01004659 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4660 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004661 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004662 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004663 else
4664 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004665 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004666 return ret;
4667}
4668
Josef Bacik695a0d02011-03-04 15:46:53 -05004669/*
4670 * This function puts in dummy file extents for the area we're creating a hole
4671 * for. So if we are truncating this file to a larger size we need to insert
4672 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4673 * the range between oldsize and size
4674 */
Josef Bacika41ad392011-01-31 15:30:16 -05004675int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004676{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004678 struct btrfs_root *root = BTRFS_I(inode)->root;
4679 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004680 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004681 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004682 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004683 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4684 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004685 u64 last_byte;
4686 u64 cur_offset;
4687 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004688 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004689
Josef Bacika71754f2013-06-17 17:14:39 -04004690 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304691 * If our size started in the middle of a block we need to zero out the
4692 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004693 * expose stale data.
4694 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304695 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004696 if (err)
4697 return err;
4698
Yan Zheng9036c102008-10-30 14:19:41 -04004699 if (size <= hole_start)
4700 return 0;
4701
David Sterbab272ae22020-02-05 19:09:33 +01004702 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004703 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004704 cur_offset = hole_start;
4705 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004706 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004707 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004708 if (IS_ERR(em)) {
4709 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004710 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004711 break;
4712 }
Yan Zheng9036c102008-10-30 14:19:41 -04004713 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004714 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004715 hole_size = last_byte - cur_offset;
4716
Yan, Zheng80825102009-11-12 09:35:36 +00004717 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004718 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004719
Josef Bacik16e75492013-10-22 12:18:51 -04004720 err = maybe_insert_hole(root, inode, cur_offset,
4721 hole_size);
4722 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004723 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004724
4725 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4726 cur_offset, hole_size);
4727 if (err)
4728 break;
4729
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004730 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004731 cur_offset + hole_size - 1, 0);
4732 hole_em = alloc_extent_map();
4733 if (!hole_em) {
4734 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4735 &BTRFS_I(inode)->runtime_flags);
4736 goto next;
4737 }
4738 hole_em->start = cur_offset;
4739 hole_em->len = hole_size;
4740 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004741
Josef Bacik5dc562c2012-08-17 13:14:17 -04004742 hole_em->block_start = EXTENT_MAP_HOLE;
4743 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004744 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004745 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004746 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004747 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004748
4749 while (1) {
4750 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004751 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004752 write_unlock(&em_tree->lock);
4753 if (err != -EEXIST)
4754 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004755 btrfs_drop_extent_cache(BTRFS_I(inode),
4756 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004757 cur_offset +
4758 hole_size - 1, 0);
4759 }
4760 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004761 } else {
4762 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4763 cur_offset, hole_size);
4764 if (err)
4765 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004766 }
Josef Bacik16e75492013-10-22 12:18:51 -04004767next:
Yan Zheng9036c102008-10-30 14:19:41 -04004768 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004769 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004770 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004771 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004772 break;
4773 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004774 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004775 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004776 return err;
4777}
4778
Eric Sandeen3972f262013-01-12 02:57:22 +00004779static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004780{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004781 struct btrfs_root *root = BTRFS_I(inode)->root;
4782 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004783 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004784 loff_t newsize = attr->ia_size;
4785 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004786 int ret;
4787
Eric Sandeen3972f262013-01-12 02:57:22 +00004788 /*
4789 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4790 * special case where we need to update the times despite not having
4791 * these flags set. For all other operations the VFS set these flags
4792 * explicitly if it wants a timestamp update.
4793 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004794 if (newsize != oldsize) {
4795 inode_inc_iversion(inode);
4796 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4797 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004798 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004799 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004800
Josef Bacika41ad392011-01-31 15:30:16 -05004801 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004802 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004803 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004804 * This is to ensure the snapshot captures a fully consistent
4805 * state of this file - if the snapshot captures this expanding
4806 * truncation, it must capture all writes that happened before
4807 * this truncation.
4808 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004809 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004810 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004811 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004812 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004813 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004814 }
Yan, Zheng80825102009-11-12 09:35:36 +00004815
Miao Xief4a2f4c2011-12-14 20:12:01 -05004816 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004817 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004818 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004819 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004821
4822 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004823 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304824 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004825 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004826 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004827 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004828 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004829
Josef Bacika41ad392011-01-31 15:30:16 -05004830 /*
4831 * We're truncating a file that used to have good data down to
4832 * zero. Make sure it gets into the ordered flush list so that
4833 * any new writes get down to disk quickly.
4834 */
4835 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004836 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4837 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004838
Josef Bacika41ad392011-01-31 15:30:16 -05004839 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004840
David Sterba8e0fa5d2020-06-09 19:21:48 +02004841 /* Disable nonlocked read DIO to avoid the endless truncate */
4842 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004843 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004844 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004845
Filipe Manana213e8c52018-02-06 20:40:31 +00004846 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004847 if (ret && inode->i_nlink) {
4848 int err;
4849
4850 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004851 * Truncate failed, so fix up the in-memory size. We
4852 * adjusted disk_i_size down as we removed extents, so
4853 * wait for disk_i_size to be stable and then update the
4854 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004855 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004856 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004857 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004858 return err;
4859 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004860 }
Yan, Zheng80825102009-11-12 09:35:36 +00004861 }
4862
Josef Bacika41ad392011-01-31 15:30:16 -05004863 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004864}
4865
Chris Mason39279cc2007-06-12 06:35:45 -04004866static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4867{
David Howells2b0143b2015-03-17 22:25:59 +00004868 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004869 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004870 int err;
4871
Li Zefanb83cc962010-12-20 16:04:08 +08004872 if (btrfs_root_readonly(root))
4873 return -EROFS;
4874
Jan Kara31051c82016-05-26 16:55:18 +02004875 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004876 if (err)
4877 return err;
4878
Chris Mason5a3f23d2009-03-31 13:27:11 -04004879 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004880 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004881 if (err)
4882 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004883 }
Yan Zheng9036c102008-10-30 14:19:41 -04004884
Christoph Hellwig10257742010-06-04 11:30:02 +02004885 if (attr->ia_valid) {
4886 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004887 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004888 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004889
Josef Bacik22c44fe2011-11-30 10:45:38 -05004890 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004891 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004892 }
4893
Chris Mason39279cc2007-06-12 06:35:45 -04004894 return err;
4895}
Chris Mason61295eb2008-01-14 16:24:38 -05004896
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004897/*
4898 * While truncating the inode pages during eviction, we get the VFS calling
4899 * btrfs_invalidatepage() against each page of the inode. This is slow because
4900 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4901 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4902 * extent_state structures over and over, wasting lots of time.
4903 *
4904 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4905 * those expensive operations on a per page basis and do only the ordered io
4906 * finishing, while we release here the extent_map and extent_state structures,
4907 * without the excessive merging and splitting.
4908 */
4909static void evict_inode_truncate_pages(struct inode *inode)
4910{
4911 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4912 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4913 struct rb_node *node;
4914
4915 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004916 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004917
4918 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004919 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004920 struct extent_map *em;
4921
Liu Bo07e1ce02018-08-23 03:51:52 +08004922 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004923 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004924 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4925 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004926 remove_extent_mapping(map_tree, em);
4927 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004928 if (need_resched()) {
4929 write_unlock(&map_tree->lock);
4930 cond_resched();
4931 write_lock(&map_tree->lock);
4932 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004933 }
4934 write_unlock(&map_tree->lock);
4935
Filipe Manana6ca07092015-05-26 00:55:42 +01004936 /*
4937 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004938 * We can have ongoing bios started by readahead that have
4939 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004940 * still in progress (unlocked the pages in the bio but did not yet
4941 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004942 * ranges can still be locked and eviction started because before
4943 * submitting those bios, which are executed by a separate task (work
4944 * queue kthread), inode references (inode->i_count) were not taken
4945 * (which would be dropped in the end io callback of each bio).
4946 * Therefore here we effectively end up waiting for those bios and
4947 * anyone else holding locked ranges without having bumped the inode's
4948 * reference count - if we don't do it, when they access the inode's
4949 * io_tree to unlock a range it may be too late, leading to an
4950 * use-after-free issue.
4951 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004952 spin_lock(&io_tree->lock);
4953 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4954 struct extent_state *state;
4955 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004956 u64 start;
4957 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004958 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004959
4960 node = rb_first(&io_tree->state);
4961 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004962 start = state->start;
4963 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004964 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004965 spin_unlock(&io_tree->lock);
4966
David Sterbaff13db42015-12-03 14:30:40 +01004967 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004968
4969 /*
4970 * If still has DELALLOC flag, the extent didn't reach disk,
4971 * and its reserved space won't be freed by delayed_ref.
4972 * So we need to free its reserved space here.
4973 * (Refer to comment in btrfs_invalidatepage, case 2)
4974 *
4975 * Note, end is the bytenr of last byte, so we need + 1 here.
4976 */
Filipe Manana421f0922018-10-12 13:02:48 +01004977 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004978 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004979
Filipe Manana6ca07092015-05-26 00:55:42 +01004980 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004981 EXTENT_LOCKED | EXTENT_DELALLOC |
4982 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4983 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004984
Filipe Manana7064dd52014-08-08 02:47:05 +01004985 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004986 spin_lock(&io_tree->lock);
4987 }
4988 spin_unlock(&io_tree->lock);
4989}
4990
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004991static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004992 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004993{
4994 struct btrfs_fs_info *fs_info = root->fs_info;
4995 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004996 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004997 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004998 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004999
Josef Bacikd3984c92019-08-01 18:19:37 -04005000 /*
5001 * Eviction should be taking place at some place safe because of our
5002 * delayed iputs. However the normal flushing code will run delayed
5003 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5004 *
5005 * We reserve the delayed_refs_extra here again because we can't use
5006 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5007 * above. We reserve our extra bit here because we generate a ton of
5008 * delayed refs activity by truncating.
5009 *
5010 * If we cannot make our reservation we'll attempt to steal from the
5011 * global reserve, because we really want to be able to free up space.
5012 */
5013 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5014 BTRFS_RESERVE_FLUSH_EVICT);
5015 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005016 /*
5017 * Try to steal from the global reserve if there is space for
5018 * it.
5019 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005020 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5021 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5022 btrfs_warn(fs_info,
5023 "could not allocate space for delete; will truncate on mount");
5024 return ERR_PTR(-ENOSPC);
5025 }
5026 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005027 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005028
5029 trans = btrfs_join_transaction(root);
5030 if (IS_ERR(trans))
5031 return trans;
5032
5033 if (delayed_refs_extra) {
5034 trans->block_rsv = &fs_info->trans_block_rsv;
5035 trans->bytes_reserved = delayed_refs_extra;
5036 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5037 delayed_refs_extra, 1);
5038 }
5039 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005040}
5041
Al Virobd555972010-06-07 11:35:40 -04005042void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005044 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005045 struct btrfs_trans_handle *trans;
5046 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005047 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005048 int ret;
5049
liubo1abe9b82011-03-24 11:18:59 +00005050 trace_btrfs_inode_evict(inode);
5051
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005052 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005053 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005054 return;
5055 }
5056
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005057 evict_inode_truncate_pages(inode);
5058
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005059 if (inode->i_nlink &&
5060 ((btrfs_root_refs(&root->root_item) != 0 &&
5061 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005062 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005063 goto no_delete;
5064
Omar Sandoval27919062018-05-11 13:13:37 -07005065 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005066 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005067
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005068 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005069
Omar Sandoval7b40b692018-05-11 13:13:33 -07005070 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005071 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005072
Yan, Zheng76dda932009-09-21 16:00:26 -04005073 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005074 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5075 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005076 goto no_delete;
5077 }
5078
Nikolay Borisovaa790212017-01-10 20:35:40 +02005079 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005080 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005081 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005082
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005083 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005084 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005085 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005086 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005087 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005088
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005089 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005090
Yan, Zheng80825102009-11-12 09:35:36 +00005091 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005092 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005093 if (IS_ERR(trans))
5094 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005095
5096 trans->block_rsv = rsv;
5097
Yan, Zhengd68fc572010-05-16 10:49:58 -04005098 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005099 trans->block_rsv = &fs_info->trans_block_rsv;
5100 btrfs_end_transaction(trans);
5101 btrfs_btree_balance_dirty(fs_info);
5102 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5103 goto free_rsv;
5104 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005105 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005106 }
5107
Josef Bacik4ef31a42013-08-13 14:10:08 -04005108 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005109 * Errors here aren't a big deal, it just means we leave orphan items in
5110 * the tree. They will be cleaned up on the next mount. If the inode
5111 * number gets reused, cleanup deletes the orphan item without doing
5112 * anything, and unlink reuses the existing orphan item.
5113 *
5114 * If it turns out that we are dropping too many of these, we might want
5115 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005116 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005117 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005118 if (!IS_ERR(trans)) {
5119 trans->block_rsv = rsv;
5120 btrfs_orphan_del(trans, BTRFS_I(inode));
5121 trans->block_rsv = &fs_info->trans_block_rsv;
5122 btrfs_end_transaction(trans);
5123 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005124
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005125 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005126 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005127 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005128
Omar Sandoval27919062018-05-11 13:13:37 -07005129free_rsv:
5130 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005131no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005132 /*
5133 * If we didn't successfully delete, the orphan item will still be in
5134 * the tree and we'll retry on the next mount. Again, we might also want
5135 * to retry these periodically in the future.
5136 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005137 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005138 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005139}
5140
5141/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005142 * Return the key found in the dir entry in the location pointer, fill @type
5143 * with BTRFS_FT_*, and return 0.
5144 *
Su Yue005d6712018-03-05 17:13:37 +08005145 * If no dir entries were found, returns -ENOENT.
5146 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005147 */
5148static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005149 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005150{
5151 const char *name = dentry->d_name.name;
5152 int namelen = dentry->d_name.len;
5153 struct btrfs_dir_item *di;
5154 struct btrfs_path *path;
5155 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005156 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005157
5158 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005159 if (!path)
5160 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005161
David Sterbaf85b7372017-01-20 14:54:07 +01005162 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5163 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005164 if (IS_ERR_OR_NULL(di)) {
5165 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005166 goto out;
5167 }
Chris Masond3977122009-01-05 21:25:51 -05005168
Chris Mason5f39d392007-10-15 16:14:19 -04005169 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005170 if (location->type != BTRFS_INODE_ITEM_KEY &&
5171 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005172 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005173 btrfs_warn(root->fs_info,
5174"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5175 __func__, name, btrfs_ino(BTRFS_I(dir)),
5176 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005177 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005178 if (!ret)
5179 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005180out:
Chris Mason39279cc2007-06-12 06:35:45 -04005181 btrfs_free_path(path);
5182 return ret;
5183}
5184
5185/*
5186 * when we hit a tree root in a directory, the btrfs part of the inode
5187 * needs to be changed to reflect the root directory of the tree root. This
5188 * is kind of like crossing a mount point.
5189 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005190static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005191 struct inode *dir,
5192 struct dentry *dentry,
5193 struct btrfs_key *location,
5194 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005195{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005196 struct btrfs_path *path;
5197 struct btrfs_root *new_root;
5198 struct btrfs_root_ref *ref;
5199 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005200 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005201 int ret;
5202 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005203
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005204 path = btrfs_alloc_path();
5205 if (!path) {
5206 err = -ENOMEM;
5207 goto out;
5208 }
Chris Mason39279cc2007-06-12 06:35:45 -04005209
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005210 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005211 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5212 key.type = BTRFS_ROOT_REF_KEY;
5213 key.offset = location->objectid;
5214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005215 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005216 if (ret) {
5217 if (ret < 0)
5218 err = ret;
5219 goto out;
5220 }
Chris Mason39279cc2007-06-12 06:35:45 -04005221
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005222 leaf = path->nodes[0];
5223 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005224 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005225 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5226 goto out;
5227
5228 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5229 (unsigned long)(ref + 1),
5230 dentry->d_name.len);
5231 if (ret)
5232 goto out;
5233
David Sterbab3b4aa72011-04-21 01:20:15 +02005234 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005235
David Sterba56e93572020-05-15 19:35:55 +02005236 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005237 if (IS_ERR(new_root)) {
5238 err = PTR_ERR(new_root);
5239 goto out;
5240 }
5241
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005242 *sub_root = new_root;
5243 location->objectid = btrfs_root_dirid(&new_root->root_item);
5244 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005245 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005246 err = 0;
5247out:
5248 btrfs_free_path(path);
5249 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005250}
5251
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005252static void inode_tree_add(struct inode *inode)
5253{
5254 struct btrfs_root *root = BTRFS_I(inode)->root;
5255 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005256 struct rb_node **p;
5257 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005258 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005259 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005260
Al Viro1d3382cb2010-10-23 15:19:20 -04005261 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005262 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005263 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005264 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005265 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005266 while (*p) {
5267 parent = *p;
5268 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5269
David Sterba37508512018-06-29 10:56:40 +02005270 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005271 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005272 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005273 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005274 else {
5275 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005276 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005277 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005278 RB_CLEAR_NODE(parent);
5279 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005280 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 }
5282 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005283 rb_link_node(new, parent, p);
5284 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005285 spin_unlock(&root->inode_lock);
5286}
5287
5288static void inode_tree_del(struct inode *inode)
5289{
5290 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005291 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005292
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005293 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005294 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005295 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005296 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005297 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005298 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005299 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005300
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005301 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005302 spin_lock(&root->inode_lock);
5303 empty = RB_EMPTY_ROOT(&root->inode_tree);
5304 spin_unlock(&root->inode_lock);
5305 if (empty)
5306 btrfs_add_dead_root(root);
5307 }
5308}
5309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005310
Chris Masone02119d2008-09-05 16:13:11 -04005311static int btrfs_init_locked_inode(struct inode *inode, void *p)
5312{
5313 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005314
5315 inode->i_ino = args->ino;
5316 BTRFS_I(inode)->location.objectid = args->ino;
5317 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5318 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005319 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5320 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005321 return 0;
5322}
5323
5324static int btrfs_find_actor(struct inode *inode, void *opaque)
5325{
5326 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005327
5328 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005329 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005330}
5331
David Sterba0202e832020-05-15 19:35:59 +02005332static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005333 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005334{
5335 struct inode *inode;
5336 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005337 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005338
David Sterba0202e832020-05-15 19:35:59 +02005339 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005340 args.root = root;
5341
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005342 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005343 btrfs_init_locked_inode,
5344 (void *)&args);
5345 return inode;
5346}
5347
David Sterba4c66e0d2019-10-03 19:09:35 +02005348/*
David Sterba0202e832020-05-15 19:35:59 +02005349 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005350 * Path can be preallocated to prevent recursing back to iget through
5351 * allocator. NULL is also valid but may require an additional allocation
5352 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305353 */
David Sterba0202e832020-05-15 19:35:59 +02005354struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005355 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305356{
5357 struct inode *inode;
5358
David Sterba0202e832020-05-15 19:35:59 +02005359 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305360 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005361 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305362
5363 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005364 int ret;
5365
Filipe Manana4222ea72018-10-24 10:13:03 +01005366 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005367 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005368 inode_tree_add(inode);
5369 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005370 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005371 iget_failed(inode);
5372 /*
5373 * ret > 0 can come from btrfs_search_slot called by
5374 * btrfs_read_locked_inode, this means the inode item
5375 * was not found.
5376 */
5377 if (ret > 0)
5378 ret = -ENOENT;
5379 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005380 }
5381 }
5382
Balaji Rao1a54ef82008-07-21 02:01:04 +05305383 return inode;
5384}
5385
David Sterba0202e832020-05-15 19:35:59 +02005386struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005387{
David Sterba0202e832020-05-15 19:35:59 +02005388 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005389}
5390
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391static struct inode *new_simple_dir(struct super_block *s,
5392 struct btrfs_key *key,
5393 struct btrfs_root *root)
5394{
5395 struct inode *inode = new_inode(s);
5396
5397 if (!inode)
5398 return ERR_PTR(-ENOMEM);
5399
Josef Bacik5c8fd992020-02-14 16:11:43 -05005400 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005401 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005402 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005403
5404 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005405 /*
5406 * We only need lookup, the rest is read-only and there's no inode
5407 * associated with the dentry
5408 */
5409 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005410 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411 inode->i_fop = &simple_dir_operations;
5412 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005413 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305414 inode->i_atime = inode->i_mtime;
5415 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005416 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417
5418 return inode;
5419}
5420
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005421static inline u8 btrfs_inode_type(struct inode *inode)
5422{
5423 /*
5424 * Compile-time asserts that generic FT_* types still match
5425 * BTRFS_FT_* types
5426 */
5427 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5428 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5429 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5430 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5431 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5432 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5433 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5434 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5435
5436 return fs_umode_to_ftype(inode->i_mode);
5437}
5438
Chris Mason3de45862008-11-17 21:02:50 -05005439struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005441 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005442 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005443 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005444 struct btrfs_root *sub_root = root;
5445 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005446 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005447 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005448
5449 if (dentry->d_name.len > BTRFS_NAME_LEN)
5450 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005451
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005452 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005453 if (ret < 0)
5454 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005455
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005456 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005457 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005458 if (IS_ERR(inode))
5459 return inode;
5460
5461 /* Do extra check against inode mode with di_type */
5462 if (btrfs_inode_type(inode) != di_type) {
5463 btrfs_crit(fs_info,
5464"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5465 inode->i_mode, btrfs_inode_type(inode),
5466 di_type);
5467 iput(inode);
5468 return ERR_PTR(-EUCLEAN);
5469 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005470 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005471 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005473 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005474 &location, &sub_root);
5475 if (ret < 0) {
5476 if (ret != -ENOENT)
5477 inode = ERR_PTR(ret);
5478 else
5479 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5480 } else {
David Sterba0202e832020-05-15 19:35:59 +02005481 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005482 }
Josef Bacik87270022020-01-24 09:32:31 -05005483 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005484 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005485
Julia Lawall34d19ba2011-01-24 19:55:19 +00005486 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005487 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005488 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005489 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005490 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005491 if (ret) {
5492 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005493 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005494 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005495 }
5496
Chris Mason3de45862008-11-17 21:02:50 -05005497 return inode;
5498}
5499
Nick Pigginfe15ce42011-01-07 17:49:23 +11005500static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005501{
5502 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005503 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005504
Li Zefan848cce02012-02-21 17:04:28 +08005505 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005506 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005507
Li Zefan848cce02012-02-21 17:04:28 +08005508 if (inode) {
5509 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005510 if (btrfs_root_refs(&root->root_item) == 0)
5511 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005512
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005513 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005514 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005515 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005516 return 0;
5517}
5518
Chris Mason3de45862008-11-17 21:02:50 -05005519static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005520 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005521{
Al Viro3837d202018-10-10 16:38:27 -04005522 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005523
Al Viro3837d202018-10-10 16:38:27 -04005524 if (inode == ERR_PTR(-ENOENT))
5525 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005526 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005527}
5528
Josef Bacik23b5ec72017-07-24 15:14:25 -04005529/*
5530 * All this infrastructure exists because dir_emit can fault, and we are holding
5531 * the tree lock when doing readdir. For now just allocate a buffer and copy
5532 * our information into that, and then dir_emit from the buffer. This is
5533 * similar to what NFS does, only we don't keep the buffer around in pagecache
5534 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5535 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5536 * tree lock.
5537 */
5538static int btrfs_opendir(struct inode *inode, struct file *file)
5539{
5540 struct btrfs_file_private *private;
5541
5542 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5543 if (!private)
5544 return -ENOMEM;
5545 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5546 if (!private->filldir_buf) {
5547 kfree(private);
5548 return -ENOMEM;
5549 }
5550 file->private_data = private;
5551 return 0;
5552}
5553
5554struct dir_entry {
5555 u64 ino;
5556 u64 offset;
5557 unsigned type;
5558 int name_len;
5559};
5560
5561static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5562{
5563 while (entries--) {
5564 struct dir_entry *entry = addr;
5565 char *name = (char *)(entry + 1);
5566
David Sterba92d32172018-04-16 21:10:14 +02005567 ctx->pos = get_unaligned(&entry->offset);
5568 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5569 get_unaligned(&entry->ino),
5570 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005571 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005572 addr += sizeof(struct dir_entry) +
5573 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005574 ctx->pos++;
5575 }
5576 return 0;
5577}
5578
Al Viro9cdda8d2013-05-22 16:48:09 -04005579static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005580{
Al Viro9cdda8d2013-05-22 16:48:09 -04005581 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005582 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005583 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005584 struct btrfs_dir_item *di;
5585 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005586 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005587 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005588 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005589 struct list_head ins_list;
5590 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005591 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005592 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005593 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005594 char *name_ptr;
5595 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005596 int entries = 0;
5597 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005598 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005599 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005600
Al Viro9cdda8d2013-05-22 16:48:09 -04005601 if (!dir_emit_dots(file, ctx))
5602 return 0;
5603
David Woodhouse49593bf2008-08-17 17:08:36 +01005604 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005605 if (!path)
5606 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005607
Josef Bacik23b5ec72017-07-24 15:14:25 -04005608 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005609 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005610
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005611 INIT_LIST_HEAD(&ins_list);
5612 INIT_LIST_HEAD(&del_list);
5613 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005614
Josef Bacik23b5ec72017-07-24 15:14:25 -04005615again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005616 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005617 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005618 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005619
Chris Mason39279cc2007-06-12 06:35:45 -04005620 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5621 if (ret < 0)
5622 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005623
5624 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005625 struct dir_entry *entry;
5626
Chris Mason5f39d392007-10-15 16:14:19 -04005627 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005628 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005629 if (slot >= btrfs_header_nritems(leaf)) {
5630 ret = btrfs_next_leaf(root, path);
5631 if (ret < 0)
5632 goto err;
5633 else if (ret > 0)
5634 break;
5635 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005636 }
Chris Mason3de45862008-11-17 21:02:50 -05005637
Chris Mason5f39d392007-10-15 16:14:19 -04005638 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5639
5640 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005641 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005642 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005643 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005644 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005645 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005646 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005647 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005648 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005649 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005650 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5651 PAGE_SIZE) {
5652 btrfs_release_path(path);
5653 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5654 if (ret)
5655 goto nopos;
5656 addr = private->filldir_buf;
5657 entries = 0;
5658 total_len = 0;
5659 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005660 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005661
5662 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005663 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005664 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005665 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5666 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005667 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005668 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005669 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005670 put_unaligned(location.objectid, &entry->ino);
5671 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005672 entries++;
5673 addr += sizeof(struct dir_entry) + name_len;
5674 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005675next:
5676 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005677 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005678 btrfs_release_path(path);
5679
5680 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5681 if (ret)
5682 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005683
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005684 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005685 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005686 goto nopos;
5687
Zach Browndb62efb2013-07-11 16:19:42 -07005688 /*
5689 * Stop new entries from being returned after we return the last
5690 * entry.
5691 *
5692 * New directory entries are assigned a strictly increasing
5693 * offset. This means that new entries created during readdir
5694 * are *guaranteed* to be seen in the future by that readdir.
5695 * This has broken buggy programs which operate on names as
5696 * they're returned by readdir. Until we re-use freed offsets
5697 * we have this hack to stop new entries from being returned
5698 * under the assumption that they'll never reach this huge
5699 * offset.
5700 *
5701 * This is being careful not to overflow 32bit loff_t unless the
5702 * last entry requires it because doing so has broken 32bit apps
5703 * in the past.
5704 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005705 if (ctx->pos >= INT_MAX)
5706 ctx->pos = LLONG_MAX;
5707 else
5708 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005709nopos:
5710 ret = 0;
5711err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005712 if (put)
5713 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005714 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005715 return ret;
5716}
5717
Chris Mason39279cc2007-06-12 06:35:45 -04005718/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005719 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005720 * inode changes. But, it is most likely to find the inode in cache.
5721 * FIXME, needs more benchmarking...there are no reasons other than performance
5722 * to keep or drop this code.
5723 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005724static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005725{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005726 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005727 struct btrfs_root *root = BTRFS_I(inode)->root;
5728 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005729 int ret;
5730
Josef Bacik72ac3c02012-05-23 14:13:11 -04005731 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005732 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005733
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005734 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005735 if (IS_ERR(trans))
5736 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005737
5738 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005739 if (ret && ret == -ENOSPC) {
5740 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005741 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005742 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005743 if (IS_ERR(trans))
5744 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005745
Chris Mason94b60442010-05-26 11:02:00 -04005746 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005747 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005748 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005749 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005750 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005751
5752 return ret;
5753}
5754
5755/*
5756 * This is a copy of file_update_time. We need this so we can return error on
5757 * ENOSPC for updating the inode in the case of file write and mmap writes.
5758 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005759static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005760 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005761{
Alexander Block2bc5565282012-06-15 09:49:33 +02005762 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005763 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005764
5765 if (btrfs_root_readonly(root))
5766 return -EROFS;
5767
Josef Bacike41f9412012-03-26 09:46:47 -04005768 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005769 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005770 if (flags & S_CTIME)
5771 inode->i_ctime = *now;
5772 if (flags & S_MTIME)
5773 inode->i_mtime = *now;
5774 if (flags & S_ATIME)
5775 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005776 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005777}
5778
Chris Masond352ac62008-09-29 15:18:18 -04005779/*
5780 * find the highest existing sequence number in a directory
5781 * and then set the in-memory index_cnt variable to reflect
5782 * free sequence numbers
5783 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005784static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005785{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005786 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005787 struct btrfs_key key, found_key;
5788 struct btrfs_path *path;
5789 struct extent_buffer *leaf;
5790 int ret;
5791
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005792 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005793 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005794 key.offset = (u64)-1;
5795
5796 path = btrfs_alloc_path();
5797 if (!path)
5798 return -ENOMEM;
5799
5800 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5801 if (ret < 0)
5802 goto out;
5803 /* FIXME: we should be able to handle this */
5804 if (ret == 0)
5805 goto out;
5806 ret = 0;
5807
5808 /*
5809 * MAGIC NUMBER EXPLANATION:
5810 * since we search a directory based on f_pos we have to start at 2
5811 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5812 * else has to start at 2
5813 */
5814 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005815 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005816 goto out;
5817 }
5818
5819 path->slots[0]--;
5820
5821 leaf = path->nodes[0];
5822 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5823
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005824 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005825 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005826 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005827 goto out;
5828 }
5829
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005830 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005831out:
5832 btrfs_free_path(path);
5833 return ret;
5834}
5835
Chris Masond352ac62008-09-29 15:18:18 -04005836/*
5837 * helper to find a free sequence number in a given directory. This current
5838 * code is very simple, later versions will do smarter things in the btree
5839 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005840int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005841{
5842 int ret = 0;
5843
Nikolay Borisov877574e2017-02-20 13:50:33 +02005844 if (dir->index_cnt == (u64)-1) {
5845 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005846 if (ret) {
5847 ret = btrfs_set_inode_index_count(dir);
5848 if (ret)
5849 return ret;
5850 }
Josef Bacikaec74772008-07-24 12:12:38 -04005851 }
5852
Nikolay Borisov877574e2017-02-20 13:50:33 +02005853 *index = dir->index_cnt;
5854 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005855
5856 return ret;
5857}
5858
Chris Masonb0d5d102014-09-08 13:08:51 -07005859static int btrfs_insert_inode_locked(struct inode *inode)
5860{
5861 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005862
5863 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005864 args.root = BTRFS_I(inode)->root;
5865
5866 return insert_inode_locked4(inode,
5867 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5868 btrfs_find_actor, &args);
5869}
5870
Anand Jain19aee8d2017-07-18 17:37:05 +08005871/*
5872 * Inherit flags from the parent inode.
5873 *
5874 * Currently only the compression flags and the cow flags are inherited.
5875 */
5876static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5877{
5878 unsigned int flags;
5879
5880 if (!dir)
5881 return;
5882
5883 flags = BTRFS_I(dir)->flags;
5884
5885 if (flags & BTRFS_INODE_NOCOMPRESS) {
5886 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5887 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5888 } else if (flags & BTRFS_INODE_COMPRESS) {
5889 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5890 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5891 }
5892
5893 if (flags & BTRFS_INODE_NODATACOW) {
5894 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5895 if (S_ISREG(inode->i_mode))
5896 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5897 }
5898
David Sterba7b6a2212018-03-26 18:40:21 +02005899 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005900}
5901
Chris Mason39279cc2007-06-12 06:35:45 -04005902static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5903 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005904 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005905 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005906 u64 ref_objectid, u64 objectid,
5907 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005908{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005909 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005910 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005911 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005912 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005913 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005914 struct btrfs_inode_ref *ref;
5915 struct btrfs_key key[2];
5916 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005917 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005918 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005919 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005920 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005921
Chris Mason5f39d392007-10-15 16:14:19 -04005922 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005923 if (!path)
5924 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005925
Josef Bacik11a19a92019-09-09 10:12:04 -04005926 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005927 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005928 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005929 if (!inode) {
5930 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005931 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005932 }
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Li Zefan581bb052011-04-20 10:06:11 +08005934 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005935 * O_TMPFILE, set link count to 0, so that after this point,
5936 * we fill in an inode item with the correct link count.
5937 */
5938 if (!name)
5939 set_nlink(inode, 0);
5940
5941 /*
Li Zefan581bb052011-04-20 10:06:11 +08005942 * we have to initialize this early, so we can reclaim the inode
5943 * number if we fail afterwards in this function.
5944 */
5945 inode->i_ino = objectid;
5946
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005947 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005948 trace_btrfs_inode_request(dir);
5949
Nikolay Borisov877574e2017-02-20 13:50:33 +02005950 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005951 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005952 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005953 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005954 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005955 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005956 } else if (dir) {
5957 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005958 }
5959 /*
5960 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005961 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005962 * number
5963 */
5964 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005965 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005966 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005967 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005968 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005969
Josef Bacik5dc562c2012-08-17 13:14:17 -04005970 /*
5971 * We could have gotten an inode number from somebody who was fsynced
5972 * and then removed in this same transaction, so let's just set full
5973 * sync since it will be a full sync anyway and this will blow away the
5974 * old info in the log.
5975 */
5976 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5977
Chris Mason9c583092008-01-29 15:15:18 -05005978 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005979 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005980 key[0].offset = 0;
5981
Chris Mason9c583092008-01-29 15:15:18 -05005982 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005983
5984 if (name) {
5985 /*
5986 * Start new inodes with an inode_ref. This is slightly more
5987 * efficient for small numbers of hard links since they will
5988 * be packed into one item. Extended refs will kick in if we
5989 * add more hard links than can fit in the ref item.
5990 */
5991 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005992 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005993 key[1].offset = ref_objectid;
5994
5995 sizes[1] = name_len + sizeof(*ref);
5996 }
Chris Mason9c583092008-01-29 15:15:18 -05005997
Chris Masonb0d5d102014-09-08 13:08:51 -07005998 location = &BTRFS_I(inode)->location;
5999 location->objectid = objectid;
6000 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006001 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006002
6003 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006004 if (ret < 0) {
6005 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006006 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006007 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006008
Chris Masonb9473432009-03-13 11:00:37 -04006009 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006010 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006011 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006012 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006013
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006014 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006015 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306016
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006017 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306018 inode->i_atime = inode->i_mtime;
6019 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006020 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306021
Chris Mason5f39d392007-10-15 16:14:19 -04006022 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6023 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006024 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006025 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006026 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006027
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006028 if (name) {
6029 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6030 struct btrfs_inode_ref);
6031 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6032 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6033 ptr = (unsigned long)(ref + 1);
6034 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6035 }
Chris Mason9c583092008-01-29 15:15:18 -05006036
Chris Mason5f39d392007-10-15 16:14:19 -04006037 btrfs_mark_buffer_dirty(path->nodes[0]);
6038 btrfs_free_path(path);
6039
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006040 btrfs_inherit_iflags(inode, dir);
6041
Al Viro569254b2011-07-24 17:08:40 -04006042 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006043 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006044 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006045 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006046 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6047 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006048 }
6049
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006050 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006051
6052 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006053 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006054
Alexander Block8ea05e32012-07-25 17:35:53 +02006055 btrfs_update_root_times(trans, root);
6056
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006057 ret = btrfs_inode_inherit_props(trans, inode, dir);
6058 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006059 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006060 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006061 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006062
Chris Mason39279cc2007-06-12 06:35:45 -04006063 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006064
6065fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006066 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006067fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006068 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006069 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006070 btrfs_free_path(path);
6071 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006072}
6073
Chris Masond352ac62008-09-29 15:18:18 -04006074/*
6075 * utility function to add 'inode' into 'parent_inode' with
6076 * a give name and a given sequence number.
6077 * if 'add_backref' is true, also insert a backref from the
6078 * inode to the parent directory.
6079 */
Chris Masone02119d2008-09-05 16:13:11 -04006080int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006081 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006082 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006083{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006084 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006085 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006086 struct btrfs_root *root = parent_inode->root;
6087 u64 ino = btrfs_ino(inode);
6088 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006089
Li Zefan33345d012011-04-20 10:31:50 +08006090 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006091 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006092 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006093 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006094 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006095 key.offset = 0;
6096 }
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Li Zefan33345d012011-04-20 10:31:50 +08006098 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006099 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006100 root->root_key.objectid, parent_ino,
6101 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006102 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006103 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6104 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006105 }
6106
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006107 /* Nothing to clean up yet */
6108 if (ret)
6109 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006110
Lu Fengqi684572d2018-08-04 21:10:57 +08006111 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006112 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006113 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006114 goto fail_dir_item;
6115 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006116 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006117 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006118 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006119
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006120 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006121 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006122 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006123 /*
6124 * If we are replaying a log tree, we do not want to update the mtime
6125 * and ctime of the parent directory with the current time, since the
6126 * log replay procedure is responsible for setting them to their correct
6127 * values (the ones it had when the fsync was done).
6128 */
6129 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6130 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6131
6132 parent_inode->vfs_inode.i_mtime = now;
6133 parent_inode->vfs_inode.i_ctime = now;
6134 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006135 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006136 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006137 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006138 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006139
6140fail_dir_item:
6141 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6142 u64 local_index;
6143 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006144 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006145 root->root_key.objectid, parent_ino,
6146 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006147 if (err)
6148 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006149 } else if (add_backref) {
6150 u64 local_index;
6151 int err;
6152
6153 err = btrfs_del_inode_ref(trans, root, name, name_len,
6154 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006155 if (err)
6156 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006157 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006158
6159 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006160 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006161}
6162
6163static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006164 struct btrfs_inode *dir, struct dentry *dentry,
6165 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006166{
Josef Bacika1b075d2010-11-19 20:36:11 +00006167 int err = btrfs_add_link(trans, dir, inode,
6168 dentry->d_name.name, dentry->d_name.len,
6169 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006170 if (err > 0)
6171 err = -EEXIST;
6172 return err;
6173}
6174
Josef Bacik618e21d2007-07-11 10:18:17 -04006175static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006176 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006177{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006178 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006179 struct btrfs_trans_handle *trans;
6180 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006181 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006182 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006183 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006184 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006185
Josef Bacik9ed74f22009-09-11 16:12:44 -04006186 /*
6187 * 2 for inode item and ref
6188 * 2 for dir items
6189 * 1 for xattr if selinux is on
6190 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006191 trans = btrfs_start_transaction(root, 5);
6192 if (IS_ERR(trans))
6193 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006194
Li Zefan581bb052011-04-20 10:06:11 +08006195 err = btrfs_find_free_ino(root, &objectid);
6196 if (err)
6197 goto out_unlock;
6198
Josef Bacikaec74772008-07-24 12:12:38 -04006199 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006200 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6201 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006202 if (IS_ERR(inode)) {
6203 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006204 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006205 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006206 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006207
Casey Schauflerad19db72011-12-15 10:09:07 -05006208 /*
6209 * If the active LSM wants to access the inode during
6210 * d_instantiate it needs these. Smack checks to see
6211 * if the filesystem supports xattrs by looking at the
6212 * ops vector.
6213 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006214 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006215 init_special_inode(inode, inode->i_mode, rdev);
6216
6217 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006218 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006219 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006220
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006221 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6222 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006223 if (err)
6224 goto out_unlock;
6225
6226 btrfs_update_inode(trans, root, inode);
6227 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006228
Josef Bacik618e21d2007-07-11 10:18:17 -04006229out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006230 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006231 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006232 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006233 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006234 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006235 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006236 return err;
6237}
6238
Chris Mason39279cc2007-06-12 06:35:45 -04006239static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006240 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006241{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006242 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006243 struct btrfs_trans_handle *trans;
6244 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006245 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006246 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006247 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006248 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006249
Josef Bacik9ed74f22009-09-11 16:12:44 -04006250 /*
6251 * 2 for inode item and ref
6252 * 2 for dir items
6253 * 1 for xattr if selinux is on
6254 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006255 trans = btrfs_start_transaction(root, 5);
6256 if (IS_ERR(trans))
6257 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006258
Li Zefan581bb052011-04-20 10:06:11 +08006259 err = btrfs_find_free_ino(root, &objectid);
6260 if (err)
6261 goto out_unlock;
6262
Josef Bacikaec74772008-07-24 12:12:38 -04006263 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006264 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6265 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006266 if (IS_ERR(inode)) {
6267 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006268 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006269 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006270 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006271 /*
6272 * If the active LSM wants to access the inode during
6273 * d_instantiate it needs these. Smack checks to see
6274 * if the filesystem supports xattrs by looking at the
6275 * ops vector.
6276 */
6277 inode->i_fop = &btrfs_file_operations;
6278 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006279 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006280
6281 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6282 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006283 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006284
6285 err = btrfs_update_inode(trans, root, inode);
6286 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006287 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006288
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006289 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6290 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006291 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006292 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006293
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006294 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006295 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006296
Chris Mason39279cc2007-06-12 06:35:45 -04006297out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006298 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006299 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006300 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006301 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006302 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006303 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006304 return err;
6305}
6306
6307static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6308 struct dentry *dentry)
6309{
Filipe Manana271dba452016-01-05 16:24:05 +00006310 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006311 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006312 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006313 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006314 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006315 int err;
6316 int drop_inode = 0;
6317
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006318 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006319 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006320 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006321
Mark Fashehf1863732012-08-08 11:32:27 -07006322 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006323 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006324
Nikolay Borisov877574e2017-02-20 13:50:33 +02006325 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006326 if (err)
6327 goto fail;
6328
Yan, Zhenga22285a2010-05-16 10:48:46 -04006329 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006330 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006331 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006332 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006333 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006334 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006335 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006336 if (IS_ERR(trans)) {
6337 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006338 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006339 goto fail;
6340 }
Chris Mason5f39d392007-10-15 16:14:19 -04006341
Miao Xie67de1172013-12-26 13:07:06 +08006342 /* There are several dir indexes for this inode, clear the cache. */
6343 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006344 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006345 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006346 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006347 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006348 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006349
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006350 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6351 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006352
Yan, Zhenga5719522009-09-24 09:17:31 -04006353 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006354 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006355 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006356 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006357 int ret;
6358
Yan, Zhenga5719522009-09-24 09:17:31 -04006359 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006360 if (err)
6361 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006362 if (inode->i_nlink == 1) {
6363 /*
6364 * If new hard link count is 1, it's a file created
6365 * with open(2) O_TMPFILE flag.
6366 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006367 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006368 if (err)
6369 goto fail;
6370 }
Al Viro08c422c2011-12-23 07:58:13 -05006371 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006372 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6373 true, NULL);
6374 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6375 err = btrfs_commit_transaction(trans);
6376 trans = NULL;
6377 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006378 }
Chris Mason39279cc2007-06-12 06:35:45 -04006379
Chris Mason1832a6d2007-12-21 16:27:21 -05006380fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006381 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006382 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006383 if (drop_inode) {
6384 inode_dec_link_count(inode);
6385 iput(inode);
6386 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006387 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006388 return err;
6389}
6390
Al Viro18bb1db2011-07-26 01:41:39 -04006391static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006392{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006393 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006394 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006395 struct btrfs_trans_handle *trans;
6396 struct btrfs_root *root = BTRFS_I(dir)->root;
6397 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006398 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006399 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006400
Josef Bacik9ed74f22009-09-11 16:12:44 -04006401 /*
6402 * 2 items for inode and ref
6403 * 2 items for dir items
6404 * 1 for xattr if selinux is on
6405 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006406 trans = btrfs_start_transaction(root, 5);
6407 if (IS_ERR(trans))
6408 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006409
Li Zefan581bb052011-04-20 10:06:11 +08006410 err = btrfs_find_free_ino(root, &objectid);
6411 if (err)
6412 goto out_fail;
6413
Josef Bacikaec74772008-07-24 12:12:38 -04006414 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006415 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6416 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006417 if (IS_ERR(inode)) {
6418 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006419 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006420 goto out_fail;
6421 }
Chris Mason5f39d392007-10-15 16:14:19 -04006422
Chris Masonb0d5d102014-09-08 13:08:51 -07006423 /* these must be set before we unlock the inode */
6424 inode->i_op = &btrfs_dir_inode_operations;
6425 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006426
Eric Paris2a7dba32011-02-01 11:05:39 -05006427 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006428 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006429 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006430
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006431 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006432 err = btrfs_update_inode(trans, root, inode);
6433 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006434 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006435
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006436 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6437 dentry->d_name.name,
6438 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006439 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006440 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006441
Al Viro1e2e5472018-05-04 08:23:01 -04006442 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006443
6444out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006445 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006446 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006447 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006448 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006449 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006450 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 return err;
6452}
6453
Chris Masonc8b97812008-10-29 14:49:59 -04006454static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006455 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006456 size_t pg_offset, u64 extent_offset,
6457 struct btrfs_file_extent_item *item)
6458{
6459 int ret;
6460 struct extent_buffer *leaf = path->nodes[0];
6461 char *tmp;
6462 size_t max_size;
6463 unsigned long inline_size;
6464 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006465 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006466
6467 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006468 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006469 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6470 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006471 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006472 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006473 if (!tmp)
6474 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006475 ptr = btrfs_file_extent_inline_start(item);
6476
6477 read_extent_buffer(leaf, tmp, ptr, inline_size);
6478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006479 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006480 ret = btrfs_decompress(compress_type, tmp, page,
6481 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006482
6483 /*
6484 * decompression code contains a memset to fill in any space between the end
6485 * of the uncompressed data and the end of max_size in case the decompressed
6486 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6487 * the end of an inline extent and the beginning of the next block, so we
6488 * cover that region here.
6489 */
6490
6491 if (max_size + pg_offset < PAGE_SIZE) {
6492 char *map = kmap(page);
6493 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6494 kunmap(page);
6495 }
Chris Masonc8b97812008-10-29 14:49:59 -04006496 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006497 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006498}
6499
Omar Sandoval39b07b52019-12-02 17:34:23 -08006500/**
6501 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6502 * @inode: file to search in
6503 * @page: page to read extent data into if the extent is inline
6504 * @pg_offset: offset into @page to copy to
6505 * @start: file offset
6506 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006507 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006508 * This returns the first &struct extent_map which overlaps with the given
6509 * range, reading it from the B-tree and caching it if necessary. Note that
6510 * there may be more extents which overlap the given range after the returned
6511 * extent_map.
6512 *
6513 * If @page is not NULL and the extent is inline, this also reads the extent
6514 * data directly into the page and marks the extent up to date in the io_tree.
6515 *
6516 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006517 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006518struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006519 struct page *page, size_t pg_offset,
6520 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006521{
David Sterba3ffbd682018-06-29 10:56:42 +02006522 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006523 int ret;
6524 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006525 u64 extent_start = 0;
6526 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006527 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006528 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006529 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006530 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006531 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006532 struct extent_buffer *leaf;
6533 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006534 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006535 struct extent_map_tree *em_tree = &inode->extent_tree;
6536 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006537
Chris Mason890871b2009-09-02 16:24:52 -04006538 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006539 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006540 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006541
Chris Masona52d9a82007-08-27 16:49:44 -04006542 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006543 if (em->start > start || em->start + em->len <= start)
6544 free_extent_map(em);
6545 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006546 free_extent_map(em);
6547 else
6548 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006549 }
David Sterba172ddd62011-04-21 00:48:27 +02006550 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006551 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006552 err = -ENOMEM;
6553 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006554 }
Chris Masond1310b22008-01-24 16:13:08 -05006555 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006556 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006557 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006558 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006559
Liu Bobee6ec82018-08-17 05:05:28 +08006560 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006561 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006562 err = -ENOMEM;
6563 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006564 }
6565
Liu Bobee6ec82018-08-17 05:05:28 +08006566 /* Chances are we'll be called again, so go ahead and do readahead */
6567 path->reada = READA_FORWARD;
6568
Liu Boe49aabd2018-08-25 13:47:09 +08006569 /*
6570 * Unless we're going to uncompress the inline extent, no sleep would
6571 * happen.
6572 */
6573 path->leave_spinning = 1;
6574
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006575 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006576 if (ret < 0) {
6577 err = ret;
6578 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006579 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006580 if (path->slots[0] == 0)
6581 goto not_found;
6582 path->slots[0]--;
6583 }
6584
Chris Mason5f39d392007-10-15 16:14:19 -04006585 leaf = path->nodes[0];
6586 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006587 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006588 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006589 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006590 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006591 /*
6592 * If we backup past the first extent we want to move forward
6593 * and see if there is an extent in front of us, otherwise we'll
6594 * say there is a hole for our whole search range which can
6595 * cause problems.
6596 */
6597 extent_end = start;
6598 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006599 }
6600
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006601 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006602 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006603 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006604 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6605 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006606 /* Only regular file could have regular/prealloc extent */
6607 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6608 ret = -EUCLEAN;
6609 btrfs_crit(fs_info,
6610 "regular/prealloc extent found for non-regular inode %llu",
6611 btrfs_ino(inode));
6612 goto out;
6613 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006614 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6615 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006616 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006617 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6618 path->slots[0],
6619 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006620 }
Josef Bacik25a50342013-10-14 12:08:38 -04006621next:
Yan Zheng9036c102008-10-30 14:19:41 -04006622 if (start >= extent_end) {
6623 path->slots[0]++;
6624 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6625 ret = btrfs_next_leaf(root, path);
6626 if (ret < 0) {
6627 err = ret;
6628 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006629 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006630 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006631 }
Yan Zheng9036c102008-10-30 14:19:41 -04006632 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006633 }
Yan Zheng9036c102008-10-30 14:19:41 -04006634 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6635 if (found_key.objectid != objectid ||
6636 found_key.type != BTRFS_EXTENT_DATA_KEY)
6637 goto not_found;
6638 if (start + len <= found_key.offset)
6639 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006640 if (start > found_key.offset)
6641 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006642
6643 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006644 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006645 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006646 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006647 em->block_start = EXTENT_MAP_HOLE;
6648 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006649 }
6650
Omar Sandoval39b07b52019-12-02 17:34:23 -08006651 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006652
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006653 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6654 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006655 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006656 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006657 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006658 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006659 size_t size;
6660 size_t extent_offset;
6661 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006662
Omar Sandoval39b07b52019-12-02 17:34:23 -08006663 if (!page)
Yan689f9342007-10-29 11:41:07 -04006664 goto out;
Yan689f9342007-10-29 11:41:07 -04006665
Qu Wenruoe41ca582018-06-06 15:41:49 +08006666 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006667 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006668 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6669 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006670 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006671 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006672 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006673 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006674 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006675
6676 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006677 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006678 if (btrfs_file_extent_compression(leaf, item) !=
6679 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006680 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006681 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006682 if (ret) {
6683 err = ret;
6684 goto out;
6685 }
Chris Masonc8b97812008-10-29 14:49:59 -04006686 } else {
6687 map = kmap(page);
6688 read_extent_buffer(leaf, map + pg_offset, ptr,
6689 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006690 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006691 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006692 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006693 copy_size);
6694 }
Chris Masonc8b97812008-10-29 14:49:59 -04006695 kunmap(page);
6696 }
Chris Mason179e29e2007-11-01 11:28:41 -04006697 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006698 }
Chris Masond1310b22008-01-24 16:13:08 -05006699 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006700 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006701 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006702 }
6703not_found:
6704 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006705 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006706 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006707 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006708insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006709 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006710 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006711 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006712 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6713 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006714 err = -EIO;
6715 goto out;
6716 }
Chris Masond1310b22008-01-24 16:13:08 -05006717
6718 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006719 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006720 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006721 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006722out:
Liu Boc6414282018-08-23 07:36:17 +08006723 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006724
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006725 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006726
Chris Masona52d9a82007-08-27 16:49:44 -04006727 if (err) {
6728 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006729 return ERR_PTR(err);
6730 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006731 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006732 return em;
6733}
6734
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006735struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006736 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006737{
6738 struct extent_map *em;
6739 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006740 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006741 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006742 u64 delalloc_len;
6743 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006744 int err = 0;
6745
Omar Sandoval39b07b52019-12-02 17:34:23 -08006746 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006747 if (IS_ERR(em))
6748 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006749 /*
6750 * If our em maps to:
6751 * - a hole or
6752 * - a pre-alloc extent,
6753 * there might actually be delalloc bytes behind it.
6754 */
6755 if (em->block_start != EXTENT_MAP_HOLE &&
6756 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6757 return em;
6758 else
6759 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006760
6761 /* check to see if we've wrapped (len == -1 or similar) */
6762 end = start + len;
6763 if (end < start)
6764 end = (u64)-1;
6765 else
6766 end -= 1;
6767
6768 em = NULL;
6769
6770 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006771 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006772 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006773 delalloc_end = delalloc_start + delalloc_len;
6774 if (delalloc_end < delalloc_start)
6775 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006776
6777 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006778 * We didn't find anything useful, return the original results from
6779 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006780 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006781 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006782 em = hole_em;
6783 hole_em = NULL;
6784 goto out;
6785 }
6786
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006787 /*
6788 * Adjust the delalloc_start to make sure it doesn't go backwards from
6789 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006790 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006791 delalloc_start = max(start, delalloc_start);
6792 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006793
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006794 if (delalloc_len > 0) {
6795 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006796 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006797 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006798
David Sterba172ddd62011-04-21 00:48:27 +02006799 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006800 if (!em) {
6801 err = -ENOMEM;
6802 goto out;
6803 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006804
6805 ASSERT(hole_em);
6806 /*
6807 * When btrfs_get_extent can't find anything it returns one
6808 * huge hole
6809 *
6810 * Make sure what it found really fits our range, and adjust to
6811 * make sure it is based on the start from the caller
6812 */
6813 if (hole_end <= start || hole_em->start > end) {
6814 free_extent_map(hole_em);
6815 hole_em = NULL;
6816 } else {
6817 hole_start = max(hole_em->start, start);
6818 hole_len = hole_end - hole_start;
6819 }
6820
6821 if (hole_em && delalloc_start > hole_start) {
6822 /*
6823 * Our hole starts before our delalloc, so we have to
6824 * return just the parts of the hole that go until the
6825 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006826 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006827 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006828 em->start = hole_start;
6829 em->orig_start = hole_start;
6830 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006831 * Don't adjust block start at all, it is fixed at
6832 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006833 */
6834 em->block_start = hole_em->block_start;
6835 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006836 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6837 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006838 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006839 /*
6840 * Hole is out of passed range or it starts after
6841 * delalloc range
6842 */
6843 em->start = delalloc_start;
6844 em->len = delalloc_len;
6845 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006846 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006847 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006848 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006849 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006850 return hole_em;
6851 }
6852out:
6853
6854 free_extent_map(hole_em);
6855 if (err) {
6856 free_extent_map(em);
6857 return ERR_PTR(err);
6858 }
6859 return em;
6860}
6861
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006862static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6863 const u64 start,
6864 const u64 len,
6865 const u64 orig_start,
6866 const u64 block_start,
6867 const u64 block_len,
6868 const u64 orig_block_len,
6869 const u64 ram_bytes,
6870 const int type)
6871{
6872 struct extent_map *em = NULL;
6873 int ret;
6874
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006875 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006876 em = create_io_em(inode, start, len, orig_start,
6877 block_start, block_len, orig_block_len,
6878 ram_bytes,
6879 BTRFS_COMPRESS_NONE, /* compress_type */
6880 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006881 if (IS_ERR(em))
6882 goto out;
6883 }
6884 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6885 len, block_len, type);
6886 if (ret) {
6887 if (em) {
6888 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006889 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006890 start + len - 1, 0);
6891 }
6892 em = ERR_PTR(ret);
6893 }
6894 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006895
6896 return em;
6897}
6898
Josef Bacik4b46fce2010-05-23 11:00:55 -04006899static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6900 u64 start, u64 len)
6901{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006903 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006904 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 struct btrfs_key ins;
6906 u64 alloc_hint;
6907 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006908
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006910 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006911 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006912 if (ret)
6913 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006915 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6916 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006917 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006918 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006919 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006920 btrfs_free_reserved_extent(fs_info, ins.objectid,
6921 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006922
Josef Bacik4b46fce2010-05-23 11:00:55 -04006923 return em;
6924}
6925
Chris Mason46bfbb52010-05-26 11:04:10 -04006926/*
6927 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6928 * block must be cow'd
6929 */
Josef Bacik00361582013-08-14 14:02:47 -04006930noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006931 u64 *orig_start, u64 *orig_block_len,
6932 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006933{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006935 struct btrfs_path *path;
6936 int ret;
6937 struct extent_buffer *leaf;
6938 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006939 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006940 struct btrfs_file_extent_item *fi;
6941 struct btrfs_key key;
6942 u64 disk_bytenr;
6943 u64 backref_offset;
6944 u64 extent_end;
6945 u64 num_bytes;
6946 int slot;
6947 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006948 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006949
Chris Mason46bfbb52010-05-26 11:04:10 -04006950 path = btrfs_alloc_path();
6951 if (!path)
6952 return -ENOMEM;
6953
David Sterbaf85b7372017-01-20 14:54:07 +01006954 ret = btrfs_lookup_file_extent(NULL, root, path,
6955 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006956 if (ret < 0)
6957 goto out;
6958
6959 slot = path->slots[0];
6960 if (ret == 1) {
6961 if (slot == 0) {
6962 /* can't find the item, must cow */
6963 ret = 0;
6964 goto out;
6965 }
6966 slot--;
6967 }
6968 ret = 0;
6969 leaf = path->nodes[0];
6970 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006971 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006972 key.type != BTRFS_EXTENT_DATA_KEY) {
6973 /* not our file or wrong item type, must cow */
6974 goto out;
6975 }
6976
6977 if (key.offset > offset) {
6978 /* Wrong offset, must cow */
6979 goto out;
6980 }
6981
6982 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6983 found_type = btrfs_file_extent_type(leaf, fi);
6984 if (found_type != BTRFS_FILE_EXTENT_REG &&
6985 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6986 /* not a regular extent, must cow */
6987 goto out;
6988 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006989
6990 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6991 goto out;
6992
Miao Xiee77751a2013-12-27 21:11:50 +08006993 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6994 if (extent_end <= offset)
6995 goto out;
6996
Chris Mason46bfbb52010-05-26 11:04:10 -04006997 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006998 if (disk_bytenr == 0)
6999 goto out;
7000
7001 if (btrfs_file_extent_compression(leaf, fi) ||
7002 btrfs_file_extent_encryption(leaf, fi) ||
7003 btrfs_file_extent_other_encoding(leaf, fi))
7004 goto out;
7005
Ethan Lien78d42952018-05-17 14:58:29 +08007006 /*
7007 * Do the same check as in btrfs_cross_ref_exist but without the
7008 * unnecessary search.
7009 */
7010 if (btrfs_file_extent_generation(leaf, fi) <=
7011 btrfs_root_last_snapshot(&root->root_item))
7012 goto out;
7013
Chris Mason46bfbb52010-05-26 11:04:10 -04007014 backref_offset = btrfs_file_extent_offset(leaf, fi);
7015
Josef Bacik7ee9e442013-06-21 16:37:03 -04007016 if (orig_start) {
7017 *orig_start = key.offset - backref_offset;
7018 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7019 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7020 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007021
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007022 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007023 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007024
7025 num_bytes = min(offset + *len, extent_end) - offset;
7026 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7027 u64 range_end;
7028
Jeff Mahoneyda170662016-06-15 09:22:56 -04007029 range_end = round_up(offset + num_bytes,
7030 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007031 ret = test_range_bit(io_tree, offset, range_end,
7032 EXTENT_DELALLOC, 0, NULL);
7033 if (ret) {
7034 ret = -EAGAIN;
7035 goto out;
7036 }
7037 }
7038
Josef Bacik1bda19e2013-10-18 12:10:36 -04007039 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007040
7041 /*
7042 * look for other files referencing this extent, if we
7043 * find any we must cow
7044 */
Josef Bacik00361582013-08-14 14:02:47 -04007045
Liu Boe4c3b2d2017-01-30 12:25:28 -08007046 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007047 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007048 if (ret) {
7049 ret = 0;
7050 goto out;
7051 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007052
7053 /*
7054 * adjust disk_bytenr and num_bytes to cover just the bytes
7055 * in this extent we are about to write. If there
7056 * are any csums in that range we have to cow in order
7057 * to keep the csums correct
7058 */
7059 disk_bytenr += backref_offset;
7060 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007061 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7062 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007063 /*
7064 * all of the above have passed, it is safe to overwrite this extent
7065 * without cow
7066 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007067 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007068 ret = 1;
7069out:
7070 btrfs_free_path(path);
7071 return ret;
7072}
7073
Josef Bacikeb838e72012-07-31 16:28:48 -04007074static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007075 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007076{
7077 struct btrfs_ordered_extent *ordered;
7078 int ret = 0;
7079
7080 while (1) {
7081 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007082 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007083 /*
7084 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007085 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007086 * extents in this range.
7087 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007088 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007089 lockend - lockstart + 1);
7090
7091 /*
7092 * We need to make sure there are no buffered pages in this
7093 * range either, we could have raced between the invalidate in
7094 * generic_file_direct_write and locking the extent. The
7095 * invalidate needs to happen so that reads after a write do not
7096 * get stale data.
7097 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007098 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007099 (!writing || !filemap_range_has_page(inode->i_mapping,
7100 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007101 break;
7102
7103 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007104 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007105
7106 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007107 /*
7108 * If we are doing a DIO read and the ordered extent we
7109 * found is for a buffered write, we can not wait for it
7110 * to complete and retry, because if we do so we can
7111 * deadlock with concurrent buffered writes on page
7112 * locks. This happens only if our DIO read covers more
7113 * than one extent map, if at this point has already
7114 * created an ordered extent for a previous extent map
7115 * and locked its range in the inode's io tree, and a
7116 * concurrent write against that previous extent map's
7117 * range and this range started (we unlock the ranges
7118 * in the io tree only when the bios complete and
7119 * buffered writes always lock pages before attempting
7120 * to lock range in the io tree).
7121 */
7122 if (writing ||
7123 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7124 btrfs_start_ordered_extent(inode, ordered, 1);
7125 else
7126 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007127 btrfs_put_ordered_extent(ordered);
7128 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007129 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007130 * We could trigger writeback for this range (and wait
7131 * for it to complete) and then invalidate the pages for
7132 * this range (through invalidate_inode_pages2_range()),
7133 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007134 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007135 * triggered a readahead) on a page lock due to an
7136 * ordered dio extent we created before but did not have
7137 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007138 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007139 * ordered extent to complete while holding a lock on
7140 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007141 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007142 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007143 }
7144
Filipe Mananaade77022016-02-18 14:28:55 +00007145 if (ret)
7146 break;
7147
Josef Bacikeb838e72012-07-31 16:28:48 -04007148 cond_resched();
7149 }
7150
7151 return ret;
7152}
7153
Liu Bo6f9994d2017-01-31 07:50:22 -08007154/* The callers of this must take lock_extent() */
7155static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7156 u64 orig_start, u64 block_start,
7157 u64 block_len, u64 orig_block_len,
7158 u64 ram_bytes, int compress_type,
7159 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007160{
7161 struct extent_map_tree *em_tree;
7162 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007163 int ret;
7164
Liu Bo6f9994d2017-01-31 07:50:22 -08007165 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7166 type == BTRFS_ORDERED_COMPRESSED ||
7167 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007168 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007169
Josef Bacik69ffb542012-09-11 15:40:07 -04007170 em_tree = &BTRFS_I(inode)->extent_tree;
7171 em = alloc_extent_map();
7172 if (!em)
7173 return ERR_PTR(-ENOMEM);
7174
7175 em->start = start;
7176 em->orig_start = orig_start;
7177 em->len = len;
7178 em->block_len = block_len;
7179 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007180 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007181 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007182 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007183 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007184 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007185 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007186 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007187 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7188 em->compress_type = compress_type;
7189 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007190
7191 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007192 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007193 em->start + em->len - 1, 0);
7194 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007195 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007196 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007197 /*
7198 * The caller has taken lock_extent(), who could race with us
7199 * to add em?
7200 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007201 } while (ret == -EEXIST);
7202
7203 if (ret) {
7204 free_extent_map(em);
7205 return ERR_PTR(ret);
7206 }
7207
Liu Bo6f9994d2017-01-31 07:50:22 -08007208 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007209 return em;
7210}
7211
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007212
David Sterba55e20bd2020-06-09 19:56:06 +02007213static int btrfs_get_blocks_direct_read(struct extent_map *em,
7214 struct buffer_head *bh_result,
7215 struct inode *inode,
7216 u64 start, u64 len)
7217{
7218 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7219
7220 if (em->block_start == EXTENT_MAP_HOLE ||
7221 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7222 return -ENOENT;
7223
7224 len = min(len, em->len - (start - em->start));
7225
7226 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7227 inode->i_blkbits;
7228 bh_result->b_size = len;
7229 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7230 set_buffer_mapped(bh_result);
7231
7232 return 0;
7233}
7234
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007235static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007236 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007237 struct inode *inode,
7238 struct btrfs_dio_data *dio_data,
7239 u64 start, u64 len)
7240{
7241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7242 struct extent_map *em = *map;
7243 int ret = 0;
7244
7245 /*
7246 * We don't allocate a new extent in the following cases
7247 *
7248 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7249 * existing extent.
7250 * 2) The extent is marked as PREALLOC. We're good to go here and can
7251 * just use the extent.
7252 *
7253 */
7254 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7255 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7256 em->block_start != EXTENT_MAP_HOLE)) {
7257 int type;
7258 u64 block_start, orig_start, orig_block_len, ram_bytes;
7259
7260 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7261 type = BTRFS_ORDERED_PREALLOC;
7262 else
7263 type = BTRFS_ORDERED_NOCOW;
7264 len = min(len, em->len - (start - em->start));
7265 block_start = em->block_start + (start - em->start);
7266
7267 if (can_nocow_extent(inode, start, &len, &orig_start,
7268 &orig_block_len, &ram_bytes) == 1 &&
7269 btrfs_inc_nocow_writers(fs_info, block_start)) {
7270 struct extent_map *em2;
7271
7272 em2 = btrfs_create_dio_extent(inode, start, len,
7273 orig_start, block_start,
7274 len, orig_block_len,
7275 ram_bytes, type);
7276 btrfs_dec_nocow_writers(fs_info, block_start);
7277 if (type == BTRFS_ORDERED_PREALLOC) {
7278 free_extent_map(em);
7279 *map = em = em2;
7280 }
7281
7282 if (em2 && IS_ERR(em2)) {
7283 ret = PTR_ERR(em2);
7284 goto out;
7285 }
7286 /*
7287 * For inode marked NODATACOW or extent marked PREALLOC,
7288 * use the existing or preallocated extent, so does not
7289 * need to adjust btrfs_space_info's bytes_may_use.
7290 */
Filipe Manana46d4dac2020-06-09 11:19:33 +01007291 btrfs_free_reserved_data_space_noquota(inode, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007292 goto skip_cow;
7293 }
7294 }
7295
7296 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007297 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007298 free_extent_map(em);
7299 *map = em = btrfs_new_extent_direct(inode, start, len);
7300 if (IS_ERR(em)) {
7301 ret = PTR_ERR(em);
7302 goto out;
7303 }
7304
7305 len = min(len, em->len - (start - em->start));
7306
7307skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007308 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7309 inode->i_blkbits;
7310 bh_result->b_size = len;
7311 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7312 set_buffer_mapped(bh_result);
7313
7314 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7315 set_buffer_new(bh_result);
7316
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007317 /*
7318 * Need to update the i_size under the extent lock so buffered
7319 * readers will get the updated i_size when we unlock.
7320 */
David Sterba55e20bd2020-06-09 19:56:06 +02007321 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007322 i_size_write(inode, start + len);
7323
David Sterba55e20bd2020-06-09 19:56:06 +02007324 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007325 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007326 dio_data->unsubmitted_oe_range_end = start + len;
7327 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007328out:
7329 return ret;
7330}
7331
David Sterba55e20bd2020-06-09 19:56:06 +02007332static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7333 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007334{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007335 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007336 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007337 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307338 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007339 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007340 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007341 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007342 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007343
David Sterba55e20bd2020-06-09 19:56:06 +02007344 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007345 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007346
Josef Bacikc3298612012-08-03 16:49:19 -04007347 lockstart = start;
7348 lockend = start + len - 1;
7349
David Sterba55e20bd2020-06-09 19:56:06 +02007350 if (current->journal_info) {
7351 /*
7352 * Need to pull our outstanding extents and set journal_info to NULL so
7353 * that anything that needs to check if there's a transaction doesn't get
7354 * confused.
7355 */
7356 dio_data = current->journal_info;
7357 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007358 }
7359
Josef Bacikeb838e72012-07-31 16:28:48 -04007360 /*
7361 * If this errors out it's because we couldn't invalidate pagecache for
7362 * this range and we need to fallback to buffered.
7363 */
David Sterba55e20bd2020-06-09 19:56:06 +02007364 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7365 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007366 ret = -ENOTBLK;
7367 goto err;
7368 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007369
Omar Sandoval39b07b52019-12-02 17:34:23 -08007370 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007371 if (IS_ERR(em)) {
7372 ret = PTR_ERR(em);
7373 goto unlock_err;
7374 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007375
7376 /*
7377 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7378 * io. INLINE is special, and we could probably kludge it in here, but
7379 * it's still buffered so for safety lets just fall back to the generic
7380 * buffered path.
7381 *
7382 * For COMPRESSED we _have_ to read the entire extent in so we can
7383 * decompress it, so there will be buffering required no matter what we
7384 * do, so go ahead and fallback to buffered.
7385 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007386 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007387 * to buffered IO. Don't blame me, this is the price we pay for using
7388 * the generic code.
7389 */
7390 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7391 em->block_start == EXTENT_MAP_INLINE) {
7392 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 ret = -ENOTBLK;
7394 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007395 }
7396
David Sterba55e20bd2020-06-09 19:56:06 +02007397 if (create) {
7398 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7399 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007400 if (ret < 0)
7401 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007402
7403 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7404 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007405 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007406 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7407 start, len);
7408 /* Can be negative only if we read from a hole */
7409 if (ret < 0) {
7410 ret = 0;
7411 free_extent_map(em);
7412 goto unlock_err;
7413 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007414 /*
7415 * We need to unlock only the end area that we aren't using.
7416 * The rest is going to be unlocked by the endio routine.
7417 */
David Sterba55e20bd2020-06-09 19:56:06 +02007418 lockstart = start + bh_result->b_size;
7419 if (lockstart < lockend) {
7420 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7421 lockstart, lockend, &cached_state);
7422 } else {
7423 free_extent_state(cached_state);
7424 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007425 }
7426
Josef Bacik4b46fce2010-05-23 11:00:55 -04007427 free_extent_map(em);
7428
7429 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007430
7431unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007432 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7433 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007434err:
David Sterba55e20bd2020-06-09 19:56:06 +02007435 if (dio_data)
7436 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007437 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007438}
7439
Omar Sandoval769b4f22020-04-16 14:46:22 -07007440static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7441{
7442 /*
7443 * This implies a barrier so that stores to dio_bio->bi_status before
7444 * this and loads of dio_bio->bi_status after this are fully ordered.
7445 */
7446 if (!refcount_dec_and_test(&dip->refs))
7447 return;
7448
7449 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7450 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7451 dip->bytes,
7452 !dip->dio_bio->bi_status);
7453 } else {
7454 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7455 dip->logical_offset,
7456 dip->logical_offset + dip->bytes - 1);
7457 }
7458
David Sterba55e20bd2020-06-09 19:56:06 +02007459 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007460 kfree(dip);
7461}
7462
Omar Sandoval77d5d682020-04-16 14:46:25 -07007463static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7464 int mirror_num,
7465 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007466{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007467 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007468 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007469 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007470
Mike Christie37226b22016-06-05 14:31:52 -05007471 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007472
Omar Sandoval5c047a62020-04-16 14:46:24 -07007473 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007474 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007475 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007476
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007477 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007478 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007479 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007480 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007481 return ret;
7482}
7483
Omar Sandoval769b4f22020-04-16 14:46:22 -07007484static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7485 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007486 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007487{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007488 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7489 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007490 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007491 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7492 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007493 struct bio_vec bvec;
7494 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007495 u64 start = io_bio->logical;
7496 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007497 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007498
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007499 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7500 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307501
Liu Bo17347ce2017-05-15 15:33:27 -07007502 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7503 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007504 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007505 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007506 if (uptodate &&
7507 (!csum || !check_data_csum(inode, io_bio, icsum,
7508 bvec.bv_page, pgoff,
7509 start, sectorsize))) {
7510 clean_io_failure(fs_info, failure_tree, io_tree,
7511 start, bvec.bv_page,
7512 btrfs_ino(BTRFS_I(inode)),
7513 pgoff);
7514 } else {
7515 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007516
Omar Sandoval77d5d682020-04-16 14:46:25 -07007517 status = btrfs_submit_read_repair(inode,
7518 &io_bio->bio,
7519 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007520 bvec.bv_page, pgoff,
7521 start,
7522 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007523 io_bio->mirror_num,
7524 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007525 if (status)
7526 err = status;
7527 }
7528 start += sectorsize;
7529 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307530 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307531 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007532 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007533 return err;
7534}
7535
Qu Wenruo524272602017-03-08 10:25:52 +08007536static void __endio_write_update_ordered(struct inode *inode,
7537 const u64 offset, const u64 bytes,
7538 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007539{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007540 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007541 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007542 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007543 u64 ordered_offset = offset;
7544 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007545 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007546
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007547 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007548 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007549 else
Qu Wenruo524272602017-03-08 10:25:52 +08007550 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007551
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007552 while (ordered_offset < offset + bytes) {
7553 last_offset = ordered_offset;
7554 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7555 &ordered_offset,
7556 ordered_bytes,
7557 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007558 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7559 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007560 btrfs_queue_work(wq, &ordered->work);
7561 }
7562 /*
7563 * If btrfs_dec_test_ordered_pending does not find any ordered
7564 * extent in the range, we can exit.
7565 */
7566 if (ordered_offset == last_offset)
7567 return;
7568 /*
7569 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007570 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007571 */
7572 if (ordered_offset < offset + bytes) {
7573 ordered_bytes = offset + bytes - ordered_offset;
7574 ordered = NULL;
7575 }
Chris Mason163cf092010-11-28 19:56:33 -05007576 }
Filipe Manana14543772015-11-24 16:23:54 +00007577}
7578
David Sterbad0ee3932018-03-08 14:35:48 +01007579static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007580 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007581{
Josef Bacikc6100a42017-05-05 11:57:13 -04007582 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007583 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007584 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007585 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007586 return 0;
7587}
7588
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007589static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007590{
7591 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007592 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007593
Miao Xie8b110e32014-09-12 18:44:03 +08007594 if (err)
7595 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007596 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007597 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7598 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007599 (unsigned long long)bio->bi_iter.bi_sector,
7600 bio->bi_iter.bi_size, err);
7601
Omar Sandoval769b4f22020-04-16 14:46:22 -07007602 if (bio_op(bio) == REQ_OP_READ) {
7603 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007604 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007605 }
7606
Omar Sandoval769b4f22020-04-16 14:46:22 -07007607 if (err)
7608 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007609
Miao Xiee65e1532010-11-22 03:04:43 +00007610 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007611 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007612}
7613
David Sterbad0ee3932018-03-08 14:35:48 +01007614static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7615 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007616{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007617 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007618 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007619 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007620 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007621
Liu Bo4c274bc2017-11-01 17:19:27 -06007622 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007623 if (async_submit)
7624 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7625
Josef Bacik5fd02042012-05-02 14:00:54 -04007626 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007627 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007628 if (ret)
7629 goto err;
7630 }
Miao Xiee65e1532010-11-22 03:04:43 +00007631
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007632 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007633 goto map;
7634
7635 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007636 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7637 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007638 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007639 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007640 } else if (write) {
7641 /*
7642 * If we aren't doing async submit, calculate the csum of the
7643 * bio now.
7644 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007645 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007646 if (ret)
7647 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007648 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007649 u64 csum_offset;
7650
7651 csum_offset = file_offset - dip->logical_offset;
7652 csum_offset >>= inode->i_sb->s_blocksize_bits;
7653 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7654 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007655 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007656map:
Chris Mason08635ba2019-07-10 12:28:14 -07007657 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007658err:
Miao Xiee65e1532010-11-22 03:04:43 +00007659 return ret;
7660}
7661
Omar Sandovalc36cac22020-04-16 14:46:13 -07007662/*
7663 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7664 * or ordered extents whether or not we submit any bios.
7665 */
7666static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7667 struct inode *inode,
7668 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007669{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007670 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007671 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7672 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007673 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007674
Omar Sandoval85879572020-04-16 14:46:21 -07007675 dip_size = sizeof(*dip);
7676 if (!write && csum) {
7677 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7678 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7679 size_t nblocks;
7680
7681 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7682 dip_size += csum_size * nblocks;
7683 }
7684
7685 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007686 if (!dip)
7687 return NULL;
7688
Omar Sandovalc36cac22020-04-16 14:46:13 -07007689 dip->inode = inode;
7690 dip->logical_offset = file_offset;
7691 dip->bytes = dio_bio->bi_iter.bi_size;
7692 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007693 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007694 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007695
7696 if (write) {
7697 struct btrfs_dio_data *dio_data = current->journal_info;
7698
7699 /*
7700 * Setting range start and end to the same value means that
7701 * no cleanup will happen in btrfs_direct_IO
7702 */
7703 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7704 dip->bytes;
7705 dio_data->unsubmitted_oe_range_start =
7706 dio_data->unsubmitted_oe_range_end;
7707 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007708 return dip;
7709}
7710
David Sterba55e20bd2020-06-09 19:56:06 +02007711static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7712 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007713{
7714 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007715 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007716 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007717 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7718 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007719 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007720 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007721 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007722 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007723 u64 submit_len;
7724 int clone_offset = 0;
7725 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307726 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007727 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007728 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007729
Omar Sandovalc36cac22020-04-16 14:46:13 -07007730 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7731 if (!dip) {
7732 if (!write) {
7733 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7734 file_offset + dio_bio->bi_iter.bi_size - 1);
7735 }
7736 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007737 dio_end_io(dio_bio);
7738 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007739 }
7740
Omar Sandoval85879572020-04-16 14:46:21 -07007741 if (!write && csum) {
7742 /*
7743 * Load the csums up front to reduce csum tree searches and
7744 * contention when submitting bios.
7745 */
7746 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7747 dip->csums);
7748 if (status != BLK_STS_OK)
7749 goto out_err;
7750 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007751
Omar Sandoval769b4f22020-04-16 14:46:22 -07007752 start_sector = dio_bio->bi_iter.bi_sector;
7753 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007754
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007755 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007756 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7757 start_sector << 9, submit_len,
7758 &geom);
7759 if (ret) {
7760 status = errno_to_blk_status(ret);
7761 goto out_err;
7762 }
7763 ASSERT(geom.len <= INT_MAX);
7764
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007765 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007766
Liu Bo725130b2017-05-16 09:51:39 -07007767 /*
7768 * This will never fail as it's passing GPF_NOFS and
7769 * the allocation is backed by btrfs_bioset.
7770 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007771 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007772 bio->bi_private = dip;
7773 bio->bi_end_io = btrfs_end_dio_bio;
7774 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007775
Liu Bo725130b2017-05-16 09:51:39 -07007776 ASSERT(submit_len >= clone_len);
7777 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007778
Liu Bo725130b2017-05-16 09:51:39 -07007779 /*
7780 * Increase the count before we submit the bio so we know
7781 * the end IO handler won't happen before we increase the
7782 * count. Otherwise, the dip might get freed before we're
7783 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007784 *
7785 * We transfer the initial reference to the last bio, so we
7786 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007787 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007788 if (submit_len > 0) {
7789 refcount_inc(&dip->refs);
7790 /*
7791 * If we are submitting more than one bio, submit them
7792 * all asynchronously. The exception is RAID 5 or 6, as
7793 * asynchronous checksums make it difficult to collect
7794 * full stripe writes.
7795 */
7796 if (!raid56)
7797 async_submit = 1;
7798 }
Miao Xiee65e1532010-11-22 03:04:43 +00007799
David Sterbad0ee3932018-03-08 14:35:48 +01007800 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007801 async_submit);
7802 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007803 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007804 if (submit_len > 0)
7805 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007806 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007807 }
Liu Bo725130b2017-05-16 09:51:39 -07007808
7809 clone_offset += clone_len;
7810 start_sector += clone_len >> 9;
7811 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007812 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007813 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007814
Miao Xiee65e1532010-11-22 03:04:43 +00007815out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007816 dip->dio_bio->bi_status = status;
7817 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007818}
7819
David Sterbaf4c48b42020-06-09 19:19:27 +02007820static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7821 const struct iov_iter *iter, loff_t offset)
7822{
7823 int seg;
7824 int i;
7825 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7826 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827
David Sterbaf4c48b42020-06-09 19:19:27 +02007828 if (offset & blocksize_mask)
7829 goto out;
7830
7831 if (iov_iter_alignment(iter) & blocksize_mask)
7832 goto out;
7833
7834 /* If this is a write we don't need to check anymore */
7835 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7836 return 0;
7837 /*
7838 * Check to make sure we don't have duplicate iov_base's in this
7839 * iovec, if so return EINVAL, otherwise we'll get csum errors
7840 * when reading back.
7841 */
7842 for (seg = 0; seg < iter->nr_segs; seg++) {
7843 for (i = seg + 1; i < iter->nr_segs; i++) {
7844 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7845 goto out;
7846 }
7847 }
7848 retval = 0;
7849out:
7850 return retval;
7851}
7852
David Sterba55e20bd2020-06-09 19:56:06 +02007853static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007854{
7855 struct file *file = iocb->ki_filp;
7856 struct inode *inode = file->f_mapping->host;
7857 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007858 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007859 struct extent_changeset *data_reserved = NULL;
7860 loff_t offset = iocb->ki_pos;
7861 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007862 int flags = 0;
7863 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007864 bool relock = false;
7865 ssize_t ret;
7866
7867 if (check_direct_IO(fs_info, iter, offset))
7868 return 0;
7869
David Sterba55e20bd2020-06-09 19:56:06 +02007870 inode_dio_begin(inode);
7871
7872 /*
7873 * The generic stuff only does filemap_write_and_wait_range, which
7874 * isn't enough if we've written compressed pages to this area, so
7875 * we need to flush the dirty pages again to make absolutely sure
7876 * that any outstanding dirty pages are on disk.
7877 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007878 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007879 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7880 &BTRFS_I(inode)->runtime_flags))
7881 filemap_fdatawrite_range(inode->i_mapping, offset,
7882 offset + count - 1);
7883
David Sterbaf4c48b42020-06-09 19:19:27 +02007884 if (iov_iter_rw(iter) == WRITE) {
7885 /*
7886 * If the write DIO is beyond the EOF, we need update
7887 * the isize, but it is protected by i_mutex. So we can
7888 * not unlock the i_mutex at this case.
7889 */
7890 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007891 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007892 inode_unlock(inode);
7893 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007894 }
David Sterba55e20bd2020-06-09 19:56:06 +02007895 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7896 offset, count);
7897 if (ret)
7898 goto out;
7899
7900 /*
7901 * We need to know how many extents we reserved so that we can
7902 * do the accounting properly if we go over the number we
7903 * originally calculated. Abuse current->journal_info for this.
7904 */
7905 dio_data.reserve = round_up(count,
7906 fs_info->sectorsize);
7907 dio_data.unsubmitted_oe_range_start = (u64)offset;
7908 dio_data.unsubmitted_oe_range_end = (u64)offset;
7909 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007910 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007911 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7912 &BTRFS_I(inode)->runtime_flags)) {
7913 inode_dio_end(inode);
7914 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7915 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007916 }
7917
David Sterba55e20bd2020-06-09 19:56:06 +02007918 ret = __blockdev_direct_IO(iocb, inode,
7919 fs_info->fs_devices->latest_bdev,
7920 iter, btrfs_get_blocks_direct, NULL,
7921 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007922 if (iov_iter_rw(iter) == WRITE) {
7923 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007924 current->journal_info = NULL;
7925 if (ret < 0 && ret != -EIOCBQUEUED) {
7926 if (dio_data.reserve)
7927 btrfs_delalloc_release_space(inode, data_reserved,
7928 offset, dio_data.reserve, true);
7929 /*
7930 * On error we might have left some ordered extents
7931 * without submitting corresponding bios for them, so
7932 * cleanup them up to avoid other tasks getting them
7933 * and waiting for them to complete forever.
7934 */
7935 if (dio_data.unsubmitted_oe_range_start <
7936 dio_data.unsubmitted_oe_range_end)
7937 __endio_write_update_ordered(inode,
7938 dio_data.unsubmitted_oe_range_start,
7939 dio_data.unsubmitted_oe_range_end -
7940 dio_data.unsubmitted_oe_range_start,
7941 false);
7942 } else if (ret >= 0 && (size_t)ret < count)
7943 btrfs_delalloc_release_space(inode, data_reserved,
7944 offset, count - (size_t)ret, true);
7945 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007946 }
7947out:
David Sterba55e20bd2020-06-09 19:56:06 +02007948 if (wakeup)
7949 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007950 if (relock)
7951 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007952
David Sterbaf4c48b42020-06-09 19:19:27 +02007953 extent_changeset_free(data_reserved);
7954 return ret;
7955}
Chris Mason16432982008-04-10 10:23:21 -04007956
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007957static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7958 __u64 start, __u64 len)
7959{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007960 int ret;
7961
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007962 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007963 if (ret)
7964 return ret;
7965
David Sterba2135fb92017-06-23 04:09:57 +02007966 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007967}
7968
Chris Mason9ebefb182007-06-15 13:50:00 -04007969int btrfs_readpage(struct file *file, struct page *page)
7970{
David Sterba71ad38b2020-02-05 19:09:35 +01007971 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007972}
Chris Mason1832a6d2007-12-21 16:27:21 -05007973
Chris Mason39279cc2007-06-12 06:35:45 -04007974static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7975{
Josef Bacikbe7bd732015-10-22 15:05:09 -04007976 struct inode *inode = page->mapping->host;
7977 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04007978
7979 if (current->flags & PF_MEMALLOC) {
7980 redirty_page_for_writepage(wbc, page);
7981 unlock_page(page);
7982 return 0;
7983 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04007984
7985 /*
7986 * If we are under memory pressure we will call this directly from the
7987 * VM, we need to make sure we have the inode referenced for the ordered
7988 * extent. If not just return like we didn't do anything.
7989 */
7990 if (!igrab(inode)) {
7991 redirty_page_for_writepage(wbc, page);
7992 return AOP_WRITEPAGE_ACTIVATE;
7993 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02007994 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04007995 btrfs_add_delayed_iput(inode);
7996 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04007997}
Chris Mason39279cc2007-06-12 06:35:45 -04007998
Eric Sandeen48a3b632013-04-25 20:41:01 +00007999static int btrfs_writepages(struct address_space *mapping,
8000 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008001{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008002 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008003}
8004
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008005static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008006{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008007 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008008}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008009
Chris Masone6dcd2d2008-07-17 12:53:50 -04008010static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008011{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008012 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008013 if (ret == 1)
8014 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008015 return ret;
8016}
Chris Mason39279cc2007-06-12 06:35:45 -04008017
Chris Masone6dcd2d2008-07-17 12:53:50 -04008018static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8019{
Chris Mason98509cf2008-09-11 15:51:43 -04008020 if (PageWriteback(page) || PageDirty(page))
8021 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008022 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008023}
8024
Roman Gushchinf8e66082020-03-04 16:57:35 -08008025#ifdef CONFIG_MIGRATION
8026static int btrfs_migratepage(struct address_space *mapping,
8027 struct page *newpage, struct page *page,
8028 enum migrate_mode mode)
8029{
8030 int ret;
8031
8032 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8033 if (ret != MIGRATEPAGE_SUCCESS)
8034 return ret;
8035
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008036 if (page_has_private(page))
8037 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008038
8039 if (PagePrivate2(page)) {
8040 ClearPagePrivate2(page);
8041 SetPagePrivate2(newpage);
8042 }
8043
8044 if (mode != MIGRATE_SYNC_NO_COPY)
8045 migrate_page_copy(newpage, page);
8046 else
8047 migrate_page_states(newpage, page);
8048 return MIGRATEPAGE_SUCCESS;
8049}
8050#endif
8051
Lukas Czernerd47992f2013-05-21 23:17:23 -04008052static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8053 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008054{
Josef Bacik5fd02042012-05-02 14:00:54 -04008055 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008056 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008057 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008058 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008059 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008060 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308061 u64 start;
8062 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008063 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008064
Chris Mason8b62b722009-09-02 16:53:46 -04008065 /*
8066 * we have the page locked, so new writeback can't start,
8067 * and the dirty bit won't be cleared while we are here.
8068 *
8069 * Wait for IO on this page so that we can safely clear
8070 * the PagePrivate2 bit and do ordered accounting
8071 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008072 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008073
Josef Bacik5fd02042012-05-02 14:00:54 -04008074 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008075 if (offset) {
8076 btrfs_releasepage(page, GFP_NOFS);
8077 return;
8078 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008079
8080 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008081 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308082again:
8083 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008084 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308085 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008086 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008087 end = min(page_end,
8088 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008089 /*
8090 * IO on this page will never be started, so we need
8091 * to account for any ordered extents now
8092 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008093 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308094 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008095 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008096 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008097 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008098 /*
8099 * whoever cleared the private bit is responsible
8100 * for the finish_ordered_io
8101 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008102 if (TestClearPagePrivate2(page)) {
8103 struct btrfs_ordered_inode_tree *tree;
8104 u64 new_len;
8105
8106 tree = &BTRFS_I(inode)->ordered_tree;
8107
8108 spin_lock_irq(&tree->lock);
8109 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308110 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008111 if (new_len < ordered->truncated_len)
8112 ordered->truncated_len = new_len;
8113 spin_unlock_irq(&tree->lock);
8114
8115 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308116 start,
8117 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008118 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008119 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008120 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008121 if (!inode_evicting) {
8122 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308123 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008124 &cached_state);
8125 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308126
8127 start = end + 1;
8128 if (start < page_end)
8129 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008130 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008131
Qu Wenruob9d0b382015-09-29 10:35:16 +08008132 /*
8133 * Qgroup reserved space handler
8134 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008135 * 1) Already written to disk or ordered extent already submitted
8136 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8137 * Qgroup will be handled by its qgroup_record then.
8138 * btrfs_qgroup_free_data() call will do nothing here.
8139 *
8140 * 2) Not written to disk yet
8141 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8142 * bit of its io_tree, and free the qgroup reserved data space.
8143 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008144 */
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008145 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008146 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008147 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008148 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8149 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008150 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008151
8152 __btrfs_releasepage(page, GFP_NOFS);
8153 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008154
Chris Mason4a096752008-07-21 10:29:44 -04008155 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008156 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008157}
8158
Chris Mason9ebefb182007-06-15 13:50:00 -04008159/*
8160 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8161 * called from a page fault handler when a page is first dirtied. Hence we must
8162 * be careful to check for EOF conditions here. We set the page up correctly
8163 * for a written page which means we get ENOSPC checking when writing into
8164 * holes and correct delalloc and unwritten extent mapping on filesystems that
8165 * support these features.
8166 *
8167 * We are not allowed to take the i_mutex here so we have to play games to
8168 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008169 * truncate_setsize() writes the inode size before removing pages, once we have
8170 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008171 * beyond EOF, then the page is guaranteed safe against truncation until we
8172 * unlock the page.
8173 */
Souptick Joardera528a242018-06-06 19:54:44 +05308174vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008175{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008176 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008177 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008179 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8180 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008181 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008182 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008183 char *kaddr;
8184 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008185 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308186 vm_fault_t ret;
8187 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008188 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308189 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008190 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008191 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308192 u64 end;
8193
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008194 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008195
Jan Karab2b5ef52012-06-12 16:20:45 +02008196 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008197 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008198 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308199 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008200
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308201 /*
8202 * Reserving delalloc space after obtaining the page lock can lead to
8203 * deadlock. For example, if a dirty page is locked by this function
8204 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8205 * dirty page write out, then the btrfs_writepage() function could
8206 * end up waiting indefinitely to get a lock on the page currently
8207 * being processed by btrfs_page_mkwrite() function.
8208 */
Souptick Joardera528a242018-06-06 19:54:44 +05308209 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308210 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308211 if (!ret2) {
8212 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008213 reserved = 1;
8214 }
Souptick Joardera528a242018-06-06 19:54:44 +05308215 if (ret2) {
8216 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008217 if (reserved)
8218 goto out;
8219 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008220 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008221
Nick Piggin56a76f82009-03-31 15:23:23 -07008222 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008223again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008224 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008225 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008226
Chris Mason9ebefb182007-06-15 13:50:00 -04008227 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008228 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008229 /* page got truncated out from underneath us */
8230 goto out_unlock;
8231 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008232 wait_on_page_writeback(page);
8233
David Sterbaff13db42015-12-03 14:30:40 +01008234 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008235 set_page_extent_mapped(page);
8236
Chris Masoneb84ae02008-07-17 13:53:27 -04008237 /*
8238 * we can't set the delalloc bits if there are pending ordered
8239 * extents. Drop our locks and wait for them to finish
8240 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008241 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8242 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008243 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008244 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008245 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008246 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008247 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248 btrfs_put_ordered_extent(ordered);
8249 goto again;
8250 }
8251
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008252 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008253 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008254 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008255 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308256 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008257 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008258 page_start, PAGE_SIZE - reserved_space,
8259 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308260 }
8261 }
8262
Josef Bacikfbf19082009-10-01 17:10:23 -04008263 /*
Liu Bo54160342016-12-13 12:15:19 -08008264 * page_mkwrite gets called when the page is firstly dirtied after it's
8265 * faulted in, but write(2) could also dirty a page and set delalloc
8266 * bits, thus in this case for space account reason, we still need to
8267 * clear any delalloc bits within this page range since we have to
8268 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008269 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308270 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008271 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8272 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008273
Souptick Joardera528a242018-06-06 19:54:44 +05308274 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008275 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308276 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008277 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008278 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008279 ret = VM_FAULT_SIGBUS;
8280 goto out_unlock;
8281 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008282
8283 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008284 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008285 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008286 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008287 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008288
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008289 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008290 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008291 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008292 flush_dcache_page(page);
8293 kunmap(page);
8294 }
Chris Mason247e7432008-07-17 12:53:51 -04008295 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008296 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008297 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008298
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008299 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008300 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008301 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008302
David Sterbae43bbe52017-12-12 21:43:52 +01008303 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008304
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008305 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8306 sb_end_pagefault(inode->i_sb);
8307 extent_changeset_free(data_reserved);
8308 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008309
8310out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008311 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008312out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008313 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008314 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008315 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008316out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008317 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008318 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008319 return ret;
8320}
8321
Filipe Manana213e8c52018-02-06 20:40:31 +00008322static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008323{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008324 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008325 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008326 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008327 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008328 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008329 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008330 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008331
Filipe Manana213e8c52018-02-06 20:40:31 +00008332 if (!skip_writeback) {
8333 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8334 (u64)-1);
8335 if (ret)
8336 return ret;
8337 }
Chris Mason39279cc2007-06-12 06:35:45 -04008338
Josef Bacikfcb80c22011-05-03 10:40:22 -04008339 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008340 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8341 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008342 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008343 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008344 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008345 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008346 * be free'd up by the truncate operation, but also have some slack
8347 * space reserved in case it uses space during the truncate (thank you
8348 * very much snapshotting).
8349 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008350 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008351 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008352 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008353 * doesn't end up using space reserved for updating the inode. We also
8354 * need to be able to stop the transaction and start a new one, which
8355 * means we need to be able to update the inode several times, and we
8356 * have no idea of knowing how many times that will be, so we can't just
8357 * reserve 1 item for the entirety of the operation, so that has to be
8358 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008359 *
8360 * So that leaves us with
8361 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008362 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008363 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008364 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008365 * updating the inode.
8366 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008367 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008368 if (!rsv)
8369 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008370 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008371 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008372
Josef Bacik907cbce2011-08-08 13:46:15 -04008373 /*
Josef Bacik07127182011-08-19 10:29:59 -04008374 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008375 * 1 for updating the inode.
8376 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008377 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008378 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008379 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008380 goto out;
8381 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008382
Josef Bacik907cbce2011-08-08 13:46:15 -04008383 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008384 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008385 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008386 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008387
Chris Mason5a3f23d2009-03-31 13:27:11 -04008388 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008389 * So if we truncate and then write and fsync we normally would just
8390 * write the extents that changed, which is a problem if we need to
8391 * first truncate that entire inode. So set this flag so we write out
8392 * all of the extents in the inode to the sync log so we're completely
8393 * safe.
8394 */
8395 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008396 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008397
Yan, Zheng80825102009-11-12 09:35:36 +00008398 while (1) {
8399 ret = btrfs_truncate_inode_items(trans, root, inode,
8400 inode->i_size,
8401 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008402 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008403 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008404 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008405
Yan, Zheng80825102009-11-12 09:35:36 +00008406 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008407 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008408 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008409
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008410 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008411 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008412
8413 trans = btrfs_start_transaction(root, 2);
8414 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008415 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008416 trans = NULL;
8417 break;
8418 }
8419
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008420 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008421 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008422 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008423 BUG_ON(ret); /* shouldn't happen */
8424 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008425 }
8426
Josef Bacikddfae632017-10-19 14:16:02 -04008427 /*
8428 * We can't call btrfs_truncate_block inside a trans handle as we could
8429 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8430 * we've truncated everything except the last little bit, and can do
8431 * btrfs_truncate_block and then update the disk_i_size.
8432 */
8433 if (ret == NEED_TRUNCATE_BLOCK) {
8434 btrfs_end_transaction(trans);
8435 btrfs_btree_balance_dirty(fs_info);
8436
8437 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8438 if (ret)
8439 goto out;
8440 trans = btrfs_start_transaction(root, 1);
8441 if (IS_ERR(trans)) {
8442 ret = PTR_ERR(trans);
8443 goto out;
8444 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008445 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008446 }
8447
Chris Mason917c16b2011-11-08 14:49:59 -05008448 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008449 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008450
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008451 trans->block_rsv = &fs_info->trans_block_rsv;
8452 ret2 = btrfs_update_inode(trans, root, inode);
8453 if (ret2 && !ret)
8454 ret = ret2;
8455
8456 ret2 = btrfs_end_transaction(trans);
8457 if (ret2 && !ret)
8458 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008459 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008460 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008461out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008462 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008463
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008464 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008465}
8466
Sven Wegener3b963622008-06-09 21:57:42 -04008467/*
Chris Masond352ac62008-09-29 15:18:18 -04008468 * create a new subvolume directory/inode (helper for the ioctl).
8469 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008470int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008471 struct btrfs_root *new_root,
8472 struct btrfs_root *parent_root,
8473 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008474{
Chris Mason39279cc2007-06-12 06:35:45 -04008475 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008476 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008477 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008478
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008479 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8480 new_dirid, new_dirid,
8481 S_IFDIR | (~current_umask() & S_IRWXUGO),
8482 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008483 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008484 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008485 inode->i_op = &btrfs_dir_inode_operations;
8486 inode->i_fop = &btrfs_dir_file_operations;
8487
Miklos Szeredibfe86842011-10-28 14:13:29 +02008488 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008489 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008490 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008491
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008492 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8493 if (err)
8494 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008495 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008496 new_root->root_key.objectid, err);
8497
Yan, Zheng76dda932009-09-21 16:00:26 -04008498 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008499
Yan, Zheng76dda932009-09-21 16:00:26 -04008500 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008501 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008502}
8503
Chris Mason39279cc2007-06-12 06:35:45 -04008504struct inode *btrfs_alloc_inode(struct super_block *sb)
8505{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008506 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008507 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008508 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008509
David Sterba712e36c2017-10-31 17:08:27 +01008510 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008511 if (!ei)
8512 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008513
8514 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008515 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008516 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008517 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008518 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008519 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008520 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008521 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008522 ei->disk_i_size = 0;
8523 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008524 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008525 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008526 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008527 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008528 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008529
Josef Bacik9e0baf62011-07-15 15:16:44 +00008530 spin_lock_init(&ei->lock);
8531 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008532 if (sb->s_magic != BTRFS_TEST_MAGIC)
8533 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8534 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008535 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008536 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008537 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008538
Miao Xie16cdcec2011-04-22 18:12:22 +08008539 ei->delayed_node = NULL;
8540
chandan r9cc97d62012-07-04 12:48:07 +05308541 ei->i_otime.tv_sec = 0;
8542 ei->i_otime.tv_nsec = 0;
8543
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008544 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008545 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008546 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8547 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8548 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008549 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8550 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008551 ei->io_tree.track_uptodate = true;
8552 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008553 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008554 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008555 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008556 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008557 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008558 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008559 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008560
8561 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008562}
8563
Josef Bacikaaedb552013-10-11 14:44:09 -04008564#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8565void btrfs_test_destroy_inode(struct inode *inode)
8566{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008567 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008568 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8569}
8570#endif
8571
Al Viro26602ca2019-04-10 15:14:41 -04008572void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008573{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008574 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8575}
8576
Chris Mason39279cc2007-06-12 06:35:45 -04008577void btrfs_destroy_inode(struct inode *inode)
8578{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008580 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008581 struct btrfs_root *root = BTRFS_I(inode)->root;
8582
Al Virob3d9b7a2012-06-09 13:51:19 -04008583 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008584 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008585 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8586 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008587 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008588 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008589 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008590 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008591 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008592
Chris Mason5a3f23d2009-03-31 13:27:11 -04008593 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008594 * This can happen where we create an inode, but somebody else also
8595 * created the same inode and we need to destroy the one we already
8596 * created.
8597 */
8598 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008599 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008600
Chris Masond3977122009-01-05 21:25:51 -05008601 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008602 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8603 if (!ordered)
8604 break;
8605 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008606 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008607 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008608 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008609 btrfs_remove_ordered_extent(inode, ordered);
8610 btrfs_put_ordered_extent(ordered);
8611 btrfs_put_ordered_extent(ordered);
8612 }
8613 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008614 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008615 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008616 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008617 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008618 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008619}
8620
Al Viro45321ac2010-06-07 13:43:19 -04008621int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008622{
8623 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008624
Naohiro Aota6379ef92013-06-06 09:56:34 +00008625 if (root == NULL)
8626 return 1;
8627
Liu Bofa6ac872013-02-20 14:10:23 +00008628 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008629 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008630 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008631 else
Al Viro45321ac2010-06-07 13:43:19 -04008632 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008633}
8634
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008635static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008636{
8637 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8638
8639 inode_init_once(&ei->vfs_inode);
8640}
8641
David Sterbae67c7182018-02-19 17:24:18 +01008642void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008643{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008644 /*
8645 * Make sure all delayed rcu free inodes are flushed before we
8646 * destroy cache.
8647 */
8648 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008649 kmem_cache_destroy(btrfs_inode_cachep);
8650 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008651 kmem_cache_destroy(btrfs_path_cachep);
8652 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008653 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008654}
8655
Liu Bof5c29bd2017-11-02 17:21:50 -06008656int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008657{
David Sterba837e1972012-09-07 03:00:48 -06008658 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008659 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008660 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8661 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008662 if (!btrfs_inode_cachep)
8663 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008664
David Sterba837e1972012-09-07 03:00:48 -06008665 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008666 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008667 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008668 if (!btrfs_trans_handle_cachep)
8669 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008670
David Sterba837e1972012-09-07 03:00:48 -06008671 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008672 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008673 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008674 if (!btrfs_path_cachep)
8675 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008676
David Sterba837e1972012-09-07 03:00:48 -06008677 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008678 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008679 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008680 if (!btrfs_free_space_cachep)
8681 goto fail;
8682
Christophe Leroy3acd4852019-08-21 15:05:55 +00008683 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8684 PAGE_SIZE, PAGE_SIZE,
8685 SLAB_RED_ZONE, NULL);
8686 if (!btrfs_free_space_bitmap_cachep)
8687 goto fail;
8688
Chris Mason39279cc2007-06-12 06:35:45 -04008689 return 0;
8690fail:
8691 btrfs_destroy_cachep();
8692 return -ENOMEM;
8693}
8694
David Howellsa528d352017-01-31 16:46:22 +00008695static int btrfs_getattr(const struct path *path, struct kstat *stat,
8696 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008697{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008698 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008699 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008700 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008701 u32 bi_flags = BTRFS_I(inode)->flags;
8702
8703 stat->result_mask |= STATX_BTIME;
8704 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8705 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8706 if (bi_flags & BTRFS_INODE_APPEND)
8707 stat->attributes |= STATX_ATTR_APPEND;
8708 if (bi_flags & BTRFS_INODE_COMPRESS)
8709 stat->attributes |= STATX_ATTR_COMPRESSED;
8710 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8711 stat->attributes |= STATX_ATTR_IMMUTABLE;
8712 if (bi_flags & BTRFS_INODE_NODUMP)
8713 stat->attributes |= STATX_ATTR_NODUMP;
8714
8715 stat->attributes_mask |= (STATX_ATTR_APPEND |
8716 STATX_ATTR_COMPRESSED |
8717 STATX_ATTR_IMMUTABLE |
8718 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008719
Chris Mason39279cc2007-06-12 06:35:45 -04008720 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008721 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008722
8723 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008724 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008725 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008726 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008727 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008728 return 0;
8729}
8730
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008731static int btrfs_rename_exchange(struct inode *old_dir,
8732 struct dentry *old_dentry,
8733 struct inode *new_dir,
8734 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008735{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008736 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008737 struct btrfs_trans_handle *trans;
8738 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008739 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008740 struct inode *new_inode = new_dentry->d_inode;
8741 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008742 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008743 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008744 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8745 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008746 u64 old_idx = 0;
8747 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008748 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008749 bool root_log_pinned = false;
8750 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008751 struct btrfs_log_ctx ctx_root;
8752 struct btrfs_log_ctx ctx_dest;
8753 bool sync_log_root = false;
8754 bool sync_log_dest = false;
8755 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008756
8757 /* we only allow rename subvolume link between subvolumes */
8758 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8759 return -EXDEV;
8760
Filipe Mananad4682ba2018-06-11 19:24:28 +01008761 btrfs_init_log_ctx(&ctx_root, old_inode);
8762 btrfs_init_log_ctx(&ctx_dest, new_inode);
8763
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008764 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008765 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8766 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008767 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008768
8769 /*
8770 * We want to reserve the absolute worst case amount of items. So if
8771 * both inodes are subvols and we need to unlink them then that would
8772 * require 4 item modifications, but if they are both normal inodes it
8773 * would require 5 item modifications, so we'll assume their normal
8774 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8775 * should cover the worst case number of items we'll modify.
8776 */
8777 trans = btrfs_start_transaction(root, 12);
8778 if (IS_ERR(trans)) {
8779 ret = PTR_ERR(trans);
8780 goto out_notrans;
8781 }
8782
Josef Bacik3e174092019-11-15 15:43:06 -05008783 if (dest != root)
8784 btrfs_record_root_in_trans(trans, dest);
8785
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008786 /*
8787 * We need to find a free sequence number both in the source and
8788 * in the destination directory for the exchange.
8789 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008790 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008791 if (ret)
8792 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008793 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008794 if (ret)
8795 goto out_fail;
8796
8797 BTRFS_I(old_inode)->dir_index = 0ULL;
8798 BTRFS_I(new_inode)->dir_index = 0ULL;
8799
8800 /* Reference for the source. */
8801 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8802 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008803 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008804 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008805 btrfs_pin_log_trans(root);
8806 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008807 ret = btrfs_insert_inode_ref(trans, dest,
8808 new_dentry->d_name.name,
8809 new_dentry->d_name.len,
8810 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008811 btrfs_ino(BTRFS_I(new_dir)),
8812 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008813 if (ret)
8814 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008815 }
8816
8817 /* And now for the dest. */
8818 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8819 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008820 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008821 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008822 btrfs_pin_log_trans(dest);
8823 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008824 ret = btrfs_insert_inode_ref(trans, root,
8825 old_dentry->d_name.name,
8826 old_dentry->d_name.len,
8827 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008828 btrfs_ino(BTRFS_I(old_dir)),
8829 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008830 if (ret)
8831 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008832 }
8833
8834 /* Update inode version and ctime/mtime. */
8835 inode_inc_iversion(old_dir);
8836 inode_inc_iversion(new_dir);
8837 inode_inc_iversion(old_inode);
8838 inode_inc_iversion(new_inode);
8839 old_dir->i_ctime = old_dir->i_mtime = ctime;
8840 new_dir->i_ctime = new_dir->i_mtime = ctime;
8841 old_inode->i_ctime = ctime;
8842 new_inode->i_ctime = ctime;
8843
8844 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008845 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8846 BTRFS_I(old_inode), 1);
8847 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8848 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008849 }
8850
8851 /* src is a subvolume */
8852 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008853 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008854 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008855 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8856 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008857 old_dentry->d_name.name,
8858 old_dentry->d_name.len);
8859 if (!ret)
8860 ret = btrfs_update_inode(trans, root, old_inode);
8861 }
8862 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008863 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008864 goto out_fail;
8865 }
8866
8867 /* dest is a subvolume */
8868 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008869 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008870 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008871 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8872 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008873 new_dentry->d_name.name,
8874 new_dentry->d_name.len);
8875 if (!ret)
8876 ret = btrfs_update_inode(trans, dest, new_inode);
8877 }
8878 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008879 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008880 goto out_fail;
8881 }
8882
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008883 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008884 new_dentry->d_name.name,
8885 new_dentry->d_name.len, 0, old_idx);
8886 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008887 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008888 goto out_fail;
8889 }
8890
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008891 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008892 old_dentry->d_name.name,
8893 old_dentry->d_name.len, 0, new_idx);
8894 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008895 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008896 goto out_fail;
8897 }
8898
8899 if (old_inode->i_nlink == 1)
8900 BTRFS_I(old_inode)->dir_index = old_idx;
8901 if (new_inode->i_nlink == 1)
8902 BTRFS_I(new_inode)->dir_index = new_idx;
8903
Filipe Manana86e8aa02016-05-05 02:02:27 +01008904 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008905 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008906 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8907 BTRFS_I(old_dir), parent,
8908 false, &ctx_root);
8909 if (ret == BTRFS_NEED_LOG_SYNC)
8910 sync_log_root = true;
8911 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8912 commit_transaction = true;
8913 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008914 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008915 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008916 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008917 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008918 if (!commit_transaction) {
8919 parent = old_dentry->d_parent;
8920 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8921 BTRFS_I(new_dir), parent,
8922 false, &ctx_dest);
8923 if (ret == BTRFS_NEED_LOG_SYNC)
8924 sync_log_dest = true;
8925 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8926 commit_transaction = true;
8927 ret = 0;
8928 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008929 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008930 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008931 }
8932out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008933 /*
8934 * If we have pinned a log and an error happened, we unpin tasks
8935 * trying to sync the log and force them to fallback to a transaction
8936 * commit if the log currently contains any of the inodes involved in
8937 * this rename operation (to ensure we do not persist a log with an
8938 * inconsistent state for any of these inodes or leading to any
8939 * inconsistencies when replayed). If the transaction was aborted, the
8940 * abortion reason is propagated to userspace when attempting to commit
8941 * the transaction. If the log does not contain any of these inodes, we
8942 * allow the tasks to sync it.
8943 */
8944 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008945 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8946 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8947 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008948 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008949 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008950 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008951
8952 if (root_log_pinned) {
8953 btrfs_end_log_trans(root);
8954 root_log_pinned = false;
8955 }
8956 if (dest_log_pinned) {
8957 btrfs_end_log_trans(dest);
8958 dest_log_pinned = false;
8959 }
8960 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008961 if (!ret && sync_log_root && !commit_transaction) {
8962 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8963 &ctx_root);
8964 if (ret)
8965 commit_transaction = true;
8966 }
8967 if (!ret && sync_log_dest && !commit_transaction) {
8968 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8969 &ctx_dest);
8970 if (ret)
8971 commit_transaction = true;
8972 }
8973 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008974 /*
8975 * We may have set commit_transaction when logging the new name
8976 * in the destination root, in which case we left the source
8977 * root context in the list of log contextes. So make sure we
8978 * remove it to avoid invalid memory accesses, since the context
8979 * was allocated in our stack frame.
8980 */
8981 if (sync_log_root) {
8982 mutex_lock(&root->log_mutex);
8983 list_del_init(&ctx_root.list);
8984 mutex_unlock(&root->log_mutex);
8985 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008986 ret = btrfs_commit_transaction(trans);
8987 } else {
8988 int ret2;
8989
8990 ret2 = btrfs_end_transaction(trans);
8991 ret = ret ? ret : ret2;
8992 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008993out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05008994 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8995 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008996 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008997
Filipe Mananae6c61712019-11-08 16:11:56 +00008998 ASSERT(list_empty(&ctx_root.list));
8999 ASSERT(list_empty(&ctx_dest.list));
9000
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009001 return ret;
9002}
9003
9004static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9005 struct btrfs_root *root,
9006 struct inode *dir,
9007 struct dentry *dentry)
9008{
9009 int ret;
9010 struct inode *inode;
9011 u64 objectid;
9012 u64 index;
9013
9014 ret = btrfs_find_free_ino(root, &objectid);
9015 if (ret)
9016 return ret;
9017
9018 inode = btrfs_new_inode(trans, root, dir,
9019 dentry->d_name.name,
9020 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009021 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009022 objectid,
9023 S_IFCHR | WHITEOUT_MODE,
9024 &index);
9025
9026 if (IS_ERR(inode)) {
9027 ret = PTR_ERR(inode);
9028 return ret;
9029 }
9030
9031 inode->i_op = &btrfs_special_inode_operations;
9032 init_special_inode(inode, inode->i_mode,
9033 WHITEOUT_DEV);
9034
9035 ret = btrfs_init_inode_security(trans, inode, dir,
9036 &dentry->d_name);
9037 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009038 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009039
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009040 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9041 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009042 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009043 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009044
9045 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009046out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009047 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009048 if (ret)
9049 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009050 iput(inode);
9051
Filipe Mananac9901612016-05-05 01:41:57 +01009052 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009053}
9054
Chris Mason39279cc2007-06-12 06:35:45 -04009055static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009056 struct inode *new_dir, struct dentry *new_dentry,
9057 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009058{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009059 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009060 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009061 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009062 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9063 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009064 struct inode *new_inode = d_inode(new_dentry);
9065 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009066 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009067 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009068 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009069 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009070 struct btrfs_log_ctx ctx;
9071 bool sync_log = false;
9072 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009073
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009074 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009075 return -EPERM;
9076
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009077 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009078 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009079 return -EXDEV;
9080
Li Zefan33345d012011-04-20 10:31:50 +08009081 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009082 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009083 return -ENOTEMPTY;
9084
Chris Mason39279cc2007-06-12 06:35:45 -04009085 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009086 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009087 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009088
9089
9090 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009091 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009092 new_dentry->d_name.name,
9093 new_dentry->d_name.len);
9094
9095 if (ret) {
9096 if (ret == -EEXIST) {
9097 /* we shouldn't get
9098 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309099 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009100 return ret;
9101 }
9102 } else {
9103 /* maybe -EOVERFLOW */
9104 return ret;
9105 }
9106 }
9107 ret = 0;
9108
Chris Mason5a3f23d2009-03-31 13:27:11 -04009109 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009110 * we're using rename to replace one file with another. Start IO on it
9111 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009112 */
Chris Mason8d875f92014-08-12 10:47:42 -07009113 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009114 filemap_flush(old_inode->i_mapping);
9115
Yan, Zheng76dda932009-09-21 16:00:26 -04009116 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009117 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009118 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009119 /*
9120 * We want to reserve the absolute worst case amount of items. So if
9121 * both inodes are subvols and we need to unlink them then that would
9122 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009123 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009124 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9125 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009126 * If our rename has the whiteout flag, we need more 5 units for the
9127 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9128 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009129 */
Filipe Manana5062af32016-05-05 10:26:26 +01009130 trans_num_items = 11;
9131 if (flags & RENAME_WHITEOUT)
9132 trans_num_items += 5;
9133 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009134 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009135 ret = PTR_ERR(trans);
9136 goto out_notrans;
9137 }
Chris Mason5f39d392007-10-15 16:14:19 -04009138
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009139 if (dest != root)
9140 btrfs_record_root_in_trans(trans, dest);
9141
Nikolay Borisov877574e2017-02-20 13:50:33 +02009142 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009143 if (ret)
9144 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009145
Miao Xie67de1172013-12-26 13:07:06 +08009146 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009147 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009148 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009149 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009150 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009151 btrfs_pin_log_trans(root);
9152 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009153 ret = btrfs_insert_inode_ref(trans, dest,
9154 new_dentry->d_name.name,
9155 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009156 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009157 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009158 if (ret)
9159 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009160 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009161
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009162 inode_inc_iversion(old_dir);
9163 inode_inc_iversion(new_dir);
9164 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009165 old_dir->i_ctime = old_dir->i_mtime =
9166 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009167 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009168
Chris Mason12fcfd22009-03-24 10:24:20 -04009169 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009170 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9171 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009172
Li Zefan33345d012011-04-20 10:31:50 +08009173 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009174 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009175 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009176 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9177 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009178 old_dentry->d_name.name,
9179 old_dentry->d_name.len);
9180 if (!ret)
9181 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009182 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009183 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009184 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009185 goto out_fail;
9186 }
Chris Mason39279cc2007-06-12 06:35:45 -04009187
9188 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009189 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009190 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009191 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009192 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009193 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009194 BUG_ON(new_inode->i_nlink == 0);
9195 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009196 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9197 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009198 new_dentry->d_name.name,
9199 new_dentry->d_name.len);
9200 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009201 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009202 ret = btrfs_orphan_add(trans,
9203 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009204 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009205 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009206 goto out_fail;
9207 }
Chris Mason39279cc2007-06-12 06:35:45 -04009208 }
Josef Bacikaec74772008-07-24 12:12:38 -04009209
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009210 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009211 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009212 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009213 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009214 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009215 goto out_fail;
9216 }
Chris Mason39279cc2007-06-12 06:35:45 -04009217
Miao Xie67de1172013-12-26 13:07:06 +08009218 if (old_inode->i_nlink == 1)
9219 BTRFS_I(old_inode)->dir_index = index;
9220
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009221 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009222 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009223
Filipe Mananad4682ba2018-06-11 19:24:28 +01009224 btrfs_init_log_ctx(&ctx, old_inode);
9225 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9226 BTRFS_I(old_dir), parent,
9227 false, &ctx);
9228 if (ret == BTRFS_NEED_LOG_SYNC)
9229 sync_log = true;
9230 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9231 commit_transaction = true;
9232 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009233 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009234 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009235 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009236
9237 if (flags & RENAME_WHITEOUT) {
9238 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9239 old_dentry);
9240
9241 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009242 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009243 goto out_fail;
9244 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009245 }
Chris Mason39279cc2007-06-12 06:35:45 -04009246out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009247 /*
9248 * If we have pinned the log and an error happened, we unpin tasks
9249 * trying to sync the log and force them to fallback to a transaction
9250 * commit if the log currently contains any of the inodes involved in
9251 * this rename operation (to ensure we do not persist a log with an
9252 * inconsistent state for any of these inodes or leading to any
9253 * inconsistencies when replayed). If the transaction was aborted, the
9254 * abortion reason is propagated to userspace when attempting to commit
9255 * the transaction. If the log does not contain any of these inodes, we
9256 * allow the tasks to sync it.
9257 */
9258 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009259 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9260 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9261 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009262 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009263 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009264 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009265
9266 btrfs_end_log_trans(root);
9267 log_pinned = false;
9268 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009269 if (!ret && sync_log) {
9270 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9271 if (ret)
9272 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009273 } else if (sync_log) {
9274 mutex_lock(&root->log_mutex);
9275 list_del(&ctx.list);
9276 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009277 }
9278 if (commit_transaction) {
9279 ret = btrfs_commit_transaction(trans);
9280 } else {
9281 int ret2;
9282
9283 ret2 = btrfs_end_transaction(trans);
9284 ret = ret ? ret : ret2;
9285 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009286out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009287 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009288 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009289
Chris Mason39279cc2007-06-12 06:35:45 -04009290 return ret;
9291}
9292
Miklos Szeredi80ace852014-07-23 15:15:32 +02009293static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9294 struct inode *new_dir, struct dentry *new_dentry,
9295 unsigned int flags)
9296{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009297 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009298 return -EINVAL;
9299
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009300 if (flags & RENAME_EXCHANGE)
9301 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9302 new_dentry);
9303
9304 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009305}
9306
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009307struct btrfs_delalloc_work {
9308 struct inode *inode;
9309 struct completion completion;
9310 struct list_head list;
9311 struct btrfs_work work;
9312};
9313
Miao Xie8ccf6f192012-10-25 09:28:04 +00009314static void btrfs_run_delalloc_work(struct btrfs_work *work)
9315{
9316 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009317 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009318
9319 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9320 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009321 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009322 filemap_flush(inode->i_mapping);
9323 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9324 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009325 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009326
Nikolay Borisov076da912018-04-23 10:54:16 +03009327 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009328 complete(&delalloc_work->completion);
9329}
9330
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009331static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009332{
9333 struct btrfs_delalloc_work *work;
9334
David Sterba100d5702015-12-08 14:39:32 +01009335 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009336 if (!work)
9337 return NULL;
9338
9339 init_completion(&work->completion);
9340 INIT_LIST_HEAD(&work->list);
9341 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009342 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009343
9344 return work;
9345}
9346
Chris Masond352ac62008-09-29 15:18:18 -04009347/*
9348 * some fairly slow code that needs optimization. This walks the list
9349 * of all the inodes with pending delalloc and forces them to disk.
9350 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009351static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009352{
Chris Masonea8c2812008-08-04 23:17:27 -04009353 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009354 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009355 struct btrfs_delalloc_work *work, *next;
9356 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009357 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009358 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009359
Miao Xie8ccf6f192012-10-25 09:28:04 +00009360 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009361 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009362
Miao Xie573bfb72014-03-06 13:55:03 +08009363 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009364 spin_lock(&root->delalloc_lock);
9365 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009366 while (!list_empty(&splice)) {
9367 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009368 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009369
Miao Xieeb73c1b2013-05-15 07:48:22 +00009370 list_move_tail(&binode->delalloc_inodes,
9371 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009372 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009373 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009374 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009375 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009376 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009377 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009378
Ethan Lien3cd24c62018-11-01 14:49:03 +08009379 if (snapshot)
9380 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9381 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009382 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009383 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009384 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009385 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009386 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009387 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009388 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009389 btrfs_queue_work(root->fs_info->flush_workers,
9390 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009391 ret++;
9392 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009393 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009394 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009395 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009396 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009397 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009398
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009399out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009400 list_for_each_entry_safe(work, next, &works, list) {
9401 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009402 wait_for_completion(&work->completion);
9403 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009404 }
9405
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009406 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009407 spin_lock(&root->delalloc_lock);
9408 list_splice_tail(&splice, &root->delalloc_inodes);
9409 spin_unlock(&root->delalloc_lock);
9410 }
Miao Xie573bfb72014-03-06 13:55:03 +08009411 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009412 return ret;
9413}
9414
Ethan Lien3cd24c62018-11-01 14:49:03 +08009415int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009417 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009418 int ret;
9419
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009420 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009421 return -EROFS;
9422
Ethan Lien3cd24c62018-11-01 14:49:03 +08009423 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009424 if (ret > 0)
9425 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009426 return ret;
9427}
9428
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009429int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009430{
9431 struct btrfs_root *root;
9432 struct list_head splice;
9433 int ret;
9434
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009435 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009436 return -EROFS;
9437
9438 INIT_LIST_HEAD(&splice);
9439
Miao Xie573bfb72014-03-06 13:55:03 +08009440 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009441 spin_lock(&fs_info->delalloc_root_lock);
9442 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009443 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009444 root = list_first_entry(&splice, struct btrfs_root,
9445 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009446 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009447 BUG_ON(!root);
9448 list_move_tail(&root->delalloc_root,
9449 &fs_info->delalloc_roots);
9450 spin_unlock(&fs_info->delalloc_root_lock);
9451
Ethan Lien3cd24c62018-11-01 14:49:03 +08009452 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009453 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009454 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009455 goto out;
9456
Miao Xie6c255e62014-03-06 13:55:01 +08009457 if (nr != -1) {
9458 nr -= ret;
9459 WARN_ON(nr < 0);
9460 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009461 spin_lock(&fs_info->delalloc_root_lock);
9462 }
9463 spin_unlock(&fs_info->delalloc_root_lock);
9464
Miao Xie6c255e62014-03-06 13:55:01 +08009465 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009466out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009467 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009468 spin_lock(&fs_info->delalloc_root_lock);
9469 list_splice_tail(&splice, &fs_info->delalloc_roots);
9470 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009471 }
Miao Xie573bfb72014-03-06 13:55:03 +08009472 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009473 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009474}
9475
Chris Mason39279cc2007-06-12 06:35:45 -04009476static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9477 const char *symname)
9478{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009479 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009480 struct btrfs_trans_handle *trans;
9481 struct btrfs_root *root = BTRFS_I(dir)->root;
9482 struct btrfs_path *path;
9483 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009484 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009485 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009486 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309487 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009488 int name_len;
9489 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009490 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009491 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009492 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009493
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009494 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009495 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009496 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009497
Josef Bacik9ed74f22009-09-11 16:12:44 -04009498 /*
9499 * 2 items for inode item and ref
9500 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009501 * 1 item for updating parent inode item
9502 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009503 * 1 item for xattr if selinux is on
9504 */
Filipe Manana9269d122015-12-31 18:16:29 +00009505 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009506 if (IS_ERR(trans))
9507 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009508
Li Zefan581bb052011-04-20 10:06:11 +08009509 err = btrfs_find_free_ino(root, &objectid);
9510 if (err)
9511 goto out_unlock;
9512
Josef Bacikaec74772008-07-24 12:12:38 -04009513 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009514 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9515 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009516 if (IS_ERR(inode)) {
9517 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009518 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009519 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009520 }
Chris Mason39279cc2007-06-12 06:35:45 -04009521
Casey Schauflerad19db72011-12-15 10:09:07 -05009522 /*
9523 * If the active LSM wants to access the inode during
9524 * d_instantiate it needs these. Smack checks to see
9525 * if the filesystem supports xattrs by looking at the
9526 * ops vector.
9527 */
9528 inode->i_fop = &btrfs_file_operations;
9529 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009530 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009531 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9532
9533 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9534 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009535 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009536
Chris Mason39279cc2007-06-12 06:35:45 -04009537 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009538 if (!path) {
9539 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009540 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009541 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009542 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009543 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009544 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009545 datasize = btrfs_file_extent_calc_inline_size(name_len);
9546 err = btrfs_insert_empty_item(trans, root, path, &key,
9547 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009548 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009549 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009550 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009551 }
Chris Mason5f39d392007-10-15 16:14:19 -04009552 leaf = path->nodes[0];
9553 ei = btrfs_item_ptr(leaf, path->slots[0],
9554 struct btrfs_file_extent_item);
9555 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9556 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009557 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009558 btrfs_set_file_extent_encryption(leaf, ei, 0);
9559 btrfs_set_file_extent_compression(leaf, ei, 0);
9560 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9561 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9562
Chris Mason39279cc2007-06-12 06:35:45 -04009563 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009564 write_extent_buffer(leaf, symname, ptr, name_len);
9565 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009566 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009567
Chris Mason39279cc2007-06-12 06:35:45 -04009568 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009569 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009570 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009571 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009572 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009573 /*
9574 * Last step, add directory indexes for our symlink inode. This is the
9575 * last step to avoid extra cleanup of these indexes if an error happens
9576 * elsewhere above.
9577 */
9578 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009579 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9580 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009581 if (err)
9582 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009583
Al Viro1e2e5472018-05-04 08:23:01 -04009584 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009585
9586out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009587 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009588 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009589 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009590 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009591 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009592 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009593 return err;
9594}
Chris Mason16432982008-04-10 10:23:21 -04009595
Qu Wenruo203f44c52020-06-10 09:04:40 +08009596static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9597 struct inode *inode, struct btrfs_key *ins,
9598 u64 file_offset)
9599{
9600 struct btrfs_file_extent_item stack_fi;
9601 u64 start = ins->objectid;
9602 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009603 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009604
9605 memset(&stack_fi, 0, sizeof(stack_fi));
9606
9607 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9608 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9609 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9610 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9611 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9612 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9613 /* Encryption and other encoding is reserved and all 0 */
9614
Qu Wenruo9729f102020-06-10 09:04:41 +08009615 ret = btrfs_qgroup_release_data(inode, file_offset, len);
9616 if (ret < 0)
9617 return ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009618 return insert_reserved_file_extent(trans, inode, file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009619 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009620}
Josef Bacik0af3d002010-06-21 14:48:16 -04009621static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9622 u64 start, u64 num_bytes, u64 min_size,
9623 loff_t actual_len, u64 *alloc_hint,
9624 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009625{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009626 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009627 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9628 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009629 struct btrfs_root *root = BTRFS_I(inode)->root;
9630 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009631 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009632 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009633 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009634 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009635 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009636 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009637 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009638 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009639
Josef Bacik0af3d002010-06-21 14:48:16 -04009640 if (trans)
9641 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009642 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009643 if (own_trans) {
9644 trans = btrfs_start_transaction(root, 3);
9645 if (IS_ERR(trans)) {
9646 ret = PTR_ERR(trans);
9647 break;
9648 }
Yan Zhengd899e052008-10-30 14:25:28 -04009649 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009650
Byongho Leeee221842015-12-15 01:42:10 +09009651 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009652 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009653 /*
9654 * If we are severely fragmented we could end up with really
9655 * small allocations, so if the allocator is returning small
9656 * chunks lets make its job easier by only searching for those
9657 * sized chunks.
9658 */
9659 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009660 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9661 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009662 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009663 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009664 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009665 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009666 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009667
9668 /*
9669 * We've reserved this space, and thus converted it from
9670 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9671 * from here on out we will only need to clear our reservation
9672 * for the remaining unreserved area, so advance our
9673 * clear_offset by our extent size.
9674 */
9675 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009676 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009677
Josef Bacik0b670dc2015-09-23 17:11:16 -04009678 last_alloc = ins.offset;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009679 ret = insert_prealloc_file_extent(trans, inode, &ins,
9680 cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009681 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009682 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009683 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009684 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009685 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009686 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009687 break;
9688 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009689
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009690 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009691 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009692
Josef Bacik5dc562c2012-08-17 13:14:17 -04009693 em = alloc_extent_map();
9694 if (!em) {
9695 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9696 &BTRFS_I(inode)->runtime_flags);
9697 goto next;
9698 }
9699
9700 em->start = cur_offset;
9701 em->orig_start = cur_offset;
9702 em->len = ins.offset;
9703 em->block_start = ins.objectid;
9704 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009705 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009706 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009707 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9708 em->generation = trans->transid;
9709
9710 while (1) {
9711 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009712 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009713 write_unlock(&em_tree->lock);
9714 if (ret != -EEXIST)
9715 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009716 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009717 cur_offset + ins.offset - 1,
9718 0);
9719 }
9720 free_extent_map(em);
9721next:
Yan Zhengd899e052008-10-30 14:25:28 -04009722 num_bytes -= ins.offset;
9723 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009724 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009725
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009726 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009727 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009728 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009729 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009730 (actual_len > inode->i_size) &&
9731 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009732 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009733 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009734 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009735 i_size = cur_offset;
9736 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009737 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009738 }
9739
Yan Zhengd899e052008-10-30 14:25:28 -04009740 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009741
9742 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009743 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009744 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009745 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009746 break;
9747 }
Yan Zhengd899e052008-10-30 14:25:28 -04009748
Josef Bacik0af3d002010-06-21 14:48:16 -04009749 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009750 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009751 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009752 if (clear_offset < end)
9753 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9754 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009755 return ret;
9756}
9757
Josef Bacik0af3d002010-06-21 14:48:16 -04009758int btrfs_prealloc_file_range(struct inode *inode, int mode,
9759 u64 start, u64 num_bytes, u64 min_size,
9760 loff_t actual_len, u64 *alloc_hint)
9761{
9762 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9763 min_size, actual_len, alloc_hint,
9764 NULL);
9765}
9766
9767int btrfs_prealloc_file_range_trans(struct inode *inode,
9768 struct btrfs_trans_handle *trans, int mode,
9769 u64 start, u64 num_bytes, u64 min_size,
9770 loff_t actual_len, u64 *alloc_hint)
9771{
9772 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9773 min_size, actual_len, alloc_hint, trans);
9774}
9775
Chris Masone6dcd2d2008-07-17 12:53:50 -04009776static int btrfs_set_page_dirty(struct page *page)
9777{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009778 return __set_page_dirty_nobuffers(page);
9779}
9780
Al Viro10556cb22011-06-20 19:28:19 -04009781static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009782{
Li Zefanb83cc962010-12-20 16:04:08 +08009783 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009784 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009785
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009786 if (mask & MAY_WRITE &&
9787 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9788 if (btrfs_root_readonly(root))
9789 return -EROFS;
9790 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9791 return -EACCES;
9792 }
Al Viro2830ba72011-06-20 19:16:29 -04009793 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009794}
Chris Mason39279cc2007-06-12 06:35:45 -04009795
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009796static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9797{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009798 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009799 struct btrfs_trans_handle *trans;
9800 struct btrfs_root *root = BTRFS_I(dir)->root;
9801 struct inode *inode = NULL;
9802 u64 objectid;
9803 u64 index;
9804 int ret = 0;
9805
9806 /*
9807 * 5 units required for adding orphan entry
9808 */
9809 trans = btrfs_start_transaction(root, 5);
9810 if (IS_ERR(trans))
9811 return PTR_ERR(trans);
9812
9813 ret = btrfs_find_free_ino(root, &objectid);
9814 if (ret)
9815 goto out;
9816
9817 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009818 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009819 if (IS_ERR(inode)) {
9820 ret = PTR_ERR(inode);
9821 inode = NULL;
9822 goto out;
9823 }
9824
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009825 inode->i_fop = &btrfs_file_operations;
9826 inode->i_op = &btrfs_file_inode_operations;
9827
9828 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009829 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9830
Chris Masonb0d5d102014-09-08 13:08:51 -07009831 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9832 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009833 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009834
9835 ret = btrfs_update_inode(trans, root, inode);
9836 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009837 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009838 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009839 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009840 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009841
Filipe Manana5762b5c2014-08-01 00:10:32 +01009842 /*
9843 * We set number of links to 0 in btrfs_new_inode(), and here we set
9844 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9845 * through:
9846 *
9847 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9848 */
9849 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009850 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009851 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009852 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009853out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009854 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009855 if (ret && inode)
9856 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009857 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009858 return ret;
9859}
9860
David Sterba5cdc84b2018-07-18 20:32:52 +02009861void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009862{
David Sterba5cdc84b2018-07-18 20:32:52 +02009863 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009864 unsigned long index = start >> PAGE_SHIFT;
9865 unsigned long end_index = end >> PAGE_SHIFT;
9866 struct page *page;
9867
9868 while (index <= end_index) {
9869 page = find_get_page(inode->i_mapping, index);
9870 ASSERT(page); /* Pages should be in the extent_io_tree */
9871 set_page_writeback(page);
9872 put_page(page);
9873 index++;
9874 }
9875}
9876
Omar Sandovaled46ff32016-11-03 10:28:14 -07009877#ifdef CONFIG_SWAP
9878/*
9879 * Add an entry indicating a block group or device which is pinned by a
9880 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9881 * negative errno on failure.
9882 */
9883static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9884 bool is_block_group)
9885{
9886 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9887 struct btrfs_swapfile_pin *sp, *entry;
9888 struct rb_node **p;
9889 struct rb_node *parent = NULL;
9890
9891 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9892 if (!sp)
9893 return -ENOMEM;
9894 sp->ptr = ptr;
9895 sp->inode = inode;
9896 sp->is_block_group = is_block_group;
9897
9898 spin_lock(&fs_info->swapfile_pins_lock);
9899 p = &fs_info->swapfile_pins.rb_node;
9900 while (*p) {
9901 parent = *p;
9902 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9903 if (sp->ptr < entry->ptr ||
9904 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9905 p = &(*p)->rb_left;
9906 } else if (sp->ptr > entry->ptr ||
9907 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9908 p = &(*p)->rb_right;
9909 } else {
9910 spin_unlock(&fs_info->swapfile_pins_lock);
9911 kfree(sp);
9912 return 1;
9913 }
9914 }
9915 rb_link_node(&sp->node, parent, p);
9916 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9917 spin_unlock(&fs_info->swapfile_pins_lock);
9918 return 0;
9919}
9920
9921/* Free all of the entries pinned by this swapfile. */
9922static void btrfs_free_swapfile_pins(struct inode *inode)
9923{
9924 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9925 struct btrfs_swapfile_pin *sp;
9926 struct rb_node *node, *next;
9927
9928 spin_lock(&fs_info->swapfile_pins_lock);
9929 node = rb_first(&fs_info->swapfile_pins);
9930 while (node) {
9931 next = rb_next(node);
9932 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9933 if (sp->inode == inode) {
9934 rb_erase(&sp->node, &fs_info->swapfile_pins);
9935 if (sp->is_block_group)
9936 btrfs_put_block_group(sp->ptr);
9937 kfree(sp);
9938 }
9939 node = next;
9940 }
9941 spin_unlock(&fs_info->swapfile_pins_lock);
9942}
9943
9944struct btrfs_swap_info {
9945 u64 start;
9946 u64 block_start;
9947 u64 block_len;
9948 u64 lowest_ppage;
9949 u64 highest_ppage;
9950 unsigned long nr_pages;
9951 int nr_extents;
9952};
9953
9954static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9955 struct btrfs_swap_info *bsi)
9956{
9957 unsigned long nr_pages;
9958 u64 first_ppage, first_ppage_reported, next_ppage;
9959 int ret;
9960
9961 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9962 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9963 PAGE_SIZE) >> PAGE_SHIFT;
9964
9965 if (first_ppage >= next_ppage)
9966 return 0;
9967 nr_pages = next_ppage - first_ppage;
9968
9969 first_ppage_reported = first_ppage;
9970 if (bsi->start == 0)
9971 first_ppage_reported++;
9972 if (bsi->lowest_ppage > first_ppage_reported)
9973 bsi->lowest_ppage = first_ppage_reported;
9974 if (bsi->highest_ppage < (next_ppage - 1))
9975 bsi->highest_ppage = next_ppage - 1;
9976
9977 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9978 if (ret < 0)
9979 return ret;
9980 bsi->nr_extents += ret;
9981 bsi->nr_pages += nr_pages;
9982 return 0;
9983}
9984
9985static void btrfs_swap_deactivate(struct file *file)
9986{
9987 struct inode *inode = file_inode(file);
9988
9989 btrfs_free_swapfile_pins(inode);
9990 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9991}
9992
9993static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9994 sector_t *span)
9995{
9996 struct inode *inode = file_inode(file);
9997 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9998 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9999 struct extent_state *cached_state = NULL;
10000 struct extent_map *em = NULL;
10001 struct btrfs_device *device = NULL;
10002 struct btrfs_swap_info bsi = {
10003 .lowest_ppage = (sector_t)-1ULL,
10004 };
10005 int ret = 0;
10006 u64 isize;
10007 u64 start;
10008
10009 /*
10010 * If the swap file was just created, make sure delalloc is done. If the
10011 * file changes again after this, the user is doing something stupid and
10012 * we don't really care.
10013 */
10014 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10015 if (ret)
10016 return ret;
10017
10018 /*
10019 * The inode is locked, so these flags won't change after we check them.
10020 */
10021 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10022 btrfs_warn(fs_info, "swapfile must not be compressed");
10023 return -EINVAL;
10024 }
10025 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10026 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10027 return -EINVAL;
10028 }
10029 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10030 btrfs_warn(fs_info, "swapfile must not be checksummed");
10031 return -EINVAL;
10032 }
10033
10034 /*
10035 * Balance or device remove/replace/resize can move stuff around from
10036 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10037 * concurrently while we are mapping the swap extents, and
10038 * fs_info->swapfile_pins prevents them from running while the swap file
10039 * is active and moving the extents. Note that this also prevents a
10040 * concurrent device add which isn't actually necessary, but it's not
10041 * really worth the trouble to allow it.
10042 */
10043 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10044 btrfs_warn(fs_info,
10045 "cannot activate swapfile while exclusive operation is running");
10046 return -EBUSY;
10047 }
10048 /*
10049 * Snapshots can create extents which require COW even if NODATACOW is
10050 * set. We use this counter to prevent snapshots. We must increment it
10051 * before walking the extents because we don't want a concurrent
10052 * snapshot to run after we've already checked the extents.
10053 */
10054 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10055
10056 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10057
10058 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10059 start = 0;
10060 while (start < isize) {
10061 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010062 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010063 u64 len = isize - start;
10064
Omar Sandoval39b07b52019-12-02 17:34:23 -080010065 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010066 if (IS_ERR(em)) {
10067 ret = PTR_ERR(em);
10068 goto out;
10069 }
10070
10071 if (em->block_start == EXTENT_MAP_HOLE) {
10072 btrfs_warn(fs_info, "swapfile must not have holes");
10073 ret = -EINVAL;
10074 goto out;
10075 }
10076 if (em->block_start == EXTENT_MAP_INLINE) {
10077 /*
10078 * It's unlikely we'll ever actually find ourselves
10079 * here, as a file small enough to fit inline won't be
10080 * big enough to store more than the swap header, but in
10081 * case something changes in the future, let's catch it
10082 * here rather than later.
10083 */
10084 btrfs_warn(fs_info, "swapfile must not be inline");
10085 ret = -EINVAL;
10086 goto out;
10087 }
10088 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10089 btrfs_warn(fs_info, "swapfile must not be compressed");
10090 ret = -EINVAL;
10091 goto out;
10092 }
10093
10094 logical_block_start = em->block_start + (start - em->start);
10095 len = min(len, em->len - (start - em->start));
10096 free_extent_map(em);
10097 em = NULL;
10098
10099 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10100 if (ret < 0) {
10101 goto out;
10102 } else if (ret) {
10103 ret = 0;
10104 } else {
10105 btrfs_warn(fs_info,
10106 "swapfile must not be copy-on-write");
10107 ret = -EINVAL;
10108 goto out;
10109 }
10110
10111 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10112 if (IS_ERR(em)) {
10113 ret = PTR_ERR(em);
10114 goto out;
10115 }
10116
10117 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10118 btrfs_warn(fs_info,
10119 "swapfile must have single data profile");
10120 ret = -EINVAL;
10121 goto out;
10122 }
10123
10124 if (device == NULL) {
10125 device = em->map_lookup->stripes[0].dev;
10126 ret = btrfs_add_swapfile_pin(inode, device, false);
10127 if (ret == 1)
10128 ret = 0;
10129 else if (ret)
10130 goto out;
10131 } else if (device != em->map_lookup->stripes[0].dev) {
10132 btrfs_warn(fs_info, "swapfile must be on one device");
10133 ret = -EINVAL;
10134 goto out;
10135 }
10136
10137 physical_block_start = (em->map_lookup->stripes[0].physical +
10138 (logical_block_start - em->start));
10139 len = min(len, em->len - (logical_block_start - em->start));
10140 free_extent_map(em);
10141 em = NULL;
10142
10143 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10144 if (!bg) {
10145 btrfs_warn(fs_info,
10146 "could not find block group containing swapfile");
10147 ret = -EINVAL;
10148 goto out;
10149 }
10150
10151 ret = btrfs_add_swapfile_pin(inode, bg, true);
10152 if (ret) {
10153 btrfs_put_block_group(bg);
10154 if (ret == 1)
10155 ret = 0;
10156 else
10157 goto out;
10158 }
10159
10160 if (bsi.block_len &&
10161 bsi.block_start + bsi.block_len == physical_block_start) {
10162 bsi.block_len += len;
10163 } else {
10164 if (bsi.block_len) {
10165 ret = btrfs_add_swap_extent(sis, &bsi);
10166 if (ret)
10167 goto out;
10168 }
10169 bsi.start = start;
10170 bsi.block_start = physical_block_start;
10171 bsi.block_len = len;
10172 }
10173
10174 start += len;
10175 }
10176
10177 if (bsi.block_len)
10178 ret = btrfs_add_swap_extent(sis, &bsi);
10179
10180out:
10181 if (!IS_ERR_OR_NULL(em))
10182 free_extent_map(em);
10183
10184 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10185
10186 if (ret)
10187 btrfs_swap_deactivate(file);
10188
10189 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10190
10191 if (ret)
10192 return ret;
10193
10194 if (device)
10195 sis->bdev = device->bdev;
10196 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10197 sis->max = bsi.nr_pages;
10198 sis->pages = bsi.nr_pages - 1;
10199 sis->highest_bit = bsi.nr_pages - 1;
10200 return bsi.nr_extents;
10201}
10202#else
10203static void btrfs_swap_deactivate(struct file *file)
10204{
10205}
10206
10207static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10208 sector_t *span)
10209{
10210 return -EOPNOTSUPP;
10211}
10212#endif
10213
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010214static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010215 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010216 .lookup = btrfs_lookup,
10217 .create = btrfs_create,
10218 .unlink = btrfs_unlink,
10219 .link = btrfs_link,
10220 .mkdir = btrfs_mkdir,
10221 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010222 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010223 .symlink = btrfs_symlink,
10224 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010225 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010226 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010227 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010228 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010229 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010230 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010231 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010232};
Yan, Zheng76dda932009-09-21 16:00:26 -040010233
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010234static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010235 .llseek = generic_file_llseek,
10236 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010237 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010238 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010239 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010240#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010241 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010242#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010243 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010244 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010245};
10246
David Sterba20e55062015-11-19 11:42:28 +010010247static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010248 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010249 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010250 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10251};
10252
Chris Mason35054392009-01-21 13:11:13 -050010253/*
10254 * btrfs doesn't support the bmap operation because swapfiles
10255 * use bmap to make a mapping of extents in the file. They assume
10256 * these extents won't change over the life of the file and they
10257 * use the bmap result to do IO directly to the drive.
10258 *
10259 * the btrfs bmap call would return logical addresses that aren't
10260 * suitable for IO and they also will change frequently as COW
10261 * operations happen. So, swapfile + btrfs == corruption.
10262 *
10263 * For now we're avoiding this by dropping bmap.
10264 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010265static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010266 .readpage = btrfs_readpage,
10267 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010268 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010269 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010270 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010271 .invalidatepage = btrfs_invalidatepage,
10272 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010273#ifdef CONFIG_MIGRATION
10274 .migratepage = btrfs_migratepage,
10275#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010276 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010277 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010278 .swap_activate = btrfs_swap_activate,
10279 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010280};
10281
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010282static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010283 .getattr = btrfs_getattr,
10284 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010285 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010286 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010287 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010288 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010289 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010290 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010291};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010292static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010293 .getattr = btrfs_getattr,
10294 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010295 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010296 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010297 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010298 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010299 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010300};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010301static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010302 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010303 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010304 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010305 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010306 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010307 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010308};
Yan, Zheng76dda932009-09-21 16:00:26 -040010309
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010310const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010311 .d_delete = btrfs_dentry_delete,
10312};